| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentBoards\App\Models; |
| 4 | 4 | |
| 5 | -use FluentBoards\App\Services\BoardService; | |
| 6 | 5 | use FluentBoards\App\Services\Constant; |
| 7 | 6 | use FluentBoards\App\Services\PermissionManager; |
| 8 | 7 | use FluentBoards\App\Services\UserService; |
| 9 | 8 | use FluentBoards\Framework\Database\Orm\Builder; |
| 10 | -use FluentBoards\Framework\Support\Arr; | |
| 11 | 9 | use FluentBoardsPro\App\Models\CustomField; |
| 12 | 10 | |
| 13 | 11 | class Board extends Model |
| 14 | 12 | { |
| @@ -34,14 +32,17 @@ | ||
| 34 | 32 | ]; |
| 35 | 33 | |
| 36 | 34 | protected $appends = ['meta', 'isUserOnlyViewer']; |
| 37 | 35 | |
| 36 | + /** | |
| 37 | + * Boot board defaults while preserving empty backgrounds for newly created boards. | |
| 38 | + */ | |
| 38 | 39 | public static function boot() |
| 39 | 40 | { |
| 40 | 41 | static::creating(function ($model) { |
| 41 | 42 | $model->created_by = $model->created_by ?: get_current_user_id(); |
| 42 | 43 | $model->type = $model->type ?: 'to-do'; // default board type is to-do |
| 43 | - $model->background = $model->background ?: $model->randomBackground(); | |
| 44 | + $model->background = $model->background ?: ''; | |
| 44 | 45 | }); |
| 45 | 46 | /* global scope for board type which means only type = to-do will be fetched from everywhere */ |
| 46 | 47 | parent::boot(); |
| 47 | 48 | static::addGlobalScope('type', function (Builder $builder) { |
| @@ -218,21 +219,49 @@ | ||
| 218 | 219 | } |
| 219 | 220 | |
| 220 | 221 | return $query->whereIn('id', $boardIds); |
| 221 | 222 | } |
| 222 | - private function randomBackground() | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Exclude template boards (settings['is_template'] === true). | |
| 226 | + * The settings column is serialized text, so both the PHP-serialized | |
| 227 | + * and JSON representations of the flag are matched. | |
| 228 | + */ | |
| 229 | + public function scopeExcludeTemplates($query) | |
| 223 | 230 | { |
| 224 | - $solids = Constant::BOARD_BACKGROUND_DEFAULT_SOLID_COLORS; | |
| 225 | - $solid = $solids[wp_rand(0, count($solids) - 1)]; | |
| 231 | + return $query->where(function ($query) { | |
| 232 | + $query->whereNull('settings') | |
| 233 | + ->orWhere(function ($subQuery) { | |
| 234 | + $subQuery->where('settings', 'NOT LIKE', '%"is_template";b:1%') | |
| 235 | + ->where('settings', 'NOT LIKE', '%"is_template":true%'); | |
| 236 | + }); | |
| 237 | + }); | |
| 238 | + } | |
| 226 | 239 | |
| 227 | - return [ | |
| 228 | - 'id' => $solid['id'], | |
| 229 | - 'is_image' => false, | |
| 230 | - 'image_url' => null, | |
| 231 | - 'color' => $solid['value'] | |
| 232 | - ]; | |
| 240 | + /** | |
| 241 | + * Limit boards to types supported by the currently loaded plugins. | |
| 242 | + */ | |
| 243 | + public function scopeAvailableInCurrentInstall($query) | |
| 244 | + { | |
| 245 | + if (!defined('FLUENT_ROADMAP')) { | |
| 246 | + return $query->where('type', 'to-do'); | |
| 247 | + } | |
| 248 | + | |
| 249 | + return $query; | |
| 233 | 250 | } |
| 234 | 251 | |
| 252 | + /** | |
| 253 | + * Only template boards (inverse of scopeExcludeTemplates). | |
| 254 | + */ | |
| 255 | + public function scopeOnlyTemplates($query) | |
| 256 | + { | |
| 257 | + return $query->whereNotNull('settings') | |
| 258 | + ->where(function ($subQuery) { | |
| 259 | + $subQuery->where('settings', 'LIKE', '%"is_template";b:1%') | |
| 260 | + ->orWhere('settings', 'LIKE', '%"is_template":true%'); | |
| 261 | + }); | |
| 262 | + } | |
| 263 | + | |
| 235 | 264 | public function getUsers() |
| 236 | 265 | { |
| 237 | 266 | return (new UserService())->allFluentBoardsUsers($this->id); |
| 238 | 267 | } |
| @@ -258,8 +287,20 @@ | ||
| 258 | 287 | |
| 259 | 288 | return $formattedMeta; |
| 260 | 289 | } |
| 261 | 290 | |
| 291 | + | |
| 292 | + | |
| 293 | + public function getMetaByKey($key) // get Board Meta only | |
| 294 | + { | |
| 295 | + $meta = Meta::where('object_id', $this->id) | |
| 296 | + ->where('object_type', Constant::OBJECT_TYPE_BOARD) | |
| 297 | + ->where('key', $key) | |
| 298 | + ->first(); | |
| 299 | + | |
| 300 | + return $meta->value ?? null; | |
| 301 | + } | |
| 302 | + | |
| 262 | 303 | public function updateMeta($key, $value) |
| 263 | 304 | { |
| 264 | 305 | $meta = Meta::where('object_id', $this->id) |
| 265 | 306 | ->where('object_type', Constant::OBJECT_TYPE_BOARD) |
| @@ -281,8 +322,15 @@ | ||
| 281 | 322 | return $meta; |
| 282 | 323 | |
| 283 | 324 | } |
| 284 | 325 | |
| 326 | + | |
| 327 | + public function activities() | |
| 328 | + { | |
| 329 | + return $this->hasMany(Activity::class, 'object_id') | |
| 330 | + ->where('object_type', Constant::ACTIVITY_BOARD); | |
| 331 | + } | |
| 332 | + | |
| 285 | 333 | public function getisUserOnlyViewerAttribute() |
| 286 | 334 | { |
| 287 | 335 | $userId = get_current_user_id(); |
| 288 | 336 | if(PermissionManager::isAdmin()) { |
| @@ -295,6 +343,18 @@ | ||
| 295 | 343 | |
| 296 | 344 | return $boardPermissions->settings['is_viewer_only'] ?? false; |
| 297 | 345 | } |
| 298 | 346 | |
| 347 | + public function removeBoardFromFolder() | |
| 348 | + { | |
| 349 | + $relation = Relation::where('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD) | |
| 350 | + ->where('foreign_id', $this->id) | |
| 351 | + ->first(); | |
| 352 | + | |
| 353 | + if (!$relation) { | |
| 354 | + return; | |
| 355 | + } | |
| 356 | + $relation->delete(); | |
| 357 | + | |
| 358 | + } | |
| 299 | 359 | |
| 300 | 360 | } |