PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.0.1
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.0.1
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 / Board.php

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

349 lines 9.2 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\App\Services\PermissionManager;
7 use FluentBoards\App\Services\UserService;
8 use FluentBoards\Framework\Database\Orm\Builder;
9 use FluentBoardsPro\App\Models\CustomField;
10
11 class Board extends Model
12 {
13 protected $table = 'fbs_boards';
14
15 protected $guarded = ['id'];
16
17 protected $hidden = [
18 'created_at',
19 'updated_at',
20 ];
21
22 protected $fillable = [
23 'parent_id',
24 'title',
25 'description',
26 'type',
27 'currency',
28 'background',
29 'settings',
30 'created_by',
31 'archived_at',
32 ];
33
34 protected $appends = ['meta', 'isUserOnlyViewer'];
35
36 /**
37 * Boot board defaults while preserving empty backgrounds for newly created boards.
38 */
39 public static function boot()
40 {
41 static::creating(function ($model) {
42 $model->created_by = $model->created_by ?: get_current_user_id();
43 $model->type = $model->type ?: 'to-do'; // default board type is to-do
44 $model->background = $model->background ?: '';
45 });
46 /* global scope for board type which means only type = to-do will be fetched from everywhere */
47 parent::boot();
48 static::addGlobalScope('type', function (Builder $builder) {
49 $builder = $builder->where('type', '=', 'to-do')
50 ->orWhere('type', '=', 'roadmap');
51 });
52 }
53
54 public static function getColor()
55 {
56 $colors = [
57 '#673AB7', // deep purple
58 '#3F51B5', // indigo
59 '#14508C', // blue
60 '#009688', // teal
61 '#519839', // green
62 '#795548', // brown
63 '#607D8B', // blue grey
64 '#03A9F4', // light blue
65 '#00BCD4', // cyan
66 '#CDDC39', // lime
67 '#838c91', // grey
68 ];
69
70 return $colors[array_rand($colors)];
71 }
72
73 public function setSettingsAttribute($settings)
74 {
75 $this->attributes['settings'] = \maybe_serialize($settings);
76 }
77
78 public function getSettingsAttribute($settings)
79 {
80 return \maybe_unserialize($settings);
81 }
82
83 public function getMetaAttribute()
84 {
85 return $this->getMeta();
86 }
87
88 public function setBackgroundAttribute($background)
89 {
90 $this->attributes['background'] = \maybe_serialize($background);
91 }
92
93 public function getBackgroundAttribute($background)
94 {
95 return \maybe_unserialize($background);
96 }
97
98 public function tasks()
99 {
100 return $this->hasMany(Task::class, 'board_id');
101 }
102
103 public function completedTasks()
104 {
105 return $this->hasMany(Task::class, 'board_id')
106 ->whereNull('archived_at')
107 ->where('parent_id', null)
108 ->where('status', 'closed');
109 }
110
111 public function stages()
112 {
113 return $this->hasMany(Stage::class, 'board_id')
114 ->whereNull('archived_at')
115 ->orderBy('position', 'asc');
116 }
117
118 public function labels()
119 {
120 return $this->hasMany(Label::class, 'board_id')
121 ->whereNull('archived_at')
122 ->orderBy('position', 'asc');
123 }
124
125 public function users()
126 {
127 return $this->belongsToMany(
128 User::class,
129 'fbs_relations',
130 'object_id',
131 'foreign_id'
132 )->withPivot('settings','preferences')
133 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER)
134 ->withTimestamps();
135 }
136
137
138 public function boardUserEmailNotificationSettings()
139 {
140 return $this->belongsToMany(
141 User::class,
142 'fbs_relations',
143 'object_id',
144 'foreign_id'
145 )->withTimestamps()
146 ->withPivot('settings')
147 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER_EMAIL_NOTIFICATION);
148 }
149
150 public function boardUserNotificationSettings() //will delete later
151 {
152 return $this->belongsToMany(
153 User::class,
154 'fbs_relations',
155 'object_id',
156 'foreign_id'
157 )->withTimestamps()
158 ->withPivot('settings')
159 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER_NOTIFICATION);
160 }
161
162 public function syncUsers($userIds)
163 {
164 $exists = $this->users;
165 $existIds = [];
166 foreach ($exists as $exist) {
167 $existIds[] = $exist->ID;
168 }
169
170 $newIds = array_diff($userIds, $existIds);
171
172 if ($newIds) {
173 $this->users()->attach(
174 $newIds,
175 [
176 'object_type' => Constant::OBJECT_TYPE_BOARD_USER,
177 'settings' => maybe_serialize(Constant::BOARD_USER_SETTINGS),
178 'preferences' => maybe_serialize(Constant::BOARD_NOTIFICATION_TYPES)
179 ]
180 );
181 }
182
183 return $newIds;
184 }
185
186 public static function isBoardExists($boardId)
187 {
188 return self::where('id', $boardId)->exists();
189 }
190
191 public function notifications()
192 {
193 return $this->hasMany(Notification::class, 'object_id', 'id')
194 ->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION);
195 }
196
197 public function comments()
198 {
199 return $this->hasMany(Comment::class, 'board_id', 'id');
200 }
201
202 public function owner()
203 {
204 return $this->belongsTo(User::class, 'created_by');
205 }
206
207 public function scopeByAccessUser($query, $userId)
208 {
209 if (PermissionManager::isAdmin($userId)) {
210 return $query;
211 }
212
213 $user = User::find($userId);
214
215 $boardIds = $user->whichBoards()->pluck('object_id')->toArray();
216
217 if (!$boardIds) {
218 return $query->where('id', 0);
219 }
220
221 return $query->whereIn('id', $boardIds);
222 }
223
224 /**
225 * Exclude template boards (settings['is_template'] === true).
226 * The settings column is serialized text, so both the PHP-serialized
227 * and JSON representations of the flag are matched.
228 */
229 public function scopeExcludeTemplates($query)
230 {
231 return $query->where(function ($query) {
232 $query->whereNull('settings')
233 ->orWhere(function ($subQuery) {
234 $subQuery->where('settings', 'NOT LIKE', '%"is_template";b:1%')
235 ->where('settings', 'NOT LIKE', '%"is_template":true%');
236 });
237 });
238 }
239
240 /**
241 * Only template boards (inverse of scopeExcludeTemplates).
242 */
243 public function scopeOnlyTemplates($query)
244 {
245 return $query->whereNotNull('settings')
246 ->where(function ($subQuery) {
247 $subQuery->where('settings', 'LIKE', '%"is_template";b:1%')
248 ->orWhere('settings', 'LIKE', '%"is_template":true%');
249 });
250 }
251
252 public function getUsers()
253 {
254 return (new UserService())->allFluentBoardsUsers($this->id);
255 }
256
257 public function customFields()
258 {
259 return $this->hasMany(CustomField::class, 'board_id')
260 ->whereNull('archived_at')
261 ->orderBy('position', 'asc');
262 }
263
264 public function getMeta() // get Board Meta only
265 {
266 $meta = Meta::where('object_id', $this->id)
267 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
268 ->get();
269
270 $formattedMeta = [];
271
272 foreach ($meta as $m) {
273 $formattedMeta[$m->key] = $m->value;
274 }
275
276 return $formattedMeta;
277 }
278
279
280
281 public function getMetaByKey($key) // get Board Meta only
282 {
283 $meta = Meta::where('object_id', $this->id)
284 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
285 ->where('key', $key)
286 ->first();
287
288 return $meta->value ?? null;
289 }
290
291 public function updateMeta($key, $value)
292 {
293 $meta = Meta::where('object_id', $this->id)
294 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
295 ->where('key', $key)
296 ->first();
297
298 if ($meta) {
299 $meta->value = $value;
300 $meta->save();
301 } else {
302 $meta = Meta::create([
303 'object_id' => $this->id,
304 'object_type' => Constant::OBJECT_TYPE_BOARD,
305 'key' => $key,
306 'value' => $value,
307 ]);
308 }
309
310 return $meta;
311
312 }
313
314
315 public function activities()
316 {
317 return $this->hasMany(Activity::class, 'object_id')
318 ->where('object_type', Constant::ACTIVITY_BOARD);
319 }
320
321 public function getisUserOnlyViewerAttribute()
322 {
323 $userId = get_current_user_id();
324 if(PermissionManager::isAdmin()) {
325 return false;
326 }
327 $boardPermissions = Relation::where('object_id', $this->id)
328 ->where('foreign_id', $userId)
329 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
330 ->first();
331
332 return $boardPermissions->settings['is_viewer_only'] ?? false;
333 }
334
335 public function removeBoardFromFolder()
336 {
337 $relation = Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
338 ->where('foreign_id', $this->id)
339 ->first();
340
341 if (!$relation) {
342 return;
343 }
344 $relation->delete();
345
346 }
347
348 }
349