Compare commits
24 Commits
d47f148f12
...
new-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcea6423ff | ||
|
|
51c4570f97 | ||
|
|
19db0fd24d | ||
|
|
4d15014621 | ||
|
|
6c0b1f83f2 | ||
|
|
cad3fa9e1f | ||
|
|
1a3f41ca8a | ||
|
|
e4dad6f9a4 | ||
| 16e68c3048 | |||
|
|
10463e2f71 | ||
| ed95f5b3ee | |||
|
|
1eedb30acc | ||
| 0962801c96 | |||
|
|
b2ac3d6ec8 | ||
| ecccab9edc | |||
|
|
737f06f68e | ||
| 370f4c0a78 | |||
|
|
a95826e5e5 | ||
|
|
398fcb4eec | ||
| 4214a4b007 | |||
|
|
9d124a07c1 | ||
|
|
ffea0e3c11 | ||
| 2a6313802b | |||
|
|
3b9fcd76df |
@@ -6,8 +6,11 @@ use App\Http\Controllers\Controller;
|
|||||||
use App\Models\BylawDocument;
|
use App\Models\BylawDocument;
|
||||||
use App\Models\BylawLegalAuthority;
|
use App\Models\BylawLegalAuthority;
|
||||||
use App\Models\BylawSubjectCategory;
|
use App\Models\BylawSubjectCategory;
|
||||||
|
use App\Models\BylawNode;
|
||||||
|
use App\Models\BylawCodificationLink;
|
||||||
use App\Traits\BaseApiResponse;
|
use App\Traits\BaseApiResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class BylawApiController extends Controller
|
class BylawApiController extends Controller
|
||||||
{
|
{
|
||||||
@@ -92,6 +95,63 @@ class BylawApiController extends Controller
|
|||||||
return $this->success($responseData, 'Success', 'Bylaws Fetched');
|
return $this->success($responseData, 'Success', 'Bylaws Fetched');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function single($id)
|
||||||
|
{
|
||||||
|
$bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||||
|
->where('is_published', true)
|
||||||
|
->find($id);
|
||||||
|
|
||||||
|
if (!$bylaw) {
|
||||||
|
return $this->failed([], [
|
||||||
|
'title' => 'خطا',
|
||||||
|
'message' => 'آییننامه مورد نظر یافت نشد.'
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$formatted = $this->formatBylaw($bylaw);
|
||||||
|
|
||||||
|
if (!$formatted['is_locked']) {
|
||||||
|
$nodes = BylawNode::with('type')
|
||||||
|
->where('document_id', $bylaw->id)
|
||||||
|
->orderBy('sort_order', 'asc')
|
||||||
|
->get()
|
||||||
|
->map(function ($node) {
|
||||||
|
return [
|
||||||
|
'id' => $node->id,
|
||||||
|
'parent_id' => $node->parent_id,
|
||||||
|
'node_type' => optional($node->type)->name,
|
||||||
|
'node_identifier' => $node->node_identifier,
|
||||||
|
'title' => $node->title,
|
||||||
|
'node_content' => $node->node_content,
|
||||||
|
'sort_order' => $node->sort_order,
|
||||||
|
'valid_from' => $node->valid_from,
|
||||||
|
'valid_to' => $node->valid_to,
|
||||||
|
'node_status' => $node->node_status,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
$formatted['nodes'] = $nodes->toArray();
|
||||||
|
|
||||||
|
$links = BylawCodificationLink::where('source_entity_type', 'bylaw_doc')
|
||||||
|
->where('source_entity_id', $bylaw->id)
|
||||||
|
->get()
|
||||||
|
->map(function ($link) {
|
||||||
|
return [
|
||||||
|
'id' => $link->id,
|
||||||
|
'target_entity_type' => $link->target_entity_type,
|
||||||
|
'target_entity_id' => $link->target_entity_id,
|
||||||
|
'link_type' => $link->link_type,
|
||||||
|
'effective_date' => $link->effective_date,
|
||||||
|
'justification' => $link->justification,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
$formatted['links'] = $links->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->success($formatted, 'Success', 'Bylaw Document Details Fetched');
|
||||||
|
}
|
||||||
|
|
||||||
public function search(Request $request)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
$request->validate([
|
$request->validate([
|
||||||
@@ -111,7 +171,12 @@ class BylawApiController extends Controller
|
|||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('description', 'LIKE', "%{$searchTerm}%")
|
->orWhere('description', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
|
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('official_gazette_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}%");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +220,12 @@ class BylawApiController extends Controller
|
|||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('description', 'LIKE', "%{$searchTerm}%")
|
->orWhere('description', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
|
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('official_gazette_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}%");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,13 +308,31 @@ class BylawApiController extends Controller
|
|||||||
{
|
{
|
||||||
$isLocked = $this->isLockedForCurrentUser($item);
|
$isLocked = $this->isLockedForCurrentUser($item);
|
||||||
|
|
||||||
|
$description = $item->description;
|
||||||
|
|
||||||
|
// Check if description is practically empty (null, empty string, or just empty WYSIWYG HTML like <p></p>)
|
||||||
|
$cleanDesc = trim(strip_tags($description ?? ''));
|
||||||
|
|
||||||
|
// Direct Query Fallback: Fetch node_content manually via DB facade to bypass any Model restrictions
|
||||||
|
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 [
|
return [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
'title' => $item->title,
|
'title' => $item->title,
|
||||||
'category_id' => $item->category_id,
|
'category_id' => $item->category_id,
|
||||||
'category_name' => optional($item->category)->name,
|
'category_name' => optional($item->category)->name,
|
||||||
|
'approval_authority_id' => $item->approval_authority_id,
|
||||||
'approval_authority_name' => optional($item->approvalAuthority)->name,
|
'approval_authority_name' => optional($item->approvalAuthority)->name,
|
||||||
'approval_date' => $item->approval_date,
|
'approval_date' => $item->approval_date,
|
||||||
|
'promulgation_authority_id' => $item->promulgation_authority_id,
|
||||||
'promulgation_authority_name'=> optional($item->promulgationAuthority)->name,
|
'promulgation_authority_name'=> optional($item->promulgationAuthority)->name,
|
||||||
'promulgation_date' => $item->promulgation_date,
|
'promulgation_date' => $item->promulgation_date,
|
||||||
'promulgation_number' => $item->promulgation_number,
|
'promulgation_number' => $item->promulgation_number,
|
||||||
@@ -254,7 +342,7 @@ class BylawApiController extends Controller
|
|||||||
'bylaw_type' => $item->bylaw_type,
|
'bylaw_type' => $item->bylaw_type,
|
||||||
'global_status' => $item->global_status,
|
'global_status' => $item->global_status,
|
||||||
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
|
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
|
||||||
'description' => $isLocked ? null : $item->description,
|
'description' => $isLocked ? null : $description,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,13 +19,14 @@ use App\Models\Volum;
|
|||||||
use App\Traits\BaseApiResponse;
|
use App\Traits\BaseApiResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Pagination\LengthAwarePaginator;
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class GlobalSearchApiController extends Controller
|
class GlobalSearchApiController extends Controller
|
||||||
{
|
{
|
||||||
use BaseApiResponse;
|
use BaseApiResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Massive Search across ALL data types (With dynamic Date/Number Sorting)
|
* Massive Search across ALL data types
|
||||||
*/
|
*/
|
||||||
public function search(Request $request)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
@@ -41,65 +42,30 @@ class GlobalSearchApiController extends Controller
|
|||||||
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
|
||||||
$page = (int) $request->input('page', 1);
|
$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)) {
|
if (!empty($searchTerm)) {
|
||||||
$limitPerTable = 100; // Performance safeguard
|
$limitPerTable = 100;
|
||||||
|
|
||||||
// 1. Search Bylaw Documents
|
// =========================================================
|
||||||
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
// 1. Search Arts (Exact match to ArtController)
|
||||||
->where('is_published', true)
|
// =========================================================
|
||||||
|
$arts = Art::with(['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('description', 'LIKE', "%{$searchTerm}%")
|
->orWhere('text', 'LIKE', "%{$searchTerm}%");
|
||||||
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
|
})
|
||||||
->orWhere('official_gazette_number', '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
|
|
||||||
$arts = Art::with(['law.category'])
|
|
||||||
->where('title', 'LIKE', "%{$searchTerm}%")
|
|
||||||
->orWhere('text', '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->merge($arts);
|
$staticResults = $staticResults->concat($arts);
|
||||||
|
|
||||||
// 5. Search Laws and Structural Models
|
// =========================================================
|
||||||
|
// 2. Search Laws and Structural Models (Exact match to ArtController)
|
||||||
|
// =========================================================
|
||||||
$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,
|
||||||
@@ -119,42 +85,95 @@ class GlobalSearchApiController extends Controller
|
|||||||
return $formatted;
|
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 ---
|
// --- Apply Dynamic Memory Sorting ONLY to Legal Docs ---
|
||||||
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');
|
||||||
|
|
||||||
if ($sortBy === 'number') {
|
if ($sortBy === 'number') {
|
||||||
$sortCallback = function ($item) {
|
$sortCallback = function ($item) {
|
||||||
// Extract whatever number property this specific item uses
|
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? '0');
|
||||||
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? $item['number'] ?? '');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Use Natural Sorting so "2" comes before "10"
|
|
||||||
if ($sortOrder === 'asc') {
|
if ($sortOrder === 'asc') {
|
||||||
$results = $results->sortBy($sortCallback, SORT_NATURAL)->values();
|
$sortableResults = $sortableResults->sortBy($sortCallback, SORT_NATURAL)->values();
|
||||||
} else {
|
} else {
|
||||||
$results = $results->sortByDesc($sortCallback, SORT_NATURAL)->values();
|
$sortableResults = $sortableResults->sortByDesc($sortCallback, SORT_NATURAL)->values();
|
||||||
}
|
}
|
||||||
} elseif ($sortBy === 'date') {
|
} elseif ($sortBy === 'date') {
|
||||||
$sortCallback = function ($item) {
|
$sortCallback = function ($item) {
|
||||||
// Extract whatever date property this specific item uses (Note: Structural models will default to '' and group together)
|
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '1970-01-01');
|
||||||
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if ($sortOrder === 'asc') {
|
if ($sortOrder === 'asc') {
|
||||||
$results = $results->sortBy($sortCallback)->values();
|
$sortableResults = $sortableResults->sortBy($sortCallback)->values();
|
||||||
} else {
|
} 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();
|
$total = $results->count();
|
||||||
$paginatedItems = $results->forPage($page, $perPage)->values();
|
$paginatedItems = $results->forPage($page, $perPage)->values();
|
||||||
|
|
||||||
@@ -174,7 +193,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
'total' => $paginator->total(),
|
'total' => $paginator->total(),
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this->success($responseData, 'Success', 'Global Search Results Fetched');
|
return $this->success($responseData, '', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,72 +205,25 @@ class GlobalSearchApiController extends Controller
|
|||||||
$results = collect();
|
$results = collect();
|
||||||
$totalCount = 0;
|
$totalCount = 0;
|
||||||
|
|
||||||
// Take a max of 3 from each table so the quick-search array doesn't become too heavy
|
|
||||||
$limitPerTable = 3;
|
$limitPerTable = 3;
|
||||||
|
|
||||||
if (!empty($searchTerm)) {
|
if (!empty($searchTerm)) {
|
||||||
|
|
||||||
// 1. Bylaw Documents
|
// 1. Search Arts FIRST
|
||||||
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
$artQuery = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
|
||||||
->where('is_published', true)
|
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
->orWhere('description', 'LIKE', "%{$searchTerm}%")
|
->orWhere('text', 'LIKE', "%{$searchTerm}%");
|
||||||
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
|
|
||||||
->orWhere('official_gazette_number', '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'])
|
|
||||||
->where('title', 'LIKE', "%{$searchTerm}%")
|
|
||||||
->orWhere('text', '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) {
|
||||||
$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->merge($arts);
|
$results = $results->concat($arts);
|
||||||
|
|
||||||
// 5. Structural Models
|
// 2. 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,
|
||||||
@@ -273,15 +245,68 @@ class GlobalSearchApiController extends Controller
|
|||||||
$formatted['type'] = ($table === 'section') ? 'sections' : $table;
|
$formatted['type'] = ($table === 'section') ? 'sections' : $table;
|
||||||
return $formatted;
|
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([
|
return $this->success([
|
||||||
'items' => $results->take(15)->values(),
|
'items' => $results->take(15)->values(),
|
||||||
'count' => $totalCount
|
'count' => $totalCount
|
||||||
], 'Success', 'Global Fast Search Results');
|
], '', '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------
|
/* -----------------------------------------------------------------
|
||||||
@@ -296,6 +321,19 @@ class GlobalSearchApiController extends Controller
|
|||||||
private function formatBylaw($item)
|
private function formatBylaw($item)
|
||||||
{
|
{
|
||||||
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
||||||
|
$description = $item->description;
|
||||||
|
|
||||||
|
$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 [
|
return [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
@@ -304,7 +342,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
'approval_authority_name' => optional($item->approvalAuthority)->name,
|
'approval_authority_name' => optional($item->approvalAuthority)->name,
|
||||||
'approval_date' => $item->approval_date,
|
'approval_date' => $item->approval_date,
|
||||||
'promulgation_number' => $item->promulgation_number,
|
'promulgation_number' => $item->promulgation_number,
|
||||||
'description' => $isLocked ? null : $item->description,
|
'description' => $isLocked ? null : $description,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -341,47 +379,52 @@ 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)
|
private function formatArt($item, $searchTerm, $modelClass)
|
||||||
{
|
{
|
||||||
$law = $item->law ?? Law::find($item->law_id);
|
$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 ?? '';
|
$text = $item->text ?? '';
|
||||||
$searchQuoted = preg_quote($searchTerm, '/');
|
$searchQuoted = preg_quote($searchTerm, '/');
|
||||||
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
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] ?? '');
|
$context = trim($matches[1] ?? '').' '.($matches[2] ?? '').' '.trim($matches[3] ?? '');
|
||||||
} else {
|
} else {
|
||||||
$context = $text;
|
$context = $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
'title' => $item->title,
|
'title' => $item->title,
|
||||||
'number' => $item->number, // Added for sorting capability
|
'number' => $item->number,
|
||||||
'text' => $isLocked ? null : $context,
|
'text' => $isLocked ? null : $context,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
'route' => array_values($this->route($modelClass, $item)),
|
'route' => array_values($this->route($modelClass, $item)),
|
||||||
'category' => optional($law->category)->name,
|
'category' => optional($law?->category)->name,
|
||||||
'law' => $law?->title,
|
'law' => $law?->title ?? '', // STRICLY STRING! Prevents Android App Crash!
|
||||||
'count_art' => $law?->arts?->count() ?? 0,
|
'count_art' => $law?->arts?->count() ?? 0,
|
||||||
'count_volums'=> $law?->volums?->count() ?? 0,
|
'count_volums' => $law?->volums?->count() ?? 0, // WITH S
|
||||||
'price' => $law?->price,
|
'count_volum' => $law?->volums?->count() ?? 0, // WITHOUT S (Fallback)
|
||||||
'image' => $law?->image,
|
'price' => $law?->price,
|
||||||
|
'image' => $law?->image,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatStructure($item, $modelClass)
|
private function formatStructure($item, $modelClass)
|
||||||
{
|
{
|
||||||
$law = Law::find($item?->law_id ?? $item->id);
|
$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 = [
|
$payload = [
|
||||||
'id' => $item->id,
|
'id' => $item->id,
|
||||||
'title' => $item->title,
|
'title' => $item->title,
|
||||||
'number' => $item->number, // Available for sorting capability
|
'number' => $item->number,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
'route' => array_values($this->route($modelClass, $item)),
|
'route' => array_values($this->route($modelClass, $item)),
|
||||||
'law' => $law?->title ?? '',
|
'law' => $law?->title ?? '', // STRICTLY STRING
|
||||||
];
|
];
|
||||||
|
|
||||||
if ((new $modelClass)->getTable() == 'laws') {
|
if ((new $modelClass)->getTable() == 'laws') {
|
||||||
@@ -396,14 +439,14 @@ class GlobalSearchApiController extends Controller
|
|||||||
{
|
{
|
||||||
if ((new $modelClass)->getTable() == 'art') {
|
if ((new $modelClass)->getTable() == 'art') {
|
||||||
$route = array_filter([
|
$route = array_filter([
|
||||||
Law::find($query->law_id)?->title,
|
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
|
||||||
Volum::find($query->volum_id)?->title,
|
$query->relationLoaded('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title,
|
||||||
Book::find($query->book_id)?->title,
|
$query->relationLoaded('book') ? $query->book?->title : Book::find($query->book_id)?->title,
|
||||||
Division::find($query->division_id)?->title,
|
Division::find($query->division_id)?->title,
|
||||||
Section::find($query->section_id)?->title,
|
$query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_id)?->title,
|
||||||
Chapter::find($query->chapter_id)?->title,
|
$query->relationLoaded('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title,
|
||||||
Part::find($query->part_id)?->title,
|
$query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_id)?->title,
|
||||||
Gate::find($query->gate_id)?->title,
|
$query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title,
|
||||||
Branch::find($query->branch_id)?->title,
|
Branch::find($query->branch_id)?->title,
|
||||||
]);
|
]);
|
||||||
return array_values($route);
|
return array_values($route);
|
||||||
|
|||||||
Reference in New Issue
Block a user