products.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. include __DIR__ . '/../includes/header.php';
  107. ?>
  108. <div class="admin-header">
  109. <h2>Produkte verwalten</h2>
  110. <div>
  111. <a href="index.php" class="btn btn-secondary">Zurück zum Dashboard</a>
  112. </div>
  113. </div>
  114. <?php if ($message): ?>
  115. <div class="alert alert-<?php echo $messageType; ?>">
  116. <?php echo htmlspecialchars($message); ?>
  117. </div>
  118. <?php endif; ?>
  119. <?php if ($editingProduct): ?>
  120. <div style="background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 2rem;">
  121. <h3>Produkt bearbeiten</h3>
  122. <form method="POST">
  123. <input type="hidden" name="product_id" value="<?php echo $editingProduct['id']; ?>">
  124. <div class="form-group">
  125. <label for="name">Name *</label>
  126. <input type="text" id="name" name="name" required value="<?php echo htmlspecialchars($editingProduct['name']); ?>">
  127. </div>
  128. <div class="form-group">
  129. <label for="description">Beschreibung</label>
  130. <textarea id="description" name="description" rows="4"><?php echo htmlspecialchars($editingProduct['description']); ?></textarea>
  131. </div>
  132. <div class="form-group">
  133. <label for="price">Preis (€) *</label>
  134. <input type="number" id="price" name="price" step="0.01" min="0" required value="<?php echo $editingProduct['price']; ?>">
  135. </div>
  136. <div class="form-group">
  137. <label for="category">Kategorie *</label>
  138. <select id="category" name="category" required onchange="toggleStockFields()">
  139. <option value="apparel" <?php echo $editingProduct['category'] === 'apparel' ? 'selected' : ''; ?>>Bekleidung</option>
  140. <option value="merch" <?php echo $editingProduct['category'] === 'merch' ? 'selected' : ''; ?>>Merchandise</option>
  141. </select>
  142. </div>
  143. <div class="form-group" id="stock-group" style="<?php echo $editingProduct['category'] === 'merch' ? '' : 'display: none;'; ?>">
  144. <label for="stock">Lagerbestand *</label>
  145. <input type="number" id="stock" name="stock" min="0" value="<?php echo isset($editingProduct['stock']) ? (int)$editingProduct['stock'] : 0; ?>">
  146. </div>
  147. <div class="form-group" id="sizes-group" style="<?php echo $editingProduct['category'] === 'apparel' ? '' : 'display: none;'; ?>">
  148. <label for="sizes">Größen (kommagetrennt, z.B. S,M,L,XL,XXL)</label>
  149. <input type="text" id="sizes" name="sizes" value="<?php echo isset($editingProduct['sizes']) ? htmlspecialchars($editingProduct['sizes']) : ''; ?>" placeholder="S,M,L,XL" onchange="updateSizeStockFields()">
  150. </div>
  151. <div id="size-stock-group" style="<?php echo $editingProduct['category'] === 'apparel' ? '' : 'display: none;'; ?>">
  152. <?php if ($editingProduct['category'] === 'apparel' && !empty($editingProduct['sizes'])): ?>
  153. <?php
  154. $sizes = array_map('trim', explode(',', $editingProduct['sizes']));
  155. $stockBySize = isset($editingProduct['stock_by_size']) && is_array($editingProduct['stock_by_size']) ? $editingProduct['stock_by_size'] : [];
  156. foreach ($sizes as $size):
  157. ?>
  158. <div class="form-group">
  159. <label>Lagerbestand für Größe "<?php echo htmlspecialchars($size); ?>" *</label>
  160. <input type="number" name="stock_<?php echo htmlspecialchars(str_replace([' ', ','], '_', $size)); ?>" min="0" value="<?php echo isset($stockBySize[$size]) ? (int)$stockBySize[$size] : 0; ?>" required>
  161. </div>
  162. <?php endforeach; ?>
  163. <?php endif; ?>
  164. </div>
  165. <div class="form-group">
  166. <label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
  167. <input type="text" id="image" name="image" value="<?php echo htmlspecialchars($editingProduct['image']); ?>">
  168. </div>
  169. <script>
  170. function toggleStockFields() {
  171. const category = document.getElementById('category').value;
  172. const stockGroup = document.getElementById('stock-group');
  173. const sizesGroup = document.getElementById('sizes-group');
  174. const sizeStockGroup = document.getElementById('size-stock-group');
  175. if (category === 'apparel') {
  176. stockGroup.style.display = 'none';
  177. sizesGroup.style.display = 'block';
  178. sizeStockGroup.style.display = 'block';
  179. updateSizeStockFields();
  180. } else {
  181. stockGroup.style.display = 'block';
  182. sizesGroup.style.display = 'none';
  183. sizeStockGroup.style.display = 'none';
  184. }
  185. }
  186. function updateSizeStockFields() {
  187. const sizesInput = document.getElementById('sizes');
  188. const sizeStockGroup = document.getElementById('size-stock-group');
  189. const sizes = sizesInput.value.split(',').map(s => s.trim()).filter(s => s);
  190. let html = '';
  191. sizes.forEach(size => {
  192. const safeName = size.replace(/[ ,]/g, '_');
  193. html += '<div class="form-group">';
  194. html += '<label>Lagerbestand für Größe "' + size + '" *</label>';
  195. html += '<input type="number" name="stock_' + safeName + '" min="0" value="0" required>';
  196. html += '</div>';
  197. });
  198. sizeStockGroup.innerHTML = html;
  199. }
  200. </script>
  201. <button type="submit" name="update_product" class="btn">Produkt aktualisieren</button>
  202. <a href="products.php" class="btn btn-secondary">Abbrechen</a>
  203. </form>
  204. </div>
  205. <?php else: ?>
  206. <div style="background: white; padding: 2rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 2rem;">
  207. <h3>Neues Produkt hinzufügen</h3>
  208. <form method="POST">
  209. <div class="form-group">
  210. <label for="name">Name *</label>
  211. <input type="text" id="name" name="name" required>
  212. </div>
  213. <div class="form-group">
  214. <label for="description">Beschreibung</label>
  215. <textarea id="description" name="description" rows="4"></textarea>
  216. </div>
  217. <div class="form-group">
  218. <label for="price">Preis (€) *</label>
  219. <input type="number" id="price" name="price" step="0.01" min="0" required>
  220. </div>
  221. <div class="form-group">
  222. <label for="category">Kategorie *</label>
  223. <select id="category" name="category" required onchange="toggleStockFields()">
  224. <option value="apparel">Bekleidung</option>
  225. <option value="merch">Merchandise</option>
  226. </select>
  227. </div>
  228. <div class="form-group" id="stock-group" style="display: none;">
  229. <label for="stock">Lagerbestand *</label>
  230. <input type="number" id="stock" name="stock" min="0" value="0">
  231. </div>
  232. <div class="form-group" id="sizes-group" style="display: none;">
  233. <label for="sizes">Größen (kommagetrennt, z.B. S,M,L,XL,XXL)</label>
  234. <input type="text" id="sizes" name="sizes" placeholder="S,M,L,XL" onchange="updateSizeStockFields()">
  235. </div>
  236. <div id="size-stock-group" style="display: none;"></div>
  237. <div class="form-group">
  238. <label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
  239. <input type="text" id="image" name="image">
  240. </div>
  241. <script>
  242. function toggleStockFields() {
  243. const category = document.getElementById('category').value;
  244. const stockGroup = document.getElementById('stock-group');
  245. const sizesGroup = document.getElementById('sizes-group');
  246. const sizeStockGroup = document.getElementById('size-stock-group');
  247. if (category === 'apparel') {
  248. stockGroup.style.display = 'none';
  249. sizesGroup.style.display = 'block';
  250. sizeStockGroup.style.display = 'block';
  251. updateSizeStockFields();
  252. } else {
  253. stockGroup.style.display = 'block';
  254. sizesGroup.style.display = 'none';
  255. sizeStockGroup.style.display = 'none';
  256. }
  257. }
  258. function updateSizeStockFields() {
  259. const sizesInput = document.getElementById('sizes');
  260. const sizeStockGroup = document.getElementById('size-stock-group');
  261. const sizes = sizesInput.value.split(',').map(s => s.trim()).filter(s => s);
  262. let html = '';
  263. sizes.forEach(size => {
  264. const safeName = size.replace(/[ ,]/g, '_');
  265. html += '<div class="form-group">';
  266. html += '<label>Lagerbestand für Größe "' + size + '" *</label>';
  267. html += '<input type="number" name="stock_' + safeName + '" min="0" value="0" required>';
  268. html += '</div>';
  269. });
  270. sizeStockGroup.innerHTML = html;
  271. }
  272. </script>
  273. <button type="submit" name="add_product" class="btn">Produkt hinzufügen</button>
  274. </form>
  275. </div>
  276. <?php endif; ?>
  277. <h3>Alle Produkte</h3>
  278. <?php if (empty($products)): ?>
  279. <p>Keine Produkte vorhanden.</p>
  280. <?php else: ?>
  281. <table>
  282. <thead>
  283. <tr>
  284. <th>ID</th>
  285. <th>Name</th>
  286. <th>Kategorie</th>
  287. <th>Preis</th>
  288. <th>Lagerbestand</th>
  289. <th>Aktionen</th>
  290. </tr>
  291. </thead>
  292. <tbody>
  293. <?php foreach ($products as $product): ?>
  294. <tr>
  295. <td><?php echo $product['id']; ?></td>
  296. <td><?php echo htmlspecialchars($product['name']); ?></td>
  297. <td><?php echo $product['category'] === 'apparel' ? 'Bekleidung' : 'Merchandise'; ?></td>
  298. <td><?php echo formatPrice($product['price']); ?></td>
  299. <td>
  300. <?php
  301. if ($product['category'] === 'apparel' && isset($product['stock_by_size']) && is_array($product['stock_by_size'])) {
  302. $stockInfo = [];
  303. foreach ($product['stock_by_size'] as $size => $stock) {
  304. $stockInfo[] = "$size: $stock";
  305. }
  306. echo implode(', ', $stockInfo) ?: '0';
  307. } else {
  308. echo isset($product['stock']) ? (int)$product['stock'] : 0;
  309. }
  310. ?>
  311. </td>
  312. <td>
  313. <a href="?edit=<?php echo $product['id']; ?>" class="btn btn-small">Bearbeiten</a>
  314. <form method="POST" style="display: inline;" onsubmit="return confirm('Möchten Sie dieses Produkt wirklich löschen?');">
  315. <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
  316. <button type="submit" name="delete_product" class="btn btn-secondary btn-small">Löschen</button>
  317. </form>
  318. </td>
  319. </tr>
  320. <?php endforeach; ?>
  321. </tbody>
  322. </table>
  323. <?php endif; ?>
  324. <?php include __DIR__ . '/../includes/footer.php'; ?>