| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\BaseSpace; |
| 7 |
use FluentCommunity\App\Models\User; |
| 8 |
use FluentCommunity\App\Models\XProfile; |
| 9 |
use FluentCommunity\App\Services\Helper; |
| 10 |
use FluentCommunity\App\Services\ProfileHelper; |
| 11 |
use FluentCommunity\Framework\Http\Request\Request; |
| 12 |
|
| 13 |
class MembersController extends Controller |
| 14 |
{ |
| 15 |
public function getMembers(Request $request) |
| 16 |
{ |
| 17 |
$start = microtime(true); |
| 18 |
$mention = $request->getSafe('mention', 'sanitize_text_field'); |
| 19 |
if($mention && !get_current_user_id()) { |
| 20 |
return $this->sendError([ |
| 21 |
'message' => __('You must be logged in to mention someone', 'fluent-community'), |
| 22 |
]); |
| 23 |
} |
| 24 |
|
| 25 |
$canAccess = Utility::canViewMembersPage(); |
| 26 |
$isMod = Helper::isModerator(); |
| 27 |
|
| 28 |
$members = XProfile::select(ProfileHelper::getXProfilePublicFields()) |
| 29 |
->whereHas('user'); |
| 30 |
|
| 31 |
if($mention) { |
| 32 |
$spaceSlug = $request->getSafe('space', 'sanitize_text_field'); |
| 33 |
$space = null; |
| 34 |
if ($spaceSlug) { |
| 35 |
$space = BaseSpace::where('slug', $spaceSlug)->first(); |
| 36 |
if (!$space || !Helper::isUserInSpace(get_current_user_id(), $space->id)) { |
| 37 |
return $this->sendError([ |
| 38 |
'message' => __('Please join the space first to view the members', 'fluent-community'), |
| 39 |
'permission_failed' => true |
| 40 |
]); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
if (!$space) { |
| 45 |
$spaceId = $request->getSafe('space_id', 'intval'); |
| 46 |
if ($spaceId) { |
| 47 |
$space = BaseSpace::withoutGlobalScopes()->find($spaceId); |
| 48 |
if (!$space || !Helper::isUserInSpace(get_current_user_id(), $space->id)) { |
| 49 |
return $this->sendError([ |
| 50 |
'message' => __('Space not found', 'fluent-community'), |
| 51 |
'permission_failed' => true |
| 52 |
]); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if ($space) { |
| 58 |
$members = $members->whereHas('spaces', function ($query) use ($space) { |
| 59 |
$query->withoutGlobalScopes()->where('space_id', $space->id); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
$members = $members->where('status', 'active') |
| 64 |
->where('user_id', '!=', get_current_user_id()) |
| 65 |
->mentionBy($mention) |
| 66 |
->limit(10) |
| 67 |
->get(); |
| 68 |
|
| 69 |
$data = [ |
| 70 |
'members' => [ |
| 71 |
'data' => $members |
| 72 |
], |
| 73 |
'execution_time' => microtime(true) - $start |
| 74 |
]; |
| 75 |
return apply_filters('fluent_community/mention_members_api_response', $data, $request->all()); |
| 76 |
} |
| 77 |
|
| 78 |
if(!$canAccess) { |
| 79 |
return $this->sendError([ |
| 80 |
'message' => __('You do not have permission to view members', 'fluent-community'), |
| 81 |
'permission_failed' => true |
| 82 |
]); |
| 83 |
} |
| 84 |
|
| 85 |
$sortBy = $request->getSafe('sort_by', 'sanitize_text_field', 'last_activity'); |
| 86 |
|
| 87 |
$validSortFields = ['last_activity', 'display_name', 'created_at']; |
| 88 |
|
| 89 |
$sortColumn = in_array($sortBy, $validSortFields, true) ? $sortBy : 'last_activity'; |
| 90 |
|
| 91 |
$defaultDirections = [ |
| 92 |
'last_activity' => 'DESC', |
| 93 |
'display_name' => 'ASC', |
| 94 |
'created_at' => 'DESC', |
| 95 |
]; |
| 96 |
|
| 97 |
$sortDir = strtoupper($request->getSafe('sort_dir', 'sanitize_text_field', '')); |
| 98 |
|
| 99 |
$sortDirection = in_array($sortDir, ['ASC', 'DESC'], true) |
| 100 |
? $sortDir |
| 101 |
: $defaultDirections[$sortColumn]; |
| 102 |
|
| 103 |
$members = $members->orderBy($sortColumn, $sortDirection); |
| 104 |
|
| 105 |
$members = $members |
| 106 |
->searchBy($request->getSafe('search', 'sanitize_text_field')); |
| 107 |
|
| 108 |
if($isMod) { |
| 109 |
$statuses = $request->getSafe('status', 'sanitize_text_field', 'active'); |
| 110 |
if ($statuses == 'pending') { |
| 111 |
$statuses = ['pending']; |
| 112 |
} else if ($statuses == 'blocked') { |
| 113 |
$statuses = ['blocked']; |
| 114 |
} else if($statuses == 'deactivated') { |
| 115 |
$statuses = ['']; |
| 116 |
} else { |
| 117 |
$statuses = ['active']; |
| 118 |
} |
| 119 |
$members = $members->whereIn('status', $statuses); |
| 120 |
} else { |
| 121 |
$members = $members->where('status', 'active'); |
| 122 |
} |
| 123 |
|
| 124 |
do_action_ref_array('fluent_community/members_query_ref', [&$members, $request->all()]); |
| 125 |
|
| 126 |
$members = $members->paginate(); |
| 127 |
|
| 128 |
return apply_filters('fluent_community/members_api_response', [ |
| 129 |
'members' => $members, |
| 130 |
'execution_time' => microtime(true) - $start |
| 131 |
], $members, $request->all()); |
| 132 |
} |
| 133 |
|
| 134 |
public function patchMember(Request $request, $userId) |
| 135 |
{ |
| 136 |
$currentUser = $this->getUser(true); |
| 137 |
$currentUser->verifyCommunityPermission('delete_any_feed'); |
| 138 |
|
| 139 |
$newStatus = $request->getSafe('status', 'sanitize_text_field'); |
| 140 |
|
| 141 |
$wpUser = User::findOrFail($userId); |
| 142 |
|
| 143 |
if ($wpUser->isCommunityAdmin() && $newStatus != 'active') { |
| 144 |
return $this->sendError([ |
| 145 |
'message' => __('Sorry, you cannot change the status of another admin. Remove the user from moderators', 'fluent-community') |
| 146 |
]); |
| 147 |
} |
| 148 |
|
| 149 |
$xProfile = XProfile::findOrFail($userId); |
| 150 |
|
| 151 |
$newStatus = $request->getSafe('status', 'sanitize_text_field'); |
| 152 |
$validStatuses = ['active', 'pending', 'blocked']; |
| 153 |
|
| 154 |
if (in_array($newStatus, $validStatuses)) { |
| 155 |
$xProfile->status = $newStatus; |
| 156 |
$xProfile->save(); |
| 157 |
} |
| 158 |
|
| 159 |
return [ |
| 160 |
'message' => __('Member status has been updated', 'fluent-community'), |
| 161 |
'member' => $xProfile |
| 162 |
]; |
| 163 |
} |
| 164 |
} |
| 165 |
|