102 lines
3.7 KiB
PHP
102 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 = true;
|
|
public function __construct()
|
|
{
|
|
parent::__construct('User');
|
|
$this->allowedFields = [
|
|
...$this->allowedFields,
|
|
"id", "passwd", 'name', "email", "phone", "mobile", "role", "status"
|
|
];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitleField(): string
|
|
{
|
|
return 'name';
|
|
}
|
|
public 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] = "if_exist|trim|string";
|
|
if ($action != "") {
|
|
$rules[$field] = $action == "insert" ? "required|trim|string" : "if_exist|trim|string";
|
|
$rules["confirmpassword"] = $action == "insert" ? "required|trim|string|matches[passwd]" : "if_exist|trim|string|matches[passwd]";
|
|
}
|
|
break;
|
|
case $this->getTitleField():
|
|
$rules[$field] = "required|trim|string";
|
|
break;
|
|
case "email":
|
|
$rules[$field] = "required|trim|valid_email";
|
|
break;
|
|
case "role":
|
|
//아래 Rule은 입력시에는 되는데 수정시에는 않됨 이유를 ?
|
|
// $rules[$field] = "required|in_list[master,director,cloudflare,manager,gold,silver,brone,vip,user]";
|
|
//아래 Rule은 checkbox를 사용시에는 required만 우선 써야 수정시 validate문제없음
|
|
$rules[$field] = "required";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
public function getEntity($conditions): UserEntity
|
|
{
|
|
return parent::getEntity($conditions);
|
|
}
|
|
|
|
protected function changeFormData(string $action, string $field, array $formDatas, $entity)
|
|
{
|
|
switch ($field) {
|
|
case "role":
|
|
if (array_key_exists($field, $formDatas)) {
|
|
$entity->$field = is_array($formDatas[$field]) ? implode(DEFAULTS['DELIMITER_ROLE'], $formDatas[$field]) : $formDatas[$field];
|
|
}
|
|
break;
|
|
case "head":
|
|
case "tail":
|
|
if (array_key_exists($field, $formDatas)) {
|
|
$entity->$field = htmlentities($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)
|
|
{
|
|
if ($word !== DEFAULTS['EMPTY']) {
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitleField(), $word, "both"); //befor , after , both
|
|
$this->orLike("id", $word, "both");
|
|
}
|
|
}
|
|
}
|