diff --git a/app/Http/Controllers/api/GlobalSearchApiController.php b/app/Http/Controllers/api/GlobalSearchApiController.php index 8cccc4f..4276f82 100644 --- a/app/Http/Controllers/api/GlobalSearchApiController.php +++ b/app/Http/Controllers/api/GlobalSearchApiController.php @@ -48,27 +48,20 @@ class GlobalSearchApiController extends Controller $limitPerTable = 100; // ========================================================= - // 1. Search Arts FIRST (Highest Priority for Law Apps) + // 1. Search Arts FIRST // ========================================================= $arts = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) ->where(function ($q) use ($searchTerm) { $q->where('title', '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}%"); }); + ->orWhere('number', 'LIKE', "%{$searchTerm}%"); }) ->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { $formatted = $this->formatArt($item, $searchTerm, Art::class); $formatted['type'] = 'art'; return $formatted; }); - $results = $results->concat($arts); // Arts are now at the very top of the list! + $results = $results->concat($arts); // Arts are prioritized! // ========================================================= // 2. Search Bylaw Documents @@ -149,7 +142,7 @@ class GlobalSearchApiController extends Controller } } - // --- Apply Dynamic Memory Sorting ONLY if requested --- + // --- Apply Dynamic Memory Sorting --- if ($request->has('sort_by') && $request->input('sort_by') != null) { $sortBy = $request->input('sort_by'); $sortOrder = $request->input('sort', 'desc'); @@ -166,7 +159,8 @@ class GlobalSearchApiController extends Controller } } elseif ($sortBy === 'date') { $sortCallback = function ($item) { - return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? ''); + // FIX: Fallback to created_at so Articles are not pushed to the end of the list! + return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? $item['created_at'] ?? '1970-01-01'); }; if ($sortOrder === 'asc') { @@ -213,19 +207,12 @@ class GlobalSearchApiController extends Controller if (!empty($searchTerm)) { - // 1. Search Arts FIRST for Fast Search + // 1. Search Arts FIRST $artQuery = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) ->where(function ($q) use ($searchTerm) { $q->where('title', '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}%"); }); + ->orWhere('number', 'LIKE', "%{$searchTerm}%"); }); $totalCount += $artQuery->count(); $arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { @@ -356,6 +343,7 @@ class GlobalSearchApiController extends Controller 'promulgation_number' => $item->promulgation_number, 'description' => $isLocked ? null : $description, 'is_locked' => $isLocked, + 'created_at' => $item->created_at ? (string)$item->created_at : null, // Added for date sorting fallback ]; } @@ -372,6 +360,7 @@ class GlobalSearchApiController extends Controller 'issuing_authority' => $item->issuing_authority, 'category_name' => optional($item->category)->name, 'is_locked' => $isLocked, + 'created_at' => $item->created_at ? (string)$item->created_at : null, ]; } @@ -388,6 +377,7 @@ class GlobalSearchApiController extends Controller 'issuing_authority' => $item->issuing_authority, 'category_name' => optional($item->category)->name, 'is_locked' => $isLocked, + 'created_at' => $item->created_at ? (string)$item->created_at : null, ]; } @@ -400,11 +390,19 @@ class GlobalSearchApiController extends Controller $isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false); $text = $q->text ?? ''; - $searchQuoted = preg_quote($searchTerm, '/'); - // Exact regex match logic from ArtController - if (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; + } } else { $context = $text; } @@ -417,13 +415,19 @@ class GlobalSearchApiController extends Controller 'is_locked' => $isLocked, 'route' => array_values($this->route($modelClass, $q)), 'category' => optional($law?->category)->name, - 'law' => $law?->title, + 'count_art' => $law?->arts?->count() ?? 0, - 'count_volum' => $law?->volums?->count() ?? 0, + 'count_volums'=> $law?->volums?->count() ?? 0, 'price' => $law?->price, 'image' => $law?->image, + 'created_at' => $q->created_at ? (string)$q->created_at : null, - // Nested Relations exactly like ArtController + // Relational Objects + 'law' => $q->law != null ? [ + 'id' => $q->law->id, + 'title' => $q->law->title, + 'number' => $q->law->number, + ] : null, 'chapter' => $q->chapter != null ? [ 'id' => $q->chapter->id, 'title' => $q->chapter->title, @@ -439,11 +443,6 @@ class GlobalSearchApiController extends Controller 'title' => $q->volum->title, 'number' => $q->volum->number, ] : null, - 'law_object' => $q->law != null ? [ - 'id' => $q->law->id, - 'title' => $q->law->title, - 'number' => $q->law->number, - ] : null, 'book' => $q->book != null ? [ 'id' => $q->book->id, 'title' => $q->book->title, @@ -473,6 +472,7 @@ class GlobalSearchApiController extends Controller 'number' => $item->number, 'is_locked' => $isLocked, 'route' => array_values($this->route($modelClass, $item)), + 'created_at'=> $item->created_at ? (string)$item->created_at : null, 'law' => $law ? [ 'id' => $law->id, 'title' => $law->title,