| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\Constant; |
| 6 |
use FluentBoards\Framework\Database\Orm\Builder; |
| 7 |
use FluentBoards\Framework\Support\Arr; |
| 8 |
|
| 9 |
class Folder extends Model |
| 10 |
{ |
| 11 |
protected $table = 'fbs_boards'; |
| 12 |
|
| 13 |
protected $guarded = ['id']; |
| 14 |
|
| 15 |
protected $hidden = [ |
| 16 |
'created_at', |
| 17 |
'updated_at', |
| 18 |
]; |
| 19 |
|
| 20 |
protected $appends = ['meta']; |
| 21 |
|
| 22 |
public static function boot() |
| 23 |
{ |
| 24 |
static::creating(function ($model) { |
| 25 |
$model->created_by = $model->created_by ?: get_current_user_id(); |
| 26 |
$model->type = $model->type ?: Constant::OBJECT_TYPE_FOLDER; |
| 27 |
}); |
| 28 |
|
| 29 |
parent::boot(); |
| 30 |
static::addGlobalScope('type', function (Builder $builder) { |
| 31 |
$builder->where('type', Constant::OBJECT_TYPE_FOLDER); |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
public function setSettingsAttribute($settings) |
| 36 |
{ |
| 37 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 38 |
} |
| 39 |
|
| 40 |
public function getSettingsAttribute($settings) |
| 41 |
{ |
| 42 |
return \maybe_unserialize($settings); |
| 43 |
} |
| 44 |
|
| 45 |
public function setBackgroundAttribute($background) |
| 46 |
{ |
| 47 |
$this->attributes['background'] = \maybe_serialize($background); |
| 48 |
} |
| 49 |
|
| 50 |
public function getBackgroundAttribute($background) |
| 51 |
{ |
| 52 |
return \maybe_unserialize($background); |
| 53 |
} |
| 54 |
|
| 55 |
public function getMetaAttribute() |
| 56 |
{ |
| 57 |
$meta = Meta::where('object_id', $this->id) |
| 58 |
->where('object_type', Constant::OBJECT_TYPE_BOARD) |
| 59 |
->get(); |
| 60 |
|
| 61 |
$formattedMeta = []; |
| 62 |
foreach ($meta as $item) { |
| 63 |
$formattedMeta[$item->key] = $item->value; |
| 64 |
} |
| 65 |
|
| 66 |
return $formattedMeta; |
| 67 |
} |
| 68 |
|
| 69 |
public function parentFolder() |
| 70 |
{ |
| 71 |
return $this->belongsTo(Folder::class, 'parent_id'); |
| 72 |
} |
| 73 |
|
| 74 |
public function subFolders() |
| 75 |
{ |
| 76 |
return $this->hasMany(Folder::class, 'parent_id') |
| 77 |
->whereNull('archived_at'); |
| 78 |
} |
| 79 |
|
| 80 |
public function boards() |
| 81 |
{ |
| 82 |
return $this->belongsToMany( |
| 83 |
Board::class, |
| 84 |
'fbs_relations', |
| 85 |
'object_id', |
| 86 |
'foreign_id' |
| 87 |
)->withTimestamps() |
| 88 |
->withPivot('settings') |
| 89 |
->wherePivot('object_type', Constant::OBJECT_TYPE_FOLDER_BOARD); |
| 90 |
} |
| 91 |
|
| 92 |
public function toArray() |
| 93 |
{ |
| 94 |
return [ |
| 95 |
'id' => $this->id, |
| 96 |
'title' => $this->title, |
| 97 |
'created_by' => $this->created_by, |
| 98 |
'boards_ids' => Arr::pluck($this->boards, 'id'), |
| 99 |
]; |
| 100 |
} |
| 101 |
} |
| 102 |
|