| 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) |
| 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 |
return $membership->pivot->role == 'admin'; |
| 173 |
} |
| 174 |
|
| 175 |
public function updateCustomData($data, $removeSrc = false) |
| 176 |
{ |
| 177 |
$deletePhotos = []; |
| 178 |
if (isset($data['logo'])) { |
| 179 |
$deletePhotos[] = $this->logo; |
| 180 |
$this->logo = sanitize_url($data['logo']); |
| 181 |
} |
| 182 |
|
| 183 |
if (isset($data['cover_photo'])) { |
| 184 |
$deletePhotos[] = $this->cover_photo; |
| 185 |
$this->cover_photo = sanitize_url($data['cover_photo']); |
| 186 |
} |
| 187 |
|
| 188 |
if (isset($data['description'])) { |
| 189 |
$this->description = wp_kses_post($data['description']); |
| 190 |
} |
| 191 |
|
| 192 |
if (isset($data['title'])) { |
| 193 |
$this->title = sanitize_text_field($data['title']); |
| 194 |
} |
| 195 |
|
| 196 |
if (isset($data['privacy'])) { |
| 197 |
$this->privacy = sanitize_text_field($data['privacy']); |
| 198 |
} |
| 199 |
|
| 200 |
if (isset($data['parent_id'])) { |
| 201 |
$group = SpaceGroup::find($data['parent_id']); |
| 202 |
if (!$group) { |
| 203 |
throw new \Exception('Invalid group id', 400); |
| 204 |
} |
| 205 |
|
| 206 |
$this->parent_id = $group->id; |
| 207 |
} |
| 208 |
|
| 209 |
if (isset($data['settings'])) { |
| 210 |
$shapSvg = ''; |
| 211 |
if (!empty($data['settings']['shape_svg'])) { |
| 212 |
$shapSvg = CustomSanitizer::sanitizeSvg($data['settings']['shape_svg']); |
| 213 |
} |
| 214 |
|
| 215 |
$exisitingSetting = $this->settings; |
| 216 |
$settings = Arr::only(fluentCommunitySanitizeArray($data['settings']), array_keys($this->defaultSettings())); |
| 217 |
|
| 218 |
if ($shapSvg) { |
| 219 |
$settings['shape_svg'] = $shapSvg; |
| 220 |
} else if (!empty($settings['emoji'])) { |
| 221 |
$settings['emoji'] = CustomSanitizer::sanitizeEmoji($settings['emoji']); |
| 222 |
} |
| 223 |
|
| 224 |
$settings['links'] = Arr::get($exisitingSetting, 'links', []); |
| 225 |
|
| 226 |
if (isset($settings['og_image'])) { |
| 227 |
$ogImageUrl = Arr::get($settings, 'og_image'); |
| 228 |
if ($ogImageUrl) { |
| 229 |
$ogImageMedia = Helper::getMediaFromUrl($ogImageUrl); |
| 230 |
if ($ogImageMedia && $ogImageMedia->is_active) { |
| 231 |
unset($settings['og_image']); |
| 232 |
} else if ($ogImageMedia) { |
| 233 |
$ogImageMedia->update([ |
| 234 |
'is_active' => true, |
| 235 |
'user_id' => get_current_user_id(), |
| 236 |
'sub_object_id' => $this->id, |
| 237 |
'object_source' => 'space_og_media' |
| 238 |
]); |
| 239 |
$settings['og_image'] = $ogImageMedia->public_url; |
| 240 |
} else { |
| 241 |
$settings['og_image'] = sanitize_url($ogImageUrl); |
| 242 |
} |
| 243 |
} |
| 244 |
} else { |
| 245 |
$deletePhotos[] = Arr::get($exisitingSetting, 'og_image'); |
| 246 |
} |
| 247 |
|
| 248 |
$settings = wp_parse_args($settings, $exisitingSetting); |
| 249 |
$this->settings = $settings; |
| 250 |
} |
| 251 |
|
| 252 |
$deletePhotos = array_filter($deletePhotos); |
| 253 |
if ($removeSrc && $deletePhotos) { |
| 254 |
$deletePhotos = array_filter($deletePhotos); |
| 255 |
do_action('fluent_community/remove_medias_by_url', $deletePhotos, [ |
| 256 |
'sub_object_id' => $this->id, |
| 257 |
]); |
| 258 |
} |
| 259 |
|
| 260 |
$this->save(); |
| 261 |
|
| 262 |
return $this; |
| 263 |
} |
| 264 |
|
| 265 |
public function getSettingsAttribute($value) |
| 266 |
{ |
| 267 |
$settings = maybe_unserialize($value); |
| 268 |
|
| 269 |
if (!$settings) { |
| 270 |
$settings = []; |
| 271 |
} |
| 272 |
|
| 273 |
return wp_parse_args($settings, $this->defaultSettings()); |
| 274 |
} |
| 275 |
|
| 276 |
public function defaultSettings() |
| 277 |
{ |
| 278 |
return [ |
| 279 |
'restricted_post_only' => 'no', |
| 280 |
'emoji' => '', |
| 281 |
'shape_svg' => '', |
| 282 |
'custom_lock_screen' => 'no', |
| 283 |
'can_request_join' => 'no', |
| 284 |
'layout_style' => 'timeline', |
| 285 |
'show_sidebar' => 'yes', |
| 286 |
'og_image' => '', |
| 287 |
'links' => [] |
| 288 |
]; |
| 289 |
} |
| 290 |
|
| 291 |
public function setSettingsAttribute($value) |
| 292 |
{ |
| 293 |
$this->attributes['settings'] = maybe_serialize($value); |
| 294 |
} |
| 295 |
|
| 296 |
public function getPublicPermissions() |
| 297 |
{ |
| 298 |
if ($this->privacy == 'public') { |
| 299 |
return [ |
| 300 |
'can_view_info' => true, |
| 301 |
'can_view_posts' => true, |
| 302 |
'can_view_members' => true, |
| 303 |
'can_create_post' => false |
| 304 |
]; |
| 305 |
} |
| 306 |
|
| 307 |
return [ |
| 308 |
'can_view_info' => false, |
| 309 |
'can_view_posts' => false, |
| 310 |
]; |
| 311 |
} |
| 312 |
|
| 313 |
public function getUserPermissions($user = null) |
| 314 |
{ |
| 315 |
if (!$user) { |
| 316 |
return $this->getPublicPermissions(); |
| 317 |
} |
| 318 |
|
| 319 |
return $user->getSpacePermissions($this); |
| 320 |
} |
| 321 |
|
| 322 |
public function verifyUserPermisson($user, $permission, $exception = true) |
| 323 |
{ |
| 324 |
$permissions = $this->getUserPermissions($user); |
| 325 |
|
| 326 |
$hasPermission = $permissions[$permission] ?? false; |
| 327 |
|
| 328 |
if (!$hasPermission && $exception) { |
| 329 |
throw new \Exception('Sorry you do not have permission ' . esc_html($permission), 403); |
| 330 |
} |
| 331 |
|
| 332 |
return $hasPermission; |
| 333 |
} |
| 334 |
|
| 335 |
public function setLockscreen($settingFields) |
| 336 |
{ |
| 337 |
return $this->updateCustomMeta('lockscreen_settings', $settingFields); |
| 338 |
} |
| 339 |
|
| 340 |
public function getPermalink() |
| 341 |
{ |
| 342 |
if ($this->type == 'community') { |
| 343 |
return Helper::baseUrl('space/' . $this->slug . '/home'); |
| 344 |
} |
| 345 |
|
| 346 |
if ($this->type == 'course') { |
| 347 |
return Helper::baseUrl('course/' . $this->slug . '/lessons'); |
| 348 |
} |
| 349 |
|
| 350 |
return Helper::baseUrl('/'); |
| 351 |
} |
| 352 |
|
| 353 |
public function getLockscreen() |
| 354 |
{ |
| 355 |
return LockscreenService::getLockscreenSettings($this); |
| 356 |
} |
| 357 |
|
| 358 |
public function getCustomMeta($key, $default = null) |
| 359 |
{ |
| 360 |
return Helper::getSpaceMeta($this->id, $key, $default); |
| 361 |
} |
| 362 |
|
| 363 |
public function updateCustomMeta($key, $value) |
| 364 |
{ |
| 365 |
return Helper::updateSpaceMeta($this->id, $key, $value); |
| 366 |
} |
| 367 |
|
| 368 |
public function getIconMark($isHtml = true) |
| 369 |
{ |
| 370 |
if (!$isHtml) { |
| 371 |
return ''; |
| 372 |
} |
| 373 |
|
| 374 |
if ($this->logo) { |
| 375 |
return '<img alt="" src="' . $this->logo . '"/>'; |
| 376 |
} |
| 377 |
|
| 378 |
if ($imoji = Arr::get($this->settings, 'emoji')) { |
| 379 |
return '<span class="fcom_emoji">' . $imoji . '</span>'; |
| 380 |
} |
| 381 |
|
| 382 |
if ($svg = Arr::get($this->settings, 'shape_svg')) { |
| 383 |
return '<span class="fcom_shape"><i class="el-icon">' . $svg . '</i></span>'; |
| 384 |
} |
| 385 |
|
| 386 |
return ''; |
| 387 |
} |
| 388 |
|
| 389 |
public function syncTopics($topicIds) |
| 390 |
{ |
| 391 |
$topics = Term::where('taxonomy_name', 'post_topic')->whereIn('id', $topicIds)->get(); |
| 392 |
$topicIds = $topics->pluck('id')->toArray(); |
| 393 |
$existIds = []; |
| 394 |
|
| 395 |
foreach ($topicIds as $topicId) { |
| 396 |
$relation = Meta::where('object_id', $topicId) |
| 397 |
->where('object_type', 'term_space_relation') |
| 398 |
->where('meta_key', $this->id) |
| 399 |
->first(); |
| 400 |
|
| 401 |
if ($relation) { |
| 402 |
$existIds[] = $relation->id; |
| 403 |
continue; |
| 404 |
} |
| 405 |
|
| 406 |
// We have to create one |
| 407 |
$new = Meta::create([ |
| 408 |
'object_id' => $topicId, |
| 409 |
'meta_key' => $this->id, |
| 410 |
'object_type' => 'term_space_relation' |
| 411 |
]); |
| 412 |
|
| 413 |
$existIds[] = $new->id; |
| 414 |
} |
| 415 |
|
| 416 |
// Remove other relations |
| 417 |
Meta::whereNotIn('id', $existIds) |
| 418 |
->where('object_type', 'term_space_relation') |
| 419 |
->where('meta_key', $this->id) |
| 420 |
->delete(); |
| 421 |
|
| 422 |
Utility::forgetCache('fluent_community_post_topics'); |
| 423 |
|
| 424 |
return $this; |
| 425 |
} |
| 426 |
|
| 427 |
} |
| 428 |
|