Fixed api issue
This commit is contained in:
@@ -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,34 +322,41 @@ 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 [
|
||||
'id' => $item->id,
|
||||
'title' => $item->title,
|
||||
|
||||
Reference in New Issue
Block a user