113 lines
5.0 KiB
PHP
Executable File
113 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
$autoloadPath = __DIR__ . '/../vendor/autoload.php';
|
|
if (!file_exists($autoloadPath)) {
|
|
http_response_code(500);
|
|
die("FATAL ERROR: Composer autoloader not found. Please run 'composer install' in the project root.");
|
|
}
|
|
require $autoloadPath;
|
|
|
|
require __DIR__ . '/../config.php';
|
|
|
|
use App\Router;
|
|
use App\Controllers\ItemController;
|
|
use App\Controllers\CategoryController;
|
|
use App\Database;
|
|
|
|
// Initialize Database (ensures tables exist)
|
|
Database::getInstance();
|
|
|
|
$router = new Router();
|
|
|
|
// --- Language Switch Route ---
|
|
$router->addRoute('GET', '/lang/{locale}', function ($locale) {
|
|
if (in_array($locale, SUPPORTED_LOCALES)) {
|
|
$_SESSION['locale'] = $locale;
|
|
}
|
|
// Redirect back to the page the user came from, or home
|
|
$referer = $_SERVER['HTTP_REFERER'] ?? '/';
|
|
header("Location: " . $referer);
|
|
exit;
|
|
});
|
|
|
|
// --- Web Routes (Full Page/Initial Load) ---
|
|
$router->addRoute('GET', '/', [ItemController::class, 'overview']);
|
|
$router->addRoute('GET', '/categories', [CategoryController::class, 'index']);
|
|
$router->addRoute('GET', '/parts', [ItemController::class, 'addForm']);
|
|
$router->addRoute('GET', '/print/{id:\d+}', [ItemController::class, 'printQR']);
|
|
|
|
// --- API Routes (AJAX Content) ---
|
|
// These routes return only the Twig block content, not the full layout.
|
|
$router->addRoute('GET', '/api/items', [ItemController::class, 'listItems']);
|
|
$router->addRoute('GET', '/api/items/{id:\d+}', [ItemController::class, 'getItem']);
|
|
$router->addRoute('GET', '/api/categories', [CategoryController::class, 'listCategories']);
|
|
$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 .= '<li>';
|
|
$html .= '<span class="category" onclick="toggleCategory(this)">' . htmlspecialchars($node['name']) . '</span>';
|
|
if (!empty($node['children'])) {
|
|
$html .= '<ul style="display: none;">';
|
|
$html .= render_category_tree($node['children'], $depth + 1);
|
|
$html .= '</ul>';
|
|
}
|
|
$html .= '</li>';
|
|
}
|
|
return $html;
|
|
}
|
|
|
|
$nodeCount = 0;
|
|
$categoryTree = build_category_tree($categories, null, $visited = [], 0, $nodeCount);
|
|
$html = '<ul class="category-tree">' . render_category_tree($categoryTree) . '</ul>';
|
|
if (strlen($html) > 10000) {
|
|
$html = '<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>';
|
|
}
|
|
header('Content-Type: text/html');
|
|
echo $html;
|
|
} catch (Exception $e) {
|
|
header('Content-Type: text/html');
|
|
echo '<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>';
|
|
}
|
|
});
|
|
|
|
// --- API CRUD Routes ---
|
|
// Items
|
|
$router->addRoute('POST', '/api/items', [ItemController::class, 'create']);
|
|
$router->addRoute('POST', '/api/items/{id:\d+}', [ItemController::class, 'update']);
|
|
$router->addRoute('DELETE', '/api/items/{id:\d+}', [ItemController::class, 'delete']);
|
|
|
|
// Categories
|
|
$router->addRoute('POST', '/api/categories', [CategoryController::class, 'create']);
|
|
$router->addRoute('PUT', '/api/categories/{id:\d+}', [CategoryController::class, 'update']);
|
|
$router->addRoute('DELETE', '/api/categories/{id:\d+}', [CategoryController::class, 'delete']);
|
|
|
|
|
|
$router->dispatch(); |