Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 17:38:35 +03:30
parent 677e101181
commit 2fab528d0a

View File

@@ -79,7 +79,7 @@ class BylawApiController extends Controller
$perPage = min(max((int) $request->input('per_page', 15), 10), 100); $perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$paginator = $query->paginate($perPage); $paginator = $query->paginate($perPage);
// Pre-fetch ALL nodes for the current page in ONE query (Prevents Database crashing/emptying) // Pre-fetch ALL nodes for the current page in ONE query
$documentIds = collect($paginator->items())->pluck('id'); $documentIds = collect($paginator->items())->pluck('id');
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id'); $allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
@@ -111,7 +111,6 @@ class BylawApiController extends Controller
], 404); ], 404);
} }
// Fetch nodes safely
$nodes = BylawNode::with('type') $nodes = BylawNode::with('type')
->where('document_id', $bylaw->id) ->where('document_id', $bylaw->id)
->orderBy('sort_order', 'asc') ->orderBy('sort_order', 'asc')
@@ -323,34 +322,41 @@ class BylawApiController extends Controller
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false); return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
} }
/**
* 100% Bulletproof formatting with aggressive HTML empty-checking
*/
private function formatBylaw($item, $preloadedNodes = null) private function formatBylaw($item, $preloadedNodes = null)
{ {
$isLocked = $this->isLockedForCurrentUser($item); $isLocked = $this->isLockedForCurrentUser($item);
$description = $item->description; $description = $item->description;
// AGGRESSIVE HTML CLEANER: Destroys hidden HTML tags,  , and whitespaces // AGGRESSIVE HTML CLEANER: Destroys hidden HTML tags,  , and whitespaces
$cleanDesc = trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($description ?? '')))); $cleanDesc = trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($description ?? ''))));
// If the description is genuinely empty, pull from the nodes! // If the description is genuinely empty, pull from the nodes!
if ($cleanDesc === '') { if ($cleanDesc === '' || strtolower($cleanDesc) === 'null') {
// Uses nodes passed from the parent query, OR natively queries them if missing $nodes = $preloadedNodes !== null
$nodes = $preloadedNodes !== null ? $preloadedNodes : BylawNode::where('document_id', $item->id)->orderBy('sort_order', 'asc')->get(); ? $preloadedNodes
: BylawNode::where('document_id', $item->id)->orderBy('sort_order', 'asc')->get();
if ($nodes && $nodes->count() > 0) { $descParts = [];
// Extracts node_content, drops any that are pure empty space, and joins them foreach ($nodes as $node) {
$description = $nodes->pluck('node_content')->filter(function ($content) { // Grab all possible text fields
return trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($content ?? '')))) !== ''; $identifier = trim(strip_tags($node->node_identifier ?? ''));
})->implode("\n\n"); $title = trim(strip_tags($node->title ?? ''));
} else { $content = trim(strip_tags($node->node_content ?? ''));
$description = null;
// Combine them neatly (e.g. "Article 1 - Definitions - This is the text...")
$combined = array_filter([$identifier, $title, $content], function($val) {
return $val !== '';
});
if (!empty($combined)) {
$descParts[] = implode(" : ", $combined);
} }
} }
$description = implode("\n\n", $descParts);
}
return [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->title, 'title' => $item->title,