PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / trunk
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration vtrunk
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
← All changes | app/Models/Comment.php +53 -18 1.12trunk View file →
@@ -1,8 +1,11 @@
1 1 <?php
2 2
3 3 namespace FluentBoards\App\Models;
4 4
5 +use FluentBoards\App\Services\Constant;
6 +use FluentBoards\Framework\Database\Orm\Builder;
7 +
5 8 class Comment extends Model
6 9 {
7 10 protected $table = 'fbs_comments';
8 11
@@ -27,8 +30,14 @@
27 30
28 31 public static function boot()
29 32 {
30 33 parent::boot();
34 + static::addGlobalScope('images', function (Builder $builder) {
35 + $builder->with('images');
36 + });
37 + static::addGlobalScope('replies', function (Builder $builder) {
38 + $builder->with('replies');
39 + });
31 40 static::creating(function ($model) {
32 41 if (empty($model->created_by) && is_user_logged_in()) {
33 42 $model->created_by = get_current_user_id();
34 43 }
@@ -36,8 +45,12 @@
36 45 if (empty($model->type)) {
37 46 $model->type = 'comment';
38 47 }
39 48
49 + if ($model->parent_id < 1) {
50 + $model->parent_id = Null;
51 + }
52 +
40 53 if (empty($model->privacy)) {
41 54 $model->privacy = 'private';
42 55 }
43 56
@@ -51,29 +64,39 @@
51 64 }
52 65 $model->author_name = $name;
53 66 }
54 67 }
55 -
56 - static::created(function ($model) {
57 - if ($model->type == 'comment') {
58 - $task = $model->task;
59 - if ($task) {
60 - $task->comments_count = $task->comments_count + 1;
61 - $task->save();
62 - }
68 +
69 + });
70 +
71 + static::created(function ($model) {
72 + $task = $model->task;
73 + if ($model->type == 'comment') {
74 + if ($task) {
75 + $task->comments_count = (int) $task->comments_count + 1;
76 + $task->save();
63 77 }
64 - });
78 + }
79 + });
65 80
66 - static::deleted(function ($model) {
67 - if ($model->type == 'comment') {
68 - $task = $model->task;
69 - if ($task) {
70 - $task->comments_count = $task->comments_count - 1;
71 - $task->save();
72 - }
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 +
91 + static::deleted(function ($model) {
92 + if ($model->type == 'comment') {
93 + $task = $model->task;
94 + if ($task) {
95 + $task->comments_count = $task->comments_count - 1;
96 + $task->save();
73 97 }
74 - });
75 -
98 + }
76 99 });
77 100 }
78 101
79 102 /**
@@ -145,7 +168,19 @@
145 168
146 169 public function parentComment()
147 170 {
148 171 return $this->hasOne(Comment::class, 'parent_id', 'id');
172 + }
173 +
174 + public function images()
175 + {
176 + return $this->hasMany(CommentImage::class, 'object_id', 'id')
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);
149 184 }
150 185
151 186 }