Fixed api issue
This commit is contained in:
@@ -10,7 +10,6 @@ use App\Models\BylawNode;
|
|||||||
use App\Models\BylawCodificationLink;
|
use App\Models\BylawCodificationLink;
|
||||||
use App\Traits\BaseApiResponse;
|
use App\Traits\BaseApiResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
|
|
||||||
class BylawApiController extends Controller
|
class BylawApiController extends Controller
|
||||||
{
|
{
|
||||||
@@ -80,8 +79,12 @@ 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);
|
||||||
|
|
||||||
$items = collect($paginator->items())->map(function ($item) {
|
// Pre-fetch ALL nodes for the current page in ONE query (Prevents Database crashing/emptying)
|
||||||
return $this->formatBylaw($item);
|
$documentIds = collect($paginator->items())->pluck('id');
|
||||||
|
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
|
||||||
|
|
||||||
|
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
||||||
|
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
||||||
});
|
});
|
||||||
|
|
||||||
$responseData = [
|
$responseData = [
|
||||||
@@ -108,29 +111,29 @@ class BylawApiController extends Controller
|
|||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$formatted = $this->formatBylaw($bylaw);
|
// Fetch nodes safely
|
||||||
|
$nodes = BylawNode::with('type')
|
||||||
|
->where('document_id', $bylaw->id)
|
||||||
|
->orderBy('sort_order', 'asc')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$formatted = $this->formatBylaw($bylaw, $nodes);
|
||||||
|
|
||||||
if (!$formatted['is_locked']) {
|
if (!$formatted['is_locked']) {
|
||||||
$nodes = BylawNode::with('type')
|
$formatted['nodes'] = $nodes->map(function ($node) {
|
||||||
->where('document_id', $bylaw->id)
|
return [
|
||||||
->orderBy('sort_order', 'asc')
|
'id' => $node->id,
|
||||||
->get()
|
'parent_id' => $node->parent_id,
|
||||||
->map(function ($node) {
|
'node_type' => optional($node->type)->name,
|
||||||
return [
|
'node_identifier' => $node->node_identifier,
|
||||||
'id' => $node->id,
|
'title' => $node->title,
|
||||||
'parent_id' => $node->parent_id,
|
'node_content' => $node->node_content,
|
||||||
'node_type' => optional($node->type)->name,
|
'sort_order' => $node->sort_order,
|
||||||
'node_identifier' => $node->node_identifier,
|
'valid_from' => $node->valid_from,
|
||||||
'title' => $node->title,
|
'valid_to' => $node->valid_to,
|
||||||
'node_content' => $node->node_content,
|
'node_status' => $node->node_status,
|
||||||
'sort_order' => $node->sort_order,
|
];
|
||||||
'valid_from' => $node->valid_from,
|
})->toArray();
|
||||||
'valid_to' => $node->valid_to,
|
|
||||||
'node_status' => $node->node_status,
|
|
||||||
];
|
|
||||||
});
|
|
||||||
|
|
||||||
$formatted['nodes'] = $nodes->toArray();
|
|
||||||
|
|
||||||
$links = BylawCodificationLink::where('source_entity_type', 'bylaw_doc')
|
$links = BylawCodificationLink::where('source_entity_type', 'bylaw_doc')
|
||||||
->where('source_entity_id', $bylaw->id)
|
->where('source_entity_id', $bylaw->id)
|
||||||
@@ -194,8 +197,12 @@ 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);
|
||||||
|
|
||||||
$items = collect($paginator->items())->map(function ($item) {
|
// Pre-fetch ALL nodes for the current page in ONE query
|
||||||
return $this->formatBylaw($item);
|
$documentIds = collect($paginator->items())->pluck('id');
|
||||||
|
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
|
||||||
|
|
||||||
|
$items = collect($paginator->items())->map(function ($item) use ($allNodes) {
|
||||||
|
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
||||||
});
|
});
|
||||||
|
|
||||||
$responseData = [
|
$responseData = [
|
||||||
@@ -232,12 +239,18 @@ class BylawApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$count = $query->count();
|
$count = $query->count();
|
||||||
$items = $query->orderBy('approval_date', 'desc')->take(5)->get()->map(function ($item) {
|
$items = $query->orderBy('approval_date', 'desc')->take(5)->get();
|
||||||
return $this->formatBylaw($item);
|
|
||||||
|
// Pre-fetch ALL nodes for the current page in ONE query
|
||||||
|
$documentIds = $items->pluck('id');
|
||||||
|
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
|
||||||
|
|
||||||
|
$formattedItems = $items->map(function ($item) use ($allNodes) {
|
||||||
|
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
||||||
});
|
});
|
||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
'items' => $items,
|
'items' => $formattedItems,
|
||||||
'count' => $count
|
'count' => $count
|
||||||
], 'Success', 'Fast Search Results');
|
], 'Success', 'Fast Search Results');
|
||||||
}
|
}
|
||||||
@@ -292,11 +305,15 @@ class BylawApiController extends Controller
|
|||||||
->orderBy('id', 'asc')
|
->orderBy('id', 'asc')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
$results = array_values(array_filter([
|
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
|
||||||
$previousItem ? $this->formatBylaw($previousItem) : null,
|
|
||||||
$this->formatBylaw($current),
|
// Pre-fetch ALL nodes
|
||||||
$nextItem ? $this->formatBylaw($nextItem) : null
|
$documentIds = collect($itemsToFormat)->pluck('id');
|
||||||
]));
|
$allNodes = BylawNode::whereIn('document_id', $documentIds)->orderBy('sort_order', 'asc')->get()->groupBy('document_id');
|
||||||
|
|
||||||
|
$results = array_values(array_map(function($item) use ($allNodes) {
|
||||||
|
return $this->formatBylaw($item, $allNodes->get($item->id, collect()));
|
||||||
|
}, $itemsToFormat));
|
||||||
|
|
||||||
return $this->success($results, 'Success', 'Pager Window Details');
|
return $this->success($results, 'Success', 'Pager Window Details');
|
||||||
}
|
}
|
||||||
@@ -306,36 +323,32 @@ class BylawApiController extends Controller
|
|||||||
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatBylaw($item)
|
/**
|
||||||
|
* 100% Bulletproof formatting with aggressive HTML empty-checking
|
||||||
|
*/
|
||||||
|
private function formatBylaw($item, $preloadedNodes = null)
|
||||||
{
|
{
|
||||||
$isLocked = $this->isLockedForCurrentUser($item);
|
$isLocked = $this->isLockedForCurrentUser($item);
|
||||||
|
|
||||||
$description = $item->description;
|
$description = $item->description;
|
||||||
|
|
||||||
// HARDCORE EMPTY CHECK: Destroy all tags, non-breaking spaces, and newlines.
|
// AGGRESSIVE HTML CLEANER: Destroys hidden HTML tags, , and whitespaces
|
||||||
$cleanDesc = trim(str_replace([' ', '‌', "\r", "\n", "\t", ' '], '', strip_tags($description ?? '')));
|
$cleanDesc = trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($description ?? ''))));
|
||||||
|
|
||||||
// If the description is genuinely empty, pull from the nodes table!
|
// If the description is genuinely empty, pull from the nodes!
|
||||||
if (empty($cleanDesc)) {
|
if ($cleanDesc === '') {
|
||||||
$nodes = DB::table('bylaw_nodes')
|
|
||||||
->where('document_id', $item->id)
|
|
||||||
->orderBy('sort_order', 'asc')
|
|
||||||
->get(['title', 'node_content']); // Fetch BOTH title and node_content
|
|
||||||
|
|
||||||
$descParts = [];
|
// Uses nodes passed from the parent query, OR natively queries them if missing
|
||||||
foreach ($nodes as $node) {
|
$nodes = $preloadedNodes !== null ? $preloadedNodes : BylawNode::where('document_id', $item->id)->orderBy('sort_order', 'asc')->get();
|
||||||
$nodeTitle = trim($node->title ?? '');
|
|
||||||
$nodeContent = trim($node->node_content ?? '');
|
|
||||||
|
|
||||||
if (!empty($nodeTitle)) {
|
if ($nodes && $nodes->count() > 0) {
|
||||||
$descParts[] = $nodeTitle;
|
// Extracts node_content, drops any that are pure empty space, and joins them
|
||||||
}
|
$description = $nodes->pluck('node_content')->filter(function ($content) {
|
||||||
if (!empty($nodeContent)) {
|
return trim(preg_replace('/\s+/', '', html_entity_decode(strip_tags($content ?? '')))) !== '';
|
||||||
$descParts[] = $nodeContent;
|
})->implode("\n\n");
|
||||||
}
|
} else {
|
||||||
|
$description = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$description = implode("\n\n", $descParts);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
Reference in New Issue
Block a user