new-api #8
@@ -10,6 +10,7 @@ use App\Models\BylawNode;
|
|||||||
use App\Models\BylawCodificationLink;
|
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
|
||||||
{
|
{
|
||||||
@@ -309,13 +310,18 @@ class BylawApiController extends Controller
|
|||||||
|
|
||||||
$description = $item->description;
|
$description = $item->description;
|
||||||
|
|
||||||
// Direct Query Fallback: If description is empty, fetch node_content manually
|
// Check if description is practically empty (null, empty string, or just empty WYSIWYG HTML like <p></p>)
|
||||||
if (empty($description)) {
|
$cleanDesc = trim(strip_tags($description ?? ''));
|
||||||
$description = BylawNode::where('document_id', $item->id)
|
|
||||||
|
// 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')
|
->orderBy('sort_order', 'asc')
|
||||||
->pluck('node_content')
|
->pluck('node_content')
|
||||||
->filter()
|
->toArray();
|
||||||
->implode("\n\n");
|
|
||||||
|
$description = implode("\n\n", array_filter($nodeContents));
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ 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
|
||||||
{
|
{
|
||||||
@@ -48,7 +49,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
$limitPerTable = 100; // Performance safeguard
|
$limitPerTable = 100; // Performance safeguard
|
||||||
|
|
||||||
// 1. Search Bylaw Documents
|
// 1. Search Bylaw Documents
|
||||||
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
|
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||||
->where('is_published', true)
|
->where('is_published', true)
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
@@ -94,17 +95,11 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->merge($opinions);
|
$results = $results->merge($opinions);
|
||||||
|
|
||||||
// 4. Search Arts (Removed missing 'division' and 'branch' from eager load)
|
// 4. Search Arts (Optimized to mirror ArtController speed)
|
||||||
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', '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}%");
|
||||||
->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}%"); });
|
|
||||||
})
|
})
|
||||||
->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);
|
||||||
@@ -202,7 +197,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
if (!empty($searchTerm)) {
|
if (!empty($searchTerm)) {
|
||||||
|
|
||||||
// 1. Bylaw Documents
|
// 1. Bylaw Documents
|
||||||
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes'])
|
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
|
||||||
->where('is_published', true)
|
->where('is_published', true)
|
||||||
->where(function ($q) use ($searchTerm) {
|
->where(function ($q) use ($searchTerm) {
|
||||||
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
$q->where('title', 'LIKE', "%{$searchTerm}%")
|
||||||
@@ -258,13 +253,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
$artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', '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}%");
|
||||||
->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}%"); });
|
|
||||||
});
|
});
|
||||||
$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) {
|
||||||
@@ -321,8 +310,19 @@ class GlobalSearchApiController extends Controller
|
|||||||
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
$isLocked = $this->isLockedForCurrentUserGeneric($item);
|
||||||
|
|
||||||
$description = $item->description;
|
$description = $item->description;
|
||||||
if (empty($description) && $item->relationLoaded('nodes')) {
|
|
||||||
$description = $item->nodes->pluck('node_content')->filter()->implode("\n\n");
|
// 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 [
|
||||||
@@ -396,7 +396,7 @@ class GlobalSearchApiController extends Controller
|
|||||||
'price' => $law?->price,
|
'price' => $law?->price,
|
||||||
'image' => $law?->image,
|
'image' => $law?->image,
|
||||||
|
|
||||||
// Relational Objects
|
// Relational Objects matching ArtController exactly
|
||||||
'law' => $item->law != null ? [
|
'law' => $item->law != null ? [
|
||||||
'id' => $item->law->id,
|
'id' => $item->law->id,
|
||||||
'title' => $item->law->title,
|
'title' => $item->law->title,
|
||||||
@@ -464,7 +464,6 @@ class GlobalSearchApiController extends Controller
|
|||||||
private function route($modelClass, $query)
|
private function route($modelClass, $query)
|
||||||
{
|
{
|
||||||
// Safe Breadcrumb Building - Uses relation if loaded, else manual lookup.
|
// Safe Breadcrumb Building - Uses relation if loaded, else manual lookup.
|
||||||
// Explicitly forces manual lookup for division and branch to avoid crash.
|
|
||||||
if ((new $modelClass)->getTable() == 'art') {
|
if ((new $modelClass)->getTable() == 'art') {
|
||||||
$route = array_filter([
|
$route = array_filter([
|
||||||
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
|
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
|
||||||
|
|||||||
Reference in New Issue
Block a user