Replace sidebar menu with collapsible category tree showing items
This commit is contained in:
29
config.php
29
config.php
@@ -45,3 +45,32 @@ $twig->addGlobal('app_name', APP_NAME);
|
||||
// Add translated confirm messages for JS
|
||||
$twig->addGlobal('delete_part_confirm', $translator->trans('Are you sure you want to delete this part?'));
|
||||
$twig->addGlobal('delete_category_confirm', $translator->trans('Are you sure you want to delete this category?'));
|
||||
|
||||
// Build category tree for sidebar
|
||||
$db = App\Database\Database::getInstance();
|
||||
$categories = App\Models\Category::getAll($db);
|
||||
$items = App\Models\Item::getAll($db);
|
||||
|
||||
function buildTree($categories, $items, $parentId = null) {
|
||||
$tree = [];
|
||||
foreach ($categories as $cat) {
|
||||
if ($cat['parent_id'] == $parentId) {
|
||||
$node = [
|
||||
'id' => $cat['id'],
|
||||
'name' => $cat['name'],
|
||||
'children' => buildTree($categories, $items, $cat['id']),
|
||||
'items' => []
|
||||
];
|
||||
foreach ($items as $item) {
|
||||
if ($item['category_id'] == $cat['id']) {
|
||||
$node['items'][] = $item;
|
||||
}
|
||||
}
|
||||
$tree[] = $node;
|
||||
}
|
||||
}
|
||||
return $tree;
|
||||
}
|
||||
|
||||
$categoryTree = buildTree($categories, $items);
|
||||
$twig->addGlobal('category_tree', $categoryTree);
|
||||
|
||||
Reference in New Issue
Block a user