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

247 lines 6.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 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->whereNull('archived_at');
47 $builder->where('type', '=', 'to-do')
48 ->orWhere('type', '=', 'roadmap');
49 });
50 }
51
52 public static function getColor()
53 {
54 $colors = [
55 '#673AB7', // deep purple
56 '#3F51B5', // indigo
57 '#14508C', // blue
58 '#009688', // teal
59 '#519839', // green
60 '#795548', // brown
61 '#607D8B', // blue grey
62 '#03A9F4', // light blue
63 '#00BCD4', // cyan
64 '#CDDC39', // lime
65 '#838c91', // grey
66 ];
67
68 return $colors[array_rand($colors)];
69 }
70
71 public function setSettingsAttribute($settings)
72 {
73 $this->attributes['settings'] = \maybe_serialize($settings);
74 }
75
76 public function getSettingsAttribute($settings)
77 {
78 return \maybe_unserialize($settings);
79 }
80
81 public function setBackgroundAttribute($background)
82 {
83 $this->attributes['background'] = \maybe_serialize($background);
84 }
85
86 public function getBackgroundAttribute($background)
87 {
88 return \maybe_unserialize($background);
89 }
90
91 public function tasks()
92 {
93 return $this->hasMany(Task::class, 'board_id');
94 }
95
96 public function completedTasks()
97 {
98 return $this->hasMany(Task::class, 'board_id')
99 ->whereNull('archived_at')
100 ->where('parent_id', null)
101 ->where('status', 'closed');
102 }
103
104 public function boardTerm()
105 {
106 return $this->hasMany(BoardTerm::class, 'board_id');
107 }
108
109 public function stages()
110 {
111 return $this->hasMany(Stage::class, 'board_id')
112 ->whereNull('archived_at')
113 ->orderBy('position', 'asc');
114 }
115
116 public function labels()
117 {
118 return $this->hasMany(Label::class, 'board_id')
119 ->whereNull('archived_at')
120 ->orderBy('position', 'asc');
121 }
122
123 public function users()
124 {
125 return $this->belongsToMany(
126 User::class,
127 'fbs_relations',
128 'object_id',
129 'foreign_id'
130 )->withPivot('settings')
131 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER)
132 ->withTimestamps();
133 }
134
135
136 public function boardUserEmailNotificationSettings()
137 {
138 return $this->belongsToMany(
139 User::class,
140 'fbs_relations',
141 'object_id',
142 'foreign_id'
143 )->withTimestamps()
144 ->withPivot('settings')
145 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER_EMAIL_NOTIFICATION);
146 }
147
148 public function boardUserNotificationSettings() //will delete later
149 {
150 return $this->belongsToMany(
151 User::class,
152 'fbs_relations',
153 'object_id',
154 'foreign_id'
155 )->withTimestamps()
156 ->withPivot('settings')
157 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER_NOTIFICATION);
158 }
159
160 public function syncUsers($userIds)
161 {
162 $exists = $this->users;
163 $existIds = [];
164 foreach ($exists as $exist) {
165 $existIds[] = $exist->ID;
166 }
167
168 $newIds = array_diff($userIds, $existIds);
169
170 if ($newIds) {
171 $this->users()->attach(
172 $newIds,
173 [
174 'object_type' => Constant::OBJECT_TYPE_BOARD_USER,
175 'settings' => maybe_serialize(Constant::BOARD_USER_SETTINGS),
176 'preferences' => maybe_serialize(Constant::BOARD_NOTIFICATION_TYPES)
177 ]
178 );
179 }
180
181 return $newIds;
182 }
183
184 public static function isBoardExists($boardId)
185 {
186 return self::where('id', $boardId)->exists();
187 }
188
189 public function notifications()
190 {
191 return $this->hasMany(Notification::class, 'object_id', 'id')
192 ->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION);
193 }
194
195 public function comments()
196 {
197 return $this->hasMany(Comment::class, 'board_id', 'id');
198 }
199
200 public function owner()
201 {
202 return $this->belongsTo(User::class, 'created_by');
203 }
204
205 public function scopeByAccessUser($query, $userId)
206 {
207 if (PermissionManager::isAdmin($userId)) {
208 return $query;
209 }
210
211 $user = User::find($userId);
212
213 $boardIds = $user->whichBoards()->pluck('object_id')->toArray();
214
215 if (!$boardIds) {
216 return $query->where('id', 0);
217 }
218
219 return $query->whereIn('id', $boardIds);
220 }
221 private function randomBackground()
222 {
223 $solids = Constant::BOARD_BACKGROUND_DEFAULT_SOLID_COLORS;
224 $solid = $solids[wp_rand(0, count($solids) - 1)];
225
226 return [
227 'id' => $solid['id'],
228 'is_image' => false,
229 'image_url' => null,
230 'color' => $solid['value']
231 ];
232 }
233
234 public function getUsers()
235 {
236 return (new UserService())->allFluentBoardsUsers($this->id);
237 }
238
239 public function customFields()
240 {
241 return $this->hasMany(CustomField::class, 'board_id')
242 ->whereNull('archived_at')
243 ->orderBy('position', 'asc');
244 }
245
246 }
247