Fixed api issue
This commit is contained in:
@@ -7,7 +7,6 @@ use App\Models\Art;
|
|||||||
use App\Models\Book;
|
use App\Models\Book;
|
||||||
use App\Models\Branch;
|
use App\Models\Branch;
|
||||||
use App\Models\BylawDocument;
|
use App\Models\BylawDocument;
|
||||||
use App\Models\BylawNode;
|
|
||||||
use App\Models\Chapter;
|
use App\Models\Chapter;
|
||||||
use App\Models\Division;
|
use App\Models\Division;
|
||||||
use App\Models\Gate;
|
use App\Models\Gate;
|
||||||
@@ -27,7 +26,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
use BaseApiResponse;
|
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)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
@@ -46,56 +45,11 @@ class GlobalSearchApiController extends Controller
|
|||||||
$results = collect();
|
$results = collect();
|
||||||
|
|
||||||
if (!empty($searchTerm)) {
|
if (!empty($searchTerm)) {
|
||||||
$limitPerTable = 100; // Performance safeguard
|
$limitPerTable = 100;
|
||||||
|
|
||||||
// 1. Search Bylaw Documents
|
// =========================================================
|
||||||
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
// 1. Search Arts FIRST (Highest Priority for Law Apps)
|
||||||
->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}%")
|
|
||||||
->orWhereIn('id', function($subQuery) use ($searchTerm) {
|
|
||||||
$subQuery->select('document_id')
|
|
||||||
->from('bylaw_nodes')
|
|
||||||
->where('node_content', 'LIKE', "%{$searchTerm}%");
|
|
||||||
});
|
|
||||||
})->take($limitPerTable)->get()->map(function($item) {
|
|
||||||
$formatted = $this->formatBylaw($item);
|
|
||||||
$formatted['type'] = 'bylaw';
|
|
||||||
return $formatted;
|
|
||||||
});
|
|
||||||
$results = $results->concat($bylaws); // Changed to concat to prevent overwriting
|
|
||||||
|
|
||||||
// 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->concat($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->concat($opinions);
|
|
||||||
|
|
||||||
// 4. Search Arts (Fully restored deep relational search without breaking relationships)
|
|
||||||
$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}%")
|
||||||
@@ -114,9 +68,64 @@ class GlobalSearchApiController extends Controller
|
|||||||
$formatted['type'] = 'art';
|
$formatted['type'] = 'art';
|
||||||
return $formatted;
|
return $formatted;
|
||||||
});
|
});
|
||||||
$results = $results->concat($arts);
|
$results = $results->concat($arts); // Arts are now at the very top of the list!
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 2. 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}%")
|
||||||
|
->orWhereIn('id', function($subQuery) use ($searchTerm) {
|
||||||
|
$subQuery->select('document_id')
|
||||||
|
->from('bylaw_nodes')
|
||||||
|
->where('node_content', 'LIKE', "%{$searchTerm}%");
|
||||||
|
});
|
||||||
|
})->take($limitPerTable)->get()->map(function($item) {
|
||||||
|
$formatted = $this->formatBylaw($item);
|
||||||
|
$formatted['type'] = 'bylaw';
|
||||||
|
return $formatted;
|
||||||
|
});
|
||||||
|
$results = $results->concat($bylaws);
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 3. 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->concat($judicials);
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
|
// 4. 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->concat($opinions);
|
||||||
|
|
||||||
|
// =========================================================
|
||||||
// 5. Search Laws and Structural Models
|
// 5. Search Laws and Structural Models
|
||||||
|
// =========================================================
|
||||||
$structuralModels = [
|
$structuralModels = [
|
||||||
Law::class, Volum::class, Book::class, Division::class,
|
Law::class, Volum::class, Book::class, Division::class,
|
||||||
Section::class, Chapter::class, Part::class, Gate::class,
|
Section::class, Chapter::class, Part::class, Gate::class,
|
||||||
@@ -140,8 +149,8 @@ class GlobalSearchApiController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Apply Dynamic Memory Sorting ---
|
// --- Apply Dynamic Memory Sorting ONLY if requested ---
|
||||||
if ($request->has('sort_by')) {
|
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');
|
||||||
|
|
||||||
@@ -204,7 +213,29 @@ class GlobalSearchApiController extends Controller
|
|||||||
|
|
||||||
if (!empty($searchTerm)) {
|
if (!empty($searchTerm)) {
|
||||||
|
|
||||||
// 1. Bylaw Documents
|
// 1. Search Arts FIRST for Fast Search
|
||||||
|
$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}%"); });
|
||||||
|
});
|
||||||
|
$totalCount += $artQuery->count();
|
||||||
|
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
||||||
|
$formatted = $this->formatArt($item, $searchTerm, Art::class);
|
||||||
|
$formatted['type'] = 'art';
|
||||||
|
return $formatted;
|
||||||
|
});
|
||||||
|
$results = $results->concat($arts);
|
||||||
|
|
||||||
|
// 2. Bylaw Documents
|
||||||
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||||
->where('is_published', true)
|
->where('is_published', true)
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
@@ -226,7 +257,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->concat($bylaws);
|
$results = $results->concat($bylaws);
|
||||||
|
|
||||||
// 2. Judicial Precedents
|
// 3. Judicial Precedents
|
||||||
$judicialQuery = JudicialPrecedent::with('category')
|
$judicialQuery = JudicialPrecedent::with('category')
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
|
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
|
||||||
@@ -241,7 +272,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->concat($judicials);
|
$results = $results->concat($judicials);
|
||||||
|
|
||||||
// 3. Legal Opinions
|
// 4. Legal Opinions
|
||||||
$opinionQuery = LegalOpinion::with('category')
|
$opinionQuery = LegalOpinion::with('category')
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('opinion_number', 'LIKE', "%{$searchTerm}%")
|
$q->where('opinion_number', 'LIKE', "%{$searchTerm}%")
|
||||||
@@ -257,28 +288,6 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->concat($opinions);
|
$results = $results->concat($opinions);
|
||||||
|
|
||||||
// 4. Search Arts
|
|
||||||
$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}%"); });
|
|
||||||
});
|
|
||||||
$totalCount += $artQuery->count();
|
|
||||||
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
|
||||||
$formatted = $this->formatArt($item, $searchTerm, Art::class);
|
|
||||||
$formatted['type'] = 'art';
|
|
||||||
return $formatted;
|
|
||||||
});
|
|
||||||
$results = $results->concat($arts);
|
|
||||||
|
|
||||||
// 5. Structural Models
|
// 5. Structural Models
|
||||||
$structuralModels = [
|
$structuralModels = [
|
||||||
Law::class, Volum::class, Book::class, Division::class,
|
Law::class, Volum::class, Book::class, Division::class,
|
||||||
@@ -324,7 +333,6 @@ class GlobalSearchApiController extends Controller
|
|||||||
private function formatBylaw($item)
|
private function formatBylaw($item)
|
||||||
{
|
{
|
||||||
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
||||||
|
|
||||||
$description = $item->description;
|
$description = $item->description;
|
||||||
|
|
||||||
$cleanDesc = trim(strip_tags($description ?? ''));
|
$cleanDesc = trim(strip_tags($description ?? ''));
|
||||||
@@ -383,76 +391,73 @@ class GlobalSearchApiController extends Controller
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatArt($art, $searchTerm, $modelClass)
|
/**
|
||||||
|
* Exact 1:1 match with ArtController format mapping
|
||||||
|
*/
|
||||||
|
private function formatArt($q, $searchTerm, $modelClass)
|
||||||
{
|
{
|
||||||
$law = $art->law ?? Law::find($art->law_id);
|
$law = $q->law ?? Law::find($q->law_id);
|
||||||
$isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false);
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false);
|
||||||
|
|
||||||
$text = $art->text ?? '';
|
$text = $q->text ?? '';
|
||||||
|
$searchQuoted = preg_quote($searchTerm, '/');
|
||||||
|
|
||||||
if (!empty($searchTerm) && !empty($text)) {
|
// Exact regex match logic from ArtController
|
||||||
$pos = mb_stripos($text, $searchTerm);
|
if (preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
||||||
if ($pos !== false) {
|
$context = trim($matches[1] ?? '').' '.($matches[2] ?? '').' '.trim($matches[3] ?? '');
|
||||||
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $art->id,
|
'id' => $q->id,
|
||||||
'title' => $art->title,
|
'title' => $q->title,
|
||||||
'number' => $art->number,
|
'number' => $q->number,
|
||||||
'text' => $isLocked ? null : $context,
|
'text' => $isLocked ? null : $context,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
'route' => array_values($this->route($modelClass, $art)),
|
'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_volums'=> $law?->volums?->count() ?? 0,
|
'count_volum' => $law?->volums?->count() ?? 0,
|
||||||
'price' => $law?->price,
|
'price' => $law?->price,
|
||||||
'image' => $law?->image,
|
'image' => $law?->image,
|
||||||
|
|
||||||
'law' => $art->law != null ? [
|
// Nested Relations exactly like ArtController
|
||||||
'id' => $art->law->id,
|
'chapter' => $q->chapter != null ? [
|
||||||
'title' => $art->law->title,
|
'id' => $q->chapter->id,
|
||||||
'number' => $art->law->number,
|
'title' => $q->chapter->title,
|
||||||
|
'number' => $q->chapter->number,
|
||||||
] : null,
|
] : null,
|
||||||
'chapter' => $art->chapter != null ? [
|
'part' => $q->part != null ? [
|
||||||
'id' => $art->chapter->id,
|
'id' => $q->part->id,
|
||||||
'title' => $art->chapter->title,
|
'title' => $q->part->title,
|
||||||
'number' => $art->chapter->number,
|
'number' => $q->part->number,
|
||||||
] : null,
|
] : null,
|
||||||
'part' => $art->part != null ? [
|
'volum' => $q->volum != null ? [
|
||||||
'id' => $art->part->id,
|
'id' => $q->volum->id,
|
||||||
'title' => $art->part->title,
|
'title' => $q->volum->title,
|
||||||
'number' => $art->part->number,
|
'number' => $q->volum->number,
|
||||||
] : null,
|
] : null,
|
||||||
'volum' => $art->volum != null ? [
|
'law_object' => $q->law != null ? [
|
||||||
'id' => $art->volum->id,
|
'id' => $q->law->id,
|
||||||
'title' => $art->volum->title,
|
'title' => $q->law->title,
|
||||||
'number' => $art->volum->number,
|
'number' => $q->law->number,
|
||||||
] : null,
|
] : null,
|
||||||
'book' => $art->book != null ? [
|
'book' => $q->book != null ? [
|
||||||
'id' => $art->book->id,
|
'id' => $q->book->id,
|
||||||
'title' => $art->book->title,
|
'title' => $q->book->title,
|
||||||
'number' => $art->book->number,
|
'number' => $q->book->number,
|
||||||
] : null,
|
] : null,
|
||||||
'section' => $art->section != null ? [
|
'section' => $q->section != null ? [
|
||||||
'id' => $art->section->id,
|
'id' => $q->section->id,
|
||||||
'title' => $art->section->title,
|
'title' => $q->section->title,
|
||||||
'number' => $art->section->number,
|
'number' => $q->section->number,
|
||||||
] : null,
|
] : null,
|
||||||
'gate' => $art->gate != null ? [
|
'gate' => $q->gate != null ? [
|
||||||
'id' => $art->gate->id,
|
'id' => $q->gate->id,
|
||||||
'title' => $art->gate->title,
|
'title' => $q->gate->title,
|
||||||
'number' => $art->gate->number,
|
'number' => $q->gate->number,
|
||||||
] : null,
|
] : null,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user