| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- require_once __DIR__ . "/../config.php";
- require_once __DIR__ . "/../includes/functions.php";
- // Check admin login
- if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
- header("Location: login.php");
- exit();
- }
- $pageTitle = "Kategorien verwalten";
- $message = "";
- $messageType = "";
- $categories = getCategories();
- $products = getProducts();
- if ($_SERVER['REQUEST_METHOD'] === "POST") {
- // Validate CSRF token
- if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
- $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
- $messageType = "error";
- } else {
- if (isset($_POST['add_category'])) {
- $label = normalizeCategoryLabel($_POST['label'] ?? "");
- if (!isValidCategoryLabel($label)) {
- $message =
- "Bitte geben Sie einen Kategorienamen mit maximal 80 Zeichen ein.";
- $messageType = "error";
- } else {
- $categoryId = generateCategoryIdFromLabel($label, $categories);
- $categories[] = [
- "id" => $categoryId,
- "label" => $label,
- ];
- if (saveCategories($categories)) {
- logAccess("Admin added category", [
- "category_id" => $categoryId,
- "label" => $label,
- ]);
- $message = "Kategorie wurde erfolgreich angelegt.";
- $messageType = "success";
- } else {
- $message = "Kategorie konnte nicht gespeichert werden.";
- $messageType = "error";
- }
- }
- }
- if (isset($_POST['update_category'])) {
- $categoryId = normalizeCategoryId($_POST['category_id'] ?? "");
- $label = normalizeCategoryLabel($_POST['label'] ?? "");
- $found = false;
- if (!isValidCategoryLabel($label)) {
- $message =
- "Bitte geben Sie einen Kategorienamen mit maximal 80 Zeichen ein.";
- $messageType = "error";
- } else {
- foreach ($categories as &$category) {
- if ($category["id"] === $categoryId) {
- $category["label"] = $label;
- $found = true;
- break;
- }
- }
- unset($category);
- if (!$found) {
- $message = "Kategorie nicht gefunden.";
- $messageType = "error";
- } else {
- if (saveCategories($categories)) {
- logAccess("Admin updated category", [
- "category_id" => $categoryId,
- "label" => $label,
- ]);
- $message = "Kategorie wurde erfolgreich aktualisiert.";
- $messageType = "success";
- } else {
- $message =
- "Kategorie konnte nicht gespeichert werden.";
- $messageType = "error";
- }
- }
- }
- }
- if (isset($_POST['delete_category'])) {
- $categoryId = normalizeCategoryId($_POST['category_id'] ?? "");
- $label = "";
- $found = false;
- if (isCategoryInUse($categoryId)) {
- $message =
- "Diese Kategorie wird noch von Produkten verwendet und kann nicht gelöscht werden.";
- $messageType = "error";
- } else {
- $categories = array_values(
- array_filter($categories, function ($category) use (
- $categoryId,
- &$found,
- &$label,
- ) {
- if ($category["id"] === $categoryId) {
- $found = true;
- $label = $category["label"];
- return false;
- }
- return true;
- }),
- );
- if (!$found) {
- $message = "Kategorie nicht gefunden.";
- $messageType = "error";
- } else {
- if (saveCategories($categories)) {
- logAccess("Admin deleted category", [
- "category_id" => $categoryId,
- "label" => $label,
- ]);
- $message = "Kategorie wurde gelöscht.";
- $messageType = "success";
- } else {
- $message = "Kategorie konnte nicht gelöscht werden.";
- $messageType = "error";
- }
- }
- }
- }
- $categories = getCategories();
- $products = getProducts();
- }
- }
- $editCategoryId = normalizeCategoryId($_GET['edit'] ?? "");
- $editingCategory = null;
- if ($editCategoryId !== "") {
- $editingCategory = getCategoryById($editCategoryId);
- if ($editingCategory === null && $message === "") {
- $message = "Ausgewählte Kategorie wurde nicht gefunden.";
- $messageType = "error";
- }
- }
- $bodyClass = "admin-page";
- include __DIR__ . "/../includes/header.php";
- ?>
- <div class="admin-header">
- <h2>Kategorien verwalten</h2>
- <div>
- <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
- </div>
- </div>
- <?php if ($message !== ""): ?>
- <div class="alert alert-<?php echo $messageType; ?>">
- <?php echo htmlspecialchars($message); ?>
- </div>
- <?php endif; ?>
- <?php if ($editingCategory !== null): ?>
- <div class="panel panel-lg">
- <h3>Kategorie bearbeiten</h3>
- <form method="POST">
- <?php echo csrfField(); ?>
- <input type="hidden" name="category_id" value="<?php echo htmlspecialchars(
- $editingCategory["id"],
- ); ?>">
- <div class="form-group">
- <label for="category_id_display">ID</label>
- <input type="text" id="category_id_display" value="<?php echo htmlspecialchars(
- $editingCategory["id"],
- ); ?>" readonly>
- <small>Die ID sollte gleichbleiben, damit Produktzuordnungen und Filter-URLs gültig bleiben.</small>
- </div>
- <div class="form-group">
- <label for="label_edit">Name *</label>
- <input type="text" id="label_edit" name="label" maxlength="80" required value="<?php echo htmlspecialchars(
- $editingCategory["label"],
- ); ?>">
- </div>
- <button type="submit" name="update_category" class="btn">Kategorie aktualisieren</button>
- <a href="categories.php" class="btn btn-secondary">Abbrechen</a>
- </form>
- </div>
- <?php else: ?>
- <div class="panel panel-lg">
- <h3>Neue Kategorie anlegen</h3>
- <form method="POST">
- <?php echo csrfField(); ?>
- <div class="form-group">
- <label for="label">Name *</label>
- <input type="text" id="label" name="label" maxlength="80" required placeholder="z.B. Accessoires">
- <small>Die technische ID wird einmalig automatisch aus dem Namen erzeugt.</small>
- </div>
- <button type="submit" name="add_category" class="btn">Kategorie anlegen</button>
- </form>
- </div>
- <?php endif; ?>
- <div class="panel">
- <h3>Kategorien</h3>
- <div class="table-responsive">
- <table class="responsive-table">
- <thead>
- <tr>
- <th>Name</th>
- <th>ID</th>
- <th>Produkte</th>
- <th>Aktionen</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($categories as $category): ?>
- <?php
- $productCount = 0;
- foreach ($products as $product) {
- if (productHasCategory($product, $category["id"])) {
- $productCount++;
- }
- }
- ?>
- <tr>
- <td data-label="Name"><?php echo htmlspecialchars(
- $category["label"],
- ); ?></td>
- <td data-label="ID"><code><?php echo htmlspecialchars(
- $category["id"],
- ); ?></code></td>
- <td data-label="Produkte"><?php echo $productCount; ?></td>
- <td data-label="Aktionen">
- <a href="categories.php?edit=<?php echo urlencode(
- $category["id"],
- ); ?>" class="btn btn-small btn-secondary">Bearbeiten</a>
- <form method="POST" class="inline-form" onsubmit="return confirm('Kategorie wirklich löschen?');">
- <?php echo csrfField(); ?>
- <input type="hidden" name="category_id" value="<?php echo htmlspecialchars(
- $category["id"],
- ); ?>">
- <button type="submit" name="delete_category" class="btn btn-small">Löschen</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- </div>
- </div>
- <?php include __DIR__ . "/../includes/footer.php"; ?>
|