diff --git a/collections.sqlite b/collections.sqlite.backup similarity index 100% rename from collections.sqlite rename to collections.sqlite.backup diff --git a/config.php b/config.php index 4d615ad..ee13014 100755 --- a/config.php +++ b/config.php @@ -19,7 +19,6 @@ $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 --- diff --git a/public/index.php b/public/index.php index 668c535..624f94e 100755 --- a/public/index.php +++ b/public/index.php @@ -44,6 +44,59 @@ $router->addRoute('GET', '/api/categories', [CategoryController::class, 'listCat $router->addRoute('GET', '/api/categories/list', [CategoryController::class, 'listCategoriesJson']); $router->addRoute('GET', '/api/categories/{id}', [CategoryController::class, 'getCategory']); $router->addRoute('GET', '/api/parts', [ItemController::class, 'renderAddForm']); +$router->addRoute('GET', '/api/tree', function() { + try { + $db = App\Database\Database::getInstance(); + $categories = App\Models\Category::getAll($db); + + function build_category_tree($categories, $parentId = null, &$visited = [], $depth = 0, &$nodeCount = 0) { + if ($depth > 5 || $nodeCount > 100) return []; + $tree = []; + foreach ($categories as $cat) { + if ($cat['parent_id'] == $parentId && !in_array($cat['id'], $visited)) { + $visited[] = $cat['id']; + $node = [ + 'id' => $cat['id'], + 'name' => $cat['name'], + 'children' => build_category_tree($categories, $cat['id'], $visited, $depth + 1, $nodeCount) + ]; + $tree[] = $node; + $nodeCount++; + if ($nodeCount > 100) break; + } + } + return $tree; + } + + function render_category_tree($nodes, $depth = 0) { + if ($depth > 5) return ''; + $html = ''; + foreach ($nodes as $node) { + $html .= '
  • '; + $html .= '' . htmlspecialchars($node['name']) . ''; + if (!empty($node['children'])) { + $html .= ''; + } + $html .= '
  • '; + } + return $html; + } + + $nodeCount = 0; + $categoryTree = build_category_tree($categories, null, $visited = [], 0, $nodeCount); + $html = ''; + if (strlen($html) > 10000) { + $html = ''; + } + header('Content-Type: text/html'); + echo $html; + } catch (Exception $e) { + header('Content-Type: text/html'); + echo ''; + } +}); // --- API CRUD Routes --- // Items diff --git a/public/js/app.js b/public/js/app.js index a01debc..b1f247c 100755 --- a/public/js/app.js +++ b/public/js/app.js @@ -115,6 +115,16 @@ document.addEventListener('DOMContentLoaded', function() { const initialPath = window.location.pathname === '/' ? '/items' : window.location.pathname; fetchContent(initialPath, false); + // Load category tree + fetch('/api/tree') + .then(response => response.text()) + .then(html => { + document.querySelector('.sidebar').innerHTML = html; + }) + .catch(error => { + console.error('Error loading tree:', error); + }); + // Page-specific scripts (e.g., form submissions) function initPageSpecificScripts() { // Overview page: filter form diff --git a/test_db.php b/test_db.php new file mode 100644 index 0000000..8b65a90 --- /dev/null +++ b/test_db.php @@ -0,0 +1,15 @@ +query('SELECT COUNT(*) FROM categories'); + $count = $stmt->fetchColumn(); + echo "Categories: $count\n"; + $stmt = $db->query('SELECT COUNT(*) FROM items'); + $count = $stmt->fetchColumn(); + echo "Items: $count\n"; +} catch (Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; +} +?> \ No newline at end of file