From 2fab528d0ac3bdf2ecac749d620d7bcbaee2edf7 Mon Sep 17 00:00:00 2001 From: "m.razaghi" Date: Sun, 12 Jul 2026 17:38:35 +0330 Subject: [PATCH] Fixed api issue --- .../Controllers/api/BylawApiController.php | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/api/BylawApiController.php b/app/Http/Controllers/api/BylawApiController.php index 66791f5..55082d1 100644 --- a/app/Http/Controllers/api/BylawApiController.php +++ b/app/Http/Controllers/api/BylawApiController.php @@ -79,7 +79,7 @@ class BylawApiController extends Controller $perPage = min(max((int) $request->input('per_page', 15), 10), 100); $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'); $allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id'); @@ -111,7 +111,6 @@ class BylawApiController extends Controller ], 404); } - // Fetch nodes safely $nodes = BylawNode::with('type') ->where('document_id', $bylaw->id) ->orderBy('sort_order', 'asc') @@ -323,32 +322,39 @@ class BylawApiController extends Controller return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false); } - /** - * 100% Bulletproof formatting with aggressive HTML empty-checking - */ private function formatBylaw($item, $preloadedNodes = null) { $isLocked = $this->isLockedForCurrentUser($item); - $description = $item->description; // AGGRESSIVE HTML CLEANER: Destroys hidden HTML tags,  , and whitespaces $cleanDesc = trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($description ?? '')))); // 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 ? $preloadedNodes : BylawNode::where('document_id', $item->id)->orderBy('sort_order', 'asc')->get(); + $nodes = $preloadedNodes !== null + ? $preloadedNodes + : BylawNode::where('document_id', $item->id)->orderBy('sort_order', 'asc')->get(); - if ($nodes && $nodes->count() > 0) { - // Extracts node_content, drops any that are pure empty space, and joins them - $description = $nodes->pluck('node_content')->filter(function ($content) { - return trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($content ?? '')))) !== ''; - })->implode("\n\n"); - } else { - $description = null; + $descParts = []; + foreach ($nodes as $node) { + // Grab all possible text fields + $identifier = trim(strip_tags($node->node_identifier ?? '')); + $title = trim(strip_tags($node->title ?? '')); + $content = trim(strip_tags($node->node_content ?? '')); + + // 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 [