PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.98
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.98
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 1.0.98, at app/Models/SpaceGroup.php

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