| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | use FluentCommunity\App\Models\SpaceGroup; |
| 9 | 9 | use FluentCommunity\App\Models\User; |
| 10 | 10 | use FluentCommunity\App\Services\CustomSanitizer; |
| 11 | 11 | use FluentCommunity\App\Services\Helper; |
| 12 | +use FluentCommunity\App\Services\SpaceMenuService; | |
| 12 | 13 | use FluentCommunity\App\Functions\Utility; |
| 13 | 14 | use FluentCommunity\App\Services\ProfileHelper; |
| 14 | 15 | use FluentCommunity\Framework\Http\Request\Request; |
| 15 | 16 | use FluentCommunity\App\Models\Comment; |
| @@ -159,9 +160,10 @@ | ||
| 159 | 160 | $q->where('user_id', get_current_user_id()); |
| 160 | 161 | }]) |
| 161 | 162 | ->where(function ($q) { |
| 162 | 163 | $q->whereHas('space_pivot', function ($q) { |
| 163 | - $q->where('user_id', get_current_user_id()); | |
| 164 | + $q->where('user_id', get_current_user_id()) | |
| 165 | + ->where('status', 'active'); | |
| 164 | 166 | }) |
| 165 | 167 | ->orWhereIn('privacy', ['public', 'private']); |
| 166 | 168 | }) |
| 167 | 169 | ->when($type == 'joined', function ($q) { |
| @@ -208,11 +210,23 @@ | ||
| 208 | 210 | } |
| 209 | 211 | |
| 210 | 212 | public function getAllSpaces(Request $request) |
| 211 | 213 | { |
| 212 | - $spaces = Space::paginate(); | |
| 214 | + $currentUser = $this->getUser(); | |
| 213 | 215 | |
| 214 | - $currentUser = $this->getUser(); | |
| 216 | + $spacesQuery = Space::query(); | |
| 217 | + | |
| 218 | + if (!($currentUser && $currentUser->isCommunityModerator())) { | |
| 219 | + $spacesQuery->where(function ($q) { | |
| 220 | + $q->whereHas('space_pivot', function ($q) { | |
| 221 | + $q->where('user_id', get_current_user_id()) | |
| 222 | + ->where('status', 'active'); | |
| 223 | + }) | |
| 224 | + ->orWhereIn('privacy', ['public', 'private']); | |
| 225 | + }); | |
| 226 | + } | |
| 227 | + $spaces = $spacesQuery->paginate(); | |
| 228 | + | |
| 215 | 229 | $memberCounts = $this->getActiveMemberCounts($spaces->pluck('id')->toArray()); |
| 216 | 230 | |
| 217 | 231 | foreach ($spaces as $space) { |
| 218 | 232 | $shouldHideMembersCount = Arr::get($space->settings, 'hide_members_count') == 'yes'; |
| @@ -414,20 +428,37 @@ | ||
| 414 | 428 | ], $pendingRequests, $request->all()); |
| 415 | 429 | } |
| 416 | 430 | } |
| 417 | 431 | |
| 432 | + $defaultDirections = [ | |
| 433 | + 'last_activity' => 'DESC', | |
| 434 | + 'display_name' => 'ASC', | |
| 435 | + 'created_at' => 'DESC', | |
| 436 | + ]; | |
| 437 | + | |
| 438 | + $sortBy = $request->getSafe('sort_by', 'sanitize_text_field', 'created_at'); | |
| 439 | + $sortColumn = in_array($sortBy, array_keys($defaultDirections), true) ? $sortBy : 'created_at'; | |
| 440 | + $sortDir = strtoupper($request->getSafe('sort_dir', 'sanitize_text_field', '')); | |
| 441 | + $sortDirection = in_array($sortDir, ['ASC', 'DESC'], true) ? $sortDir : ($sortColumn === 'created_at' ? 'ASC' : $defaultDirections[$sortColumn]); | |
| 442 | + | |
| 443 | + $profileSort = $sortColumn !== 'created_at'; | |
| 444 | + $orderColumn = $profileSort ? 'fcom_xprofile.' . $sortColumn : 'fcom_space_user.created_at'; | |
| 445 | + | |
| 418 | 446 | $spaceMembers = SpaceUserPivot::bySpace($space->id) |
| 419 | 447 | ->whereHas('xprofile', function ($q) use ($search) { |
| 420 | - return $q->searchBy($search) | |
| 421 | - ->where('status', 'active'); | |
| 448 | + $q->searchBy($search)->where('status', 'active'); | |
| 422 | 449 | }) |
| 450 | + ->where('fcom_space_user.status', 'active') | |
| 423 | 451 | ->with(['xprofile' => function ($q) { |
| 424 | 452 | $q->select(ProfileHelper::getXProfilePublicFields()); |
| 425 | 453 | }]) |
| 426 | - ->where('status', 'active') | |
| 427 | - ->orderBy('created_at', 'ASC') | |
| 454 | + ->when($profileSort, function ($q) { | |
| 455 | + $q->join('fcom_xprofile', 'fcom_xprofile.user_id', '=', 'fcom_space_user.user_id') | |
| 456 | + ->select('fcom_space_user.*'); | |
| 457 | + }) | |
| 458 | + ->orderBy($orderColumn, $sortDirection) | |
| 428 | 459 | ->paginate(); |
| 429 | - | |
| 460 | + | |
| 430 | 461 | return apply_filters('fluent_community/space_members_api_response', [ |
| 431 | 462 | 'members' => $spaceMembers, |
| 432 | 463 | 'pending_count' => $pendingCount |
| 433 | 464 | ], $spaceMembers, $request->all()); |
| @@ -764,8 +795,61 @@ | ||
| 764 | 795 | 'links' => $links |
| 765 | 796 | ]; |
| 766 | 797 | } |
| 767 | 798 | |
| 799 | + public function getPrimaryMenu(Request $request, $slug) | |
| 800 | + { | |
| 801 | + $space = Space::where('slug', $slug)->first(); | |
| 802 | + | |
| 803 | + if (!$space) { | |
| 804 | + return $this->sendError([ | |
| 805 | + 'message' => __('Space not found', 'fluent-community'), | |
| 806 | + ]); | |
| 807 | + } | |
| 808 | + | |
| 809 | + // Lighter than formatSpaceData(), but listeners like fcom-chat need membership too. | |
| 810 | + $user = $this->getUser(); | |
| 811 | + $space->permissions = $space->getUserPermissions($user); | |
| 812 | + $space->membership = $space->getMembership($user ? $user->ID : null); | |
| 813 | + | |
| 814 | + return [ | |
| 815 | + 'menu_items' => SpaceMenuService::getManagerItems($space), | |
| 816 | + ]; | |
| 817 | + } | |
| 818 | + | |
| 819 | + public function updatePrimaryMenu(Request $request, $slug) | |
| 820 | + { | |
| 821 | + $space = Space::where('slug', $slug)->first(); | |
| 822 | + | |
| 823 | + if (!$space) { | |
| 824 | + return $this->sendError([ | |
| 825 | + 'message' => __('Space not found', 'fluent-community'), | |
| 826 | + ]); | |
| 827 | + } | |
| 828 | + | |
| 829 | + $incoming = $request->get('menu_items', []); | |
| 830 | + | |
| 831 | + if (!is_array($incoming)) { | |
| 832 | + return $this->sendError([ | |
| 833 | + 'message' => __('Menu items must be a list.', 'fluent-community'), | |
| 834 | + ], 422); | |
| 835 | + } | |
| 836 | + | |
| 837 | + $menuItems = CustomSanitizer::sanitizeSpaceMenuItems($incoming); | |
| 838 | + | |
| 839 | + SpaceMenuService::storeMenu($space, $menuItems); | |
| 840 | + | |
| 841 | + // No re-fetch needed — unlike formatSpaceData(), this doesn't make $space unsaveable. | |
| 842 | + $user = $this->getUser(); | |
| 843 | + $space->permissions = $space->getUserPermissions($user); | |
| 844 | + $space->membership = $space->getMembership($user ? $user->ID : null); | |
| 845 | + | |
| 846 | + return [ | |
| 847 | + 'message' => __('Menu has been updated for the space', 'fluent-community'), | |
| 848 | + 'menu_items' => SpaceMenuService::getManagerItems($space), | |
| 849 | + ]; | |
| 850 | + } | |
| 851 | + | |
| 768 | 852 | public function getSpaceGroups(Request $request) |
| 769 | 853 | { |
| 770 | 854 | if ($request->get('options_only')) { |
| 771 | 855 | $groups = SpaceGroup::orderBy('serial', 'ASC') |
| @@ -822,13 +906,28 @@ | ||
| 822 | 906 | 'title' => 'required|unique:fcom_spaces,title', |
| 823 | 907 | 'slug' => 'required|unique:fcom_spaces,slug' |
| 824 | 908 | ]); |
| 825 | 909 | |
| 910 | + $title = sanitize_text_field(Arr::get($data, 'title', '')); | |
| 911 | + $slug = sanitize_title(Arr::get($data, 'slug', '')); | |
| 912 | + $desc = sanitize_textarea_field(Arr::get($data, 'description', '')); | |
| 826 | 913 | |
| 914 | + if (!$title) { | |
| 915 | + return $this->sendError([ | |
| 916 | + 'message' => __('Please enter a valid group title.', 'fluent-community') | |
| 917 | + ]); | |
| 918 | + } | |
| 919 | + | |
| 920 | + if (!$slug) { | |
| 921 | + return $this->sendError([ | |
| 922 | + 'message' => __('Please enter a valid group slug.', 'fluent-community') | |
| 923 | + ]); | |
| 924 | + } | |
| 925 | + | |
| 827 | 926 | $formattedData = [ |
| 828 | - 'title' => sanitize_text_field($data['title']), | |
| 829 | - 'slug' => sanitize_title($data['slug']), | |
| 830 | - 'description' => sanitize_textarea_field($data['description']), | |
| 927 | + 'title' => $title, | |
| 928 | + 'slug' => $slug, | |
| 929 | + 'description' => $desc, | |
| 831 | 930 | 'status' => 'active', |
| 832 | 931 | 'type' => 'space_group', |
| 833 | 932 | 'settings' => [ |
| 834 | 933 | 'always_show_spaces' => Arr::get($data, 'settings.always_show_spaces', 'yes'), |