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'); } }