PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.93
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.93
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 / Http / Controllers / ProfileController.php

ProfileController.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.93, at app/Http/Controllers/ProfileController.php

428 lines 15.4 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\Http\Controllers;
4
5 use FluentCommunity\App\Models\Comment;
6 use FluentCommunity\App\Models\NotificationSubscription;
7 use FluentCommunity\App\Models\Space;
8 use FluentCommunity\App\Models\SpaceGroup;
9 use FluentCommunity\App\Models\User;
10 use FluentCommunity\App\Models\XProfile;
11 use FluentCommunity\App\Services\CustomSanitizer;
12 use FluentCommunity\App\Services\FeedsHelper;
13 use FluentCommunity\App\Services\Helper;
14 use FluentCommunity\App\Services\NotificationPref;
15 use FluentCommunity\App\Services\ProfileHelper;
16 use FluentCommunity\Framework\Http\Request\Request;
17 use FluentCommunity\Framework\Support\Arr;
18
19 class ProfileController extends Controller
20 {
21 public function getProfile(Request $request, $userName)
22 {
23 $xprofile = XProfile::where('username', $userName)->firstOrFail();
24
25 $user = get_user_by('ID', $xprofile->user_id);
26
27 $profile = [
28 'user_id' => $xprofile->user_id,
29 'is_verified' => $xprofile->is_verified,
30 'display_name' => $xprofile->display_name,
31 'username' => $xprofile->username,
32 'avatar' => $xprofile->avatar,
33 'created_at' => $xprofile->created_at->format('Y-m-d H:i:s'),
34 'last_activity' => $xprofile->last_activity,
35 'short_description_rendered' => FeedsHelper::mdToHtml($xprofile->short_description),
36 'cover_photo' => Arr::get($xprofile->meta, 'cover_photo'),
37 'website' => Arr::get($xprofile->meta, 'website'),
38 'social_links' => (object)Arr::get($xprofile->meta, 'social_links', []),
39 'status' => $xprofile->status,
40 'badge_slug' => Arr::get($xprofile->meta, 'badge_slug'),
41 'compilation_score' => $xprofile->getCompletionScore(),
42 'total_points' => $xprofile->total_points
43 ];
44
45 if ($xprofile->user_id == get_current_user_id() || current_user_can('edit_users')) {
46 $profile['email'] = $user->user_email;
47 $profile['first_name'] = $user->first_name;
48 $profile['last_name'] = $user->last_name;
49 $profile['short_description'] = $xprofile->short_description;
50 }
51
52 return [
53 'profile' => $profile
54 ];
55 }
56
57 public function patchProfile(Request $request, $userName)
58 {
59 $xprofile = $this->verfifyAndGetProfile($userName);
60
61 $updateData = $request->get('data');
62
63 $mediaTypes = ['cover_photo', 'avatar'];
64
65 foreach ($mediaTypes as $type) {
66 if (!empty($updateData[$type])) {
67 $media = Helper::getMediaFromUrl($updateData[$type]);
68 if (!$media || $media->is_active) {
69 return $this->sendError([
70 'message' => 'Invalid media image. Please upload a new one.'
71 ]);
72 }
73
74 $updateData[$type] = $media->public_url;
75
76 $media->update([
77 'is_active' => true,
78 'user_id' => $xprofile->user_id,
79 'object_source' => 'user_' . $type
80 ]);
81 }
82 }
83
84 $deletedMedias = [];
85
86 if (!empty($updateData['avatar'])) {
87
88 $deletedMedias[] = $xprofile->avatar;
89
90 $xprofile->avatar = $updateData['avatar'];
91
92 if (defined('FLUENTCRM')) {
93 $contact = $xprofile->contact;
94
95 if ($contact) {
96 $contact->update([
97 'avatar' => $updateData['avatar']
98 ]);
99 }
100 }
101
102 }
103
104 if (!empty($updateData['cover_photo'])) {
105 $deletedMedias[] = Arr::get($xprofile->meta, 'cover_photo');
106 $xprofile->meta = wp_parse_args(['cover_photo' => $updateData['cover_photo']], $xprofile->meta);
107 }
108
109 $xprofile->save();
110
111 if ($deletedMedias = array_filter($deletedMedias)) {
112 do_action('fluent_community/remove_medias_by_url', $deletedMedias, [
113 'user_id' => $xprofile->user_id,
114 'object_sources' => ['user_avatar', 'user_cover_photo']
115 ]);
116 }
117
118 return [
119 'message' => __('Profile updated', 'fluent-community')
120 ];
121 }
122
123 public function updateProfile(Request $request, $userName)
124 {
125 $currentUser = $this->getUser(true);
126 $data = $request->get('data', []);
127
128 if ($currentUser->isCommunityModerator()) {
129 $xProfile = XProfile::where('user_id', $data['user_id'])->firstOrFail();
130 } else {
131 $xProfile = XProfile::where('username', $userName)->firstOrFail();
132 if ($xProfile->user_id != get_current_user_id()) {
133 return $this->sendError([
134 'message' => 'You are not allowed to update this profile'
135 ]);
136 }
137 }
138
139 $this->validate($data, [
140 'first_name' => 'required',
141 'last_name' => 'required',
142 ], [
143 'first_name.required' => __('First name is required', 'fluent-community'),
144 'last_name.required' => __('Last name is required', 'fluent-community'),
145 ]);
146
147 $updateData = Arr::only($data, ['first_name', 'last_name', 'short_description', 'website']);
148
149 $currentUser = User::findOrFail(get_current_user_id());
150 $meta = $xProfile->meta;
151
152 $userNameChanged = false;
153
154 if ($currentUser->isCommunityModerator()) {
155 $updateData['is_verified'] = Arr::get($data, 'is_verified') ? 1 : 0;
156 $updateData['status'] = Arr::get($data, 'status', 'active');
157 $userName = Arr::get($data, 'username');
158
159 if (user_can($xProfile->user_id, 'list_users')) {
160 $updateData['status'] = 'active';
161 }
162
163 if ($userName) {
164 // Check if username is exit or not
165 $userName = CustomSanitizer::sanitizeUserName($userName);
166
167 if (!$userName) {
168 return $this->sendError([
169 'message' => __('Invalid username. Only latin chars with _ & - is allowed', 'fluent-community')
170 ]);
171 }
172
173 if (XProfile::where('username', $userName)->where('user_id', '!=', $xProfile->user_id)->exists()) {
174 return $this->sendError([
175 'message' => 'Space Username already taken by someone else'
176 ]);
177 }
178 $updateData['username'] = $userName;
179 $userNameChanged = $userName != $xProfile->username;
180 }
181
182 if (Helper::isFeatureEnabled('user_badge')) {
183 $badgeSlug = Arr::get($data, 'badge_slug');
184 $meta['badge_slug'] = $badgeSlug;
185 }
186 }
187
188 $updateData['display_name'] = trim(sanitize_text_field(Arr::get($data, 'first_name') . ' ' . Arr::get($data, 'last_name')));
189 $updateData['short_description'] = CustomSanitizer::unslashMarkdown(sanitize_textarea_field(trim(Arr::get($data, 'short_description'))));
190 $meta['website'] = sanitize_url(Arr::get($data, 'website'));
191 $socialLinks = Arr::get($data, 'social_links', []);
192
193 $maxDescriptionLength = apply_filters('fluent_community/max_profile_description_length', 5000);
194 if ($updateData['short_description'] && strlen($updateData['short_description']) > $maxDescriptionLength) {
195 return $this->sendError([
196 'message' => sprintf(__('Profile Bio should not be more than %d characters', 'fluent-community'), $maxDescriptionLength)
197 ]);
198 }
199
200 if ($socialLinks) {
201 $socialLinks = array_filter($socialLinks);
202 $formattedSocialLinkes = [];
203 $socialLinkProviders = ProfileHelper::socialLinkProviders();
204 foreach ($socialLinks as $linkName => $socialLink) {
205 if (isset($socialLinkProviders[$linkName])) {
206 $formattedSocialLinkes[$linkName] = sanitize_text_field(trim($socialLink));
207 }
208 }
209 $meta['social_links'] = $formattedSocialLinkes;
210 }
211
212 $updateData['meta'] = $meta;
213
214 $xProfile->fill($updateData);
215 $xProfile->save();
216
217
218 // Let's update the user's details
219 $xProfile->user->updateCustomData($updateData);
220 $xProfile->compilation_score = $xProfile->getCompletionScore();
221
222
223 if ($userNameChanged) {
224 return [
225 'message' => __('Profile has been updated', 'fluent-community'),
226 'profile' => $xProfile,
227 'redirect_url' => Helper::baseUrl('u/' . $xProfile->username . '/update')
228 ];
229 }
230
231 return [
232 'message' => __('Profile has been updated', 'fluent-community'),
233 'profile' => $xProfile
234 ];
235 }
236
237 public function getSpaces(Request $request, $userName)
238 {
239 $xProfile = XProfile::where('username', $userName)->firstOrFail();
240 $currentUser = $this->getUser();
241 if ($xProfile->user_id == get_current_user_id() || ($currentUser && $currentUser->isCommunityModerator())) {
242 $spaces = $xProfile->spaces()
243 ->wherePivot('status', 'active')
244 ->get();
245 } else {
246 $spaces = $xProfile->spaces()
247 ->whereIn('privacy', ['public', 'private'])
248 ->wherePivot('status', 'active')
249 ->get();
250 }
251
252 foreach ($spaces as $space) {
253 $space->members_count = $space->members()->count();
254 }
255
256 return [
257 'spaces' => $spaces
258 ];
259 }
260
261 public function getComments(Request $request, $userName)
262 {
263 $xProfile = XProfile::where('username', $userName)->first();
264
265 if (!$xProfile) {
266 return $this->sendError([
267 'message' => 'Profile not found'
268 ]);
269 }
270
271 $currentUser = $this->getUser();
272 $hasAllAccess = $xProfile->user_id == get_current_user_id() || ($currentUser && $currentUser->isCommunityModerator());
273
274 $comments = Comment::where('user_id', $xProfile->user_id)
275 ->with([
276 'post' => function ($q) {
277 $q->select(['id', 'title', 'message', 'type', 'space_id', 'slug', 'created_at'])
278 ->with([
279 'space' => function ($q) {
280 $q->select(['id', 'title', 'slug', 'type']);
281 }
282 ]);
283 }
284 ])
285 ->when(!$hasAllAccess, function ($q) use ($xProfile) {
286 $q->whereHas('post', function ($query) use ($xProfile) {
287 $query->byUserAccess(get_current_user_id());
288 });
289 })
290 ->orderBy('id', 'desc')
291 ->paginate();
292
293 return [
294 'comments' => $comments,
295 'xprofile' => $xProfile
296 ];
297 }
298
299 public function getNotificationPreferance(Request $request, $userName)
300 {
301 $xProfile = $this->verfifyAndGetProfile($userName);
302
303 $globalPreferances = NotificationPref::getGlobalPrefs();
304 $userPrefs = NotificationSubscription::where('user_id', $xProfile->user_id)
305 ->select(['notification_type', 'is_read', 'object_id'])
306 ->get();
307
308 $userGlobalPrefs = [];
309 $spaceWisePrefs = [];
310 foreach ($userPrefs as $pref) {
311 if (!$pref->object_id) {
312 $userGlobalPrefs[$pref->notification_type] = $pref->is_read ? 'yes' : 'no';
313 } else {
314 if (empty($spaceWisePrefs[$pref->object_id])) {
315 $spaceWisePrefs[$pref->object_id] = [];
316 }
317 $spaceWisePrefs[$pref->object_id][$pref->notification_type] = $pref->is_read;
318 }
319 }
320
321 if (empty($userGlobalPrefs)) {
322 $userGlobalPrefs = $globalPreferances;
323 $userGlobalPrefs = array_map(function ($item) {
324 return $item ? 'yes' : 'no';
325 }, $userGlobalPrefs);
326 }
327
328 $spaceGroups = SpaceGroup::with(['spaces' => function ($query) {
329 $query->whereHas('members', function ($q) {
330 $q->where('user_id', get_current_user_id());
331 })
332 ->where('type', 'community');
333 }])
334 ->orderBy('serial', 'ASC')
335 ->get();
336
337 $formattedSpaceGroups = [];
338
339 foreach ($spaceGroups as $group) {
340 if ($group->spaces->isEmpty()) {
341 continue;
342 }
343
344 $formattedSpaces = [];
345 foreach ($group->spaces as $space) {
346
347 $pref = '';
348 if (isset($spaceWisePrefs[$space->id])) {
349 $perfs = (array)$spaceWisePrefs[$space->id];
350 if (!empty($perfs['np_by_member_mail'])) {
351 $pref = 'all_member_posts';
352 } else if (!empty($perfs['np_by_admin_mail'])) {
353 $pref = 'admin_only_posts';
354 }
355 }
356
357 $formattedSpaces[] = [
358 'id' => $space->id,
359 'title' => $space->title,
360 'icon' => $space->getIconMark(),
361 'pref' => $pref
362 ];
363 }
364
365 if ($formattedSpaces) {
366 $formattedSpaceGroups[] = [
367 'id' => $group->id,
368 'title' => $group->title,
369 'spaces' => $formattedSpaces
370 ];
371 }
372 }
373
374 return [
375 'user_globals' => (object)$userGlobalPrefs,
376 'spaceGroups' => $formattedSpaceGroups,
377 'space_prefs' => $spaceWisePrefs,
378 'digestEmailDay' => 'Monday'
379 ];
380 }
381
382 public function saveNotificationPreferance(Request $request, $userName)
383 {
384 $xProfile = $this->verfifyAndGetProfile($userName);
385
386 $userPrefs = $request->get('user_globals', []);
387 $sapcePrefs = $request->get('space_prefs', []);
388
389 $userPrefs = array_map(function ($item) {
390 return $item == 'yes' ? 1 : 0;
391 }, $userPrefs);
392
393 foreach ($sapcePrefs as $spaceId => $pref) {
394 $spaceId = (int)$spaceId;
395 if (!$pref || !$spaceId) {
396 continue;
397 }
398
399 if ($pref == 'all_member_posts') {
400 $userPrefs['np_by_member_mail_' . $spaceId] = 1;
401 $userPrefs['np_by_admin_mail_' . $spaceId] = 1;
402 } else if ($pref == 'admin_only_posts') {
403 $userPrefs['np_by_admin_mail_' . $spaceId] = 1;
404 }
405 }
406
407
408 NotificationPref::updateUserPrefs($xProfile->user_id, $userPrefs);
409
410 return [
411 'prefs' => $userPrefs,
412 'message' => __('Email Notification preferences has been updated', 'fluent-community')
413 ];
414 }
415
416 private function verfifyAndGetProfile($userName)
417 {
418 $xProfile = XProfile::where('username', $userName)->firstOrFail();
419
420 $currentUser = $this->getUser();
421 if ($xProfile->user_id != get_current_user_id() && (!$currentUser || !$currentUser->isCommunityModerator())) {
422 throw new \Exception('You are not allowed to update this profile');
423 }
424
425 return $xProfile;
426 }
427 }
428