diff --git a/config.php b/config.php index 7beda22..80aa5fd 100755 --- a/config.php +++ b/config.php @@ -19,6 +19,7 @@ $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates'); $twig = new \Twig\Environment($loader, [ // 'cache' => __DIR__ . '/cache/twig', // Uncomment for production 'debug' => true, + 'max_recursion' => 100, ]); // --- Translation Setup --- @@ -52,7 +53,8 @@ try { $categories = App\Models\Category::getAll($db); $items = App\Models\Item::getAll($db); - function buildTree($categories, $items, $parentId = null, &$visited = []) { + function buildTree($categories, $items, $parentId = null, &$visited = [], $depth = 0) { + if ($depth > 10) return []; // Prevent deep recursion $tree = []; foreach ($categories as $cat) { if ($cat['parent_id'] == $parentId && !in_array($cat['id'], $visited)) { @@ -60,7 +62,7 @@ try { $node = [ 'id' => $cat['id'], 'name' => $cat['name'], - 'children' => buildTree($categories, $items, $cat['id'], $visited), + 'children' => buildTree($categories, $items, $cat['id'], $visited, $depth + 1), 'items' => [] ]; foreach ($items as $item) { @@ -75,8 +77,33 @@ try { } $categoryTree = buildTree($categories, $items); + + function renderTree($nodes, $depth = 0) { + if ($depth > 10) return ''; + $html = ''; + foreach ($nodes as $node) { + $html .= '
  • '; + $html .= '' . htmlspecialchars($node['name']) . ''; + if (!empty($node['children'])) { + $html .= ''; + } + if (!empty($node['items'])) { + $html .= ''; + } + $html .= '
  • '; + } + return $html; + } + + $sidebarHtml = ''; } catch (Exception $e) { error_log('Error building category tree: ' . $e->getMessage()); - $categoryTree = []; + $sidebarHtml = ''; } -$twig->addGlobal('category_tree', $categoryTree); +$twig->addGlobal('sidebar_html', $sidebarHtml); diff --git a/templates/partials/sidebar.twig b/templates/partials/sidebar.twig index bb9312d..5aeb3d7 100755 --- a/templates/partials/sidebar.twig +++ b/templates/partials/sidebar.twig @@ -1,27 +1 @@ -{% autoescape %} -{% macro render_node(node) %} -
  • - {{ node.name }} - {% if node.children %} - - {% endif %} - {% if node.items %} - - {% endif %} -
  • -{% endmacro %} - - -{% endautoescape %} \ No newline at end of file +{{ sidebar_html|raw }} \ No newline at end of file