|
|
|
|
@@ -7,7 +7,6 @@ use App\Models\Art;
|
|
|
|
|
use App\Models\Book;
|
|
|
|
|
use App\Models\Branch;
|
|
|
|
|
use App\Models\BylawDocument;
|
|
|
|
|
use App\Models\BylawNode;
|
|
|
|
|
use App\Models\Chapter;
|
|
|
|
|
use App\Models\Division;
|
|
|
|
|
use App\Models\Gate;
|
|
|
|
|
@@ -20,13 +19,14 @@ use App\Models\Volum;
|
|
|
|
|
use App\Traits\BaseApiResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
@@ -42,78 +42,30 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
|
|
|
|
$page = (int) $request->input('page', 1);
|
|
|
|
|
|
|
|
|
|
$results = collect();
|
|
|
|
|
$staticResults = collect(); // Arts and Structures (Always at the top)
|
|
|
|
|
$sortableResults = collect(); // Bylaws, Opinions, Precedents (Sorted if requested)
|
|
|
|
|
|
|
|
|
|
if (!empty($searchTerm)) {
|
|
|
|
|
$limitPerTable = 100; // Performance safeguard
|
|
|
|
|
$limitPerTable = 100;
|
|
|
|
|
|
|
|
|
|
// 1. Search Bylaw Documents
|
|
|
|
|
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
|
|
|
|
|
->where('is_published', true)
|
|
|
|
|
// =========================================================
|
|
|
|
|
// 1. Search Arts (Exact match to ArtController)
|
|
|
|
|
// =========================================================
|
|
|
|
|
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
|
|
|
|
|
->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->merge($bylaws);
|
|
|
|
|
|
|
|
|
|
// 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->merge($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->merge($opinions);
|
|
|
|
|
|
|
|
|
|
// 4. Search Arts (Deep Relational Search & Eager Loading)
|
|
|
|
|
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch'])
|
|
|
|
|
->where(function ($q) use ($searchTerm) {
|
|
|
|
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
|
|
|
|
->orWhere('text', '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}%"); });
|
|
|
|
|
->orWhere('text', 'LIKE', "%{$searchTerm}%");
|
|
|
|
|
})
|
|
|
|
|
->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
|
|
|
|
$formatted = $this->formatArt($item, $searchTerm, Art::class);
|
|
|
|
|
$formatted['type'] = 'art';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->merge($arts);
|
|
|
|
|
$staticResults = $staticResults->concat($arts);
|
|
|
|
|
|
|
|
|
|
// 5. Search Laws and Structural Models
|
|
|
|
|
// =========================================================
|
|
|
|
|
// 2. Search Laws and Structural Models (Exact match to ArtController)
|
|
|
|
|
// =========================================================
|
|
|
|
|
$structuralModels = [
|
|
|
|
|
Law::class, Volum::class, Book::class, Division::class,
|
|
|
|
|
Section::class, Chapter::class, Part::class, Gate::class,
|
|
|
|
|
@@ -133,39 +85,95 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$results = $results->merge($items);
|
|
|
|
|
$staticResults = $staticResults->concat($items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// =========================================================
|
|
|
|
|
// 3. 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;
|
|
|
|
|
});
|
|
|
|
|
$sortableResults = $sortableResults->concat($bylaws);
|
|
|
|
|
|
|
|
|
|
// =========================================================
|
|
|
|
|
// 4. 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;
|
|
|
|
|
});
|
|
|
|
|
$sortableResults = $sortableResults->concat($judicials);
|
|
|
|
|
|
|
|
|
|
// =========================================================
|
|
|
|
|
// 5. 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;
|
|
|
|
|
});
|
|
|
|
|
$sortableResults = $sortableResults->concat($opinions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Apply Dynamic Memory Sorting ---
|
|
|
|
|
if ($request->has('sort_by')) {
|
|
|
|
|
// --- Apply Dynamic Memory Sorting ONLY to Legal Docs ---
|
|
|
|
|
if ($request->has('sort_by') && $request->input('sort_by') != null) {
|
|
|
|
|
$sortBy = $request->input('sort_by');
|
|
|
|
|
$sortOrder = $request->input('sort', 'desc');
|
|
|
|
|
|
|
|
|
|
if ($sortBy === 'number') {
|
|
|
|
|
$sortCallback = function ($item) {
|
|
|
|
|
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? $item['number'] ?? '');
|
|
|
|
|
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? '0');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ($sortOrder === 'asc') {
|
|
|
|
|
$results = $results->sortBy($sortCallback, SORT_NATURAL)->values();
|
|
|
|
|
$sortableResults = $sortableResults->sortBy($sortCallback, SORT_NATURAL)->values();
|
|
|
|
|
} else {
|
|
|
|
|
$results = $results->sortByDesc($sortCallback, SORT_NATURAL)->values();
|
|
|
|
|
$sortableResults = $sortableResults->sortByDesc($sortCallback, SORT_NATURAL)->values();
|
|
|
|
|
}
|
|
|
|
|
} elseif ($sortBy === 'date') {
|
|
|
|
|
$sortCallback = function ($item) {
|
|
|
|
|
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '');
|
|
|
|
|
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '1970-01-01');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ($sortOrder === 'asc') {
|
|
|
|
|
$results = $results->sortBy($sortCallback)->values();
|
|
|
|
|
$sortableResults = $sortableResults->sortBy($sortCallback)->values();
|
|
|
|
|
} else {
|
|
|
|
|
$results = $results->sortByDesc($sortCallback)->values();
|
|
|
|
|
$sortableResults = $sortableResults->sortByDesc($sortCallback)->values();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Execute Manual Pagination on the Sorted Collection ---
|
|
|
|
|
// Combine Results: Arts and Structures ALWAYS pin to the top!
|
|
|
|
|
$results = $staticResults->concat($sortableResults);
|
|
|
|
|
|
|
|
|
|
// --- Execute Manual Pagination on the final Collection ---
|
|
|
|
|
$total = $results->count();
|
|
|
|
|
$paginatedItems = $results->forPage($page, $perPage)->values();
|
|
|
|
|
|
|
|
|
|
@@ -185,7 +193,7 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
'total' => $paginator->total(),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return $this->success($responseData, 'Success', 'Global Search Results Fetched');
|
|
|
|
|
return $this->success($responseData, '', '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -201,70 +209,11 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
|
|
|
|
|
if (!empty($searchTerm)) {
|
|
|
|
|
|
|
|
|
|
// 1. Bylaw Documents
|
|
|
|
|
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
|
|
|
|
|
->where('is_published', true)
|
|
|
|
|
// 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('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}%");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $bylawQuery->count();
|
|
|
|
|
$bylaws = $bylawQuery->take($limitPerTable)->get()->map(function($item) {
|
|
|
|
|
$formatted = $this->formatBylaw($item);
|
|
|
|
|
$formatted['type'] = 'bylaw';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->merge($bylaws);
|
|
|
|
|
|
|
|
|
|
// 2. Judicial Precedents
|
|
|
|
|
$judicialQuery = JudicialPrecedent::with('category')
|
|
|
|
|
->where(function ($q) use ($searchTerm) {
|
|
|
|
|
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
|
|
|
|
|
->orWhere('subject', 'LIKE', "%{$searchTerm}%")
|
|
|
|
|
->orWhere('full_text', 'LIKE', "%{$searchTerm}%");
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $judicialQuery->count();
|
|
|
|
|
$judicials = $judicialQuery->take($limitPerTable)->get()->map(function($item) {
|
|
|
|
|
$formatted = $this->formatJudicial($item);
|
|
|
|
|
$formatted['type'] = 'judicial_precedent';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->merge($judicials);
|
|
|
|
|
|
|
|
|
|
// 3. Legal Opinions
|
|
|
|
|
$opinionQuery = 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}%");
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $opinionQuery->count();
|
|
|
|
|
$opinions = $opinionQuery->take($limitPerTable)->get()->map(function($item) {
|
|
|
|
|
$formatted = $this->formatOpinion($item);
|
|
|
|
|
$formatted['type'] = 'legal_opinion';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->merge($opinions);
|
|
|
|
|
|
|
|
|
|
// 4. Search Arts
|
|
|
|
|
$artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch'])
|
|
|
|
|
->where(function ($q) use ($searchTerm) {
|
|
|
|
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
|
|
|
|
->orWhere('text', '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}%"); });
|
|
|
|
|
->orWhere('text', 'LIKE', "%{$searchTerm}%");
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $artQuery->count();
|
|
|
|
|
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
|
|
|
|
@@ -272,9 +221,9 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
$formatted['type'] = 'art';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->merge($arts);
|
|
|
|
|
$results = $results->concat($arts);
|
|
|
|
|
|
|
|
|
|
// 5. Structural Models
|
|
|
|
|
// 2. Structural Models
|
|
|
|
|
$structuralModels = [
|
|
|
|
|
Law::class, Volum::class, Book::class, Division::class,
|
|
|
|
|
Section::class, Chapter::class, Part::class, Gate::class,
|
|
|
|
|
@@ -296,15 +245,68 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
$formatted['type'] = ($table === 'section') ? 'sections' : $table;
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->merge($items);
|
|
|
|
|
$results = $results->concat($items);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Bylaw Documents
|
|
|
|
|
$bylawQuery = 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}%");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $bylawQuery->count();
|
|
|
|
|
$bylaws = $bylawQuery->take($limitPerTable)->get()->map(function($item) {
|
|
|
|
|
$formatted = $this->formatBylaw($item);
|
|
|
|
|
$formatted['type'] = 'bylaw';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->concat($bylaws);
|
|
|
|
|
|
|
|
|
|
// 4. Judicial Precedents
|
|
|
|
|
$judicialQuery = JudicialPrecedent::with('category')
|
|
|
|
|
->where(function ($q) use ($searchTerm) {
|
|
|
|
|
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
|
|
|
|
|
->orWhere('subject', 'LIKE', "%{$searchTerm}%")
|
|
|
|
|
->orWhere('full_text', 'LIKE', "%{$searchTerm}%");
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $judicialQuery->count();
|
|
|
|
|
$judicials = $judicialQuery->take($limitPerTable)->get()->map(function($item) {
|
|
|
|
|
$formatted = $this->formatJudicial($item);
|
|
|
|
|
$formatted['type'] = 'judicial_precedent';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->concat($judicials);
|
|
|
|
|
|
|
|
|
|
// 5. Legal Opinions
|
|
|
|
|
$opinionQuery = 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}%");
|
|
|
|
|
});
|
|
|
|
|
$totalCount += $opinionQuery->count();
|
|
|
|
|
$opinions = $opinionQuery->take($limitPerTable)->get()->map(function($item) {
|
|
|
|
|
$formatted = $this->formatOpinion($item);
|
|
|
|
|
$formatted['type'] = 'legal_opinion';
|
|
|
|
|
return $formatted;
|
|
|
|
|
});
|
|
|
|
|
$results = $results->concat($opinions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
|
'items' => $results->take(15)->values(),
|
|
|
|
|
'count' => $totalCount
|
|
|
|
|
], 'Success', 'Global Fast Search Results');
|
|
|
|
|
], '', '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
|
@@ -319,10 +321,18 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
private function formatBylaw($item)
|
|
|
|
|
{
|
|
|
|
|
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
|
|
|
|
|
|
|
|
|
$description = $item->description;
|
|
|
|
|
if (empty($description) && $item->relationLoaded('nodes')) {
|
|
|
|
|
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n");
|
|
|
|
|
|
|
|
|
|
$cleanDesc = trim(strip_tags($description ?? ''));
|
|
|
|
|
|
|
|
|
|
if (empty($cleanDesc)) {
|
|
|
|
|
$nodeContents = DB::table('bylaw_nodes')
|
|
|
|
|
->where('document_id', $item->id)
|
|
|
|
|
->orderBy('sort_order', 'asc')
|
|
|
|
|
->pluck('node_content')
|
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
|
|
$description = implode("\n\n", array_filter($nodeContents));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
@@ -369,76 +379,44 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Exact 1:1 match with ArtController::searchModel mapping
|
|
|
|
|
* All relations (law, chapter, etc) are strictly Strings to prevent App Crashes!
|
|
|
|
|
*/
|
|
|
|
|
private function formatArt($item, $searchTerm, $modelClass)
|
|
|
|
|
{
|
|
|
|
|
$law = $item->law ?? Law::find($item->law_id);
|
|
|
|
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
|
|
|
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false);
|
|
|
|
|
|
|
|
|
|
$text = $item->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] ?? '');
|
|
|
|
|
if (!empty($searchTerm) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
|
|
|
|
$context = trim($matches[1] ?? '').' '.($matches[2] ?? '').' '.trim($matches[3] ?? '');
|
|
|
|
|
} else {
|
|
|
|
|
$context = $text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => $item->id,
|
|
|
|
|
'title' => $item->title,
|
|
|
|
|
'number' => $item->number,
|
|
|
|
|
'text' => $isLocked ? null : $context,
|
|
|
|
|
'is_locked' => $isLocked,
|
|
|
|
|
'route' => array_values($this->route($modelClass, $item)),
|
|
|
|
|
'category' => optional($law->category)->name,
|
|
|
|
|
|
|
|
|
|
'count_art' => $law?->arts?->count() ?? 0,
|
|
|
|
|
'count_volums'=> $law?->volums?->count() ?? 0,
|
|
|
|
|
'price' => $law?->price,
|
|
|
|
|
'image' => $law?->image,
|
|
|
|
|
|
|
|
|
|
// Relational Objects
|
|
|
|
|
'law' => $item->law != null ? [
|
|
|
|
|
'id' => $item->law->id,
|
|
|
|
|
'title' => $item->law->title,
|
|
|
|
|
'number' => $item->law->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'chapter' => $item->chapter != null ? [
|
|
|
|
|
'id' => $item->chapter->id,
|
|
|
|
|
'title' => $item->chapter->title,
|
|
|
|
|
'number' => $item->chapter->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'part' => $item->part != null ? [
|
|
|
|
|
'id' => $item->part->id,
|
|
|
|
|
'title' => $item->part->title,
|
|
|
|
|
'number' => $item->part->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'volum' => $item->volum != null ? [
|
|
|
|
|
'id' => $item->volum->id,
|
|
|
|
|
'title' => $item->volum->title,
|
|
|
|
|
'number' => $item->volum->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'book' => $item->book != null ? [
|
|
|
|
|
'id' => $item->book->id,
|
|
|
|
|
'title' => $item->book->title,
|
|
|
|
|
'number' => $item->book->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'section' => $item->section != null ? [
|
|
|
|
|
'id' => $item->section->id,
|
|
|
|
|
'title' => $item->section->title,
|
|
|
|
|
'number' => $item->section->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'gate' => $item->gate != null ? [
|
|
|
|
|
'id' => $item->gate->id,
|
|
|
|
|
'title' => $item->gate->title,
|
|
|
|
|
'number' => $item->gate->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'id' => $item->id,
|
|
|
|
|
'title' => $item->title,
|
|
|
|
|
'number' => $item->number,
|
|
|
|
|
'text' => $isLocked ? null : $context,
|
|
|
|
|
'is_locked' => $isLocked,
|
|
|
|
|
'route' => array_values($this->route($modelClass, $item)),
|
|
|
|
|
'category' => optional($law?->category)->name,
|
|
|
|
|
'law' => $law?->title ?? '', // STRICLY STRING! Prevents Android App Crash!
|
|
|
|
|
'count_art' => $law?->arts?->count() ?? 0,
|
|
|
|
|
'count_volums' => $law?->volums?->count() ?? 0, // WITH S
|
|
|
|
|
'count_volum' => $law?->volums?->count() ?? 0, // WITHOUT S (Fallback)
|
|
|
|
|
'price' => $law?->price,
|
|
|
|
|
'image' => $law?->image,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function formatStructure($item, $modelClass)
|
|
|
|
|
{
|
|
|
|
|
$law = Law::find($item?->law_id ?? $item->id);
|
|
|
|
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
|
|
|
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false);
|
|
|
|
|
|
|
|
|
|
$payload = [
|
|
|
|
|
'id' => $item->id,
|
|
|
|
|
@@ -446,12 +424,7 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
'number' => $item->number,
|
|
|
|
|
'is_locked' => $isLocked,
|
|
|
|
|
'route' => array_values($this->route($modelClass, $item)),
|
|
|
|
|
// Keep law payload explicitly as an object to prevent DTO parsing errors
|
|
|
|
|
'law' => $law ? [
|
|
|
|
|
'id' => $law->id,
|
|
|
|
|
'title' => $law->title,
|
|
|
|
|
'number' => $law->number,
|
|
|
|
|
] : null,
|
|
|
|
|
'law' => $law?->title ?? '', // STRICTLY STRING
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ((new $modelClass)->getTable() == 'laws') {
|
|
|
|
|
@@ -464,18 +437,17 @@ class GlobalSearchApiController extends Controller
|
|
|
|
|
|
|
|
|
|
private function route($modelClass, $query)
|
|
|
|
|
{
|
|
|
|
|
// Now safely uses eager loaded relations if they exist!
|
|
|
|
|
if ((new $modelClass)->getTable() == 'art') {
|
|
|
|
|
$route = array_filter([
|
|
|
|
|
$query->law->title ?? Law::find($query->law_id)?->title,
|
|
|
|
|
$query->volum->title ?? Volum::find($query->volum_id)?->title,
|
|
|
|
|
$query->book->title ?? Book::find($query->book_id)?->title,
|
|
|
|
|
$query->division->title ?? Division::find($query->division_id)?->title,
|
|
|
|
|
$query->section->title ?? Section::find($query->section_id)?->title,
|
|
|
|
|
$query->chapter->title ?? Chapter::find($query->chapter_id)?->title,
|
|
|
|
|
$query->part->title ?? Part::find($query->part_id)?->title,
|
|
|
|
|
$query->gate->title ?? Gate::find($query->gate_id)?->title,
|
|
|
|
|
$query->branch->title ?? Branch::find($query->branch_id)?->title,
|
|
|
|
|
$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,
|
|
|
|
|
$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,
|
|
|
|
|
]);
|
|
|
|
|
return array_values($route);
|
|
|
|
|
}
|
|
|
|
|
|