| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCommunity\App\Functions\Utility; |
| 7 |
use FluentCommunity\Framework\Support\Arr; |
| 8 |
|
| 9 |
/** |
| 10 |
* @property int $id |
| 11 |
* @property int|null $created_by |
| 12 |
* @property int|null $parent_id |
| 13 |
* @property string $title |
| 14 |
* @property string $slug |
| 15 |
* @property string|null $description |
| 16 |
* @property string|null $logo |
| 17 |
* @property string|null $cover_photo |
| 18 |
* @property string|null $type |
| 19 |
* @property string|null $privacy |
| 20 |
* @property string|null $status |
| 21 |
* @property int|null $serial |
| 22 |
* @property array $settings |
| 23 |
* @property string|null $created_at |
| 24 |
* @property string|null $updated_at |
| 25 |
* @property-read mixed $spaces |
| 26 |
*/ |
| 27 |
class SpaceGroup extends Model |
| 28 |
{ |
| 29 |
protected $table = 'fcom_spaces'; |
| 30 |
|
| 31 |
protected static $type = 'space_group'; |
| 32 |
|
| 33 |
protected $guarded = ['id']; |
| 34 |
|
| 35 |
protected $fillable = [ |
| 36 |
'created_by', |
| 37 |
'parent_id', |
| 38 |
'title', |
| 39 |
'slug', |
| 40 |
'description', |
| 41 |
'logo', |
| 42 |
'cover_photo', |
| 43 |
'type', |
| 44 |
'privacy', |
| 45 |
'status', |
| 46 |
'serial', |
| 47 |
'settings' |
| 48 |
]; |
| 49 |
|
| 50 |
protected $searchable = [ |
| 51 |
'title', |
| 52 |
'description' |
| 53 |
]; |
| 54 |
|
| 55 |
public static function boot() |
| 56 |
{ |
| 57 |
parent::boot(); |
| 58 |
static::creating(function ($model) { |
| 59 |
if (empty($model->created_by)) { |
| 60 |
$model->created_by = get_current_user_id(); |
| 61 |
} |
| 62 |
|
| 63 |
if (empty($model->slug)) { |
| 64 |
$slug = sanitize_title($model->title, (string) time()); |
| 65 |
$model->slug = $slug; |
| 66 |
} |
| 67 |
|
| 68 |
$model->type = static::$type; |
| 69 |
}); |
| 70 |
|
| 71 |
static::addGlobalScope('type', function ($query) { |
| 72 |
if (static::$type) { |
| 73 |
$query->where('type', static::$type); |
| 74 |
} |
| 75 |
|
| 76 |
return $query; |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
public function owner() |
| 81 |
{ |
| 82 |
return $this->belongsTo(User::class, 'created_by', 'ID'); |
| 83 |
} |
| 84 |
|
| 85 |
public function scopeSearchBy($query, $search) |
| 86 |
{ |
| 87 |
if ($search) { |
| 88 |
$fields = $this->searchable; |
| 89 |
$query->where(function ($query) use ($fields, $search) { |
| 90 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 91 |
foreach ($fields as $field) { |
| 92 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 93 |
} |
| 94 |
}); |
| 95 |
} |
| 96 |
|
| 97 |
return $query; |
| 98 |
} |
| 99 |
|
| 100 |
public function updateCustomData($data, $removeSrc = false) |
| 101 |
{ |
| 102 |
$deletePhotos = []; |
| 103 |
if (isset($data['logo'])) { |
| 104 |
$deletePhotos[] = $this->logo; |
| 105 |
$this->logo = sanitize_url($data['logo']); |
| 106 |
} |
| 107 |
|
| 108 |
if (isset($data['cover_photo'])) { |
| 109 |
$deletePhotos[] = $this->cover_photo; |
| 110 |
$this->cover_photo = sanitize_url($data['cover_photo']); |
| 111 |
} |
| 112 |
|
| 113 |
if (isset($data['description'])) { |
| 114 |
$this->description = wp_kses_post($data['description']); |
| 115 |
} |
| 116 |
|
| 117 |
if (isset($data['title'])) { |
| 118 |
$this->title = sanitize_text_field($data['title']); |
| 119 |
} |
| 120 |
|
| 121 |
if (isset($data['privacy'])) { |
| 122 |
$this->privacy = sanitize_text_field($data['privacy']); |
| 123 |
} |
| 124 |
|
| 125 |
$deletePhotos = array_filter($deletePhotos); |
| 126 |
|
| 127 |
if ($removeSrc && $deletePhotos) { |
| 128 |
$deletePhotos = array_filter($deletePhotos); |
| 129 |
do_action('fluent_community/remove_medias_by_url', $deletePhotos, [ |
| 130 |
'sub_object_id' => $this->id, |
| 131 |
]); |
| 132 |
} |
| 133 |
|
| 134 |
if (isset($data['settings'])) { |
| 135 |
$this->settings = Arr::only(fluentCommunitySanitizeArray($data['settings']), array_keys($this->defaultSettings())); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
$this->save(); |
| 140 |
|
| 141 |
return $this; |
| 142 |
} |
| 143 |
|
| 144 |
public function getSettingsAttribute($value) |
| 145 |
{ |
| 146 |
$settings = Utility::safeUnserialize($value); |
| 147 |
|
| 148 |
if (!$settings) { |
| 149 |
$settings = []; |
| 150 |
} |
| 151 |
|
| 152 |
return array_merge($this->defaultSettings(), $settings); |
| 153 |
} |
| 154 |
|
| 155 |
public function defaultSettings() |
| 156 |
{ |
| 157 |
return [ |
| 158 |
'hide_members' => 'no', |
| 159 |
'always_show_spaces' => 'yes' |
| 160 |
]; |
| 161 |
} |
| 162 |
|
| 163 |
public function setSettingsAttribute($value) |
| 164 |
{ |
| 165 |
$this->attributes['settings'] = maybe_serialize($value); |
| 166 |
} |
| 167 |
|
| 168 |
public function spaces() |
| 169 |
{ |
| 170 |
return $this->hasMany(BaseSpace::class, 'parent_id', 'id') |
| 171 |
->withoutGlobalScopes(); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|