| @@ -45,8 +45,12 @@ | ||
| 45 | 45 | if (empty($model->type)) { |
| 46 | 46 | $model->type = 'comment'; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | + if ($model->parent_id < 1) { | |
| 50 | + $model->parent_id = Null; | |
| 51 | + } | |
| 52 | + | |
| 49 | 53 | if (empty($model->privacy)) { |
| 50 | 54 | $model->privacy = 'private'; |
| 51 | 55 | } |
| 52 | 56 | |
| @@ -73,8 +77,18 @@ | ||
| 73 | 77 | } |
| 74 | 78 | } |
| 75 | 79 | }); |
| 76 | 80 | |
| 81 | + static::deleting(function ($model) { | |
| 82 | + // Delete comment/reply images and their files | |
| 83 | + $images = $model->images()->get(); | |
| 84 | + foreach ($images as $image) { | |
| 85 | + $deletedImage = clone $image; | |
| 86 | + $image->delete(); | |
| 87 | + $model->commentImageDeleted($deletedImage, $model->board_id); | |
| 88 | + } | |
| 89 | + }); | |
| 90 | + | |
| 77 | 91 | static::deleted(function ($model) { |
| 78 | 92 | if ($model->type == 'comment') { |
| 79 | 93 | $task = $model->task; |
| 80 | 94 | if ($task) { |
| @@ -160,7 +174,13 @@ | ||
| 160 | 174 | public function images() |
| 161 | 175 | { |
| 162 | 176 | return $this->hasMany(CommentImage::class, 'object_id', 'id') |
| 163 | 177 | ->where('object_type', Constant::COMMENT_IMAGE); |
| 178 | + } | |
| 179 | + | |
| 180 | + | |
| 181 | + private function commentImageDeleted($deletedImage, $boardId) | |
| 182 | + { | |
| 183 | + (new \FluentBoards\App\Hooks\Handlers\FileHandler())->deleteAttachmentFile($deletedImage, $boardId); | |
| 164 | 184 | } |
| 165 | 185 | |
| 166 | 186 | } |