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

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