validate([ 'search' => 'nullable|string', 'per_page' => 'nullable|integer|min:1|max:100', 'page' => 'nullable|integer|min:1', ]); $searchTerm = $request->input('search'); $perPage = min(max((int) $request->input('per_page', 15), 10), 100); $page = (int) $request->input('page', 1); $results = collect(); if (!empty($searchTerm)) { $limitPerTable = 100; // Performance safeguard // 1. Search Bylaw Documents $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->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}%"); })->take($limitPerTable)->get()->map(function($item) { $formatted = $this->formatBylaw($item); $formatted['type'] = 'bylaw'; return $formatted; }); $results = $results->merge($bylaws); // 2. Search Judicial Precedents $judicials = JudicialPrecedent::with('category') ->where(function ($q) use ($searchTerm) { $q->where('ruling_number', 'LIKE', "%{$searchTerm}%") ->orWhere('subject', 'LIKE', "%{$searchTerm}%") ->orWhere('full_text', 'LIKE', "%{$searchTerm}%"); })->take($limitPerTable)->get()->map(function($item) { $formatted = $this->formatJudicial($item); $formatted['type'] = 'judicial_precedent'; return $formatted; }); $results = $results->merge($judicials); // 3. Search Legal Opinions $opinions = LegalOpinion::with('category') ->where(function ($q) use ($searchTerm) { $q->where('opinion_number', 'LIKE', "%{$searchTerm}%") ->orWhere('subject', 'LIKE', "%{$searchTerm}%") ->orWhere('full_text', 'LIKE', "%{$searchTerm}%") ->orWhere('issuing_authority', 'LIKE', "%{$searchTerm}%"); })->take($limitPerTable)->get()->map(function($item) { $formatted = $this->formatOpinion($item); $formatted['type'] = 'legal_opinion'; return $formatted; }); $results = $results->merge($opinions); // 4. Search Laws, Arts, and Structural Models $structuralModels = [ Law::class, Volum::class, Book::class, Division::class, Section::class, Chapter::class, Part::class, Gate::class, Branch::class, Art::class, ]; foreach ($structuralModels as $modelClass) { $query = $modelClass::query(); $modelInstance = new $modelClass; $table = $modelInstance->getTable(); if ($table === 'art') { $query->where('title', 'LIKE', "%{$searchTerm}%") ->orWhere('text', 'LIKE', "%{$searchTerm}%"); } else { $query->where('title', 'LIKE', "%{$searchTerm}%"); } $items = $query->take($limitPerTable)->get()->map(function($item) use ($modelClass, $table, $searchTerm) { if ($table === 'art') { $formatted = $this->formatArt($item, $searchTerm, $modelClass); $formatted['type'] = 'art'; } else { $formatted = $this->formatStructure($item, $modelClass); // Standardize the structural type names (laws, sections, etc.) $formatted['type'] = ($table === 'section') ? 'sections' : $table; } return $formatted; }); $results = $results->merge($items); } } // --- Execute Manual Pagination on the Combined Collection --- $total = $results->count(); // Slice the array for the exact page requested $paginatedItems = $results->forPage($page, $perPage)->values(); // Build a proper Laravel paginator object so the response metadata remains identical $paginator = new LengthAwarePaginator( $paginatedItems, $total, $perPage, $page, ['path' => $request->url(), 'query' => $request->query()] ); $responseData = [ 'items' => $paginator->items(), 'current_page' => $paginator->currentPage(), 'last_page' => $paginator->lastPage(), 'per_page' => $paginator->perPage(), 'total' => $paginator->total(), ]; return $this->success($responseData, 'Success', 'Global Search Results Fetched'); } /* ----------------------------------------------------------------- * FORMATTING HELPERS * ----------------------------------------------------------------- */ private function formatBylaw($item) { return [ 'id' => $item->id, 'title' => $item->title, 'category_name' => optional($item->category)->name, 'approval_authority_name' => optional($item->approvalAuthority)->name, 'approval_date' => $item->approval_date, 'promulgation_number' => $item->promulgation_number, 'description' => $item->description, ]; } private function formatJudicial($item) { return [ 'id' => $item->id, 'title' => $item->subject, // Map subject to title for universal UI parsing 'ruling_number' => $item->ruling_number, 'ruling_date' => $item->ruling_date, 'text' => $item->full_text, 'issuing_authority' => $item->issuing_authority, 'category_name' => optional($item->category)->name, ]; } private function formatOpinion($item) { return [ 'id' => $item->id, 'title' => $item->subject, // Map subject to title for universal UI parsing 'opinion_number' => $item->opinion_number, 'opinion_date' => $item->opinion_date, 'text' => $item->full_text, 'issuing_authority' => $item->issuing_authority, 'category_name' => optional($item->category)->name, ]; } private function formatArt($item, $searchTerm, $modelClass) { $law = Law::find($item->law_id); $isLocked = $this->isLockedForCurrentUser($law); // Text snippet highlighting logic (from ArtController) $text = $item->text ?? ''; $searchQuoted = preg_quote($searchTerm, '/'); if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) { $context = trim($matches[1] ?? '') . ' ' . $matches[2] . ' ' . trim($matches[3] ?? ''); } else { $context = $text; } return [ 'id' => $item->id, 'title' => $item->title, 'text' => $isLocked ? null : $context, 'is_locked' => $isLocked, 'route' => array_values($this->route($modelClass, $item)), 'category' => optional($law->category)->name, 'law' => $law?->title, 'count_art' => $law?->arts?->count() ?? 0, 'count_volums'=> $law?->volums?->count() ?? 0, 'price' => $law?->price, 'image' => $law?->image, ]; } private function formatStructure($item, $modelClass) { $law = Law::find($item?->law_id ?? $item->id); $isLocked = $this->isLockedForCurrentUser($law); $payload = [ 'id' => $item->id, 'title' => $item->title, 'number' => $item->number, 'is_locked' => $isLocked, 'route' => array_values($this->route($modelClass, $item)), 'law' => $law?->title ?? '', ]; if ((new $modelClass)->getTable() == 'laws') { $payload['category'] = optional($law?->category)?->name ?? ''; $payload['image'] = $law?->image; } return $payload; } /* ----------------------------------------------------------------- * UTILITY METHODS * ----------------------------------------------------------------- */ private function isLockedForCurrentUser(?Law $law): bool { return ! auth()->user()->isSubscriber() && (bool) $law?->is_locked; } private function route($modelClass, $query) { if ((new $modelClass)->getTable() == 'art') { $route = array_filter([ Law::find($query->law_id)?->title, Volum::find($query->volum_id)?->title, Book::find($query->book_id)?->title, Division::find($query->division_id)?->title, Section::find($query->section_id)?->title, Chapter::find($query->chapter_id)?->title, Part::find($query->part_id)?->title, Gate::find($query->gate_id)?->title, Branch::find($query->branch_id)?->title, ]); return array_values($route); } if ((new $modelClass)->getTable() == 'laws') { return array_values(array_filter([Law::find($query->id)?->title])); } return []; } }