map(function ($category) { return [ 'id' => $category->id, 'name' => $category->name, ]; }); return $this->success($categories, 'Success', 'Bylaw Categories List'); } /** * Get all Bylaw Legal Authorities */ public function authorities() { $authorities = BylawLegalAuthority::all()->map(function ($authority) { return [ 'id' => $authority->id, 'name' => $authority->name, 'authority_type' => $authority->authority_type, ]; }); return $this->success($authorities, 'Success', 'Bylaw Authorities List'); } /** * Get Paginated Bylaws (Filtered & Sorted by Approval Date) */ public function index(Request $request) { $request->validate([ 'category_id' => 'nullable|exists:bylaw_subject_categories,id', 'approval_authority_id' => 'nullable|exists:bylaw_legal_authorities,id', 'bylaw_type' => 'nullable|in:executive,delegated,independent', 'sort' => 'nullable|in:asc,desc', 'per_page' => 'nullable|integer|min:1|max:100', ]); $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true); if ($request->filled('category_id')) { $query->where('category_id', $request->category_id); } if ($request->filled('approval_authority_id')) { $query->where('approval_authority_id', $request->approval_authority_id); } if ($request->filled('bylaw_type')) { $query->where('bylaw_type', $request->bylaw_type); } $sortOrder = $request->input('sort', 'desc'); $query->orderBy('approval_date', $sortOrder); $perPage = min(max((int) $request->input('per_page', 15), 10), 100); $paginator = $query->paginate($perPage); $items = collect($paginator->items())->map(function ($item) { return $this->formatBylaw($item); }); $responseData = [ 'items' => $items, 'current_page' => $paginator->currentPage(), 'last_page' => $paginator->lastPage(), 'per_page' => $paginator->perPage(), 'total' => $paginator->total(), ]; return $this->success($responseData, 'Success', 'Bylaws Fetched'); } /** * Search Bylaws by Keyword */ public function search(Request $request) { $request->validate([ 'search' => 'nullable|string', 'per_page' => 'nullable|integer|min:1|max:100', ]); $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true); if ($request->filled('search')) { $searchTerm = $request->input('search'); $query->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%") ->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%"); }); } $perPage = min(max((int) $request->input('per_page', 15), 10), 100); // Default search sort to newest first $paginator = $query->orderBy('approval_date', 'desc')->paginate($perPage); $items = collect($paginator->items())->map(function ($item) { return $this->formatBylaw($item); }); $responseData = [ 'items' => $items, 'current_page' => $paginator->currentPage(), 'last_page' => $paginator->lastPage(), 'per_page' => $paginator->perPage(), 'total' => $paginator->total(), ]; return $this->success($responseData, 'Success', 'Search Results Fetched'); } /** * Contextual Pager API */ public function pager($id) { $current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->find($id); if (!$current) { return $this->failed([], [ 'title' => 'خطا', 'message' => 'آیین‌نامه مورد نظر یافت نشد.' ], 404); } $previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($query) use ($current) { if ($current->approval_date) { $query->where('approval_date', '<', $current->approval_date) ->orWhere(function ($subQuery) use ($current) { $subQuery->where('approval_date', '=', $current->approval_date) ->where('id', '<', $current->id); }); } else { $query->whereNull('approval_date')->where('id', '<', $current->id); } }) ->orderBy('approval_date', 'desc') ->orderBy('id', 'desc') ->first(); $nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($query) use ($current) { if ($current->approval_date) { $query->where('approval_date', '>', $current->approval_date) ->orWhere(function ($subQuery) use ($current) { $subQuery->where('approval_date', '=', $current->approval_date) ->where('id', '>', $current->id); }); } else { $query->whereNotNull('approval_date') ->orWhere(function ($subQuery) use ($current) { $subQuery->whereNull('approval_date')->where('id', '>', $current->id); }); } }) ->orderBy('approval_date', 'asc') ->orderBy('id', 'asc') ->first(); $results = array_values(array_filter([ $previousItem ? $this->formatBylaw($previousItem) : null, $this->formatBylaw($current), $nextItem ? $this->formatBylaw($nextItem) : null ])); return $this->success($results, 'Success', 'Pager Window Details'); } /** * Helper to uniformly format the Bylaw Document payload */ private function formatBylaw($item) { return [ 'id' => $item->id, 'title' => $item->title, 'category_id' => $item->category_id, 'category_name' => optional($item->category)->name, 'approval_authority_name' => optional($item->approvalAuthority)->name, 'approval_date' => $item->approval_date, 'promulgation_authority_name'=> optional($item->promulgationAuthority)->name, 'promulgation_date' => $item->promulgation_date, 'promulgation_number' => $item->promulgation_number, 'official_gazette_number' => $item->official_gazette_number, 'official_gazette_date' => $item->official_gazette_date, 'execution_date' => $item->execution_date, 'bylaw_type' => $item->bylaw_type, 'global_status' => $item->global_status, 'pdf_file_url' => $item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null, 'description' => $item->description, ]; } }