PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.95
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.95
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 / BaseSpace.php

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

464 lines 12.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 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Services\CustomSanitizer;
7 use FluentCommunity\App\Services\LockscreenService;
8 use FluentCommunity\App\Services\Helper;
9 use FluentCommunity\Framework\Support\Arr;
10
11 class BaseSpace extends Model
12 {
13 protected $table = 'fcom_spaces';
14
15 protected static $type = 'community';
16
17 protected $guarded = ['id'];
18
19 protected $fillable = [
20 'created_by',
21 'parent_id',
22 'title',
23 'slug',
24 'description',
25 'logo',
26 'cover_photo',
27 'type',
28 'privacy',
29 'status',
30 'serial',
31 'settings'
32 ];
33
34 protected $searchable = [
35 'title',
36 'description'
37 ];
38
39 public static function boot()
40 {
41 parent::boot();
42 static::creating(function ($model) {
43 if (empty($model->created_by)) {
44 $model->created_by = get_current_user_id();
45 }
46
47 if (empty($model->slug)) {
48 $slug = sanitize_title($model->tilte, time());
49 $model->slug = $slug;
50 }
51 $model->type = static::$type;
52 });
53
54 static::addGlobalScope('type', function ($query) {
55 if (static::$type) {
56 $query->where('type', static::$type);
57 }
58
59 return $query;
60 });
61
62 static::deleting(function ($space) {
63 Media::where('sub_object_id', $space->id)
64 ->whereIn('object_source', ['space_logo', 'space_cover_photo'])
65 ->update([
66 'is_active' => 0
67 ]);
68 });
69 }
70
71 public function owner()
72 {
73 return $this->belongsTo(User::class, 'created_by', 'ID');
74 }
75
76 public function space_pivot()
77 {
78 return $this->belongsTo(SpaceUserPivot::class, 'id', 'space_id');
79 }
80
81 public function scopeSearchBy($query, $search)
82 {
83 if ($search) {
84 $fields = $this->searchable;
85 $query->where(function ($query) use ($fields, $search) {
86 $query->where(array_shift($fields), 'LIKE', "%$search%");
87 foreach ($fields as $field) {
88 $query->orWhere($field, 'LIKE', "$search%");
89 }
90 });
91 }
92
93 return $query;
94 }
95
96 public function scopeFilterByUserId($query, $userId)
97 {
98 if (!$userId) {
99 return $query->where('privacy', 'public');
100 }
101
102 $ids = get_user_meta($userId, '_fcom_space_ids', true);
103
104 if (!$ids) {
105 return $query->where('privacy', 'public');
106 }
107
108 return $query->whereIn('id', $ids);
109 }
110
111 public function scopeByUserAccess($query, $userId)
112 {
113 if (!$userId) {
114 return $query->where('privacy', 'public');
115 }
116
117 return $this->where(function ($query) use ($userId) {
118 return $query->where('privacy', 'public')
119 ->orWhereHas('members', function ($query) use ($userId) {
120 return $query->where('user_id', $userId);
121 });
122 });
123 }
124
125 public function posts()
126 {
127 return $this->hasMany(Feed::class, 'space_id', 'id');
128 }
129
130 public function members()
131 {
132 return $this->belongsToMany(User::class, 'fcom_space_user', 'space_id', 'user_id')
133 ->withPivot(['role', 'created_at', 'status']);
134 }
135
136 public function x_members()
137 {
138 return $this->belongsToMany(XProfile::class, 'fcom_space_user', 'space_id', 'user_id', 'id', 'user_id')
139 ->withPivot(['role', 'created_at', 'status']);
140 }
141
142 public function group()
143 {
144 return $this->belongsTo(SpaceGroup::class, 'parent_id', 'id');
145 }
146
147 public function getMembership($userId)
148 {
149 if (!$userId) {
150 return null;
151 }
152
153 return $this->members()->where('user_id', $userId)->first();
154 }
155
156 public function isCourseSpace()
157 {
158 return $this->type == 'course';
159 }
160
161 public function isAdmin($userId, $checkModerator = false)
162 {
163 if (Helper::isSiteAdmin($userId)) {
164 return true;
165 }
166
167 $membership = $this->getMembership($userId);
168 if (!$membership) {
169 return false;
170 }
171
172 $roles = ['admin'];
173
174 if ($checkModerator) {
175 $roles[] = 'moderator';
176 }
177
178 return in_array($membership->pivot->role, $roles);
179 }
180
181 public function updateCustomData($data, $removeSrc = false)
182 {
183 $deletePhotos = [];
184 if (isset($data['logo'])) {
185 $deletePhotos[] = $this->logo;
186 $this->logo = sanitize_url($data['logo']);
187 }
188
189 if (isset($data['cover_photo'])) {
190 $deletePhotos[] = $this->cover_photo;
191 $this->cover_photo = sanitize_url($data['cover_photo']);
192 }
193
194 if (isset($data['description'])) {
195 $this->description = wp_kses_post($data['description']);
196 }
197
198 if (isset($data['title'])) {
199 $this->title = sanitize_text_field($data['title']);
200 }
201
202 if (isset($data['privacy'])) {
203 $this->privacy = sanitize_text_field($data['privacy']);
204 }
205
206 if (isset($data['parent_id'])) {
207 $group = SpaceGroup::find($data['parent_id']);
208 if (!$group) {
209 throw new \Exception('Invalid group id', 400);
210 }
211
212 $this->parent_id = $group->id;
213 }
214
215 if (isset($data['settings'])) {
216 $shapSvg = '';
217 if (!empty($data['settings']['shape_svg'])) {
218 $shapSvg = CustomSanitizer::sanitizeSvg($data['settings']['shape_svg']);
219 }
220
221 $exisitingSetting = $this->settings;
222 $settings = Arr::only(fluentCommunitySanitizeArray($data['settings']), array_keys($this->defaultSettings()));
223
224 if ($shapSvg) {
225 $settings['shape_svg'] = $shapSvg;
226 } else if (!empty($settings['emoji'])) {
227 $settings['emoji'] = CustomSanitizer::sanitizeEmoji($settings['emoji']);
228 }
229
230 $settings['links'] = Arr::get($exisitingSetting, 'links', []);
231
232 if (isset($settings['og_image'])) {
233 $ogImageUrl = Arr::get($settings, 'og_image');
234 if ($ogImageUrl) {
235 $ogImageMedia = Helper::getMediaFromUrl($ogImageUrl);
236 if ($ogImageMedia && $ogImageMedia->is_active) {
237 unset($settings['og_image']);
238 } else if ($ogImageMedia) {
239 $ogImageMedia->update([
240 'is_active' => true,
241 'user_id' => get_current_user_id(),
242 'sub_object_id' => $this->id,
243 'object_source' => 'space_og_media'
244 ]);
245 $settings['og_image'] = $ogImageMedia->public_url;
246 } else {
247 $settings['og_image'] = sanitize_url($ogImageUrl);
248 }
249 }
250 } else {
251 $deletePhotos[] = Arr::get($exisitingSetting, 'og_image');
252 }
253
254 $settings = wp_parse_args($settings, $exisitingSetting);
255 $this->settings = $settings;
256 }
257
258 $deletePhotos = array_filter($deletePhotos);
259 if ($removeSrc && $deletePhotos) {
260 $deletePhotos = array_filter($deletePhotos);
261 do_action('fluent_community/remove_medias_by_url', $deletePhotos, [
262 'sub_object_id' => $this->id,
263 ]);
264 }
265
266 $dirty = $this->getDirty();
267
268 if ($dirty) {
269 $this->save();
270 if ($this->type == 'community') {
271 do_action('fluent_community/space/updated', $this, $dirty);
272 }
273 }
274
275 return $this;
276 }
277
278 public function getSettingsAttribute($value)
279 {
280 $settings = maybe_unserialize($value);
281
282 if (!$settings) {
283 $settings = [];
284 }
285
286 return wp_parse_args($settings, $this->defaultSettings());
287 }
288
289 public function defaultSettings()
290 {
291 return [
292 'restricted_post_only' => 'no',
293 'emoji' => '',
294 'shape_svg' => '',
295 'custom_lock_screen' => 'no',
296 'can_request_join' => 'no',
297 'layout_style' => 'timeline',
298 'show_sidebar' => 'yes',
299 'og_image' => '',
300 'links' => [],
301 'members_page_status' => 'members_only', // members_only, everybody, logged_in, admin_only
302 ];
303 }
304
305 public function setSettingsAttribute($value)
306 {
307 $this->attributes['settings'] = maybe_serialize($value);
308 }
309
310 public function getPublicPermissions()
311 {
312 if ($this->privacy == 'public') {
313 return [
314 'can_view_info' => true,
315 'can_view_posts' => true,
316 'can_view_members' => $this->canViewMembers(null),
317 'can_create_post' => false
318 ];
319 }
320
321 return [
322 'can_view_info' => false,
323 'can_view_posts' => false,
324 ];
325 }
326
327 public function getUserPermissions($user = null)
328 {
329 if (!$user) {
330 return $this->getPublicPermissions();
331 }
332
333 return $user->getSpacePermissions($this);
334 }
335
336 public function canViewMembers($user)
337 {
338 $viewStatus = Arr::get($this->settings, 'members_page_status');
339 if ($viewStatus === 'everybody') {
340 return true;
341 }
342
343 if (!$user) {
344 return false;
345 }
346
347 if ($viewStatus === 'logged_in') {
348 return true;
349 }
350
351 if ($viewStatus === 'members_only') {
352 return Helper::isUserInSpace($user->ID, $this->id);
353 }
354
355 return $this->isAdmin($user->ID, true);
356 }
357
358 public function verifyUserPermisson($user, $permission, $exception = true)
359 {
360 $permissions = $this->getUserPermissions($user);
361
362 $hasPermission = $permissions[$permission] ?? false;
363
364 if (!$hasPermission && $exception) {
365 throw new \Exception('Sorry you do not have permission ' . esc_html($permission), 403);
366 }
367
368 return $hasPermission;
369 }
370
371 public function setLockscreen($settingFields)
372 {
373 return $this->updateCustomMeta('lockscreen_settings', $settingFields);
374 }
375
376 public function getPermalink()
377 {
378 if ($this->type == 'community') {
379 return Helper::baseUrl('space/' . $this->slug . '/home');
380 }
381
382 if ($this->type == 'course') {
383 return Helper::baseUrl('course/' . $this->slug . '/lessons');
384 }
385
386 return Helper::baseUrl('/');
387 }
388
389 public function getLockscreen()
390 {
391 return LockscreenService::getLockscreenSettings($this);
392 }
393
394 public function getCustomMeta($key, $default = null)
395 {
396 return Helper::getSpaceMeta($this->id, $key, $default);
397 }
398
399 public function updateCustomMeta($key, $value)
400 {
401 return Helper::updateSpaceMeta($this->id, $key, $value);
402 }
403
404 public function getIconMark($isHtml = true)
405 {
406 if (!$isHtml) {
407 return '';
408 }
409
410 if ($this->logo) {
411 return '<img alt="" src="' . $this->logo . '"/>';
412 }
413
414 if ($imoji = Arr::get($this->settings, 'emoji')) {
415 return '<span class="fcom_emoji">' . $imoji . '</span>';
416 }
417
418 if ($svg = Arr::get($this->settings, 'shape_svg')) {
419 return '<span class="fcom_shape"><i class="el-icon">' . $svg . '</i></span>';
420 }
421
422 return '';
423 }
424
425 public function syncTopics($topicIds)
426 {
427 $topics = Term::where('taxonomy_name', 'post_topic')->whereIn('id', $topicIds)->get();
428 $topicIds = $topics->pluck('id')->toArray();
429 $existIds = [];
430
431 foreach ($topicIds as $topicId) {
432 $relation = Meta::where('object_id', $topicId)
433 ->where('object_type', 'term_space_relation')
434 ->where('meta_key', $this->id)
435 ->first();
436
437 if ($relation) {
438 $existIds[] = $relation->id;
439 continue;
440 }
441
442 // We have to create one
443 $new = Meta::create([
444 'object_id' => $topicId,
445 'meta_key' => $this->id,
446 'object_type' => 'term_space_relation'
447 ]);
448
449 $existIds[] = $new->id;
450 }
451
452 // Remove other relations
453 Meta::whereNotIn('id', $existIds)
454 ->where('object_type', 'term_space_relation')
455 ->where('meta_key', $this->id)
456 ->delete();
457
458 Utility::forgetCache('fluent_community_post_topics');
459
460 return $this;
461 }
462
463 }
464