Automation/app/Models/Mangboard/FileModel.php
2024-09-16 19:44:45 +09:00

163 lines
6.6 KiB
PHP

<?php
namespace App\Models\Mangboard;
use App\Entities\Mangboard\BoardEntity;
use App\Entities\Mangboard\BoardsEntity;
use App\Entities\Mangboard\FileEntity;
use App\Entities\Mangboard\UserEntity;
use App\Libraries\MyStorage\MangboardStorage;
use App\Models\CommonModel;
// +------------------+----------------------+------+-----+---------------------+----------------+
// | Field | Type | Null | Key | Default | Extra |
// +------------------+----------------------+------+-----+---------------------+----------------+
// | pid | int(10) unsigned | NO | PRI | NULL | auto_increment |
// | user_pid | int(10) unsigned | NO | | 0 | |
// | user_name | varchar(100) | NO | MUL | | |
// | board_name | varchar(50) | NO | MUL | | |
// | table_name | varchar(100) | NO | MUL | | |
// | board_pid | int(10) unsigned | NO | MUL | 0 | |
// | file_name | varchar(255) | NO | MUL | | |
// | file_path | varchar(255) | NO | MUL | | |
// | file_type | varchar(255) | NO | MUL | | |
// | file_caption | varchar(255) | NO | | | |
// | file_alt | varchar(255) | NO | | | |
// | file_description | text | NO | | NULL | |
// | file_size | int(10) unsigned | NO | MUL | 0 | |
// | link_count | int(10) unsigned | NO | | 0 | |
// | download_count | int(10) unsigned | NO | | 0 | |
// | file_sequence | smallint(5) unsigned | NO | MUL | 1 | |
// | is_download | tinyint(3) unsigned | NO | | 0 | |
// | reg_date | datetime | NO | MUL | 0000-00-00 00:00:00 | |
// | ip | varchar(40) | NO | | | |
// | agent | varchar(30) | NO | | | |
// +------------------+----------------------+------+-----+---------------------+----------------+
class FileModel extends CommonModel
{
protected $table = 'mb_files';
protected $primaryKey = 'pid';
protected $returnType = FileEntity::class;
protected $allowedFields = [
"user_pid",
"user_name",
"board_pid",
"board_name",
"table_name",
"file_name",
"file_path",
"file_type",
"file_caption",
"file_alt",
"file_description",
"file_size",
"file_sequence",
"reg_date"
];
public function __construct()
{
parent::__construct();
}
public function getTitleField(): string
{
return 'file_name';
}
public function getFieldRule(string $field, array $rules): array
{
switch ($field) {
case "board_pid":
case "user_pid":
case "file_sequence":
case "file_size":
$rules[$field] = "if_exist|numeric";
break;
case "board_name":
case "table_name":
case "file_name":
case "file_path":
case "file_type":
$rules[$field] = "required|string";
break;
case "file_description":
case "file_caption":
case "file_alt":
$rules[$field] = "if_exist|string";
break;
case "reg_date":
$rules[$field] = "if_exist|valid_date";
break;
default:
$rules = parent::getFieldRule($field, $rules);
break;
}
return $rules;
}
public function getEntityByPK(int $uid): null | FileEntity
{
$this->where($this->getPKField(), $uid);
return $this->getEntity();
}
public function getEntityByID(string $id): null | FileEntity
{
$this->where('user_id', $id);
return $this->getEntity();
}
// protected function convertEntityData(string $field, array $formDatas): string|int
// {
// switch ($field) {
// default:
// $value = parent::convertEntityData($field, $formDatas);
// break;
// }
// return $value;
// }
//create용
public function create(array $formDatas = []): FileEntity
{
return $this->create_process(new FileEntity(), $formDatas);
}
//modify용
public function modify(FileEntity $entity, array $formDatas): FileEntity
{
return $this->modify_process($entity, $formDatas);
}
public function createByCrawler(
BoardsEntity $boards_entity,
UserEntity $user_entity,
BoardEntity $board_entity,
string $board_table,
MangboardStorage $storage
): FileEntity {
//파일관리 table에 등록
$formDatas = [];
//Board PID 넣기
$formDatas['board_pid'] = $board_entity->getPk();
$formDatas['user_pid'] = $user_entity->getPK();
$formDatas['user_name'] = $user_entity->getTitle();
$formDatas['board_name'] = $boards_entity->getTitle();
$formDatas['table_name'] = $board_table;
$formDatas['file_path'] = $storage->getBasePath() . DIRECTORY_SEPARATOR . $storage->getPath() . DIRECTORY_SEPARATOR . $storage->getOriginName();
$formDatas['file_name'] = $storage->getOriginName();
$formDatas['file_type'] = $storage->getMimeType();
$formDatas['file_caption'] = $storage->getOriginName();
$formDatas['file_alt'] = $storage->getOriginName();
$formDatas['file_description'] = "Filedata";
$formDatas['file_size'] = $storage->getFileSize();
$formDatas['file_sequence'] = $storage->getOriginSequence();
$formDatas['reg_date'] = date("Y-m-d H:i:s");
$entity = $this->create($formDatas);
log_message("notice", sprintf(
"%s -> %s 게시물의 %s번째:%s 파일 등록 완료",
__FUNCTION__,
$board_entity->getTitle(),
$storage->getOriginSequence(),
$entity->getTitle()
));
return $entity;
}
}