| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Course\Model; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\Comment; |
| 7 |
use FluentCommunity\App\Models\Model; |
| 8 |
use FluentCommunity\App\Models\Reaction; |
| 9 |
use FluentCommunity\App\Models\Term; |
| 10 |
use FluentCommunity\App\Models\User; |
| 11 |
use FluentCommunity\App\Models\Meta; |
| 12 |
|
| 13 |
class CourseTopic extends Model |
| 14 |
{ |
| 15 |
protected $table = 'fcom_posts'; |
| 16 |
|
| 17 |
protected $guarded = ['id']; |
| 18 |
|
| 19 |
protected $casts = [ |
| 20 |
'comments_count' => 'int', |
| 21 |
'reactions_count' => 'int' |
| 22 |
]; |
| 23 |
|
| 24 |
protected $fillable = [ |
| 25 |
'user_id', |
| 26 |
'title', |
| 27 |
'slug', |
| 28 |
'message', |
| 29 |
'message_rendered', |
| 30 |
'type', |
| 31 |
'space_id', |
| 32 |
'privacy', |
| 33 |
'status', |
| 34 |
'featured_image', |
| 35 |
'is_sticky', |
| 36 |
'expired_at', |
| 37 |
'comments_count', |
| 38 |
'reactions_count', |
| 39 |
'meta', |
| 40 |
'priority', |
| 41 |
'scheduled_at' |
| 42 |
]; |
| 43 |
|
| 44 |
protected $searchable = [ |
| 45 |
'message', |
| 46 |
'title' |
| 47 |
]; |
| 48 |
|
| 49 |
public static $publicColumns = [ |
| 50 |
'id', 'slug', 'title', 'message_rendered', 'featured_image', 'created_at', 'privacy', 'type', 'status', 'slug', 'space_id', 'user_id', 'meta', 'comments_count', 'reactions_count' |
| 51 |
]; |
| 52 |
|
| 53 |
protected static $type = 'course_section'; |
| 54 |
|
| 55 |
public static function boot() |
| 56 |
{ |
| 57 |
parent::boot(); |
| 58 |
|
| 59 |
static::creating(function ($model) { |
| 60 |
$model->user_id = get_current_user_id(); |
| 61 |
if (empty($model->slug)) { |
| 62 |
$model->slug = self::generateNewSlug($model); |
| 63 |
} |
| 64 |
|
| 65 |
$model->type = self::$type; |
| 66 |
|
| 67 |
if (empty($model->meta)) { |
| 68 |
$model->meta = self::getDefaultMeta(); |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
static::addGlobalScope('type', function ($builder) { |
| 73 |
$builder->where('type', self::$type); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
public function lessons() |
| 78 |
{ |
| 79 |
return $this->hasMany(CourseLesson::class, 'parent_id')->orderBy('priority', 'ASC')->orderBy('id', 'ASC'); |
| 80 |
} |
| 81 |
|
| 82 |
protected static function generateNewSlug($newModel) |
| 83 |
{ |
| 84 |
$slug = Utility::slugify($newModel->title, 'topic'); |
| 85 |
|
| 86 |
$exist = self::where('slug', $slug) |
| 87 |
->exists(); |
| 88 |
|
| 89 |
if ($exist) { |
| 90 |
$slug = $slug . '-' . time(); |
| 91 |
} |
| 92 |
|
| 93 |
return $slug; |
| 94 |
} |
| 95 |
|
| 96 |
protected static function getDefaultMeta() |
| 97 |
{ |
| 98 |
return [ |
| 99 |
'preview_data' => null |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function setMetaAttribute($value) |
| 104 |
{ |
| 105 |
$this->attributes['meta'] = maybe_serialize($value); |
| 106 |
} |
| 107 |
|
| 108 |
public function getMetaAttribute($value) |
| 109 |
{ |
| 110 |
$meta = Utility::safeUnserialize($value); |
| 111 |
|
| 112 |
if (!$meta) { |
| 113 |
$meta = []; |
| 114 |
} |
| 115 |
|
| 116 |
return $meta; |
| 117 |
} |
| 118 |
|
| 119 |
public function scopeSearchBy($query, $search) |
| 120 |
{ |
| 121 |
if (!$search) { |
| 122 |
return $query; |
| 123 |
} |
| 124 |
|
| 125 |
$fields = $this->searchable; |
| 126 |
$query->where(function ($query) use ($fields, $search) { |
| 127 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 128 |
foreach ($fields as $field) { |
| 129 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
return $query; |
| 134 |
} |
| 135 |
|
| 136 |
public function owner() |
| 137 |
{ |
| 138 |
return $this->belongsTo(User::class, 'user_id', 'ID'); |
| 139 |
} |
| 140 |
|
| 141 |
public function course() |
| 142 |
{ |
| 143 |
return $this->belongsTo(Course::class, 'space_id', 'id'); |
| 144 |
} |
| 145 |
|
| 146 |
public function comments() |
| 147 |
{ |
| 148 |
return $this->hasMany(Comment::class, 'post_id', 'id'); |
| 149 |
} |
| 150 |
|
| 151 |
public function reactions() |
| 152 |
{ |
| 153 |
return $this->hasMany(Reaction::class, 'object_id', 'id') |
| 154 |
->where('object_type', 'feed'); |
| 155 |
} |
| 156 |
|
| 157 |
public function terms() |
| 158 |
{ |
| 159 |
return $this->belongsToMany(Term::class, 'fcom_term_feed', 'post_id', 'term_id'); |
| 160 |
} |
| 161 |
|
| 162 |
public function hasUserReact($userId, $type = 'like') |
| 163 |
{ |
| 164 |
if (!$userId) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
return (bool)Reaction::where('object_id', $this->id) |
| 169 |
->select(['id']) |
| 170 |
->where('object_type', 'feed') |
| 171 |
->where('user_id', $userId) |
| 172 |
->where('type', $type) |
| 173 |
->first(); |
| 174 |
} |
| 175 |
|
| 176 |
public function updateCustomMeta($key, $value) |
| 177 |
{ |
| 178 |
$exist = Meta::where('object_id', $this->id) |
| 179 |
->where('object_type', 'course_topic') |
| 180 |
->where('meta_key', $key) |
| 181 |
->first(); |
| 182 |
|
| 183 |
if ($exist) { |
| 184 |
$exist->value = $value; |
| 185 |
$exist->save(); |
| 186 |
} else { |
| 187 |
Meta::create([ |
| 188 |
'object_id' => $this->id, |
| 189 |
'object_type' => 'course_topic', |
| 190 |
'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key |
| 191 |
'value' => $value |
| 192 |
]); |
| 193 |
} |
| 194 |
|
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
public function getCustomMeta($key, $default = null) |
| 199 |
{ |
| 200 |
$exist = Meta::where('object_id', $this->id) |
| 201 |
->where('object_type', 'course_topic') |
| 202 |
->where('meta_key', $key) |
| 203 |
->first(); |
| 204 |
|
| 205 |
if ($exist) { |
| 206 |
return $exist->value; |
| 207 |
} |
| 208 |
|
| 209 |
return $default; |
| 210 |
} |
| 211 |
|
| 212 |
public function getHumanExcerpt($length = 40) |
| 213 |
{ |
| 214 |
$content = $this->title; |
| 215 |
|
| 216 |
if (!$content) { |
| 217 |
$content = $this->message; |
| 218 |
if (!$content) { |
| 219 |
return ''; |
| 220 |
} |
| 221 |
// remove all tags |
| 222 |
$content = wp_strip_all_tags($content); |
| 223 |
// remove new lines and tabs |
| 224 |
$content = str_replace(["\r", "\n", "\t"], ' ', $content); |
| 225 |
// remove multiple spaces |
| 226 |
$content = preg_replace('/\s+/', ' ', $content); |
| 227 |
|
| 228 |
// trim |
| 229 |
$content = trim($content); |
| 230 |
} |
| 231 |
|
| 232 |
if (!$content) { |
| 233 |
return ''; |
| 234 |
} |
| 235 |
|
| 236 |
// return the first $length chars of the content with ... at the end |
| 237 |
return mb_substr($content, 0, $length) . '...'; |
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|