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
fluent-boards / app / Models / Comment.php

Comment.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration trunk, at app/Models/Comment.php

187 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Models;
4
5 use FluentBoards\App\Services\Constant;
6 use FluentBoards\Framework\Database\Orm\Builder;
7
8 class Comment extends Model
9 {
10 protected $table = 'fbs_comments';
11
12 protected $guarded = ['id'];
13
14 protected $appends = ['avatar'];
15
16 protected $fillable = [
17 'board_id',
18 'task_id',
19 'parent_id',
20 'type',
21 'privacy',
22 'status',
23 'author_name',
24 'author_email',
25 'author_ip',
26 'description',
27 'settings',
28 'created_by',
29 ];
30
31 public static function boot()
32 {
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 });
40 static::creating(function ($model) {
41 if (empty($model->created_by) && is_user_logged_in()) {
42 $model->created_by = get_current_user_id();
43 }
44
45 if (empty($model->type)) {
46 $model->type = 'comment';
47 }
48
49 if ($model->parent_id < 1) {
50 $model->parent_id = Null;
51 }
52
53 if (empty($model->privacy)) {
54 $model->privacy = 'private';
55 }
56
57 if (!empty($model->created_by) && empty($model->author_email)) {
58 $user = get_user_by('ID', $model->created_by);
59 if ($user) {
60 $model->author_email = $user->user_email;
61 $name = trim($user->first_name . ' ' . $user->last_name);
62 if (!$name) {
63 $name = $user->display_name;
64 }
65 $model->author_name = $name;
66 }
67 }
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();
77 }
78 }
79 });
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
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();
97 }
98 }
99 });
100 }
101
102 /**
103 * One2One: Activity belongs to one User
104 * @return \FluentCrm\Framework\Database\Orm\Relations\BelongsTo
105 */
106 public function user()
107 {
108 return $this->belongsTo(User::class, 'created_by', 'ID');
109 }
110
111 public function task()
112 {
113 return $this->belongsTo(Task::class, 'task_id', 'id');
114 }
115
116 public function scopeByTask($query, $taskId)
117 {
118 return $query->where('task_id', $taskId);
119 }
120
121 public function scopePrivacy($query, $type)
122 {
123 return $query->where('privacy', $type);
124 }
125
126 public function setSettingsAttribute($settings)
127 {
128 $this->attributes['settings'] = \maybe_serialize($settings);
129 }
130
131 public function getSettingsAttribute($settings)
132 {
133 return \maybe_unserialize($settings);
134 }
135
136 public function replies()
137 {
138 return $this->hasMany(Comment::class, 'parent_id', 'id');
139 }
140
141 /**
142 * Accessor to get dynamic photo attribute
143 * @return string
144 */
145 public function getAvatarAttribute()
146 {
147 $fallBack = '';
148 if (isset($this->attributes['author_name'])) {
149 $fallBack = $this->attributes['author_name'];
150 }
151
152 $email = $this->attributes['author_email'];
153
154 if (!$email && $this->created_by) {
155 $user = get_user_by('ID', $this->created_by);
156 if ($user) {
157 $email = $user->user_email;
158 }
159 }
160
161 return fluent_boards_user_avatar($email, $fallBack);
162 }
163
164 public function scopeType($query, $type)
165 {
166 return $query->where('type', $type);
167 }
168
169 public function parentComment()
170 {
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);
184 }
185
186 }
187