116 lines
3.9 KiB
PHP
116 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Entities\UserEntity;
|
|
use App\Forms\UserForm;
|
|
use App\Models\UserModel;
|
|
|
|
class UserService extends CommonService
|
|
{
|
|
public function __construct(
|
|
UserModel $model,
|
|
private UserForm $formService,
|
|
) {
|
|
parent::__construct($model);
|
|
$this->formServiceInstance = $this->formService;
|
|
// FormService에 Model 메타데이터를 설정
|
|
$this->formServiceInstance->setAttributes([
|
|
'pk_field' => $this->model->getPKField(),
|
|
'title_field' => $this->model->getTitleField(),
|
|
'table' => $this->model->getTable(),
|
|
'useAutoIncrement' => $this->model->useAutoIncrement(),
|
|
]);
|
|
$this->addClassPath('Service');
|
|
}
|
|
public function getFormService(): UserForm
|
|
{
|
|
if ($this->formServiceInstance === null) {
|
|
throw new \RuntimeException('FormService는 ' . static::class . '에서 초기화되어야 합니다.');
|
|
}
|
|
return $this->formServiceInstance;
|
|
}
|
|
public function getFormFields(): array
|
|
{
|
|
return [
|
|
'id',
|
|
'passwd',
|
|
'confirmpassword',
|
|
'name',
|
|
'email',
|
|
'mobile',
|
|
'role',
|
|
'status',
|
|
];
|
|
}
|
|
public function getFormFilters(): array
|
|
{
|
|
return [
|
|
'role',
|
|
'status',
|
|
];
|
|
}
|
|
public function getIndexFields(): array
|
|
{
|
|
return [
|
|
'id',
|
|
'name',
|
|
'email',
|
|
'mobile',
|
|
'role',
|
|
'status',
|
|
];
|
|
}
|
|
public function getBatchjobFields(): array
|
|
{
|
|
return ['status'];
|
|
}
|
|
//기본 기능부분
|
|
|
|
protected function create_process(array $formDatas): UserEntity
|
|
{
|
|
$formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
|
|
$entity = $this->model->create($formDatas);
|
|
return $entity;
|
|
}
|
|
protected function modify_process(mixed $entity, array $formDatas): UserEntity
|
|
{
|
|
// die(var_export($formDatas, true));
|
|
//암호를 입력하지 않았을시는 변경하기 않게 하기위함
|
|
if (isset($formDatas['passwd']) && $formDatas['passwd'] == "") {
|
|
unset($formDatas['passwd']);
|
|
unset($formDatas['confirmpassword']);
|
|
}
|
|
//Role을 지정이 있을경우에만 , toggle이나 batcjhjob에서는 없을수도 있으므로
|
|
if (isset($formDatas['role'])) {
|
|
$formDatas['role'] = implode(DEFAULTS["DELIMITER_ROLE"], $formDatas['role']);
|
|
}
|
|
// die(var_export($formDatas, true));
|
|
$entity = parent::modify_process($entity, $formDatas);
|
|
return $entity;
|
|
}
|
|
//List 검색용
|
|
//FormFilter 조건절 처리
|
|
public function index_condition_filterField(string $field, mixed $filter_value): void
|
|
{
|
|
switch ($field) {
|
|
case 'role':
|
|
$where = "FIND_IN_SET(" . $this->model->escape($filter_value) . ", {$this->model->getTable()}.{$field}) > 0";
|
|
//FIND_IN_SET()은 MySQL 함수이므로 CodeIgniter가 이를 일반 컬럼명으로 착각하고 escape하게 되면 오류가 발생합니다. 따라서 ->where($sql, null, false)로 명시하여 escape를 꺼줘야 정상 작동
|
|
$this->model->where($where, null, false);
|
|
break;
|
|
default:
|
|
parent::index_condition_filterField($field, $filter_value);
|
|
break;
|
|
}
|
|
}
|
|
//검색어조건절처리
|
|
public function index_condition_filterWord(string $word): void
|
|
{
|
|
$this->model->orLike($this->model->getTable() . '.id', $word, 'both');
|
|
$this->model->orLike($this->model->getTable() . "." . $this->model::TITLE, $word, 'both');
|
|
$this->model->orLike($this->model->getTable() . '.email', $word, 'both');
|
|
parent::index_condition_filterWord($word);
|
|
}
|
|
}
|