PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.0
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 / Comment.php

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

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