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

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