Merge pull request 'Fixed api issue' (#10) from new-api into clean-main
Reviewed-on: #10
This commit was merged in pull request #10.
This commit is contained in:
@@ -26,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)
|
||||||
{
|
{
|
||||||
@@ -45,7 +45,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
$results = collect();
|
$results = collect();
|
||||||
|
|
||||||
if (!empty($searchTerm)) {
|
if (!empty($searchTerm)) {
|
||||||
$limitPerTable = 100; // Performance safeguard
|
$limitPerTable = 100;
|
||||||
|
|
||||||
// 1. Search Bylaw Documents
|
// 1. Search Bylaw Documents
|
||||||
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||||
@@ -94,11 +94,19 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->merge($opinions);
|
$results = $results->merge($opinions);
|
||||||
|
|
||||||
// 4. Search Arts (Exact match to ArtController logic + eager loading relations)
|
// 4. Search Arts (DEEP RELATIONAL SEARCH INCLUDED)
|
||||||
$arts = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate'])
|
$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('text', 'LIKE', "%{$searchTerm}%");
|
->orWhere('text', 'LIKE', "%{$searchTerm}%")
|
||||||
|
->orWhere('number', 'LIKE', "%{$searchTerm}%") // Added Number Search
|
||||||
|
->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);
|
||||||
@@ -248,11 +256,19 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->merge($opinions);
|
$results = $results->merge($opinions);
|
||||||
|
|
||||||
// 4. Search Arts
|
// 4. Search Arts (DEEP RELATIONAL SEARCH INCLUDED)
|
||||||
$artQuery = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate'])
|
$artQuery = 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('text', '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();
|
$totalCount += $artQuery->count();
|
||||||
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
|
||||||
@@ -307,13 +323,10 @@ 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;
|
||||||
|
|
||||||
// Check if description is practically empty (null, empty string, or just empty WYSIWYG HTML like <p></p>)
|
|
||||||
$cleanDesc = trim(strip_tags($description ?? ''));
|
$cleanDesc = trim(strip_tags($description ?? ''));
|
||||||
|
|
||||||
// Direct Query Fallback: Fetch node_content manually via DB facade to bypass any Model restrictions
|
|
||||||
if (empty($cleanDesc)) {
|
if (empty($cleanDesc)) {
|
||||||
$nodeContents = DB::table('bylaw_nodes')
|
$nodeContents = DB::table('bylaw_nodes')
|
||||||
->where('document_id', $item->id)
|
->where('document_id', $item->id)
|
||||||
@@ -374,14 +387,23 @@ class GlobalSearchApiController extends Controller
|
|||||||
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
||||||
|
|
||||||
$text = $art->text ?? '';
|
$text = $art->text ?? '';
|
||||||
$searchQuoted = preg_quote($searchTerm, '/');
|
|
||||||
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
// Multi-byte safe Context Snippet Extraction for Persian/Arabic
|
||||||
$context = trim($matches[1] ?? '') . ' ' . $matches[2] . ' ' . trim($matches[3] ?? '');
|
if (!empty($searchTerm) && !empty($text)) {
|
||||||
|
$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; // Fallback if match was in title/relations
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$context = $text;
|
$context = $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PERFECTLY MATCHED TO YOUR ARTCONTROLLER MAPPING
|
|
||||||
return [
|
return [
|
||||||
'id' => $art->id,
|
'id' => $art->id,
|
||||||
'title' => $art->title,
|
'title' => $art->title,
|
||||||
@@ -396,7 +418,12 @@ class GlobalSearchApiController extends Controller
|
|||||||
'price' => $law?->price,
|
'price' => $law?->price,
|
||||||
'image' => $law?->image,
|
'image' => $law?->image,
|
||||||
|
|
||||||
// Full nested relationship mappings
|
// Relational Objects
|
||||||
|
'law' => $art->law != null ? [
|
||||||
|
'id' => $art->law->id,
|
||||||
|
'title' => $art->law->title,
|
||||||
|
'number' => $art->law->number,
|
||||||
|
] : null,
|
||||||
'chapter' => $art->chapter != null ? [
|
'chapter' => $art->chapter != null ? [
|
||||||
'id' => $art->chapter->id,
|
'id' => $art->chapter->id,
|
||||||
'title' => $art->chapter->title,
|
'title' => $art->chapter->title,
|
||||||
@@ -412,11 +439,6 @@ class GlobalSearchApiController extends Controller
|
|||||||
'title' => $art->volum->title,
|
'title' => $art->volum->title,
|
||||||
'number' => $art->volum->number,
|
'number' => $art->volum->number,
|
||||||
] : null,
|
] : null,
|
||||||
'law' => $art->law != null ? [
|
|
||||||
'id' => $art->law->id,
|
|
||||||
'title' => $art->law->title,
|
|
||||||
'number' => $art->law->number,
|
|
||||||
] : null,
|
|
||||||
'book' => $art->book != null ? [
|
'book' => $art->book != null ? [
|
||||||
'id' => $art->book->id,
|
'id' => $art->book->id,
|
||||||
'title' => $art->book->title,
|
'title' => $art->book->title,
|
||||||
@@ -468,12 +490,12 @@ class GlobalSearchApiController extends Controller
|
|||||||
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_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('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title,
|
||||||
$query->relationLoaded('book') ? $query->book?->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 is not in eagerly loaded relations, query safely
|
Division::find($query->division_id)?->title,
|
||||||
$query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_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('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title,
|
||||||
$query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_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,
|
$query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title,
|
||||||
Branch::find($query->branch_id)?->title, // Branch is not in eagerly loaded relations, query safely
|
Branch::find($query->branch_id)?->title,
|
||||||
]);
|
]);
|
||||||
return array_values($route);
|
return array_values($route);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user