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

288 lines 7.4 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'];
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->whereNull('archived_at');
49 $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')
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 private function randomBackground()
224 {
225 $solids = Constant::BOARD_BACKGROUND_DEFAULT_SOLID_COLORS;
226 $solid = $solids[wp_rand(0, count($solids) - 1)];
227
228 return [
229 'id' => $solid['id'],
230 'is_image' => false,
231 'image_url' => null,
232 'color' => $solid['value']
233 ];
234 }
235
236 public function getUsers()
237 {
238 return (new UserService())->allFluentBoardsUsers($this->id);
239 }
240
241 public function customFields()
242 {
243 return $this->hasMany(CustomField::class, 'board_id')
244 ->whereNull('archived_at')
245 ->orderBy('position', 'asc');
246 }
247
248 public function getMeta() // get Board Meta only
249 {
250 $meta = Meta::where('object_id', $this->id)
251 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
252 ->get();
253
254 $formattedMeta = [];
255
256 foreach ($meta as $m) {
257 $formattedMeta[$m->key] = $m->value;
258 }
259
260 return $formattedMeta;
261 }
262
263 public function updateMeta($key, $value)
264 {
265 $meta = Meta::where('object_id', $this->id)
266 ->where('object_type', Constant::OBJECT_TYPE_BOARD)
267 ->where('key', $key)
268 ->first();
269
270 if ($meta) {
271 $meta->value = $value;
272 $meta->save();
273 } else {
274 $meta = Meta::create([
275 'object_id' => $this->id,
276 'object_type' => Constant::OBJECT_TYPE_BOARD,
277 'key' => $key,
278 'value' => $value,
279 ]);
280 }
281
282 return $meta;
283
284 }
285
286
287 }
288