PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.0
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Models / SpaceGroup.php

SpaceGroup.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.7.0, at app/Models/SpaceGroup.php

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