Fixed api issue
This commit is contained in:
@@ -80,7 +80,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);
|
||||||
|
|
||||||
// BULLETPROOF NODE FETCHING
|
// واکشی بهینه گرهها از دیتابیس
|
||||||
$documentIds = collect($paginator->items())->pluck('id')->toArray();
|
$documentIds = collect($paginator->items())->pluck('id')->toArray();
|
||||||
$allNodes = collect();
|
$allNodes = collect();
|
||||||
if (!empty($documentIds)) {
|
if (!empty($documentIds)) {
|
||||||
@@ -91,8 +91,10 @@ class BylawApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
||||||
// Safely filter nodes belonging ONLY to this specific document
|
// حل مشکل فیلتر سختگیرانه (Strict) در کالکشنها
|
||||||
$itemNodes = $allNodes->where('document_id', $item->id);
|
$itemNodes = $allNodes->filter(function($node) use ($item) {
|
||||||
|
return $node->document_id == $item->id;
|
||||||
|
});
|
||||||
return $this->formatBylaw($item, $itemNodes);
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -120,7 +122,7 @@ class BylawApiController extends Controller
|
|||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch nodes using raw DB
|
// واکشی خام گرهها برای توضیحات کلی
|
||||||
$rawNodes = DB::table('bylaw_nodes')
|
$rawNodes = DB::table('bylaw_nodes')
|
||||||
->where('document_id', $bylaw->id)
|
->where('document_id', $bylaw->id)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
@@ -211,7 +213,6 @@ 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);
|
||||||
|
|
||||||
// BULLETPROOF NODE FETCHING
|
|
||||||
$documentIds = collect($paginator->items())->pluck('id')->toArray();
|
$documentIds = collect($paginator->items())->pluck('id')->toArray();
|
||||||
$allNodes = collect();
|
$allNodes = collect();
|
||||||
if (!empty($documentIds)) {
|
if (!empty($documentIds)) {
|
||||||
@@ -222,7 +223,9 @@ class BylawApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
||||||
$itemNodes = $allNodes->where('document_id', $item->id);
|
$itemNodes = $allNodes->filter(function($node) use ($item) {
|
||||||
|
return $node->document_id == $item->id;
|
||||||
|
});
|
||||||
return $this->formatBylaw($item, $itemNodes);
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -262,7 +265,6 @@ class BylawApiController extends Controller
|
|||||||
$count = $query->count();
|
$count = $query->count();
|
||||||
$items = $query->orderBy('approval_date', 'desc')->take(5)->get();
|
$items = $query->orderBy('approval_date', 'desc')->take(5)->get();
|
||||||
|
|
||||||
// BULLETPROOF NODE FETCHING
|
|
||||||
$documentIds = $items->pluck('id')->toArray();
|
$documentIds = $items->pluck('id')->toArray();
|
||||||
$allNodes = collect();
|
$allNodes = collect();
|
||||||
if (!empty($documentIds)) {
|
if (!empty($documentIds)) {
|
||||||
@@ -273,7 +275,9 @@ class BylawApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$formattedItems = $items->map(function ($item) use ($allNodes) {
|
$formattedItems = $items->map(function ($item) use ($allNodes) {
|
||||||
$itemNodes = $allNodes->where('document_id', $item->id);
|
$itemNodes = $allNodes->filter(function($node) use ($item) {
|
||||||
|
return $node->document_id == $item->id;
|
||||||
|
});
|
||||||
return $this->formatBylaw($item, $itemNodes);
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -345,7 +349,9 @@ class BylawApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$results = array_values(array_map(function($item) use ($allNodes) {
|
$results = array_values(array_map(function($item) use ($allNodes) {
|
||||||
$itemNodes = $allNodes->where('document_id', $item->id);
|
$itemNodes = $allNodes->filter(function($node) use ($item) {
|
||||||
|
return $node->document_id == $item->id;
|
||||||
|
});
|
||||||
return $this->formatBylaw($item, $itemNodes);
|
return $this->formatBylaw($item, $itemNodes);
|
||||||
}, $itemsToFormat));
|
}, $itemsToFormat));
|
||||||
|
|
||||||
@@ -365,13 +371,13 @@ class BylawApiController extends Controller
|
|||||||
$isLocked = $this->isLockedForCurrentUser($item);
|
$isLocked = $this->isLockedForCurrentUser($item);
|
||||||
$description = $item->description;
|
$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 ?? '')));
|
$cleanDesc = trim(str_replace([' ', '‌', "\r", "\n", "\t", ' '], '', strip_tags($description ?? '')));
|
||||||
|
|
||||||
|
// اگر فیلد اصلی description خالی بود (که در خزش فعلی خالی است)، به سراغ متن اصلی میرویم
|
||||||
if (strlen($cleanDesc) < 5) {
|
if (strlen($cleanDesc) < 5) {
|
||||||
|
|
||||||
// Absolute Safety Net: If somehow $itemNodes is still null, FORCE a direct query for this specific ID.
|
// شبکه اطمینان: اگر متغیر حاوی گرهها به هر دلیلی خالی بود، مستقیماً از دیتابیس میگیریم
|
||||||
if ($itemNodes === null) {
|
if ($itemNodes === null || (is_countable($itemNodes) && count($itemNodes) === 0)) {
|
||||||
$itemNodes = DB::table('bylaw_nodes')
|
$itemNodes = DB::table('bylaw_nodes')
|
||||||
->where('document_id', $item->id)
|
->where('document_id', $item->id)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
@@ -380,8 +386,10 @@ class BylawApiController extends Controller
|
|||||||
|
|
||||||
$nodeContents = [];
|
$nodeContents = [];
|
||||||
foreach ($itemNodes as $node) {
|
foreach ($itemNodes as $node) {
|
||||||
// EXPLICITLY GET node_content
|
// استخراج امن متن از آبجکت یا آرایه (برای جلوگیری از خطاهای احتمالی)
|
||||||
$content = trim(strip_tags($node->node_content ?? ''));
|
$contentRaw = is_array($node) ? ($node['node_content'] ?? '') : ($node->node_content ?? '');
|
||||||
|
|
||||||
|
$content = trim(strip_tags($contentRaw));
|
||||||
if (!empty($content)) {
|
if (!empty($content)) {
|
||||||
$nodeContents[] = $content;
|
$nodeContents[] = $content;
|
||||||
}
|
}
|
||||||
@@ -408,7 +416,7 @@ class BylawApiController extends Controller
|
|||||||
'bylaw_type' => $item->bylaw_type,
|
'bylaw_type' => $item->bylaw_type,
|
||||||
'global_status' => $item->global_status,
|
'global_status' => $item->global_status,
|
||||||
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
|
'pdf_file_url' => $isLocked ? null : ($item->pdf_file_path ? asset('storage/' . $item->pdf_file_path) : null),
|
||||||
'description' => $isLocked ? null : $description,
|
'description' => $isLocked ? null : $description, // متن کامل از node_content اینجا قرار میگیرد
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user