products.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 = 'Produkte verwalten';
  10. $message = '';
  11. $messageType = '';
  12. // Handle product operations
  13. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  14. $products = getProducts();
  15. if (isset($_POST['add_product'])) {
  16. // Generate new ID
  17. $newId = 1;
  18. if (!empty($products)) {
  19. $ids = array_column($products, 'id');
  20. $newId = max($ids) + 1;
  21. }
  22. $newProduct = [
  23. 'id' => $newId,
  24. 'name' => sanitize($_POST['name']),
  25. 'description' => sanitize($_POST['description']),
  26. 'price' => (float)$_POST['price'],
  27. 'category' => sanitize($_POST['category']),
  28. 'image' => sanitize($_POST['image'])
  29. ];
  30. // Handle stock - per size for apparel, general for merch
  31. if ($newProduct['category'] === 'apparel' && !empty($_POST['sizes'])) {
  32. $sizes = sanitize($_POST['sizes']);
  33. $newProduct['sizes'] = $sizes; // Store as comma-separated string
  34. // Initialize stock_by_size
  35. $sizeArray = array_map('trim', explode(',', $sizes));
  36. $newProduct['stock_by_size'] = [];
  37. foreach ($sizeArray as $size) {
  38. $stockKey = 'stock_' . str_replace([' ', ','], '_', $size);
  39. $newProduct['stock_by_size'][$size] = isset($_POST[$stockKey]) ? (int)$_POST[$stockKey] : 0;
  40. }
  41. } else {
  42. $newProduct['stock'] = (int)$_POST['stock'];
  43. }
  44. $products[] = $newProduct;
  45. saveProducts($products);
  46. $message = 'Produkt erfolgreich hinzugefügt.';
  47. $messageType = 'success';
  48. }
  49. if (isset($_POST['update_product'])) {
  50. $productId = (int)$_POST['product_id'];
  51. foreach ($products as &$product) {
  52. if ($product['id'] == $productId) {
  53. $product['name'] = sanitize($_POST['name']);
  54. $product['description'] = sanitize($_POST['description']);
  55. $product['price'] = (float)$_POST['price'];
  56. $product['category'] = sanitize($_POST['category']);
  57. $product['image'] = sanitize($_POST['image']);
  58. // Update stock - per size for apparel, general for merch
  59. if ($product['category'] === 'apparel') {
  60. if (!empty($_POST['sizes'])) {
  61. $product['sizes'] = sanitize($_POST['sizes']);
  62. // Update stock_by_size
  63. $sizeArray = array_map('trim', explode(',', $product['sizes']));
  64. if (!isset($product['stock_by_size']) || !is_array($product['stock_by_size'])) {
  65. $product['stock_by_size'] = [];
  66. }
  67. foreach ($sizeArray as $size) {
  68. $stockKey = 'stock_' . str_replace([' ', ','], '_', $size);
  69. $product['stock_by_size'][$size] = isset($_POST[$stockKey]) ? (int)$_POST[$stockKey] : (isset($product['stock_by_size'][$size]) ? $product['stock_by_size'][$size] : 0);
  70. }
  71. // Remove sizes that are no longer in the list
  72. $product['stock_by_size'] = array_intersect_key($product['stock_by_size'], array_flip($sizeArray));
  73. unset($product['stock']); // Remove general stock for apparel
  74. } else {
  75. unset($product['sizes']);
  76. unset($product['stock_by_size']);
  77. }
  78. } else {
  79. $product['stock'] = (int)$_POST['stock'];
  80. unset($product['sizes']);
  81. unset($product['stock_by_size']);
  82. }
  83. break;
  84. }
  85. }
  86. saveProducts($products);
  87. $message = 'Produkt erfolgreich aktualisiert.';
  88. $messageType = 'success';
  89. }
  90. if (isset($_POST['delete_product'])) {
  91. $productId = (int)$_POST['product_id'];
  92. $products = array_filter($products, function($product) use ($productId) {
  93. return $product['id'] != $productId;
  94. });
  95. $products = array_values($products); // Re-index
  96. saveProducts($products);
  97. $message = 'Produkt erfolgreich gelöscht.';
  98. $messageType = 'success';
  99. }
  100. }
  101. $products = getProducts();
  102. $editingProduct = null;
  103. if (isset($_GET['edit'])) {
  104. $editingProduct = getProductById((int)$_GET['edit']);
  105. }
  106. $bodyClass = 'admin-page';
  107. include __DIR__ . '/../includes/header.php';
  108. ?>
  109. <div class="admin-header">
  110. <h2>Produkte verwalten</h2>
  111. <div>
  112. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  113. </div>
  114. </div>
  115. <?php if ($message): ?>
  116. <div class="alert alert-<?php echo $messageType; ?>">
  117. <?php echo htmlspecialchars($message); ?>
  118. </div>
  119. <?php endif; ?>
  120. <?php if ($editingProduct): ?>
  121. <div class="panel" style="padding: 2rem;">
  122. <h3>Produkt bearbeiten</h3>
  123. <form method="POST">
  124. <input type="hidden" name="product_id" value="<?php echo $editingProduct['id']; ?>">
  125. <div class="form-group">
  126. <label for="name">Name *</label>
  127. <input type="text" id="name" name="name" required value="<?php echo htmlspecialchars($editingProduct['name']); ?>">
  128. </div>
  129. <div class="form-group">
  130. <label for="description">Beschreibung</label>
  131. <textarea id="description" name="description" rows="4"><?php echo htmlspecialchars($editingProduct['description']); ?></textarea>
  132. </div>
  133. <div class="form-group">
  134. <label for="price">Preis (€) *</label>
  135. <input type="number" id="price" name="price" step="0.01" min="0" required value="<?php echo $editingProduct['price']; ?>">
  136. </div>
  137. <div class="form-group">
  138. <label for="category">Kategorie *</label>
  139. <select id="category" name="category" required onchange="toggleStockFields()">
  140. <option value="apparel" <?php echo $editingProduct['category'] === 'apparel' ? 'selected' : ''; ?>>Bekleidung</option>
  141. <option value="merch" <?php echo $editingProduct['category'] === 'merch' ? 'selected' : ''; ?>>Merchandise</option>
  142. </select>
  143. </div>
  144. <div class="form-group" id="stock-group" style="<?php echo $editingProduct['category'] === 'merch' ? '' : 'display: none;'; ?>">
  145. <label for="stock">Lagerbestand *</label>
  146. <input type="number" id="stock" name="stock" min="0" value="<?php echo isset($editingProduct['stock']) ? (int)$editingProduct['stock'] : 0; ?>">
  147. </div>
  148. <div class="form-group" id="sizes-group" style="<?php echo $editingProduct['category'] === 'apparel' ? '' : 'display: none;'; ?>">
  149. <label for="sizes">Größen (kommagetrennt, z.B. S,M,L,XL,XXL)</label>
  150. <input type="text" id="sizes" name="sizes" value="<?php echo isset($editingProduct['sizes']) ? htmlspecialchars($editingProduct['sizes']) : ''; ?>" placeholder="S,M,L,XL" onchange="updateSizeStockFields()">
  151. </div>
  152. <div id="size-stock-group" style="<?php echo $editingProduct['category'] === 'apparel' ? '' : 'display: none;'; ?>">
  153. <?php if ($editingProduct['category'] === 'apparel' && !empty($editingProduct['sizes'])): ?>
  154. <?php
  155. $sizes = array_map('trim', explode(',', $editingProduct['sizes']));
  156. $stockBySize = isset($editingProduct['stock_by_size']) && is_array($editingProduct['stock_by_size']) ? $editingProduct['stock_by_size'] : [];
  157. foreach ($sizes as $size):
  158. ?>
  159. <div class="form-group">
  160. <label>Lagerbestand für Größe "<?php echo htmlspecialchars($size); ?>" *</label>
  161. <input type="number" name="stock_<?php echo htmlspecialchars(str_replace([' ', ','], '_', $size)); ?>" min="0" value="<?php echo isset($stockBySize[$size]) ? (int)$stockBySize[$size] : 0; ?>" required>
  162. </div>
  163. <?php endforeach; ?>
  164. <?php endif; ?>
  165. </div>
  166. <div class="form-group">
  167. <label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
  168. <input type="text" id="image" name="image" value="<?php echo htmlspecialchars($editingProduct['image']); ?>">
  169. </div>
  170. <script>
  171. function toggleStockFields() {
  172. const category = document.getElementById('category').value;
  173. const stockGroup = document.getElementById('stock-group');
  174. const sizesGroup = document.getElementById('sizes-group');
  175. const sizeStockGroup = document.getElementById('size-stock-group');
  176. if (category === 'apparel') {
  177. stockGroup.style.display = 'none';
  178. sizesGroup.style.display = 'block';
  179. sizeStockGroup.style.display = 'block';
  180. updateSizeStockFields();
  181. } else {
  182. stockGroup.style.display = 'block';
  183. sizesGroup.style.display = 'none';
  184. sizeStockGroup.style.display = 'none';
  185. }
  186. }
  187. function updateSizeStockFields() {
  188. const sizesInput = document.getElementById('sizes');
  189. const sizeStockGroup = document.getElementById('size-stock-group');
  190. const sizes = sizesInput.value.split(',').map(s => s.trim()).filter(s => s);
  191. let html = '';
  192. sizes.forEach(size => {
  193. const safeName = size.replace(/[ ,]/g, '_');
  194. html += '<div class="form-group">';
  195. html += '<label>Lagerbestand für Größe "' + size + '" *</label>';
  196. html += '<input type="number" name="stock_' + safeName + '" min="0" value="0" required>';
  197. html += '</div>';
  198. });
  199. sizeStockGroup.innerHTML = html;
  200. }
  201. </script>
  202. <button type="submit" name="update_product" class="btn">Produkt aktualisieren</button>
  203. <a href="products.php" class="btn btn-secondary">Abbrechen</a>
  204. </form>
  205. </div>
  206. <?php else: ?>
  207. <div class="panel" style="padding: 2rem;">
  208. <h3>Neues Produkt hinzufügen</h3>
  209. <form method="POST">
  210. <div class="form-group">
  211. <label for="name">Name *</label>
  212. <input type="text" id="name" name="name" required>
  213. </div>
  214. <div class="form-group">
  215. <label for="description">Beschreibung</label>
  216. <textarea id="description" name="description" rows="4"></textarea>
  217. </div>
  218. <div class="form-group">
  219. <label for="price">Preis (€) *</label>
  220. <input type="number" id="price" name="price" step="0.01" min="0" required>
  221. </div>
  222. <div class="form-group">
  223. <label for="category">Kategorie *</label>
  224. <select id="category" name="category" required onchange="toggleStockFields()">
  225. <option value="apparel">Bekleidung</option>
  226. <option value="merch">Merchandise</option>
  227. </select>
  228. </div>
  229. <div class="form-group" id="stock-group" style="display: none;">
  230. <label for="stock">Lagerbestand *</label>
  231. <input type="number" id="stock" name="stock" min="0" value="0">
  232. </div>
  233. <div class="form-group" id="sizes-group" style="display: none;">
  234. <label for="sizes">Größen (kommagetrennt, z.B. S,M,L,XL,XXL)</label>
  235. <input type="text" id="sizes" name="sizes" placeholder="S,M,L,XL" onchange="updateSizeStockFields()">
  236. </div>
  237. <div id="size-stock-group" style="display: none;"></div>
  238. <div class="form-group">
  239. <label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
  240. <input type="text" id="image" name="image">
  241. </div>
  242. <script>
  243. function toggleStockFields() {
  244. const category = document.getElementById('category').value;
  245. const stockGroup = document.getElementById('stock-group');
  246. const sizesGroup = document.getElementById('sizes-group');
  247. const sizeStockGroup = document.getElementById('size-stock-group');
  248. if (category === 'apparel') {
  249. stockGroup.style.display = 'none';
  250. sizesGroup.style.display = 'block';
  251. sizeStockGroup.style.display = 'block';
  252. updateSizeStockFields();
  253. } else {
  254. stockGroup.style.display = 'block';
  255. sizesGroup.style.display = 'none';
  256. sizeStockGroup.style.display = 'none';
  257. }
  258. }
  259. function updateSizeStockFields() {
  260. const sizesInput = document.getElementById('sizes');
  261. const sizeStockGroup = document.getElementById('size-stock-group');
  262. const sizes = sizesInput.value.split(',').map(s => s.trim()).filter(s => s);
  263. let html = '';
  264. sizes.forEach(size => {
  265. const safeName = size.replace(/[ ,]/g, '_');
  266. html += '<div class="form-group">';
  267. html += '<label>Lagerbestand für Größe "' + size + '" *</label>';
  268. html += '<input type="number" name="stock_' + safeName + '" min="0" value="0" required>';
  269. html += '</div>';
  270. });
  271. sizeStockGroup.innerHTML = html;
  272. }
  273. </script>
  274. <button type="submit" name="add_product" class="btn">Produkt hinzufügen</button>
  275. </form>
  276. </div>
  277. <?php endif; ?>
  278. <h3>Alle Produkte</h3>
  279. <?php if (empty($products)): ?>
  280. <p>Keine Produkte vorhanden.</p>
  281. <?php else: ?>
  282. <table class="responsive-table">
  283. <thead>
  284. <tr>
  285. <th>ID</th>
  286. <th>Name</th>
  287. <th>Kategorie</th>
  288. <th>Preis</th>
  289. <th>Lagerbestand</th>
  290. <th>Aktionen</th>
  291. </tr>
  292. </thead>
  293. <tbody>
  294. <?php foreach ($products as $product): ?>
  295. <tr>
  296. <td data-label="ID"><?php echo $product['id']; ?></td>
  297. <td data-label="Name"><?php echo htmlspecialchars($product['name']); ?></td>
  298. <td data-label="Kategorie"><?php echo $product['category'] === 'apparel' ? 'Bekleidung' : 'Merchandise'; ?></td>
  299. <td data-label="Preis"><?php echo formatPrice($product['price']); ?></td>
  300. <td data-label="Lagerbestand">
  301. <?php
  302. if ($product['category'] === 'apparel' && isset($product['stock_by_size']) && is_array($product['stock_by_size'])) {
  303. $stockInfo = [];
  304. foreach ($product['stock_by_size'] as $size => $stock) {
  305. $stockInfo[] = "$size: $stock";
  306. }
  307. echo implode(', ', $stockInfo) ?: '0';
  308. } else {
  309. echo isset($product['stock']) ? (int)$product['stock'] : 0;
  310. }
  311. ?>
  312. </td>
  313. <td data-label="Aktionen">
  314. <a href="?edit=<?php echo $product['id']; ?>" class="btn btn-small">Bearbeiten</a>
  315. <form method="POST" style="display: inline;" onsubmit="return confirm('Möchten Sie dieses Produkt wirklich löschen?');">
  316. <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
  317. <button type="submit" name="delete_product" class="btn btn-secondary btn-small">Löschen</button>
  318. </form>
  319. </td>
  320. </tr>
  321. <?php endforeach; ?>
  322. </tbody>
  323. </table>
  324. <?php endif; ?>
  325. <?php include __DIR__ . '/../includes/footer.php'; ?>