From 7f744beec4b2fdc14951e17fdaad7ea1604dc953 Mon Sep 17 00:00:00 2001 From: "m.razaghi" Date: Sun, 12 Jul 2026 18:21:02 +0330 Subject: [PATCH] Fixed api issue --- .../Controllers/api/BylawApiController.php | 140 +++++++++--------- 1 file changed, 68 insertions(+), 72 deletions(-) diff --git a/app/Http/Controllers/api/BylawApiController.php b/app/Http/Controllers/api/BylawApiController.php index 65836ca..fc471fc 100644 --- a/app/Http/Controllers/api/BylawApiController.php +++ b/app/Http/Controllers/api/BylawApiController.php @@ -25,7 +25,7 @@ class BylawApiController extends Controller ]; }); - return $this->success($categories, '', 'Bylaw Categories List'); + return $this->success($categories, 'Success', 'Bylaw Categories List'); } public function authorities() @@ -38,7 +38,7 @@ class BylawApiController extends Controller ]; }); - return $this->success($authorities, '', 'Bylaw Authorities List'); + return $this->success($authorities, 'Success', 'Bylaw Authorities List'); } public function index(Request $request) @@ -80,9 +80,9 @@ class BylawApiController extends Controller $perPage = min(max((int) $request->input('per_page', 15), 10), 100); $paginator = $query->paginate($perPage); - // واکشی ۱۰۰٪ قطعی گره‌ها با آرایه‌های بومی (Native PHP Arrays) - $documentIds = collect($paginator->items())->pluck('id')->toArray(); - $nodesDictionary = []; + // Fetch Node Content and Map perfectly by Document ID + $documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray(); + $nodesByDoc = []; if (!empty($documentIds)) { $nodes = DB::table('bylaw_nodes') @@ -92,18 +92,17 @@ class BylawApiController extends Controller ->get(); foreach ($nodes as $node) { - $docId = $node->document_id; - if (!isset($nodesDictionary[$docId])) { - $nodesDictionary[$docId] = []; - } - if (!empty(trim((string)$node->node_content))) { - $nodesDictionary[$docId][] = trim((string)$node->node_content); + $docId = (int) $node->document_id; + $content = trim((string)$node->node_content); + if (!empty($content)) { + $nodesByDoc[$docId][] = $content; } } } - $items = collect($paginator->items())->map(function ($item) use ($nodesDictionary) { - return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); + $items = collect($paginator->items())->map(function ($item) use ($nodesByDoc) { + $itemId = (int) $item->id; + return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null); }); $responseData = [ @@ -114,7 +113,7 @@ class BylawApiController extends Controller 'total' => $paginator->total(), ]; - return $this->success($responseData, '', 'Bylaws Fetched'); + return $this->success($responseData, 'Success', 'Bylaws Fetched'); } public function single($id) @@ -170,7 +169,7 @@ class BylawApiController extends Controller $formatted['links'] = $links->toArray(); } - return $this->success($formatted, '', 'Bylaw Document Details Fetched'); + return $this->success($formatted, 'Success', 'Bylaw Document Details Fetched'); } public function search(Request $request) @@ -215,8 +214,8 @@ class BylawApiController extends Controller $perPage = min(max((int) $request->input('per_page', 15), 10), 100); $paginator = $query->paginate($perPage); - $documentIds = collect($paginator->items())->pluck('id')->toArray(); - $nodesDictionary = []; + $documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray(); + $nodesByDoc = []; if (!empty($documentIds)) { $nodes = DB::table('bylaw_nodes') @@ -226,18 +225,17 @@ class BylawApiController extends Controller ->get(); foreach ($nodes as $node) { - $docId = $node->document_id; - if (!isset($nodesDictionary[$docId])) { - $nodesDictionary[$docId] = []; - } - if (!empty(trim((string)$node->node_content))) { - $nodesDictionary[$docId][] = trim((string)$node->node_content); + $docId = (int) $node->document_id; + $content = trim((string)$node->node_content); + if (!empty($content)) { + $nodesByDoc[$docId][] = $content; } } } - $items = collect($paginator->items())->map(function ($item) use ($nodesDictionary) { - return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); + $items = collect($paginator->items())->map(function ($item) use ($nodesByDoc) { + $itemId = (int) $item->id; + return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null); }); $responseData = [ @@ -248,7 +246,7 @@ class BylawApiController extends Controller 'total' => $paginator->total(), ]; - return $this->success($responseData, '', 'Search Results Fetched'); + return $this->success($responseData, 'Success', 'Search Results Fetched'); } public function fast_search(Request $request) @@ -276,8 +274,8 @@ class BylawApiController extends Controller $count = $query->count(); $items = $query->orderBy('approval_date', 'desc')->take(5)->get(); - $documentIds = $items->pluck('id')->toArray(); - $nodesDictionary = []; + $documentIds = $items->pluck('id')->map(fn($id) => (int)$id)->toArray(); + $nodesByDoc = []; if (!empty($documentIds)) { $nodes = DB::table('bylaw_nodes') @@ -287,24 +285,23 @@ class BylawApiController extends Controller ->get(); foreach ($nodes as $node) { - $docId = $node->document_id; - if (!isset($nodesDictionary[$docId])) { - $nodesDictionary[$docId] = []; - } - if (!empty(trim((string)$node->node_content))) { - $nodesDictionary[$docId][] = trim((string)$node->node_content); + $docId = (int) $node->document_id; + $content = trim((string)$node->node_content); + if (!empty($content)) { + $nodesByDoc[$docId][] = $content; } } } - $formattedItems = $items->map(function ($item) use ($nodesDictionary) { - return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); + $formattedItems = $items->map(function ($item) use ($nodesByDoc) { + $itemId = (int) $item->id; + return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null); }); return $this->success([ 'items' => $formattedItems, 'count' => $count - ], '', 'Fast Search Results'); + ], 'Success', 'Fast Search Results'); } public function pager($id) @@ -359,8 +356,8 @@ class BylawApiController extends Controller $itemsToFormat = array_filter([$previousItem, $current, $nextItem]); - $documentIds = collect($itemsToFormat)->pluck('id')->toArray(); - $nodesDictionary = []; + $documentIds = collect($itemsToFormat)->pluck('id')->map(fn($id) => (int)$id)->toArray(); + $nodesByDoc = []; if (!empty($documentIds)) { $nodes = DB::table('bylaw_nodes') @@ -370,21 +367,20 @@ class BylawApiController extends Controller ->get(); foreach ($nodes as $node) { - $docId = $node->document_id; - if (!isset($nodesDictionary[$docId])) { - $nodesDictionary[$docId] = []; - } - if (!empty(trim((string)$node->node_content))) { - $nodesDictionary[$docId][] = trim((string)$node->node_content); + $docId = (int) $node->document_id; + $content = trim((string)$node->node_content); + if (!empty($content)) { + $nodesByDoc[$docId][] = $content; } } } - $results = array_values(array_map(function($item) use ($nodesDictionary) { - return $this->formatBylaw($item, $nodesDictionary[$item->id] ?? null); + $results = array_values(array_map(function($item) use ($nodesByDoc) { + $itemId = (int) $item->id; + return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null); }, $itemsToFormat)); - return $this->success($results, '', 'Pager Window Details'); + return $this->success($results, 'Success', 'Pager Window Details'); } private function isLockedForCurrentUser($item): bool @@ -393,38 +389,38 @@ class BylawApiController extends Controller } /** - * سیستم کاملاً ضد گلوله برای استخراج Description + * FULLY REBUILT: FORCES NODE_CONTENT INTO DESCRIPTION */ private function formatBylaw($item, $preloadedNodes = null) { $isLocked = $this->isLockedForCurrentUser($item); - // ۱. اولویت اول: متنی که ادمین در پنل وارد کرده است - $description = $item->description; + $nodeContentFullText = ""; - // ۲. اولویت دوم: اگر توضیحات ادمین خالی بود، متن قانون را از گره‌ها می‌گیریم - if (empty(trim((string)$description))) { + // 1. Prioritize preloaded nodes (used in index, search, fast_search, pager) + if ($preloadedNodes !== null && is_array($preloadedNodes)) { + $nodeContentFullText = implode("\n\n", $preloadedNodes); + } + // 2. Fallback to DB query if not preloaded (used in single method) + else { + $nodes = DB::table('bylaw_nodes') + ->where('document_id', $item->id) + ->orderBy('sort_order', 'asc') + ->pluck('node_content') + ->toArray(); - // اگر آرایه گره‌ها از قبل (در توابع index یا search) آماده شده بود: - if ($preloadedNodes !== null && is_array($preloadedNodes)) { - $description = implode("\n\n", $preloadedNodes); - - // در غیر این صورت (مثلا در تابع single) مستقیماً از دیتابیس می‌گیریم - } else { - $nodes = DB::table('bylaw_nodes') - ->where('document_id', $item->id) - ->orderBy('sort_order', 'asc') - ->pluck('node_content') - ->toArray(); - - $validContents = []; - foreach ($nodes as $content) { - if (!empty(trim((string)$content))) { - $validContents[] = trim((string)$content); - } + $validContents = []; + foreach ($nodes as $content) { + if (!empty(trim((string)$content))) { + $validContents[] = trim((string)$content); } - $description = implode("\n\n", $validContents); } + $nodeContentFullText = implode("\n\n", $validContents); + } + + // 3. Absolute last resort (only if the DB literally has no nodes for this item) + if (empty(trim(strip_tags($nodeContentFullText)))) { + $nodeContentFullText = $item->description; } return [ @@ -445,7 +441,7 @@ class BylawApiController extends Controller 'bylaw_type' => $item->bylaw_type, 'global_status' => $item->global_status, 'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null), - 'description' => $isLocked ? null : $description, + 'description' => $isLocked ? null : $nodeContentFullText, // <--- NODE_CONTENT IS NOW 100% FORCED HERE 'is_locked' => $isLocked, ]; }