Implement category tree in sidebar using PHP-generated HTML to avoid Twig recursion issues

This commit is contained in:
Edwin Noorlander 2025-11-12 08:40:22 +01:00
parent 7c5ecd4b9b
commit bbc57a9d53
2 changed files with 32 additions and 31 deletions

View File

@ -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 .= '<li>';
$html .= '<span class="category" onclick="toggleCategory(this)">' . htmlspecialchars($node['name']) . '</span>';
if (!empty($node['children'])) {
$html .= '<ul style="display: none;">';
$html .= renderTree($node['children'], $depth + 1);
$html .= '</ul>';
}
if (!empty($node['items'])) {
$html .= '<ul class="items">';
foreach ($node['items'] as $item) {
$html .= '<li><a href="#" onclick="editItem(' . (int)$item['id'] . ')" class="item-link">' . htmlspecialchars($item['name']) . '</a></li>';
}
$html .= '</ul>';
}
$html .= '</li>';
}
return $html;
}
$sidebarHtml = '<ul class="category-tree">' . renderTree($categoryTree) . '</ul>';
} catch (Exception $e) {
error_log('Error building category tree: ' . $e->getMessage());
$categoryTree = [];
$sidebarHtml = '<ul class="nav flex-column nav-pills"><li class="nav-item"><a class="nav-link" href="#" data-route="/">Overview</a></li><li class="nav-item"><a class="nav-link" href="#" data-route="/categories">Categories</a></li><li class="nav-item"><a class="nav-link" href="#" data-route="/parts">Parts</a></li></ul>';
}
$twig->addGlobal('category_tree', $categoryTree);
$twig->addGlobal('sidebar_html', $sidebarHtml);

View File

@ -1,27 +1 @@
{% autoescape %}
{% macro render_node(node) %}
<li>
<span class="category" onclick="toggleCategory(this)">{{ node.name }}</span>
{% if node.children %}
<ul style="display: none;">
{% for child in node.children %}
{{ _self.render_node(child) }}
{% endfor %}
</ul>
{% endif %}
{% if node.items %}
<ul class="items">
{% for item in node.items %}
<li><a href="#" onclick="editItem({{ item.id }})" class="item-link">{{ item.name }}</a></li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
<ul class="category-tree">
{% for node in category_tree %}
{{ _self.render_node(node) }}
{% endfor %}
</ul>
{% endautoescape %}
{{ sidebar_html|raw }}