96 lines
3.5 KiB
PHP
96 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\BoardConfigEntity;
|
|
|
|
class BoardConfigModel extends BaseModel
|
|
{
|
|
protected $table = 'tw_board_config';
|
|
protected $primaryKey = 'uid';
|
|
protected $useAutoIncrement = false;
|
|
protected $allowedFields = ['uid', 'name', 'isaccess', 'isread', 'iswrite', 'isreply', 'isupload', 'isdownload', 'head', 'tail', 'status', 'updated_at'];
|
|
protected $validationRules = [
|
|
'uid' => 'required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]',
|
|
'name' => 'required|string',
|
|
'isaccess' => 'required',
|
|
'isread' => 'required',
|
|
'iswrite' => 'required',
|
|
'isreply' => 'required',
|
|
'isupload' => 'required',
|
|
'isdownload' => 'required',
|
|
'head' => 'if_exist|string',
|
|
'tail' => 'if_exist|string',
|
|
'status' => 'if_exist|string',
|
|
'updated_at' => 'if_exist|valid_date',
|
|
'created_at' => 'if_exist|valid_date',
|
|
];
|
|
|
|
public function getEntityByField($field, $value): ?BoardConfigEntity
|
|
{
|
|
$entity = $this->asObject(BoardConfigEntity::class)->where($field, $value)->first();
|
|
if (is_null($entity)) {
|
|
throw new \Exception("해당 데이터가 없습니다.\n {$field}->{$value}");
|
|
}
|
|
return $entity;
|
|
}
|
|
public function getEntity($uid): ?BoardConfigEntity
|
|
{
|
|
return $this->getEntityByField($this->primaryKey, $uid);
|
|
}
|
|
public function getFieldFormOptions(array $wheres = array(), $temps = array()): array
|
|
{
|
|
foreach ($this->asObject(BoardConfigEntity::class)->where($wheres)->findAll() as $entity) {
|
|
$temps[$entity->getPrimaryKey()] = $entity->getTitle();
|
|
}
|
|
return $temps;
|
|
}
|
|
|
|
protected function changeFormData($field, array $formDatas, $entity)
|
|
{
|
|
switch ($field) {
|
|
case 'isaccess':
|
|
case 'isread':
|
|
case 'iswrite':
|
|
case 'isreply':
|
|
case 'isupload':
|
|
case 'isdownload':
|
|
case 'isaccess':
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = is_array($formDatas[$field]) ? implode("|", $formDatas[$field]) : $formDatas[$field];
|
|
}
|
|
break;
|
|
case 'head':
|
|
case 'tail':
|
|
if (array_key_exists($field, $formDatas) && $formDatas[$field]) {
|
|
$entity->$field = htmlentities($formDatas[$field]);
|
|
}
|
|
break;
|
|
default:
|
|
return parent::changeFormData($field, $formDatas, $entity);
|
|
break;
|
|
}
|
|
return $entity;
|
|
}
|
|
public function create(array $formDatas): BoardConfigEntity
|
|
{
|
|
return $this->create_process(new BoardConfigEntity(), $formDatas);
|
|
}
|
|
public function modify(BoardConfigEntity $entity, array $formDatas): BoardConfigEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike('name', $word, 'both'); //befor , after , both
|
|
}
|
|
public function setIndexOrderBy($field, $order = 'ASC')
|
|
{
|
|
$this->orderBy("name", "ASC");
|
|
parent::setIndexOrderBy($field, $order);
|
|
}
|
|
}
|