categories.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. require_once __DIR__ . '/../config.php';
  3. require_once __DIR__ . '/../includes/functions.php';
  4. // Check admin login
  5. if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
  6. header('Location: login.php');
  7. exit;
  8. }
  9. $pageTitle = 'Kategorien verwalten';
  10. $message = '';
  11. $messageType = '';
  12. $categories = getCategories();
  13. $products = getProducts();
  14. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  15. if (isset($_POST['add_category'])) {
  16. $label = normalizeCategoryLabel($_POST['label'] ?? '');
  17. if (!isValidCategoryLabel($label)) {
  18. $message = 'Bitte geben Sie einen Kategorienamen mit maximal 80 Zeichen ein.';
  19. $messageType = 'error';
  20. } else {
  21. $categoryId = generateCategoryIdFromLabel($label, $categories);
  22. $categories[] = [
  23. 'id' => $categoryId,
  24. 'label' => $label
  25. ];
  26. saveCategories($categories);
  27. $message = 'Kategorie wurde erfolgreich angelegt.';
  28. $messageType = 'success';
  29. }
  30. }
  31. if (isset($_POST['update_category'])) {
  32. $categoryId = normalizeCategoryId($_POST['category_id'] ?? '');
  33. $label = normalizeCategoryLabel($_POST['label'] ?? '');
  34. $found = false;
  35. if (!isValidCategoryLabel($label)) {
  36. $message = 'Bitte geben Sie einen Kategorienamen mit maximal 80 Zeichen ein.';
  37. $messageType = 'error';
  38. } else {
  39. foreach ($categories as &$category) {
  40. if ($category['id'] === $categoryId) {
  41. $category['label'] = $label;
  42. $found = true;
  43. break;
  44. }
  45. }
  46. unset($category);
  47. if (!$found) {
  48. $message = 'Kategorie nicht gefunden.';
  49. $messageType = 'error';
  50. } else {
  51. saveCategories($categories);
  52. $message = 'Kategorie wurde erfolgreich aktualisiert.';
  53. $messageType = 'success';
  54. }
  55. }
  56. }
  57. if (isset($_POST['delete_category'])) {
  58. $categoryId = normalizeCategoryId($_POST['category_id'] ?? '');
  59. $found = false;
  60. if (isCategoryInUse($categoryId)) {
  61. $message = 'Diese Kategorie wird noch von Produkten verwendet und kann nicht gelöscht werden.';
  62. $messageType = 'error';
  63. } else {
  64. $categories = array_values(array_filter($categories, function ($category) use ($categoryId, &$found) {
  65. if ($category['id'] === $categoryId) {
  66. $found = true;
  67. return false;
  68. }
  69. return true;
  70. }));
  71. if (!$found) {
  72. $message = 'Kategorie nicht gefunden.';
  73. $messageType = 'error';
  74. } else {
  75. saveCategories($categories);
  76. $message = 'Kategorie wurde gelöscht.';
  77. $messageType = 'success';
  78. }
  79. }
  80. }
  81. $categories = getCategories();
  82. $products = getProducts();
  83. }
  84. $editCategoryId = normalizeCategoryId($_GET['edit'] ?? '');
  85. $editingCategory = null;
  86. if ($editCategoryId !== '') {
  87. $editingCategory = getCategoryById($editCategoryId);
  88. if ($editingCategory === null && $message === '') {
  89. $message = 'Ausgewählte Kategorie wurde nicht gefunden.';
  90. $messageType = 'error';
  91. }
  92. }
  93. $bodyClass = 'admin-page';
  94. include __DIR__ . '/../includes/header.php';
  95. ?>
  96. <div class="admin-header">
  97. <h2>Kategorien verwalten</h2>
  98. <div>
  99. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  100. </div>
  101. </div>
  102. <?php if ($message !== ''): ?>
  103. <div class="alert alert-<?php echo $messageType; ?>">
  104. <?php echo htmlspecialchars($message); ?>
  105. </div>
  106. <?php endif; ?>
  107. <?php if ($editingCategory !== null): ?>
  108. <div class="panel" style="padding: 2rem;">
  109. <h3>Kategorie bearbeiten</h3>
  110. <form method="POST">
  111. <input type="hidden" name="category_id" value="<?php echo htmlspecialchars($editingCategory['id']); ?>">
  112. <div class="form-group">
  113. <label for="category_id_display">ID</label>
  114. <input type="text" id="category_id_display" value="<?php echo htmlspecialchars($editingCategory['id']); ?>" readonly>
  115. <small>Die ID sollte gleichbleiben, damit Produktzuordnungen und Filter-URLs gültig bleiben.</small>
  116. </div>
  117. <div class="form-group">
  118. <label for="label_edit">Name *</label>
  119. <input type="text" id="label_edit" name="label" maxlength="80" required value="<?php echo htmlspecialchars($editingCategory['label']); ?>">
  120. </div>
  121. <button type="submit" name="update_category" class="btn">Kategorie aktualisieren</button>
  122. <a href="categories.php" class="btn btn-secondary">Abbrechen</a>
  123. </form>
  124. </div>
  125. <?php else: ?>
  126. <div class="panel" style="padding: 2rem;">
  127. <h3>Neue Kategorie anlegen</h3>
  128. <form method="POST">
  129. <div class="form-group">
  130. <label for="label">Name *</label>
  131. <input type="text" id="label" name="label" maxlength="80" required placeholder="z.B. Accessoires">
  132. <small>Die technische ID wird einmalig automatisch aus dem Namen erzeugt.</small>
  133. </div>
  134. <button type="submit" name="add_category" class="btn">Kategorie anlegen</button>
  135. </form>
  136. </div>
  137. <?php endif; ?>
  138. <div class="panel">
  139. <h3>Kategorien</h3>
  140. <div class="table-responsive">
  141. <table class="responsive-table">
  142. <thead>
  143. <tr>
  144. <th>Name</th>
  145. <th>ID</th>
  146. <th>Produkte</th>
  147. <th>Aktionen</th>
  148. </tr>
  149. </thead>
  150. <tbody>
  151. <?php foreach ($categories as $category): ?>
  152. <?php
  153. $productCount = 0;
  154. foreach ($products as $product) {
  155. if (productHasCategory($product, $category['id'])) {
  156. $productCount++;
  157. }
  158. }
  159. ?>
  160. <tr>
  161. <td data-label="Name"><?php echo htmlspecialchars($category['label']); ?></td>
  162. <td data-label="ID"><code><?php echo htmlspecialchars($category['id']); ?></code></td>
  163. <td data-label="Produkte"><?php echo $productCount; ?></td>
  164. <td data-label="Aktionen">
  165. <a href="categories.php?edit=<?php echo urlencode($category['id']); ?>" class="btn btn-small btn-secondary">Bearbeiten</a>
  166. <form method="POST" style="display: inline;" onsubmit="return confirm('Kategorie wirklich löschen?');">
  167. <input type="hidden" name="category_id" value="<?php echo htmlspecialchars($category['id']); ?>">
  168. <button type="submit" name="delete_category" class="btn btn-small">Löschen</button>
  169. </form>
  170. </td>
  171. </tr>
  172. <?php endforeach; ?>
  173. </tbody>
  174. </table>
  175. </div>
  176. </div>
  177. <?php include __DIR__ . '/../includes/footer.php'; ?>