111 lines
3.7 KiB
PHP
111 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\UserEntity;
|
|
|
|
class UserModel extends BaseModel
|
|
{
|
|
protected $table = "tw_user";
|
|
protected $useAutoIncrement = false;
|
|
protected $returnType = UserEntity::class;
|
|
protected $useSoftDeletes = false;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->allowedFields = ["uid", ...$this->allowedFields, ...$this->getFields()];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitle(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
$fields = ["id", "passwd", $this->getTitle(), "email", "role", "status"];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
return ["id", $this->getTitle(), "email", "role", "status", 'created_at'];
|
|
break;
|
|
case "view":
|
|
return ["id", $this->getTitle(), "email", "role", "status", 'updated_at', 'created_at'];
|
|
break;
|
|
default:
|
|
return $fields;
|
|
break;
|
|
}
|
|
}
|
|
public function getFieldFilters(): array
|
|
{
|
|
return ["role", "status"];
|
|
}
|
|
public function getFieldBatchFilters(): array
|
|
{
|
|
return parent::getFieldBatchFilters();
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "id":
|
|
$rules[$field] = "required|trim|min_length[4]|max_length[20]";
|
|
$rules[$field] .= $action == "insert" ? "|is_unique[{$this->table}.{$field}]" : "";
|
|
break;
|
|
case "passwd":
|
|
$rules[$field] = "required|trim|string";
|
|
if ($action != "") {
|
|
$rules["confirmpassword"] = "required|trim|string|matches[passwd]";
|
|
}
|
|
break;
|
|
case $this->getTitle():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "email":
|
|
$rules[$field] = "required|trim|valid_email";
|
|
break;
|
|
case "role":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getEntity($conditions): UserEntity
|
|
{
|
|
return $this->where($conditions)->first() ?: throw new \Exception("해당 데이터가 없습니다.\n" . var_export($conditions, true));
|
|
}
|
|
|
|
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
|
|
{
|
|
switch ($field) {
|
|
case "role":
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = is_array($formDatas[$field]) ? implode("|", $formDatas[$field]) : $formDatas[$field];
|
|
}
|
|
break;
|
|
default:
|
|
$entity = parent::changeFormData($action, $field, $formDatas, $entity);
|
|
break;
|
|
}
|
|
return $entity;
|
|
}
|
|
public function create(array $formDatas): UserEntity
|
|
{
|
|
return $this->create_process(new UserEntity(), $formDatas);
|
|
}
|
|
public function modify(UserEntity $entity, array $formDatas): UserEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike("id", $word, "both");
|
|
$this->orLike($this->getTitle(), $word, "both"); //befor , after , both
|
|
}
|
|
}
|