__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 .= '
  • '; $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()); $sidebarHtml = ''; } $twig->addGlobal('sidebar_html', $sidebarHtml);