99 lines
3.5 KiB
PHP
99 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Entities\BoardEntity;
|
|
|
|
class BoardModel extends BaseHierarchyModel
|
|
{
|
|
protected $table = "tw_board";
|
|
protected $returnType = BoardEntity::class;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->allowedFields = [...$this->allowedFields, ...$this->getFields(), "user_uid"];
|
|
$this->validationRules = [...$this->validationRules, ...$this->getFieldRules($this->allowedFields),];
|
|
}
|
|
public function getTitle(): string
|
|
{
|
|
return 'title';
|
|
}
|
|
public function getFields(string $action = ""): array
|
|
{
|
|
$fields = ["board_config_uid", $this->getTitle(), "board_file", "passwd", "status", "content"];
|
|
switch ($action) {
|
|
case "index":
|
|
case "excel":
|
|
return ["board_config_uid", "user_uid", $this->getTitle(), "board_file", "view_cnt", "status", "created_at"];
|
|
break;
|
|
case "view":
|
|
return ["board_config_uid", "user_uid", $this->getTitle(), "board_file", "view_cnt", "status", "created_at", "content"];
|
|
break;
|
|
default:
|
|
return $fields;
|
|
break;
|
|
}
|
|
}
|
|
public function getFieldFilters(array $fields = array()): array
|
|
{
|
|
return ["board_config_uid", "user_uid", "status", ...$fields];
|
|
}
|
|
protected function getFieldRule(string $field, array $rules, string $action = ""): array
|
|
{
|
|
switch ($field) {
|
|
case "board_config_uid":
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
break;
|
|
case $this->getTitle():
|
|
case "content":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
case "board_file":
|
|
$rules[$field] = !$action ? "if_exist|string" : "if_exist|uploaded[{$field}]|is_image[{$field}]|mime_in[{$field},image/jpg,image/jpeg,image/gif,image/png,image/webp]|max_size[{$field},100]|max_dims[{$field},1024,768]";
|
|
break;
|
|
case "view_cnt":
|
|
$rules[$field] = "if_exist|numeric";
|
|
break;
|
|
default:
|
|
$rules = parent::getFieldRule($field, $rules, $action);
|
|
break;
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
public function getEntity($conditions): BoardEntity
|
|
{
|
|
$entity = $this->where($conditions)->first();
|
|
return $entity ?: throw new \Exception("{$conditions}의 해당 데이터가 없습니다.\n ");
|
|
}
|
|
public function getEntitys($conditions): array
|
|
{
|
|
return $this->where($conditions)->findAll();
|
|
}
|
|
public function create(array $formDatas): BoardEntity
|
|
{
|
|
return $this->create_process(new BoardEntity(), $formDatas);
|
|
}
|
|
public function modify(BoardEntity $entity, array $formDatas): BoardEntity
|
|
{
|
|
return $this->modify_process($entity, $formDatas);
|
|
}
|
|
public function reply($parent_entity, array $formDatas): BoardEntity
|
|
{
|
|
return $this->reply_process($parent_entity, new BoardEntity(), $formDatas);
|
|
}
|
|
|
|
//Index관련
|
|
public function setIndexWordFilter(string $word)
|
|
{
|
|
parent::setIndexWordFilter($word);
|
|
$this->orLike($this->getTitle(), $word, "both");
|
|
$this->orLike("content", $word, "both"); //befor , after , both
|
|
}
|
|
public function setIndexOrderBy($field, $order = "ASC")
|
|
{
|
|
parent::setIndexOrderBy($field, $order);
|
|
$this->orderBy($this->getTitle(), "ASC");
|
|
}
|
|
}
|