Enable category tree with limits to prevent 500 errors: max depth 5, max 20 items per category

This commit is contained in:
Edwin Noorlander 2025-11-12 08:41:23 +01:00
parent bbc57a9d53
commit 67c402a37a

View File

@ -54,7 +54,7 @@ try {
$items = App\Models\Item::getAll($db); $items = App\Models\Item::getAll($db);
function buildTree($categories, $items, $parentId = null, &$visited = [], $depth = 0) { function buildTree($categories, $items, $parentId = null, &$visited = [], $depth = 0) {
if ($depth > 10) return []; // Prevent deep recursion if ($depth > 5) return []; // Limit depth
$tree = []; $tree = [];
foreach ($categories as $cat) { foreach ($categories as $cat) {
if ($cat['parent_id'] == $parentId && !in_array($cat['id'], $visited)) { if ($cat['parent_id'] == $parentId && !in_array($cat['id'], $visited)) {
@ -65,9 +65,12 @@ try {
'children' => buildTree($categories, $items, $cat['id'], $visited, $depth + 1), 'children' => buildTree($categories, $items, $cat['id'], $visited, $depth + 1),
'items' => [] 'items' => []
]; ];
// Limit items to 20 per category
$itemCount = 0;
foreach ($items as $item) { foreach ($items as $item) {
if ($item['category_id'] == $cat['id']) { if ($item['category_id'] == $cat['id'] && $itemCount < 20) {
$node['items'][] = $item; $node['items'][] = $item;
$itemCount++;
} }
} }
$tree[] = $node; $tree[] = $node;
@ -79,7 +82,7 @@ try {
$categoryTree = buildTree($categories, $items); $categoryTree = buildTree($categories, $items);
function renderTree($nodes, $depth = 0) { function renderTree($nodes, $depth = 0) {
if ($depth > 10) return ''; if ($depth > 5) return '';
$html = ''; $html = '';
foreach ($nodes as $node) { foreach ($nodes as $node) {
$html .= '<li>'; $html .= '<li>';