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

238 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('status', 'closed');
100 }
101
102 public function boardTerm()
103 {
104 return $this->hasMany(BoardTerm::class, 'board_id');
105 }
106
107 public function stages()
108 {
109 return $this->hasMany(Stage::class, 'board_id')
110 ->whereNull('archived_at')
111 ->orderBy('position', 'asc');
112 }
113
114 public function labels()
115 {
116 return $this->hasMany(Label::class, 'board_id')
117 ->whereNull('archived_at')
118 ->orderBy('position', 'asc');
119 }
120
121 public function users()
122 {
123 return $this->belongsToMany(
124 User::class,
125 'fbs_relations',
126 'object_id',
127 'foreign_id'
128 )->withPivot('settings')
129 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER)
130 ->withTimestamps();
131 }
132
133
134 public function boardUserEmailNotificationSettings()
135 {
136 return $this->belongsToMany(
137 User::class,
138 'fbs_relations',
139 'object_id',
140 'foreign_id'
141 )->withTimestamps()
142 ->withPivot('settings')
143 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER_EMAIL_NOTIFICATION);
144 }
145
146 public function boardUserNotificationSettings() //will delete later
147 {
148 return $this->belongsToMany(
149 User::class,
150 'fbs_relations',
151 'object_id',
152 'foreign_id'
153 )->withTimestamps()
154 ->withPivot('settings')
155 ->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER_NOTIFICATION);
156 }
157
158 public function syncUsers($userIds)
159 {
160 $exists = $this->users;
161 $existIds = [];
162 foreach ($exists as $exist) {
163 $existIds[] = $exist->ID;
164 }
165
166 $newIds = array_diff($userIds, $existIds);
167
168 if ($newIds) {
169 $this->users()->attach(
170 $newIds,
171 [
172 'object_type' => Constant::OBJECT_TYPE_BOARD_USER,
173 'settings' => maybe_serialize(Constant::BOARD_USER_SETTINGS),
174 'preferences' => maybe_serialize(Constant::BOARD_NOTIFICATION_TYPES)
175 ]
176 );
177 }
178
179 return $newIds;
180 }
181
182 public static function isBoardExists($boardId)
183 {
184 return self::where('id', $boardId)->exists();
185 }
186
187 public function notifications()
188 {
189 return $this->hasMany(Notification::class, 'object_id', 'id')
190 ->where('object_type', Constant::OBJECT_TYPE_BOARD_NOTIFICATION);
191 }
192
193 public function comments()
194 {
195 return $this->hasMany(Comment::class, 'board_id', 'id');
196 }
197
198 public function owner()
199 {
200 return $this->belongsTo(User::class, 'created_by');
201 }
202
203 public function scopeByAccessUser($query, $userId)
204 {
205 if (PermissionManager::isAdmin($userId)) {
206 return $query;
207 }
208
209 $user = User::find($userId);
210
211 $boardIds = $user->whichBoards()->pluck('object_id')->toArray();
212
213 if (!$boardIds) {
214 return $query->where('id', 0);
215 }
216
217 return $query->whereIn('id', $boardIds);
218 }
219 private function randomBackground()
220 {
221 $solids = Constant::BOARD_BACKGROUND_DEFAULT_SOLID_COLORS;
222 $solid = $solids[wp_rand(0, count($solids) - 1)];
223
224 return [
225 'id' => $solid['id'],
226 'is_image' => false,
227 'image_url' => null,
228 'color' => $solid['value']
229 ];
230 }
231
232 public function getUsers()
233 {
234 return (new UserService())->allFluentBoardsUsers($this->id);
235 }
236
237 }
238