Automation/app/Libraries/MyStorage/MangboardLibrary.php
2024-09-11 00:09:09 +09:00

151 lines
5.5 KiB
PHP

<?php
namespace App\Libraries\MyStorage;
use App\Libraries\MyUtil\ImageLibrary;
use App\Libraries\MyStorage\FileLibrary;
use App\Models\Mangboard\FileModel;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\FileEntity;
class MangboardLibrary extends FileLibrary
{
private $_user = null;
private $_boardName = "";
private $_boardTable = "";
private $_boardLevel = 1;
private $_model = null;
public function __construct(string $path)
{
parent::__construct($path);
}
public function getModel(): FileModel
{
if ($this->_model === null) {
return $this->_model = new FileModel();
}
return $this->_model;
}
public function getUser(): UserEntity
{
if ($this->_user === null) {
throw new \Exception("사용자정보가 없습니다.");
}
return $this->_user;
}
public function setUser(UserEntity $user): void
{
$this->_user = $user;
}
public function getBoardName(): string
{
if ($this->_boardName === null) {
throw new \Exception("BoardModel이 지정되지 않았습니다.");
}
return $this->_boardName;
}
public function setBoardName(string $boardName): void
{
$this->_boardName = $boardName;
}
public function getBoardTable(): string
{
if ($this->_boardTable === null) {
throw new \Exception("BoardModel이 지정되지 않았습니다.");
}
return $this->_boardTable;
}
public function setBoardTable(string $boardTable): void
{
$this->_boardTable = $boardTable;
}
public function getBoardLevel(): int
{
if ($this->_boardLevel === null) {
throw new \Exception("BoardModel Level이 지정되지 않았습니다.");
}
return intval($this->_boardLevel);
}
public function setBoardLevel(string $boardLevel): void
{
$this->_boardLevel = $boardLevel;
}
//주의:Override함
public function createFileEntity(): FileEntity
{
return new FileEntity();
}
private function create_small_image(FileEntity $entity, string $file, int $width = 480, int $height = 319): FileEntity
{
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$image = new ImageLibrary();
$image->setDebug($this->getDebug());
$image->setSourcePath($fullPath);
$image->setDestinationPath($fullPath);
//저장파일명
$fileInfos = pathinfo($image->getDestinationPath() . DIRECTORY_SEPARATOR . $file, PATHINFO_ALL);
$dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
$image->setDestinationFile($dstFile);
//작은이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다.
$result = $image->create($file, $width, $height);
// if ($result) {
// //URL이 되기때문에 /로 넣어야함
// $entity->setPath(sprintf(
// "/%s/%s/%s",
// $this->getUploadPath(),
// $entity->getPath(),
// $image->getDestinationFile()
// ));
// }
return $entity;
}
//망보드 파일관리 table에 등록
private function create_db(FileEntity $entity, int $file_sequence): FileEntity
{
// log_message("notice", __FUNCTION__ . " {$file_sequence}번째 작업 시작");
$entity->user_pid = $this->getUser()->getPK();
$entity->user_name = $this->getUser()->getTitle();
$entity->board_name = $this->getBoardName();
$entity->table_name = $this->getBoardTable();
$entity->reg_date = date("Y-m-d H:i:s");
$entity->file_caption = $entity->getTitle();
$entity->file_alt = $entity->getTitle();
$entity->file_description = "Filedata";
// log_message("debug", "\n-----Entity Value-----\n" . var_export($entity->toArray(), true) . "\n---------------------------\n");
$entity = $this->getModel()->create($entity);
log_message("notice", __FUNCTION__ . " {$file_sequence}번째 작업 완료");
return $entity;
}
public function save(string $fileName, string $mediaType, string $content, int $file_sequence): FileEntity
{
$entity = parent::save($fileName, $mediaType, $content, $file_sequence);
//mb_files에서 file_path가 망보드 게시판 파일관리에서 image로 표시되어 file_path+file_name로 설정
//원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다.
//URL이 되기때문에 /로 넣어야함
$entity->setPath(sprintf(
"/%s/%s/%s",
$this->getUploadPath(),
$entity->getPath(),
$entity->getTitle()
));
//작은이미지 생성
$entity = $this->create_small_image($entity, $fileName);
return $this->create_db($entity, $file_sequence);
}
public function setBoardPID(array $fileEntitys, int $board_pid)
{
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
foreach ($fileEntitys as $fileEntity) {
$fileEntity->board_pid = $board_pid;
//수정시 mb_files에서 file_path가 변경하지 못하게 하기위함
$this->getModel()->setFields($this->getModel()->getFields("except", ['file_path']));
$this->getModel()->modify($fileEntity);
}
log_message("notice", __FUNCTION__ . " 작업 완료");
}
}