| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\XProfile; |
| 6 |
|
| 7 |
class Comment extends Model |
| 8 |
{ |
| 9 |
protected $table = 'fcom_post_comments'; |
| 10 |
|
| 11 |
protected $guarded = ['id']; |
| 12 |
|
| 13 |
protected $fillable = [ |
| 14 |
'user_id', |
| 15 |
'post_id', |
| 16 |
'parent_id', |
| 17 |
'message', |
| 18 |
'message_rendered', |
| 19 |
'meta', |
| 20 |
'type', |
| 21 |
'content_type', |
| 22 |
'status', |
| 23 |
'is_sticky', |
| 24 |
'reactions_count', |
| 25 |
'created_at', |
| 26 |
'updated_at' |
| 27 |
]; |
| 28 |
|
| 29 |
protected $searchable = [ |
| 30 |
'message' |
| 31 |
]; |
| 32 |
|
| 33 |
public static function boot() |
| 34 |
{ |
| 35 |
parent::boot(); |
| 36 |
|
| 37 |
static::creating(function ($model) { |
| 38 |
if (empty($model->user_id)) { |
| 39 |
$model->user_id = get_current_user_id(); |
| 40 |
} |
| 41 |
$model->type = 'comment'; |
| 42 |
}); |
| 43 |
|
| 44 |
static::deleting(function ($comment) { |
| 45 |
Media::where('sub_object_id', $comment->id) |
| 46 |
->where('object_source', 'comment') |
| 47 |
->update([ |
| 48 |
'is_active' => 0 |
| 49 |
]); |
| 50 |
|
| 51 |
$notifications = Notification::where('object_id', $comment->id) |
| 52 |
->where('src_object_type', 'comment') |
| 53 |
->get(); |
| 54 |
|
| 55 |
foreach ($notifications as $notification) { |
| 56 |
$notification->delete(); |
| 57 |
} |
| 58 |
|
| 59 |
$childComments = Comment::where('parent_id', $comment->id) |
| 60 |
->where('post_id', $comment->post_id) |
| 61 |
->get(); |
| 62 |
|
| 63 |
foreach ($childComments as $childComment) { |
| 64 |
$childComment->delete(); |
| 65 |
} |
| 66 |
}); |
| 67 |
|
| 68 |
static::addGlobalScope('type', function ($builder) { |
| 69 |
$builder->where('type', 'comment'); |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
public function scopeSearchBy($query, $search) |
| 74 |
{ |
| 75 |
if ($search) { |
| 76 |
$fields = $this->searchable; |
| 77 |
$query->where(function ($query) use ($fields, $search) { |
| 78 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 79 |
foreach ($fields as $field) { |
| 80 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 81 |
} |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
return $query; |
| 86 |
} |
| 87 |
|
| 88 |
public function user() |
| 89 |
{ |
| 90 |
return $this->belongsTo(User::class, 'user_id', 'ID'); |
| 91 |
} |
| 92 |
|
| 93 |
public function xprofile() |
| 94 |
{ |
| 95 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 96 |
} |
| 97 |
|
| 98 |
public function media() |
| 99 |
{ |
| 100 |
return $this->hasOne(Media::class, 'sub_object_id', 'id'); |
| 101 |
} |
| 102 |
|
| 103 |
public function post() |
| 104 |
{ |
| 105 |
return $this->belongsTo(Feed::class, 'post_id', 'id')->withoutGlobalScopes(); |
| 106 |
} |
| 107 |
|
| 108 |
public function reactions() |
| 109 |
{ |
| 110 |
return $this->hasMany(Reaction::class, 'object_id', 'id') |
| 111 |
->where('object_type', 'comment'); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* Find all the user ids of a child comment who commented on the parent comment including the parent comment author |
| 116 |
*/ |
| 117 |
public function getCommentParentUserIds($lastUserId = 0) |
| 118 |
{ |
| 119 |
if (!$this->parent_id) { |
| 120 |
return []; |
| 121 |
} |
| 122 |
|
| 123 |
$parentComment = Comment::select(['user_id'])->find($this->parent_id); |
| 124 |
$allUserIds = Comment::where('parent_id', $this->parent_id) |
| 125 |
->select(['user_id']) |
| 126 |
->distinct('user_id') |
| 127 |
->when($lastUserId, function ($query) use ($lastUserId) { |
| 128 |
$query->where('user_id', '>', $lastUserId); |
| 129 |
}) |
| 130 |
->get() |
| 131 |
->pluck('user_id') |
| 132 |
->toArray(); |
| 133 |
|
| 134 |
if ($parentComment) { |
| 135 |
if ($parentComment->user_id > $lastUserId) { |
| 136 |
$allUserIds[] = $parentComment->user_id; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return array_values(array_unique($allUserIds)); |
| 141 |
} |
| 142 |
|
| 143 |
public function setMetaAttribute($value) |
| 144 |
{ |
| 145 |
$this->attributes['meta'] = maybe_serialize($value); |
| 146 |
} |
| 147 |
|
| 148 |
public function getMetaAttribute($value) |
| 149 |
{ |
| 150 |
$meta = maybe_unserialize($value); |
| 151 |
|
| 152 |
if (!$meta) { |
| 153 |
$meta = []; |
| 154 |
} |
| 155 |
|
| 156 |
return $meta; |
| 157 |
} |
| 158 |
|
| 159 |
public function getEmailSubject($feed = null) |
| 160 |
{ |
| 161 |
if (!$feed) { |
| 162 |
$feed = $this->post; |
| 163 |
} |
| 164 |
|
| 165 |
if ($this->parent_id) { |
| 166 |
if ($feed->title) { |
| 167 |
/* translators: %1$s is the feed title and %2$s is the comment author name */ |
| 168 |
$emailSubject = \sprintf(__('New reply on comment at %1$s - %2$s', 'fluent-community'), $feed->title, $this->xprofile->display_name); |
| 169 |
} else { |
| 170 |
/* translators: %1$s is the post title and %2$s is the comment author name */ |
| 171 |
$emailSubject = \sprintf(__('New reply of a comment in a post on %1$s - %2$s', 'fluent-community'), $this->post->getHumanExcerpt(40), $this->xprofile->display_name); |
| 172 |
} |
| 173 |
} else { |
| 174 |
if ($feed->title) { |
| 175 |
/* translators: %1$s is the feed title and %2$s is the comment author name */ |
| 176 |
$emailSubject = \sprintf(__('New comment on %1$s - %2$s', 'fluent-community'), $feed->title, $this->xprofile->display_name); |
| 177 |
} else { |
| 178 |
/* translators: %1$s is the post title and %2$s is the comment author name */ |
| 179 |
$emailSubject = \sprintf(__('New comment on a post on %1$s - %2$s', 'fluent-community'), $this->post->getHumanExcerpt(40), $this->xprofile->display_name); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return $emailSubject; |
| 184 |
} |
| 185 |
} |
| 186 |
|