Fixed api issue
This commit is contained in:
@@ -48,27 +48,20 @@ class GlobalSearchApiController extends Controller
|
|||||||
$limitPerTable = 100;
|
$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'])
|
$arts = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('text', 'LIKE', "%{$searchTerm}%")
|
->orWhere('text', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('number', '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}%"); });
|
|
||||||
})
|
})
|
||||||
->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
||||||
$formatted = $this->formatArt($item, $searchTerm, Art::class);
|
$formatted = $this->formatArt($item, $searchTerm, Art::class);
|
||||||
$formatted['type'] = 'art';
|
$formatted['type'] = 'art';
|
||||||
return $formatted;
|
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
|
// 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) {
|
if ($request->has('sort_by') && $request->input('sort_by') != null) {
|
||||||
$sortBy = $request->input('sort_by');
|
$sortBy = $request->input('sort_by');
|
||||||
$sortOrder = $request->input('sort', 'desc');
|
$sortOrder = $request->input('sort', 'desc');
|
||||||
@@ -166,7 +159,8 @@ class GlobalSearchApiController extends Controller
|
|||||||
}
|
}
|
||||||
} elseif ($sortBy === 'date') {
|
} elseif ($sortBy === 'date') {
|
||||||
$sortCallback = function ($item) {
|
$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') {
|
if ($sortOrder === 'asc') {
|
||||||
@@ -213,19 +207,12 @@ class GlobalSearchApiController extends Controller
|
|||||||
|
|
||||||
if (!empty($searchTerm)) {
|
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'])
|
$artQuery = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('text', 'LIKE', "%{$searchTerm}%")
|
->orWhere('text', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('number', '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();
|
$totalCount += $artQuery->count();
|
||||||
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
||||||
@@ -356,6 +343,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
'promulgation_number' => $item->promulgation_number,
|
'promulgation_number' => $item->promulgation_number,
|
||||||
'description' => $isLocked ? null : $description,
|
'description' => $isLocked ? null : $description,
|
||||||
'is_locked' => $isLocked,
|
'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,
|
'issuing_authority' => $item->issuing_authority,
|
||||||
'category_name' => optional($item->category)->name,
|
'category_name' => optional($item->category)->name,
|
||||||
'is_locked' => $isLocked,
|
'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,
|
'issuing_authority' => $item->issuing_authority,
|
||||||
'category_name' => optional($item->category)->name,
|
'category_name' => optional($item->category)->name,
|
||||||
'is_locked' => $isLocked,
|
'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);
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false);
|
||||||
|
|
||||||
$text = $q->text ?? '';
|
$text = $q->text ?? '';
|
||||||
$searchQuoted = preg_quote($searchTerm, '/');
|
|
||||||
|
|
||||||
// Exact regex match logic from ArtController
|
// Multi-byte safe Context Snippet Extraction for Persian/Arabic
|
||||||
if (preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
if (!empty($searchTerm) && !empty($text)) {
|
||||||
$context = trim($matches[1] ?? '').' '.($matches[2] ?? '').' '.trim($matches[3] ?? '');
|
$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 {
|
} else {
|
||||||
$context = $text;
|
$context = $text;
|
||||||
}
|
}
|
||||||
@@ -417,13 +415,19 @@ class GlobalSearchApiController extends Controller
|
|||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
'route' => array_values($this->route($modelClass, $q)),
|
'route' => array_values($this->route($modelClass, $q)),
|
||||||
'category' => optional($law?->category)->name,
|
'category' => optional($law?->category)->name,
|
||||||
'law' => $law?->title,
|
|
||||||
'count_art' => $law?->arts?->count() ?? 0,
|
'count_art' => $law?->arts?->count() ?? 0,
|
||||||
'count_volum' => $law?->volums?->count() ?? 0,
|
'count_volums'=> $law?->volums?->count() ?? 0,
|
||||||
'price' => $law?->price,
|
'price' => $law?->price,
|
||||||
'image' => $law?->image,
|
'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 ? [
|
'chapter' => $q->chapter != null ? [
|
||||||
'id' => $q->chapter->id,
|
'id' => $q->chapter->id,
|
||||||
'title' => $q->chapter->title,
|
'title' => $q->chapter->title,
|
||||||
@@ -439,11 +443,6 @@ class GlobalSearchApiController extends Controller
|
|||||||
'title' => $q->volum->title,
|
'title' => $q->volum->title,
|
||||||
'number' => $q->volum->number,
|
'number' => $q->volum->number,
|
||||||
] : null,
|
] : null,
|
||||||
'law_object' => $q->law != null ? [
|
|
||||||
'id' => $q->law->id,
|
|
||||||
'title' => $q->law->title,
|
|
||||||
'number' => $q->law->number,
|
|
||||||
] : null,
|
|
||||||
'book' => $q->book != null ? [
|
'book' => $q->book != null ? [
|
||||||
'id' => $q->book->id,
|
'id' => $q->book->id,
|
||||||
'title' => $q->book->title,
|
'title' => $q->book->title,
|
||||||
@@ -473,6 +472,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
'number' => $item->number,
|
'number' => $item->number,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
'route' => array_values($this->route($modelClass, $item)),
|
'route' => array_values($this->route($modelClass, $item)),
|
||||||
|
'created_at'=> $item->created_at ? (string)$item->created_at : null,
|
||||||
'law' => $law ? [
|
'law' => $law ? [
|
||||||
'id' => $law->id,
|
'id' => $law->id,
|
||||||
'title' => $law->title,
|
'title' => $law->title,
|
||||||
|
|||||||
Reference in New Issue
Block a user