PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.0.4
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.0.4
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.4, at app/Models/Board.php

361 lines 9.5 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 * Limit boards to types supported by the currently loaded plugins.
242 */
243 public function scopeAvailableInCurrentInstall($query)
244 {
245 if (!defined('FLUENT_ROADMAP')) {
246 return $query->where('type', 'to-do');
247 }
248
249 return $query;
250 }
251
252 /**
253 * Only template boards (inverse of scopeExcludeTemplates).
254 */
255 public function scopeOnlyTemplates($query)
256 {
257 return $query->whereNotNull('settings')
258 ->where(function ($subQuery) {
259 $subQuery->where('settings', 'LIKE', '%"is_template";b:1%')
260 ->orWhere('settings', 'LIKE', '%"is_template":true%');
261 });
262 }
263
264 public function getUsers()
265 {
266 return (new UserService())->allFluentBoardsUsers($this->id);
267 }
268
269 public function customFields()
270 {
271 return $this->hasMany(CustomField::class, 'board_id')
272 ->whereNull('archived_at')
273 ->orderBy('position', 'asc');
274 }
275
276 public function getMeta() // get Board Meta only
277 {
278 $meta = Meta::where('object_id', $this->id)
279 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
280 ->get();
281
282 $formattedMeta = [];
283
284 foreach ($meta as $m) {
285 $formattedMeta[$m->key] = $m->value;
286 }
287
288 return $formattedMeta;
289 }
290
291
292
293 public function getMetaByKey($key) // get Board Meta only
294 {
295 $meta = Meta::where('object_id', $this->id)
296 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
297 ->where('key', $key)
298 ->first();
299
300 return $meta->value ?? null;
301 }
302
303 public function updateMeta($key, $value)
304 {
305 $meta = Meta::where('object_id', $this->id)
306 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
307 ->where('key', $key)
308 ->first();
309
310 if ($meta) {
311 $meta->value = $value;
312 $meta->save();
313 } else {
314 $meta = Meta::create([
315 'object_id' => $this->id,
316 'object_type' => Constant::OBJECT_TYPE_BOARD,
317 'key' => $key,
318 'value' => $value,
319 ]);
320 }
321
322 return $meta;
323
324 }
325
326
327 public function activities()
328 {
329 return $this->hasMany(Activity::class, 'object_id')
330 ->where('object_type', Constant::ACTIVITY_BOARD);
331 }
332
333 public function getisUserOnlyViewerAttribute()
334 {
335 $userId = get_current_user_id();
336 if(PermissionManager::isAdmin()) {
337 return false;
338 }
339 $boardPermissions = Relation::where('object_id', $this->id)
340 ->where('foreign_id', $userId)
341 ->where('object_type', Constant::OBJECT_TYPE_BOARD_USER)
342 ->first();
343
344 return $boardPermissions->settings['is_viewer_only'] ?? false;
345 }
346
347 public function removeBoardFromFolder()
348 {
349 $relation = Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD)
350 ->where('foreign_id', $this->id)
351 ->first();
352
353 if (!$relation) {
354 return;
355 }
356 $relation->delete();
357
358 }
359
360 }
361