Collections/config.php

113 lines
4.4 KiB
PHP
Executable File

<?php
// --- Application Configuration ---
const APP_NAME = 'Collections';
const DB_PATH = __DIR__ . '/collections.sqlite';
const DEFAULT_LOCALE = 'nl';
const SUPPORTED_LOCALES = ['nl', 'en'];
// --- Session Start ---
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// De autoloader moet geladen zijn voordat we hier komen.
// Gebruik FQCNs om afhankelijkheid van 'use' statements te vermijden.
// --- Twig Setup ---
$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 ---
$translationService = new \App\Services\TranslationService(
__DIR__ . '/lang',
SUPPORTED_LOCALES,
DEFAULT_LOCALE
);
// Get current locale from session or default
$locale = $_SESSION['locale'] ?? DEFAULT_LOCALE;
$translator = $translationService->getTranslator($locale);
// Add translation function to Twig
$twig->addFunction(new \Twig\TwigFunction('trans', function (string $id, array $parameters = [], string $domain = null, string $locale = null) use ($translator) {
return $translator->trans($id, $parameters, $domain, $locale);
}));
// Add current locale to Twig globals
$twig->addGlobal('current_locale', $locale);
$twig->addGlobal('supported_locales', SUPPORTED_LOCALES);
$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
try {
$db = App\Database\Database::getInstance();
$categories = App\Models\Category::getAll($db);
$items = App\Models\Item::getAll($db);
function buildTree($categories, $items, $parentId = null, &$visited = [], $depth = 0) {
if ($depth > 5) return []; // Limit depth
$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' => buildTree($categories, $items, $cat['id'], $visited, $depth + 1),
'items' => []
];
// Limit items to 20 per category
$itemCount = 0;
foreach ($items as $item) {
if ($item['category_id'] == $cat['id'] && $itemCount < 20) {
$node['items'][] = $item;
$itemCount++;
}
}
$tree[] = $node;
}
}
return $tree;
}
$categoryTree = buildTree($categories, $items);
function renderTree($nodes, $depth = 0) {
if ($depth > 5) 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());
$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('sidebar_html', $sidebarHtml);