categories.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. // Validate CSRF token
  16. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  17. $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  18. $messageType = "error";
  19. } else {
  20. if (isset($_POST['add_category'])) {
  21. $label = normalizeCategoryLabel($_POST['label'] ?? "");
  22. if (!isValidCategoryLabel($label)) {
  23. $message =
  24. "Bitte geben Sie einen Kategorienamen mit maximal 80 Zeichen ein.";
  25. $messageType = "error";
  26. } else {
  27. $categoryId = generateCategoryIdFromLabel($label, $categories);
  28. $categories[] = [
  29. "id" => $categoryId,
  30. "label" => $label,
  31. ];
  32. if (saveCategories($categories)) {
  33. logAccess("Admin added category", [
  34. "category_id" => $categoryId,
  35. "label" => $label,
  36. ]);
  37. $message = "Kategorie wurde erfolgreich angelegt.";
  38. $messageType = "success";
  39. } else {
  40. $message = "Kategorie konnte nicht gespeichert werden.";
  41. $messageType = "error";
  42. }
  43. }
  44. }
  45. if (isset($_POST['update_category'])) {
  46. $categoryId = normalizeCategoryId($_POST['category_id'] ?? "");
  47. $label = normalizeCategoryLabel($_POST['label'] ?? "");
  48. $found = false;
  49. if (!isValidCategoryLabel($label)) {
  50. $message =
  51. "Bitte geben Sie einen Kategorienamen mit maximal 80 Zeichen ein.";
  52. $messageType = "error";
  53. } else {
  54. foreach ($categories as &$category) {
  55. if ($category["id"] === $categoryId) {
  56. $category["label"] = $label;
  57. $found = true;
  58. break;
  59. }
  60. }
  61. unset($category);
  62. if (!$found) {
  63. $message = "Kategorie nicht gefunden.";
  64. $messageType = "error";
  65. } else {
  66. if (saveCategories($categories)) {
  67. logAccess("Admin updated category", [
  68. "category_id" => $categoryId,
  69. "label" => $label,
  70. ]);
  71. $message = "Kategorie wurde erfolgreich aktualisiert.";
  72. $messageType = "success";
  73. } else {
  74. $message =
  75. "Kategorie konnte nicht gespeichert werden.";
  76. $messageType = "error";
  77. }
  78. }
  79. }
  80. }
  81. if (isset($_POST['delete_category'])) {
  82. $categoryId = normalizeCategoryId($_POST['category_id'] ?? "");
  83. $label = "";
  84. $found = false;
  85. if (isCategoryInUse($categoryId)) {
  86. $message =
  87. "Diese Kategorie wird noch von Produkten verwendet und kann nicht gelöscht werden.";
  88. $messageType = "error";
  89. } else {
  90. $categories = array_values(
  91. array_filter($categories, function ($category) use (
  92. $categoryId,
  93. &$found,
  94. &$label,
  95. ) {
  96. if ($category["id"] === $categoryId) {
  97. $found = true;
  98. $label = $category["label"];
  99. return false;
  100. }
  101. return true;
  102. }),
  103. );
  104. if (!$found) {
  105. $message = "Kategorie nicht gefunden.";
  106. $messageType = "error";
  107. } else {
  108. if (saveCategories($categories)) {
  109. logAccess("Admin deleted category", [
  110. "category_id" => $categoryId,
  111. "label" => $label,
  112. ]);
  113. $message = "Kategorie wurde gelöscht.";
  114. $messageType = "success";
  115. } else {
  116. $message = "Kategorie konnte nicht gelöscht werden.";
  117. $messageType = "error";
  118. }
  119. }
  120. }
  121. }
  122. $categories = getCategories();
  123. $products = getProducts();
  124. }
  125. }
  126. $editCategoryId = normalizeCategoryId($_GET['edit'] ?? "");
  127. $editingCategory = null;
  128. if ($editCategoryId !== "") {
  129. $editingCategory = getCategoryById($editCategoryId);
  130. if ($editingCategory === null && $message === "") {
  131. $message = "Ausgewählte Kategorie wurde nicht gefunden.";
  132. $messageType = "error";
  133. }
  134. }
  135. $bodyClass = "admin-page";
  136. include __DIR__ . "/../includes/header.php";
  137. ?>
  138. <div class="admin-header">
  139. <h2>Kategorien verwalten</h2>
  140. <div>
  141. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  142. </div>
  143. </div>
  144. <?php if ($message !== ""): ?>
  145. <div class="alert alert-<?php echo $messageType; ?>">
  146. <?php echo htmlspecialchars($message); ?>
  147. </div>
  148. <?php endif; ?>
  149. <?php if ($editingCategory !== null): ?>
  150. <div class="panel panel-lg">
  151. <h3>Kategorie bearbeiten</h3>
  152. <form method="POST">
  153. <?php echo csrfField(); ?>
  154. <input type="hidden" name="category_id" value="<?php echo htmlspecialchars(
  155. $editingCategory["id"],
  156. ); ?>">
  157. <div class="form-group">
  158. <label for="category_id_display">ID</label>
  159. <input type="text" id="category_id_display" value="<?php echo htmlspecialchars(
  160. $editingCategory["id"],
  161. ); ?>" readonly>
  162. <small>Die ID sollte gleichbleiben, damit Produktzuordnungen und Filter-URLs gültig bleiben.</small>
  163. </div>
  164. <div class="form-group">
  165. <label for="label_edit">Name *</label>
  166. <input type="text" id="label_edit" name="label" maxlength="80" required value="<?php echo htmlspecialchars(
  167. $editingCategory["label"],
  168. ); ?>">
  169. </div>
  170. <button type="submit" name="update_category" class="btn">Kategorie aktualisieren</button>
  171. <a href="categories.php" class="btn btn-secondary">Abbrechen</a>
  172. </form>
  173. </div>
  174. <?php else: ?>
  175. <div class="panel panel-lg">
  176. <h3>Neue Kategorie anlegen</h3>
  177. <form method="POST">
  178. <?php echo csrfField(); ?>
  179. <div class="form-group">
  180. <label for="label">Name *</label>
  181. <input type="text" id="label" name="label" maxlength="80" required placeholder="z.B. Accessoires">
  182. <small>Die technische ID wird einmalig automatisch aus dem Namen erzeugt.</small>
  183. </div>
  184. <button type="submit" name="add_category" class="btn">Kategorie anlegen</button>
  185. </form>
  186. </div>
  187. <?php endif; ?>
  188. <div class="panel">
  189. <h3>Kategorien</h3>
  190. <div class="table-responsive">
  191. <table class="responsive-table">
  192. <thead>
  193. <tr>
  194. <th>Name</th>
  195. <th>ID</th>
  196. <th>Produkte</th>
  197. <th>Aktionen</th>
  198. </tr>
  199. </thead>
  200. <tbody>
  201. <?php foreach ($categories as $category): ?>
  202. <?php
  203. $productCount = 0;
  204. foreach ($products as $product) {
  205. if (productHasCategory($product, $category["id"])) {
  206. $productCount++;
  207. }
  208. }
  209. ?>
  210. <tr>
  211. <td data-label="Name"><?php echo htmlspecialchars(
  212. $category["label"],
  213. ); ?></td>
  214. <td data-label="ID"><code><?php echo htmlspecialchars(
  215. $category["id"],
  216. ); ?></code></td>
  217. <td data-label="Produkte"><?php echo $productCount; ?></td>
  218. <td data-label="Aktionen">
  219. <a href="categories.php?edit=<?php echo urlencode(
  220. $category["id"],
  221. ); ?>" class="btn btn-small btn-secondary">Bearbeiten</a>
  222. <form method="POST" class="inline-form" onsubmit="return confirm('Kategorie wirklich löschen?');">
  223. <?php echo csrfField(); ?>
  224. <input type="hidden" name="category_id" value="<?php echo htmlspecialchars(
  225. $category["id"],
  226. ); ?>">
  227. <button type="submit" name="delete_category" class="btn btn-small">Löschen</button>
  228. </form>
  229. </td>
  230. </tr>
  231. <?php endforeach; ?>
  232. </tbody>
  233. </table>
  234. </div>
  235. </div>
  236. <?php include __DIR__ . "/../includes/footer.php"; ?>