diff --git a/app/Http/Controllers/api/GlobalSearchApiController.php b/app/Http/Controllers/api/GlobalSearchApiController.php index b36c3d6..8add90c 100644 --- a/app/Http/Controllers/api/GlobalSearchApiController.php +++ b/app/Http/Controllers/api/GlobalSearchApiController.php @@ -26,7 +26,7 @@ class GlobalSearchApiController extends Controller use BaseApiResponse; /** - * Massive Search across ALL data types (With dynamic Date/Number Sorting & Deep Art Search) + * Massive Search across ALL data types */ public function search(Request $request) { @@ -45,7 +45,7 @@ class GlobalSearchApiController extends Controller $results = collect(); if (!empty($searchTerm)) { - $limitPerTable = 100; // Performance safeguard + $limitPerTable = 100; // 1. Search Bylaw Documents $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) @@ -94,11 +94,19 @@ class GlobalSearchApiController extends Controller }); $results = $results->merge($opinions); - // 4. Search Arts (Exact match to ArtController logic + eager loading relations) - $arts = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate']) + // 4. Search Arts (DEEP RELATIONAL SEARCH INCLUDED) + $arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") - ->orWhere('text', 'LIKE', "%{$searchTerm}%"); + ->orWhere('text', 'LIKE', "%{$searchTerm}%") + ->orWhere('number', 'LIKE', "%{$searchTerm}%") // Added Number Search + ->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('gate', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }); }) ->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { $formatted = $this->formatArt($item, $searchTerm, Art::class); @@ -248,11 +256,19 @@ class GlobalSearchApiController extends Controller }); $results = $results->merge($opinions); - // 4. Search Arts - $artQuery = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate']) + // 4. Search Arts (DEEP RELATIONAL SEARCH INCLUDED) + $artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") - ->orWhere('text', 'LIKE', "%{$searchTerm}%"); + ->orWhere('text', 'LIKE', "%{$searchTerm}%") + ->orWhere('number', 'LIKE', "%{$searchTerm}%") + ->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) + ->orWhereHas('gate', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }); }); $totalCount += $artQuery->count(); $arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { @@ -307,13 +323,10 @@ class GlobalSearchApiController extends Controller private function formatBylaw($item) { $isLocked = $this->isLockedForCurrentUserGeneric($item); - $description = $item->description; - // Check if description is practically empty (null, empty string, or just empty WYSIWYG HTML like
) $cleanDesc = trim(strip_tags($description ?? '')); - // Direct Query Fallback: Fetch node_content manually via DB facade to bypass any Model restrictions if (empty($cleanDesc)) { $nodeContents = DB::table('bylaw_nodes') ->where('document_id', $item->id) @@ -374,14 +387,23 @@ class GlobalSearchApiController extends Controller $isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked; $text = $art->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] ?? ''); + + // Multi-byte safe Context Snippet Extraction for Persian/Arabic + if (!empty($searchTerm) && !empty($text)) { + $pos = mb_stripos($text, $searchTerm); + if ($pos !== false) { + $start = max(0, $pos - 50); + $length = mb_strlen($searchTerm) + 100; + $context = mb_substr($text, $start, $length); + if ($start > 0) $context = '...' . $context; + if ($start + $length < mb_strlen($text)) $context = $context . '...'; + } else { + $context = $text; // Fallback if match was in title/relations + } } else { $context = $text; } - // PERFECTLY MATCHED TO YOUR ARTCONTROLLER MAPPING return [ 'id' => $art->id, 'title' => $art->title, @@ -396,7 +418,12 @@ class GlobalSearchApiController extends Controller 'price' => $law?->price, 'image' => $law?->image, - // Full nested relationship mappings + // Relational Objects + 'law' => $art->law != null ? [ + 'id' => $art->law->id, + 'title' => $art->law->title, + 'number' => $art->law->number, + ] : null, 'chapter' => $art->chapter != null ? [ 'id' => $art->chapter->id, 'title' => $art->chapter->title, @@ -412,11 +439,6 @@ class GlobalSearchApiController extends Controller 'title' => $art->volum->title, 'number' => $art->volum->number, ] : null, - 'law' => $art->law != null ? [ - 'id' => $art->law->id, - 'title' => $art->law->title, - 'number' => $art->law->number, - ] : null, 'book' => $art->book != null ? [ 'id' => $art->book->id, 'title' => $art->book->title, @@ -468,12 +490,12 @@ class GlobalSearchApiController extends Controller $query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title, $query->relationLoaded('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title, $query->relationLoaded('book') ? $query->book?->title : Book::find($query->book_id)?->title, - Division::find($query->division_id)?->title, // Division is not in eagerly loaded relations, query safely + Division::find($query->division_id)?->title, $query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_id)?->title, $query->relationLoaded('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title, $query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_id)?->title, $query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title, - Branch::find($query->branch_id)?->title, // Branch is not in eagerly loaded relations, query safely + Branch::find($query->branch_id)?->title, ]); return array_values($route); }