| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\XProfile; |
| 7 |
use FluentCommunity\App\Models\Activity; |
| 8 |
use FluentCommunity\App\Services\FeedsHelper; |
| 9 |
use FluentCommunity\App\Services\Helper; |
| 10 |
|
| 11 |
class Comment extends Model |
| 12 |
{ |
| 13 |
protected $table = 'fcom_post_comments'; |
| 14 |
|
| 15 |
protected $guarded = ['id']; |
| 16 |
|
| 17 |
protected $casts = [ |
| 18 |
'is_sticky' => 'int' |
| 19 |
]; |
| 20 |
|
| 21 |
protected $fillable = [ |
| 22 |
'user_id', |
| 23 |
'post_id', |
| 24 |
'parent_id', |
| 25 |
'message', |
| 26 |
'message_rendered', |
| 27 |
'meta', |
| 28 |
'type', |
| 29 |
'content_type', |
| 30 |
'status', |
| 31 |
'is_sticky', |
| 32 |
'reactions_count', |
| 33 |
'created_at', |
| 34 |
'updated_at' |
| 35 |
]; |
| 36 |
|
| 37 |
protected $searchable = [ |
| 38 |
'message' |
| 39 |
]; |
| 40 |
|
| 41 |
public static function boot() |
| 42 |
{ |
| 43 |
parent::boot(); |
| 44 |
|
| 45 |
static::creating(function ($model) { |
| 46 |
if (empty($model->user_id)) { |
| 47 |
$model->user_id = get_current_user_id(); |
| 48 |
} |
| 49 |
$model->type = 'comment'; |
| 50 |
|
| 51 |
if(empty($model->status)) { |
| 52 |
$model->status = 'published'; |
| 53 |
} |
| 54 |
|
| 55 |
}); |
| 56 |
|
| 57 |
static::deleting(function ($comment) { |
| 58 |
Media::where('sub_object_id', $comment->id) |
| 59 |
->where('object_source', 'comment') |
| 60 |
->update([ |
| 61 |
'is_active' => 0 |
| 62 |
]); |
| 63 |
|
| 64 |
Activity::where('feed_id', $comment->post_id) |
| 65 |
->where('action_name', 'comment_added') |
| 66 |
->where('related_id', $comment->id) |
| 67 |
->delete(); |
| 68 |
|
| 69 |
$notifications = Notification::where('object_id', $comment->id) |
| 70 |
->where('src_object_type', 'comment') |
| 71 |
->get(); |
| 72 |
|
| 73 |
foreach ($notifications as $notification) { |
| 74 |
$notification->delete(); |
| 75 |
} |
| 76 |
|
| 77 |
$childComments = Comment::where('parent_id', $comment->id) |
| 78 |
->where('post_id', $comment->post_id) |
| 79 |
->get(); |
| 80 |
|
| 81 |
foreach ($childComments as $childComment) { |
| 82 |
$childComment->delete(); |
| 83 |
} |
| 84 |
}); |
| 85 |
|
| 86 |
static::addGlobalScope('type', function ($builder) { |
| 87 |
$builder->where('type', 'comment'); |
| 88 |
}); |
| 89 |
} |
| 90 |
|
| 91 |
public function scopeSearchBy($query, $search) |
| 92 |
{ |
| 93 |
if ($search) { |
| 94 |
$fields = $this->searchable; |
| 95 |
$query->where(function ($query) use ($fields, $search) { |
| 96 |
$query->where(array_shift($fields), 'LIKE', "%$search%"); |
| 97 |
foreach ($fields as $field) { |
| 98 |
$query->orWhere($field, 'LIKE', "$search%"); |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
return $query; |
| 104 |
} |
| 105 |
|
| 106 |
public function user() |
| 107 |
{ |
| 108 |
return $this->belongsTo(User::class, 'user_id', 'ID'); |
| 109 |
} |
| 110 |
|
| 111 |
public function xprofile() |
| 112 |
{ |
| 113 |
return $this->belongsTo(XProfile::class, 'user_id', 'user_id'); |
| 114 |
} |
| 115 |
|
| 116 |
public function media() |
| 117 |
{ |
| 118 |
return $this->hasOne(Media::class, 'sub_object_id', 'id'); |
| 119 |
} |
| 120 |
|
| 121 |
public function post() |
| 122 |
{ |
| 123 |
return $this->belongsTo(Feed::class, 'post_id', 'id')->withoutGlobalScopes(); |
| 124 |
} |
| 125 |
|
| 126 |
public function space() |
| 127 |
{ |
| 128 |
return $this->hasOneThrough( |
| 129 |
BaseSpace::class, |
| 130 |
Feed::class, |
| 131 |
'id', // Foreign key on the feeds table |
| 132 |
'id', // Foreign key on the spaces table |
| 133 |
'post_id', // Local key on the comments table |
| 134 |
'space_id' // Local key on the feeds table |
| 135 |
)->withoutGlobalScopes(); |
| 136 |
} |
| 137 |
|
| 138 |
public function reactions() |
| 139 |
{ |
| 140 |
return $this->hasMany(Reaction::class, 'object_id', 'id') |
| 141 |
->where('object_type', 'comment'); |
| 142 |
} |
| 143 |
|
| 144 |
public function scopeByContentModerationAccessStatus($query, $user, $space = null) |
| 145 |
{ |
| 146 |
if (!$user || !Helper::isFeatureEnabled('content_moderation')) { |
| 147 |
return $query->where('status', 'published'); |
| 148 |
} |
| 149 |
|
| 150 |
if ( |
| 151 |
$user->hasCommunityModeratorAccess() || |
| 152 |
($space && $user->hasSpacePermission('edit_any_comment', $space)) |
| 153 |
) { |
| 154 |
return $query->whereIn('status', ['published', 'pending']); |
| 155 |
} |
| 156 |
|
| 157 |
// This is a normal User. |
| 158 |
return $query->where(function ($q) use ($user) { |
| 159 |
$q->where('status', 'published') |
| 160 |
->orWhere(function ($q) use ($user) { |
| 161 |
$q->where('status', 'pending') |
| 162 |
->where('user_id', $user->ID); |
| 163 |
}); |
| 164 |
}); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
/* |
| 169 |
* Find all the user ids of a child comment who commented on the parent comment including the parent comment author |
| 170 |
*/ |
| 171 |
public function getCommentParentUserIds($lastUserId = 0) |
| 172 |
{ |
| 173 |
if (!$this->parent_id) { |
| 174 |
return []; |
| 175 |
} |
| 176 |
|
| 177 |
$parentComment = Comment::select(['user_id'])->find($this->parent_id); |
| 178 |
$allUserIds = Comment::where('parent_id', $this->parent_id) |
| 179 |
->select(['user_id']) |
| 180 |
->distinct('user_id') |
| 181 |
->when($lastUserId, function ($query) use ($lastUserId) { |
| 182 |
$query->where('user_id', '>', $lastUserId); |
| 183 |
}) |
| 184 |
->get() |
| 185 |
->pluck('user_id') |
| 186 |
->toArray(); |
| 187 |
|
| 188 |
if ($parentComment) { |
| 189 |
if ($parentComment->user_id > $lastUserId) { |
| 190 |
$allUserIds[] = $parentComment->user_id; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return array_values(array_unique($allUserIds)); |
| 195 |
} |
| 196 |
|
| 197 |
public function setMetaAttribute($value) |
| 198 |
{ |
| 199 |
$this->attributes['meta'] = maybe_serialize($value); |
| 200 |
} |
| 201 |
|
| 202 |
public function getMetaAttribute($value) |
| 203 |
{ |
| 204 |
$meta = Utility::safeUnserialize($value); |
| 205 |
|
| 206 |
if (!$meta) { |
| 207 |
$meta = []; |
| 208 |
} |
| 209 |
|
| 210 |
return $meta; |
| 211 |
} |
| 212 |
|
| 213 |
public function getHumanExcerpt($length = 30) |
| 214 |
{ |
| 215 |
$content = $this->message; |
| 216 |
|
| 217 |
return Helper::getHumanExcerpt($content, $length); |
| 218 |
} |
| 219 |
|
| 220 |
public function getEmailSubject($feed = null) |
| 221 |
{ |
| 222 |
if (!$feed) { |
| 223 |
$feed = $this->post; |
| 224 |
} |
| 225 |
|
| 226 |
if ($this->parent_id) { |
| 227 |
if ($feed->title) { |
| 228 |
/* translators: %1$s is the feed title and %2$s is the comment author name */ |
| 229 |
$emailSubject = \sprintf(__('New reply on comment at %1$s - %2$s', 'fluent-community'), $feed->title, $this->xprofile->display_name); |
| 230 |
} else { |
| 231 |
/* translators: %1$s is the post title and %2$s is the comment author name */ |
| 232 |
$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); |
| 233 |
} |
| 234 |
} else { |
| 235 |
if ($feed->title) { |
| 236 |
/* translators: %1$s is the feed title and %2$s is the comment author name */ |
| 237 |
$emailSubject = \sprintf(__('New comment on %1$s - %2$s', 'fluent-community'), $feed->title, $this->xprofile->display_name); |
| 238 |
} else { |
| 239 |
/* translators: %1$s is the post title and %2$s is the comment author name */ |
| 240 |
$emailSubject = \sprintf(__('New comment on a post on %1$s - %2$s', 'fluent-community'), $this->post->getHumanExcerpt(40), $this->xprofile->display_name); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return $emailSubject; |
| 245 |
} |
| 246 |
|
| 247 |
public function getCommentHtml($withPlaceholder = false, $buttonText = null) |
| 248 |
{ |
| 249 |
$emailComposer = new \FluentCommunity\App\Services\Libs\EmailComposer(); |
| 250 |
if ($withPlaceholder) { |
| 251 |
$postPermalink = '##feed_permalink##'; |
| 252 |
} else { |
| 253 |
$postPermalink = $this->post->getPermalink() . '?comment_id=' . $this->id; |
| 254 |
} |
| 255 |
|
| 256 |
if ($this->post->title) { |
| 257 |
$postTitle = $this->post->title; |
| 258 |
} else { |
| 259 |
$postTitle = $this->post->getHumanExcerpt(120); |
| 260 |
} |
| 261 |
|
| 262 |
$renderedMessage = $this->message_rendered; |
| 263 |
|
| 264 |
// Remove all the URLs with the text but make it underlined |
| 265 |
$renderedMessage = preg_replace('/<a href="([^"]+)">([^<]+)<\/a>/', '<span style="text-decoration: underline !important;">$2</span>', $renderedMessage); |
| 266 |
|
| 267 |
$renderedMessage .= FeedsHelper::getMediaHtml($this->meta, $postPermalink); |
| 268 |
|
| 269 |
$buttonText = $buttonText ?: __('View the comment', 'fluent-community'); |
| 270 |
|
| 271 |
$emailComposer->addBlock('boxed_content', $renderedMessage, [ |
| 272 |
'user' => $this->user, |
| 273 |
'permalink' => $postPermalink, |
| 274 |
'post_content' => $postTitle |
| 275 |
]); |
| 276 |
|
| 277 |
$emailComposer->addBlock('button', $buttonText, [ |
| 278 |
'link' => $postPermalink |
| 279 |
]); |
| 280 |
|
| 281 |
$emailComposer->setDefaultLogo(); |
| 282 |
$emailComposer->setDefaultFooter($withPlaceholder); |
| 283 |
|
| 284 |
return $emailComposer->getHtml(); |
| 285 |
} |
| 286 |
} |
| 287 |
|