Fixed major bugs

This commit is contained in:
m.razaghi
2026-07-09 17:06:07 +03:30
parent b7f139b935
commit 71dc7da8a4
4 changed files with 50 additions and 61 deletions

View File

@@ -24,9 +24,6 @@ class GlobalSearchApiController extends Controller
{
use BaseApiResponse;
/**
* Massive Search across ALL data types
*/
public function search(Request $request)
{
$request->validate([
@@ -42,7 +39,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'])
@@ -111,7 +108,6 @@ class GlobalSearchApiController extends Controller
$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;
@@ -121,12 +117,9 @@ class GlobalSearchApiController extends Controller
}
}
// --- 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,
@@ -150,8 +143,15 @@ class GlobalSearchApiController extends Controller
* FORMATTING HELPERS
* ----------------------------------------------------------------- */
private function isLockedForCurrentUserGeneric($item): bool
{
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
private function formatBylaw($item)
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
return [
'id' => $item->id,
'title' => $item->title,
@@ -159,42 +159,48 @@ class GlobalSearchApiController extends Controller
'approval_authority_name' => optional($item->approvalAuthority)->name,
'approval_date' => $item->approval_date,
'promulgation_number' => $item->promulgation_number,
'description' => $item->description,
'description' => $isLocked ? null : $item->description,
'is_locked' => $isLocked,
];
}
private function formatJudicial($item)
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
return [
'id' => $item->id,
'title' => $item->subject, // Map subject to title for universal UI parsing
'title' => $item->subject,
'ruling_number' => $item->ruling_number,
'ruling_date' => $item->ruling_date,
'text' => $item->full_text,
'text' => $isLocked ? null : $item->full_text,
'issuing_authority' => $item->issuing_authority,
'category_name' => optional($item->category)->name,
'is_locked' => $isLocked,
];
}
private function formatOpinion($item)
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
return [
'id' => $item->id,
'title' => $item->subject, // Map subject to title for universal UI parsing
'title' => $item->subject,
'opinion_number' => $item->opinion_number,
'opinion_date' => $item->opinion_date,
'text' => $item->full_text,
'text' => $isLocked ? null : $item->full_text,
'issuing_authority' => $item->issuing_authority,
'category_name' => optional($item->category)->name,
'is_locked' => $isLocked,
];
}
private function formatArt($item, $searchTerm, $modelClass)
{
$law = Law::find($item->law_id);
$isLocked = $this->isLockedForCurrentUser($law);
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
// 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)) {
@@ -221,7 +227,7 @@ class GlobalSearchApiController extends Controller
private function formatStructure($item, $modelClass)
{
$law = Law::find($item?->law_id ?? $item->id);
$isLocked = $this->isLockedForCurrentUser($law);
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
$payload = [
'id' => $item->id,
@@ -240,15 +246,6 @@ class GlobalSearchApiController extends Controller
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') {