| @@ -2,8 +2,10 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentBoards\App\Services; |
| 4 | 4 | |
| 5 | 5 | use FluentBoards\App\Models\Attachment; |
| 6 | +use FluentBoards\App\Models\Comment; | |
| 7 | +use FluentBoards\App\Models\CommentImage; | |
| 6 | 8 | use FluentBoards\App\Models\Task; |
| 7 | 9 | use FluentBoards\App\Models\TaskImage; |
| 8 | 10 | use FluentBoards\App\Services\Libs\FileSystem; |
| 9 | 11 | |
| @@ -43,8 +45,52 @@ | ||
| 43 | 45 | |
| 44 | 46 | return $task; |
| 45 | 47 | } |
| 46 | 48 | |
| 49 | + /** | |
| 50 | + * Move every comment and reply image for a task to another board. | |
| 51 | + */ | |
| 52 | + public function moveCommentImagesToBoard($taskId, $sourceBoardId, $targetBoardId) | |
| 53 | + { | |
| 54 | + $taskId = absint($taskId); | |
| 55 | + $sourceBoardId = absint($sourceBoardId); | |
| 56 | + $targetBoardId = absint($targetBoardId); | |
| 57 | + | |
| 58 | + if (!$taskId || !$sourceBoardId || !$targetBoardId || $sourceBoardId === $targetBoardId) { | |
| 59 | + return; | |
| 60 | + } | |
| 61 | + | |
| 62 | + $commentIds = Comment::where('task_id', $taskId) | |
| 63 | + ->pluck('id') | |
| 64 | + ->toArray(); | |
| 65 | + $commentIds = array_filter(array_map('intval', $commentIds)); | |
| 66 | + | |
| 67 | + if (!$commentIds) { | |
| 68 | + return; | |
| 69 | + } | |
| 70 | + | |
| 71 | + $images = CommentImage::whereIn('object_id', $commentIds) | |
| 72 | + ->where('object_type', Constant::COMMENT_IMAGE) | |
| 73 | + ->get(); | |
| 74 | + $commentCreators = Comment::whereIn('id', $commentIds) | |
| 75 | + ->pluck('created_by', 'id') | |
| 76 | + ->toArray(); | |
| 77 | + $sharedFullUrls = $this->getSharedFullUrlLookup($this->combineAttachmentCollections($images)); | |
| 78 | + $commentService = new CommentService(); | |
| 79 | + | |
| 80 | + CommentImage::withoutTimestamps(function () use ($images, $sourceBoardId, $targetBoardId, $taskId, $sharedFullUrls, $commentCreators, $commentService) { | |
| 81 | + foreach ($images as $image) { | |
| 82 | + $commentService->applyCommentImageScope( | |
| 83 | + $image, | |
| 84 | + $targetBoardId, | |
| 85 | + $taskId, | |
| 86 | + $commentCreators[$image->object_id] ?? null | |
| 87 | + ); | |
| 88 | + $this->moveAttachmentToBoard($image, $sourceBoardId, $targetBoardId, $sharedFullUrls); | |
| 89 | + } | |
| 90 | + }); | |
| 91 | + } | |
| 92 | + | |
| 47 | 93 | public function cloneTaskFilesToBoard(Task $sourceTask, Task $targetTask, $targetBoardId, array $options = []) |
| 48 | 94 | { |
| 49 | 95 | $options = array_merge([ |
| 50 | 96 | 'description_images' => true, |
| @@ -351,36 +397,9 @@ | ||
| 351 | 397 | } |
| 352 | 398 | |
| 353 | 399 | protected function resolveLocalPath(Attachment $attachment, $boardId) |
| 354 | 400 | { |
| 355 | - if (!empty($attachment->file_path) && is_file($attachment->file_path)) { | |
| 356 | - return $attachment->file_path; | |
| 357 | - } | |
| 358 | - | |
| 359 | - if (!empty($attachment->full_url)) { | |
| 360 | - $uploadDir = wp_upload_dir(); | |
| 361 | - $pathFromUrl = rawurldecode(str_replace($uploadDir['baseurl'], $uploadDir['basedir'], $attachment->full_url)); | |
| 362 | - if (is_file($pathFromUrl)) { | |
| 363 | - return $pathFromUrl; | |
| 364 | - } | |
| 365 | - | |
| 366 | - if ($boardId) { | |
| 367 | - $urlPath = wp_parse_url($attachment->full_url, PHP_URL_PATH); | |
| 368 | - $pathFromBoardUrl = $urlPath ? $this->getBoardDir($boardId) . DIRECTORY_SEPARATOR . basename(rawurldecode($urlPath)) : null; | |
| 369 | - if ($pathFromBoardUrl && is_file($pathFromBoardUrl)) { | |
| 370 | - return $pathFromBoardUrl; | |
| 371 | - } | |
| 372 | - } | |
| 373 | - } | |
| 374 | - | |
| 375 | - if ($boardId && !empty($attachment->file_path)) { | |
| 376 | - $pathFromBoard = $this->getBoardDir($boardId) . DIRECTORY_SEPARATOR . basename(rawurldecode($attachment->file_path)); | |
| 377 | - if (is_file($pathFromBoard)) { | |
| 378 | - return $pathFromBoard; | |
| 379 | - } | |
| 380 | - } | |
| 381 | - | |
| 382 | - return null; | |
| 401 | + return FileSystem::resolveLocalAttachmentPath($attachment->file_path, $boardId); | |
| 383 | 402 | } |
| 384 | 403 | |
| 385 | 404 | protected function getAttachmentFilename(Attachment $attachment) |
| 386 | 405 | { |