Fixed api issue
This commit is contained in:
@@ -80,29 +80,8 @@ 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);
|
||||||
|
|
||||||
// Fetch Node Content and Map perfectly by Document ID
|
$items = collect($paginator->items())->map(function ($item) {
|
||||||
$documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray();
|
return $this->formatBylaw($item);
|
||||||
$nodesByDoc = [];
|
|
||||||
|
|
||||||
if (!empty($documentIds)) {
|
|
||||||
$nodes = DB::table('bylaw_nodes')
|
|
||||||
->whereIn('document_id', $documentIds)
|
|
||||||
->orderBy('sort_order', 'asc')
|
|
||||||
->select('document_id', 'node_content')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
foreach ($nodes as $node) {
|
|
||||||
$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 ($nodesByDoc) {
|
|
||||||
$itemId = (int) $item->id;
|
|
||||||
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$responseData = [
|
$responseData = [
|
||||||
@@ -214,28 +193,8 @@ 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);
|
||||||
|
|
||||||
$documentIds = collect($paginator->items())->pluck('id')->map(fn($id) => (int)$id)->toArray();
|
$items = collect($paginator->items())->map(function ($item) {
|
||||||
$nodesByDoc = [];
|
return $this->formatBylaw($item);
|
||||||
|
|
||||||
if (!empty($documentIds)) {
|
|
||||||
$nodes = DB::table('bylaw_nodes')
|
|
||||||
->whereIn('document_id', $documentIds)
|
|
||||||
->orderBy('sort_order', 'asc')
|
|
||||||
->select('document_id', 'node_content')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
foreach ($nodes as $node) {
|
|
||||||
$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 ($nodesByDoc) {
|
|
||||||
$itemId = (int) $item->id;
|
|
||||||
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
$responseData = [
|
$responseData = [
|
||||||
@@ -274,28 +233,8 @@ 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();
|
||||||
|
|
||||||
$documentIds = $items->pluck('id')->map(fn($id) => (int)$id)->toArray();
|
$formattedItems = $items->map(function ($item) {
|
||||||
$nodesByDoc = [];
|
return $this->formatBylaw($item);
|
||||||
|
|
||||||
if (!empty($documentIds)) {
|
|
||||||
$nodes = DB::table('bylaw_nodes')
|
|
||||||
->whereIn('document_id', $documentIds)
|
|
||||||
->orderBy('sort_order', 'asc')
|
|
||||||
->select('document_id', 'node_content')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
foreach ($nodes as $node) {
|
|
||||||
$docId = (int) $node->document_id;
|
|
||||||
$content = trim((string)$node->node_content);
|
|
||||||
if (!empty($content)) {
|
|
||||||
$nodesByDoc[$docId][] = $content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$formattedItems = $items->map(function ($item) use ($nodesByDoc) {
|
|
||||||
$itemId = (int) $item->id;
|
|
||||||
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
@@ -356,71 +295,46 @@ class BylawApiController extends Controller
|
|||||||
|
|
||||||
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
|
$itemsToFormat = array_filter([$previousItem, $current, $nextItem]);
|
||||||
|
|
||||||
$documentIds = collect($itemsToFormat)->pluck('id')->map(fn($id) => (int)$id)->toArray();
|
$results = array_values(array_map(function($item) {
|
||||||
$nodesByDoc = [];
|
return $this->formatBylaw($item);
|
||||||
|
|
||||||
if (!empty($documentIds)) {
|
|
||||||
$nodes = DB::table('bylaw_nodes')
|
|
||||||
->whereIn('document_id', $documentIds)
|
|
||||||
->orderBy('sort_order', 'asc')
|
|
||||||
->select('document_id', 'node_content')
|
|
||||||
->get();
|
|
||||||
|
|
||||||
foreach ($nodes as $node) {
|
|
||||||
$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 ($nodesByDoc) {
|
|
||||||
$itemId = (int) $item->id;
|
|
||||||
return $this->formatBylaw($item, $nodesByDoc[$itemId] ?? null);
|
|
||||||
}, $itemsToFormat));
|
}, $itemsToFormat));
|
||||||
|
|
||||||
return $this->success($results, 'Success', 'Pager Window Details');
|
return $this->success($results, 'Success', 'Pager Window Details');
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isLockedForCurrentUser($item): bool
|
|
||||||
{
|
|
||||||
return ! auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FULLY REBUILT: FORCES NODE_CONTENT INTO DESCRIPTION
|
* COMPLETELY REWRITTEN - BULLETPROOF DIRECT QUERY
|
||||||
*/
|
*/
|
||||||
private function formatBylaw($item, $preloadedNodes = null)
|
private function formatBylaw($item)
|
||||||
{
|
{
|
||||||
$isLocked = $this->isLockedForCurrentUser($item);
|
// 1. Direct, undeniable raw query to the database for this specific document's nodes
|
||||||
|
|
||||||
$nodeContentFullText = "";
|
|
||||||
|
|
||||||
// 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')
|
$nodes = DB::table('bylaw_nodes')
|
||||||
->where('document_id', $item->id)
|
->where('document_id', $item->id)
|
||||||
->orderBy('sort_order', 'asc')
|
->orderBy('sort_order', 'asc')
|
||||||
->pluck('node_content')
|
->get();
|
||||||
->toArray();
|
|
||||||
|
|
||||||
$validContents = [];
|
$extractedTexts = [];
|
||||||
foreach ($nodes as $content) {
|
foreach ($nodes as $node) {
|
||||||
if (!empty(trim((string)$content))) {
|
$text = trim((string) $node->node_content);
|
||||||
$validContents[] = trim((string)$content);
|
if (!empty($text)) {
|
||||||
|
$extractedTexts[] = $text;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$nodeContentFullText = implode("\n\n", $validContents);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Absolute last resort (only if the DB literally has no nodes for this item)
|
// 2. Combine all node_content into the final string
|
||||||
if (empty(trim(strip_tags($nodeContentFullText)))) {
|
$nodeContentAsDescription = implode("\n\n", $extractedTexts);
|
||||||
$nodeContentFullText = $item->description;
|
|
||||||
|
// 3. Fallback ONLY if the database row is literally missing nodes
|
||||||
|
if (empty($nodeContentAsDescription)) {
|
||||||
|
$nodeContentAsDescription = $item->description;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Safe Lock Check (prevents "call to member function on null" if user isn't logged in)
|
||||||
|
$isLocked = false;
|
||||||
|
if (auth()->check() && auth()->user() && method_exists(auth()->user(), 'isSubscriber')) {
|
||||||
|
$isLocked = !auth()->user()->isSubscriber() && (bool) ($item->is_locked ?? false);
|
||||||
|
} else {
|
||||||
|
$isLocked = (bool) ($item->is_locked ?? false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
@@ -441,7 +355,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 : $nodeContentFullText, // <--- NODE_CONTENT IS NOW 100% FORCED HERE
|
'description' => $isLocked ? null : $nodeContentAsDescription, // DIRECT NODE_CONTENT HERE
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user