updated apis

This commit is contained in:
m.razaghi
2026-07-14 00:10:53 +03:30
parent 39e85df27a
commit a173f17cae
11 changed files with 304 additions and 102 deletions

View File

@@ -90,7 +90,7 @@ class GlobalSearchApiController extends Controller
}
// =========================================================
// 3. Search Bylaw Documents (With 100% Bulletproof Node Fetching)
// 3. Search Bylaw Documents
// =========================================================
$bylawsQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
@@ -103,7 +103,7 @@ class GlobalSearchApiController extends Controller
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%")
->orWhere('title', 'LIKE', "%{$searchTerm}%"); // Fixed: Title added!
->orWhere('title', 'LIKE', "%{$searchTerm}%");
});
})->take($limitPerTable)->get();
@@ -265,7 +265,7 @@ class GlobalSearchApiController extends Controller
}
}
// 3. Bylaw Documents (With 100% Bulletproof Node Fetching)
// 3. Bylaw Documents
$bylawsQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
@@ -284,7 +284,7 @@ class GlobalSearchApiController extends Controller
$totalCount += $bylawsQuery->count();
$bylawsRaw = $bylawsQuery->take($limitPerTable)->get();
// Safely bulk-fetch nodes using DB facade
// Bulk-fetch nodes safely using DB facade
$docIds = $bylawsRaw->pluck('id')->toArray();
$allNodes = collect();
if (!empty($docIds)) {
@@ -346,46 +346,35 @@ class GlobalSearchApiController extends Controller
private function isLockedForCurrentUserGeneric($item): bool
{
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
if (auth()->check() && auth()->user() && method_exists(auth()->user(), 'isSubscriber')) {
return !auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
}
return (bool) ($item->is_locked ?? false);
}
/**
* BULLETPROOF DESCRIPTION GENERATOR (Matches BylawApiController Exactly)
* Exact 1:1 match with BylawApiController's encoding approach
*/
private function formatBylaw($item, $itemNodes = null)
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
$description = $item->description;
// Check if description is practically empty (less than 5 chars = garbage HTML)
$cleanDesc = trim(str_replace([' ', '‌', "\r", "\n", "\t", ' '], '', strip_tags($description ?? '')));
if ($itemNodes === null) {
$itemNodes = DB::table('bylaw_nodes')->where('document_id', $item->id)->orderBy('sort_order', 'asc')->get();
}
if (strlen($cleanDesc) < 5) {
$nodeContentAsDescription = collect($itemNodes)->pluck('node_content')
->map(function ($text) {
$safeText = trim((string)$text);
return mb_convert_encoding($safeText, 'UTF-8', 'UTF-8');
})
->filter(function ($text) {
return $text !== '';
})
->implode("\n\n");
// Absolute Safety Net: If somehow $itemNodes is still null or empty array, FORCE a direct query.
if ($itemNodes === null || (is_object($itemNodes) && $itemNodes->isEmpty())) {
// Verify against DB just in case
$dbNodes = DB::table('bylaw_nodes')->where('document_id', $item->id)->count();
if ($dbNodes > 0) {
$itemNodes = DB::table('bylaw_nodes')
->where('document_id', $item->id)
->orderBy('sort_order', 'asc')
->get();
}
}
$nodeContents = [];
if ($itemNodes) {
foreach ($itemNodes as $node) {
// EXPLICITLY GET node_content
$content = trim(strip_tags($node->node_content ?? ''));
if (!empty($content)) {
$nodeContents[] = $content;
}
}
}
$description = implode("\n\n", $nodeContents);
if (empty($nodeContentAsDescription)) {
$nodeContentAsDescription = mb_convert_encoding(trim((string)$item->description), 'UTF-8', 'UTF-8');
}
return [
@@ -395,7 +384,7 @@ class GlobalSearchApiController extends Controller
'approval_authority_name' => optional($item->approvalAuthority)->name,
'approval_date' => $item->approval_date,
'promulgation_number' => $item->promulgation_number,
'description' => $isLocked ? null : $description,
'description' => $isLocked ? null : $nodeContentAsDescription,
'is_locked' => $isLocked,
];
}
@@ -432,9 +421,6 @@ class GlobalSearchApiController extends Controller
];
}
/**
* Exact 1:1 match with ArtController format mapping
*/
private function formatArt($item, $searchTerm, $modelClass)
{
$law = $item->law ?? Law::find($item->law_id);