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 / BaseSpace.php

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

465 lines 12.8 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 'hide_members_count' => 'no', // yes / no
302 'members_page_status' => 'members_only', // members_only, everybody, logged_in, admin_only
303 ];
304 }
305
306 public function setSettingsAttribute($value)
307 {
308 $this->attributes['settings'] = maybe_serialize($value);
309 }
310
311 public function getPublicPermissions()
312 {
313 if ($this->privacy == 'public') {
314 return [
315 'can_view_info' => true,
316 'can_view_posts' => true,
317 'can_view_members' => $this->canViewMembers(null),
318 'can_create_post' => false
319 ];
320 }
321
322 return [
323 'can_view_info' => false,
324 'can_view_posts' => false,
325 ];
326 }
327
328 public function getUserPermissions($user = null)
329 {
330 if (!$user) {
331 return $this->getPublicPermissions();
332 }
333
334 return $user->getSpacePermissions($this);
335 }
336
337 public function canViewMembers($user)
338 {
339 $viewStatus = Arr::get($this->settings, 'members_page_status');
340 if ($viewStatus === 'everybody') {
341 return true;
342 }
343
344 if (!$user) {
345 return false;
346 }
347
348 if ($viewStatus === 'logged_in') {
349 return true;
350 }
351
352 if ($viewStatus === 'members_only') {
353 return Helper::isUserInSpace($user->ID, $this->id);
354 }
355
356 return $this->isAdmin($user->ID, true);
357 }
358
359 public function verifyUserPermisson($user, $permission, $exception = true)
360 {
361 $permissions = $this->getUserPermissions($user);
362
363 $hasPermission = $permissions[$permission] ?? false;
364
365 if (!$hasPermission && $exception) {
366 throw new \Exception('Sorry you do not have permission ' . esc_html($permission), 403);
367 }
368
369 return $hasPermission;
370 }
371
372 public function setLockscreen($settingFields)
373 {
374 return $this->updateCustomMeta('lockscreen_settings', $settingFields);
375 }
376
377 public function getPermalink()
378 {
379 if ($this->type == 'community') {
380 return Helper::baseUrl('space/' . $this->slug . '/home');
381 }
382
383 if ($this->type == 'course') {
384 return Helper::baseUrl('course/' . $this->slug . '/lessons');
385 }
386
387 return Helper::baseUrl('/');
388 }
389
390 public function getLockscreen()
391 {
392 return LockscreenService::getLockscreenSettings($this);
393 }
394
395 public function getCustomMeta($key, $default = null)
396 {
397 return Helper::getSpaceMeta($this->id, $key, $default);
398 }
399
400 public function updateCustomMeta($key, $value)
401 {
402 return Helper::updateSpaceMeta($this->id, $key, $value);
403 }
404
405 public function getIconMark($isHtml = true)
406 {
407 if (!$isHtml) {
408 return '';
409 }
410
411 if ($this->logo) {
412 return '<img alt="" src="' . $this->logo . '"/>';
413 }
414
415 if ($imoji = Arr::get($this->settings, 'emoji')) {
416 return '<span class="fcom_emoji">' . $imoji . '</span>';
417 }
418
419 if ($svg = Arr::get($this->settings, 'shape_svg')) {
420 return '<span class="fcom_shape"><i class="el-icon">' . $svg . '</i></span>';
421 }
422
423 return '';
424 }
425
426 public function syncTopics($topicIds)
427 {
428 $topics = Term::where('taxonomy_name', 'post_topic')->whereIn('id', $topicIds)->get();
429 $topicIds = $topics->pluck('id')->toArray();
430 $existIds = [];
431
432 foreach ($topicIds as $topicId) {
433 $relation = Meta::where('object_id', $topicId)
434 ->where('object_type', 'term_space_relation')
435 ->where('meta_key', $this->id)
436 ->first();
437
438 if ($relation) {
439 $existIds[] = $relation->id;
440 continue;
441 }
442
443 // We have to create one
444 $new = Meta::create([
445 'object_id' => $topicId,
446 'meta_key' => $this->id,
447 'object_type' => 'term_space_relation'
448 ]);
449
450 $existIds[] = $new->id;
451 }
452
453 // Remove other relations
454 Meta::whereNotIn('id', $existIds)
455 ->where('object_type', 'term_space_relation')
456 ->where('meta_key', $this->id)
457 ->delete();
458
459 Utility::forgetCache('fluent_community_post_topics');
460
461 return $this;
462 }
463
464 }
465