| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Http\UploadedFile;
- use App\Models\DTO\SearchData;
- class PostFile extends Model
- {
- protected $table = 'tb_post_file';
- protected $primaryKey = 'id';
- public $keyType = 'int';
- public $incrementing = true;
- public $timestamps = true;
- const CREATED_AT = 'created_at';
- const UPDATED_AT = null;
- protected $guarded = [];
- public function board()
- {
- return $this->belongsTo(Board::class)->withDefault();
- }
- public function post()
- {
- return $this->belongsTo(Post::class)->withDefault();
- }
- public function postFileDownLog()
- {
- return $this->hasMany(PostFileDownLog::class);
- }
- public function user()
- {
- return $this->belongsTo(User::class)->withDefault();
- }
- /**
- * 게시글 첨부 파일 조회
- */
- public function data(SearchData $params): object
- {
- $query = $this->query();
- $query->select(
- 'tb_board.name AS boardName',
- 'tb_board.code',
- 'tb_post_file.*',
- 'tb_post.subject',
- 'users.sid',
- 'users.name'
- );
- if($params->keyword) {
- switch ($params->field) {
- case 'tb_post.id' :
- case 'tb_post_file.id' :
- case 'users.id' :
- case 'users.sid' :
- $query->where($params->field, '=', $params->keyword);
- break;
- case 'tb_post.subject' :
- case 'users.name' :
- case 'users.email' :
- $query->where($params->field, 'LIKE', "%{$params->keyword}%");
- break;
- }
- }
- if($params->startDate || $params->endDate) {
- if($params->startDate) {
- $query->where('tb_post_file.created_at', '>=', $params->startDate . ' 00:00:00');
- }
- if($params->endDate) {
- $query->where('tb_post_file.created_at', '<=', $params->endDate . ' 23:59:59');
- }
- }
- if($params->boardID) {
- $query->where('tb_post_file.board_id', '=', $params->boardID);
- }
- $query->join('tb_board', 'tb_board.id', '=', 'tb_post_file.board_id');
- $query->join('tb_post', 'tb_post.id', '=', 'tb_post_file.post_id');
- $query->leftJoin('users', 'users.id', '=', 'tb_post_file.user_id');
- $query->orderByDesc('tb_post_file.id');
- $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
- $total = $this->count();
- $rows = $list->count();
- return (object)[
- 'total' => $total,
- 'rows' => $rows,
- 'list' => $list
- ];
- }
- /**
- * 첨부파일 정보 조회
- */
- public function info(int $fileID): PostFile
- {
- return $this->findOrNew($fileID);
- }
- /**
- * 게시글 파일 삭제
- */
- public function remove(int $postFileID): mixed
- {
- return DB::transaction(function() use($postFileID) {
- $postFile = $this->info($postFileID);
- // 파일 존재 확인
- if(!$postFile->exists) {
- return false;
- }
- // 첨부파일 다운로드 기록 삭제
- $postFile->postFileDownLog->each(function($fileLog) {
- $fileLog->delete();
- });
- // 게시글 첨부파일 수 갱신
- $postFile->post->updateFileRows($postFile->post_id);
- // 파일 삭제
- Storage::delete($postFile->file_path);
- if(file_exists($postFile->file_path)) {
- unlink($postFile->file_path);
- }
- // 첨부파일 정보 삭제
- $postFile->delete();
- return true;
- });
- }
- /**
- * 게시글 파일 전부 삭제
- */
- public function truncate(int $postID): void
- {
- if($this->where('post_id', $postID)->delete()) {
- (new FileLib)->removePath(UPLOAD_PATH_POST, $postID);
- }
- }
- /**
- * 게시글 파일 첨부 등록
- */
- public function register(Post $post, UploadedFile|array|null $files): void
- {
- if ($files) {
- foreach ($files as $file) {
- $this->_upload($post, $file);
- }
- $post->updateFileRows($post->id);
- $post->updateImageRows($post->id);
- }
- }
- /**
- * 게시글 파일 첨부 수정, 삭제
- */
- public function updater(Post $post, array $filesIsDelete, UploadedFile|array|null $files)
- {
- // 삭제 요청이 있을 경우 삭제
- if($filesIsDelete) {
- foreach ($filesIsDelete as $fileID => $checked) {
- $this->remove($fileID);
- }
- }
- if($files) {
- foreach ($files as $fileID => $file)
- {
- $postFile = $this->where([
- ['id', $fileID],
- ['board_id', $post->board_id],
- ['post_id', $post->id]
- ])->firstOrNew();
- // 파일이 이미 있으면 기존 정보 삭제
- if($postFile->exists) {
- $this->remove($postFile);
- }
- $this->_upload($post->board_id, $post->id, $file);
- }
- }
- $post->updateFileRows($post->id);
- $post->updateImageRows($post->id);
- }
- /**
- * 파일 업로드 처리
- */
- private function _upload(Post $post, UploadedFile $file): bool
- {
- $fileInfo = (new FileLib)->upload($file, UPLOAD_PATH_POST, $post->id);
- return $this->insert([
- 'board_id' => $post->board_id,
- 'post_id' => $post->id,
- 'user_id' => UID,
- 'origin_name' => $fileInfo->originName,
- 'file_name' => $fileInfo->name,
- 'file_path' => $fileInfo->path,
- 'file_url' => $fileInfo->url,
- 'file_size' => $fileInfo->size,
- 'file_ext' => $fileInfo->extension,
- 'img_width' => $fileInfo->imageWidth,
- 'img_height' => $fileInfo->imageHeight,
- 'is_image' => $fileInfo->isImage,
- 'download' => 0,
- 'ip_address' => IP_ADDRESS,
- 'user_agent' => USER_AGENT,
- 'created_at' => now()
- ]);
- }
- /**
- * 게시글 다운로드 횟수 증가
- */
- public function increaseDownload(int $fileID): int
- {
- return $this->where('id', $fileID)->increment('download');
- }
- }
|