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

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