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

239 lines 6.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\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
12 class Board extends Model
13 {
14 protected $table = 'fbs_boards';
15
16 protected $guarded = ['id'];
17
18 protected $hidden = [
19 'created_at',
20 'updated_at',
21 ];
22
23 protected $fillable = [
24 'parent_id',
25 'title',
26 'description',
27 'type',
28 'currency',
29 'background',
30 'settings',
31 'created_by',
32 'archived_at',
33 ];
34
35 public static function boot()
36 {
37 static::creating(function ($model) {
38 $model->created_by = $model->created_by ?: get_current_user_id();
39 $model->type = $model->type ?: 'to-do'; // default board type is to-do
40 $model->background = $model->background ?: $model->randomBackground();
41 });
42 /* global scope for board type which means only type = to-do will be fetched from everywhere */
43 parent::boot();
44 static::addGlobalScope('type', function (Builder $builder) {
45 $builder = $builder->whereNull('archived_at');
46 $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 setBackgroundAttribute($background)
81 {
82 $this->attributes['background'] = \maybe_serialize($background);
83 }
84
85 public function getBackgroundAttribute($background)
86 {
87 return \maybe_unserialize($background);
88 }
89
90 public function tasks()
91 {
92 return $this->hasMany(Task::class, 'board_id');
93 }
94
95 public function completedTasks()
96 {
97 return $this->hasMany(Task::class, 'board_id')
98 ->whereNull('archived_at')
99 ->where('parent_id', null)
100 ->where('status', 'closed');
101 }
102
103 public function boardTerm()
104 {
105 return $this->hasMany(BoardTerm::class, 'board_id');
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')
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 }
239