| @@ -159,9 +159,10 @@ | ||
| 159 | 159 | $q->where('user_id', get_current_user_id()); |
| 160 | 160 | }]) |
| 161 | 161 | ->where(function ($q) { |
| 162 | 162 | $q->whereHas('space_pivot', function ($q) { |
| 163 | - $q->where('user_id', get_current_user_id()); | |
| 163 | + $q->where('user_id', get_current_user_id()) | |
| 164 | + ->where('status', 'active'); | |
| 164 | 165 | }) |
| 165 | 166 | ->orWhereIn('privacy', ['public', 'private']); |
| 166 | 167 | }) |
| 167 | 168 | ->when($type == 'joined', function ($q) { |
| @@ -208,11 +209,23 @@ | ||
| 208 | 209 | } |
| 209 | 210 | |
| 210 | 211 | public function getAllSpaces(Request $request) |
| 211 | 212 | { |
| 212 | - $spaces = Space::paginate(); | |
| 213 | + $currentUser = $this->getUser(); | |
| 213 | 214 | |
| 214 | - $currentUser = $this->getUser(); | |
| 215 | + $spacesQuery = Space::query(); | |
| 216 | + | |
| 217 | + if (!($currentUser && $currentUser->isCommunityModerator())) { | |
| 218 | + $spacesQuery->where(function ($q) { | |
| 219 | + $q->whereHas('space_pivot', function ($q) { | |
| 220 | + $q->where('user_id', get_current_user_id()) | |
| 221 | + ->where('status', 'active'); | |
| 222 | + }) | |
| 223 | + ->orWhereIn('privacy', ['public', 'private']); | |
| 224 | + }); | |
| 225 | + } | |
| 226 | + $spaces = $spacesQuery->paginate(); | |
| 227 | + | |
| 215 | 228 | $memberCounts = $this->getActiveMemberCounts($spaces->pluck('id')->toArray()); |
| 216 | 229 | |
| 217 | 230 | foreach ($spaces as $space) { |
| 218 | 231 | $shouldHideMembersCount = Arr::get($space->settings, 'hide_members_count') == 'yes'; |
| @@ -414,20 +427,37 @@ | ||
| 414 | 427 | ], $pendingRequests, $request->all()); |
| 415 | 428 | } |
| 416 | 429 | } |
| 417 | 430 | |
| 431 | + $defaultDirections = [ | |
| 432 | + 'last_activity' => 'DESC', | |
| 433 | + 'display_name' => 'ASC', | |
| 434 | + 'created_at' => 'DESC', | |
| 435 | + ]; | |
| 436 | + | |
| 437 | + $sortBy = $request->getSafe('sort_by', 'sanitize_text_field', 'created_at'); | |
| 438 | + $sortColumn = in_array($sortBy, array_keys($defaultDirections), true) ? $sortBy : 'created_at'; | |
| 439 | + $sortDir = strtoupper($request->getSafe('sort_dir', 'sanitize_text_field', '')); | |
| 440 | + $sortDirection = in_array($sortDir, ['ASC', 'DESC'], true) ? $sortDir : ($sortColumn === 'created_at' ? 'ASC' : $defaultDirections[$sortColumn]); | |
| 441 | + | |
| 442 | + $profileSort = $sortColumn !== 'created_at'; | |
| 443 | + $orderColumn = $profileSort ? 'fcom_xprofile.' . $sortColumn : 'fcom_space_user.created_at'; | |
| 444 | + | |
| 418 | 445 | $spaceMembers = SpaceUserPivot::bySpace($space->id) |
| 419 | 446 | ->whereHas('xprofile', function ($q) use ($search) { |
| 420 | - return $q->searchBy($search) | |
| 421 | - ->where('status', 'active'); | |
| 447 | + $q->searchBy($search)->where('status', 'active'); | |
| 422 | 448 | }) |
| 449 | + ->where('fcom_space_user.status', 'active') | |
| 423 | 450 | ->with(['xprofile' => function ($q) { |
| 424 | 451 | $q->select(ProfileHelper::getXProfilePublicFields()); |
| 425 | 452 | }]) |
| 426 | - ->where('status', 'active') | |
| 427 | - ->orderBy('created_at', 'ASC') | |
| 453 | + ->when($profileSort, function ($q) { | |
| 454 | + $q->join('fcom_xprofile', 'fcom_xprofile.user_id', '=', 'fcom_space_user.user_id') | |
| 455 | + ->select('fcom_space_user.*'); | |
| 456 | + }) | |
| 457 | + ->orderBy($orderColumn, $sortDirection) | |
| 428 | 458 | ->paginate(); |
| 429 | - | |
| 459 | + | |
| 430 | 460 | return apply_filters('fluent_community/space_members_api_response', [ |
| 431 | 461 | 'members' => $spaceMembers, |
| 432 | 462 | 'pending_count' => $pendingCount |
| 433 | 463 | ], $spaceMembers, $request->all()); |
| @@ -822,13 +852,28 @@ | ||
| 822 | 852 | 'title' => 'required|unique:fcom_spaces,title', |
| 823 | 853 | 'slug' => 'required|unique:fcom_spaces,slug' |
| 824 | 854 | ]); |
| 825 | 855 | |
| 856 | + $title = sanitize_text_field(Arr::get($data, 'title', '')); | |
| 857 | + $slug = sanitize_title(Arr::get($data, 'slug', '')); | |
| 858 | + $desc = sanitize_textarea_field(Arr::get($data, 'description', '')); | |
| 826 | 859 | |
| 860 | + if (!$title) { | |
| 861 | + return $this->sendError([ | |
| 862 | + 'message' => __('Please enter a valid group title.', 'fluent-community') | |
| 863 | + ]); | |
| 864 | + } | |
| 865 | + | |
| 866 | + if (!$slug) { | |
| 867 | + return $this->sendError([ | |
| 868 | + 'message' => __('Please enter a valid group slug.', 'fluent-community') | |
| 869 | + ]); | |
| 870 | + } | |
| 871 | + | |
| 827 | 872 | $formattedData = [ |
| 828 | - 'title' => sanitize_text_field($data['title']), | |
| 829 | - 'slug' => sanitize_title($data['slug']), | |
| 830 | - 'description' => sanitize_textarea_field($data['description']), | |
| 873 | + 'title' => $title, | |
| 874 | + 'slug' => $slug, | |
| 875 | + 'description' => $desc, | |
| 831 | 876 | 'status' => 'active', |
| 832 | 877 | 'type' => 'space_group', |
| 833 | 878 | 'settings' => [ |
| 834 | 879 | 'always_show_spaces' => Arr::get($data, 'settings.always_show_spaces', 'yes'), |