| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?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 = 'Produkte verwalten';
- $message = '';
- $messageType = '';
- // Handle product operations
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $products = getProducts();
-
- if (isset($_POST['add_product'])) {
- // Generate new ID
- $newId = 1;
- if (!empty($products)) {
- $ids = array_column($products, 'id');
- $newId = max($ids) + 1;
- }
-
- $newProduct = [
- 'id' => $newId,
- 'name' => sanitize($_POST['name']),
- 'description' => sanitize($_POST['description']),
- 'price' => (float)$_POST['price'],
- 'category' => sanitize($_POST['category']),
- 'image' => sanitize($_POST['image'])
- ];
-
- // Handle stock - per size for apparel, general for merch
- if ($newProduct['category'] === 'apparel' && !empty($_POST['sizes'])) {
- $sizes = sanitize($_POST['sizes']);
- $newProduct['sizes'] = $sizes; // Store as comma-separated string
-
- // Initialize stock_by_size
- $sizeArray = array_map('trim', explode(',', $sizes));
- $newProduct['stock_by_size'] = [];
- foreach ($sizeArray as $size) {
- $stockKey = 'stock_' . str_replace([' ', ','], '_', $size);
- $newProduct['stock_by_size'][$size] = isset($_POST[$stockKey]) ? (int)$_POST[$stockKey] : 0;
- }
- } else {
- $newProduct['stock'] = (int)$_POST['stock'];
- }
-
- $products[] = $newProduct;
- saveProducts($products);
- $message = 'Produkt erfolgreich hinzugefügt.';
- $messageType = 'success';
- }
-
- if (isset($_POST['update_product'])) {
- $productId = (int)$_POST['product_id'];
- foreach ($products as &$product) {
- if ($product['id'] == $productId) {
- $product['name'] = sanitize($_POST['name']);
- $product['description'] = sanitize($_POST['description']);
- $product['price'] = (float)$_POST['price'];
- $product['category'] = sanitize($_POST['category']);
- $product['image'] = sanitize($_POST['image']);
-
- // Update stock - per size for apparel, general for merch
- if ($product['category'] === 'apparel') {
- if (!empty($_POST['sizes'])) {
- $product['sizes'] = sanitize($_POST['sizes']);
-
- // Update stock_by_size
- $sizeArray = array_map('trim', explode(',', $product['sizes']));
- if (!isset($product['stock_by_size']) || !is_array($product['stock_by_size'])) {
- $product['stock_by_size'] = [];
- }
- foreach ($sizeArray as $size) {
- $stockKey = 'stock_' . str_replace([' ', ','], '_', $size);
- $product['stock_by_size'][$size] = isset($_POST[$stockKey]) ? (int)$_POST[$stockKey] : (isset($product['stock_by_size'][$size]) ? $product['stock_by_size'][$size] : 0);
- }
- // Remove sizes that are no longer in the list
- $product['stock_by_size'] = array_intersect_key($product['stock_by_size'], array_flip($sizeArray));
- unset($product['stock']); // Remove general stock for apparel
- } else {
- unset($product['sizes']);
- unset($product['stock_by_size']);
- }
- } else {
- $product['stock'] = (int)$_POST['stock'];
- unset($product['sizes']);
- unset($product['stock_by_size']);
- }
- break;
- }
- }
- saveProducts($products);
- $message = 'Produkt erfolgreich aktualisiert.';
- $messageType = 'success';
- }
-
- if (isset($_POST['delete_product'])) {
- $productId = (int)$_POST['product_id'];
- $products = array_filter($products, function($product) use ($productId) {
- return $product['id'] != $productId;
- });
- $products = array_values($products); // Re-index
- saveProducts($products);
- $message = 'Produkt erfolgreich gelöscht.';
- $messageType = 'success';
- }
- }
- $products = getProducts();
- $editingProduct = null;
- if (isset($_GET['edit'])) {
- $editingProduct = getProductById((int)$_GET['edit']);
- }
- $bodyClass = 'admin-page';
- include __DIR__ . '/../includes/header.php';
- ?>
- <div class="admin-header">
- <h2>Produkte 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 ($editingProduct): ?>
- <div class="panel" style="padding: 2rem;">
- <h3>Produkt bearbeiten</h3>
- <form method="POST">
- <input type="hidden" name="product_id" value="<?php echo $editingProduct['id']; ?>">
- <div class="form-group">
- <label for="name">Name *</label>
- <input type="text" id="name" name="name" required value="<?php echo htmlspecialchars($editingProduct['name']); ?>">
- </div>
- <div class="form-group">
- <label for="description">Beschreibung</label>
- <textarea id="description" name="description" rows="4"><?php echo htmlspecialchars($editingProduct['description']); ?></textarea>
- </div>
- <div class="form-group">
- <label for="price">Preis (€) *</label>
- <input type="number" id="price" name="price" step="0.01" min="0" required value="<?php echo $editingProduct['price']; ?>">
- </div>
- <div class="form-group">
- <label for="category">Kategorie *</label>
- <select id="category" name="category" required onchange="toggleStockFields()">
- <option value="apparel" <?php echo $editingProduct['category'] === 'apparel' ? 'selected' : ''; ?>>Bekleidung</option>
- <option value="merch" <?php echo $editingProduct['category'] === 'merch' ? 'selected' : ''; ?>>Merchandise</option>
- </select>
- </div>
- <div class="form-group" id="stock-group" style="<?php echo $editingProduct['category'] === 'merch' ? '' : 'display: none;'; ?>">
- <label for="stock">Lagerbestand *</label>
- <input type="number" id="stock" name="stock" min="0" value="<?php echo isset($editingProduct['stock']) ? (int)$editingProduct['stock'] : 0; ?>">
- </div>
- <div class="form-group" id="sizes-group" style="<?php echo $editingProduct['category'] === 'apparel' ? '' : 'display: none;'; ?>">
- <label for="sizes">Größen (kommagetrennt, z.B. S,M,L,XL,XXL)</label>
- <input type="text" id="sizes" name="sizes" value="<?php echo isset($editingProduct['sizes']) ? htmlspecialchars($editingProduct['sizes']) : ''; ?>" placeholder="S,M,L,XL" onchange="updateSizeStockFields()">
- </div>
- <div id="size-stock-group" style="<?php echo $editingProduct['category'] === 'apparel' ? '' : 'display: none;'; ?>">
- <?php if ($editingProduct['category'] === 'apparel' && !empty($editingProduct['sizes'])): ?>
- <?php
- $sizes = array_map('trim', explode(',', $editingProduct['sizes']));
- $stockBySize = isset($editingProduct['stock_by_size']) && is_array($editingProduct['stock_by_size']) ? $editingProduct['stock_by_size'] : [];
- foreach ($sizes as $size):
- ?>
- <div class="form-group">
- <label>Lagerbestand für Größe "<?php echo htmlspecialchars($size); ?>" *</label>
- <input type="number" name="stock_<?php echo htmlspecialchars(str_replace([' ', ','], '_', $size)); ?>" min="0" value="<?php echo isset($stockBySize[$size]) ? (int)$stockBySize[$size] : 0; ?>" required>
- </div>
- <?php endforeach; ?>
- <?php endif; ?>
- </div>
- <div class="form-group">
- <label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
- <input type="text" id="image" name="image" value="<?php echo htmlspecialchars($editingProduct['image']); ?>">
- </div>
- <script>
- function toggleStockFields() {
- const category = document.getElementById('category').value;
- const stockGroup = document.getElementById('stock-group');
- const sizesGroup = document.getElementById('sizes-group');
- const sizeStockGroup = document.getElementById('size-stock-group');
-
- if (category === 'apparel') {
- stockGroup.style.display = 'none';
- sizesGroup.style.display = 'block';
- sizeStockGroup.style.display = 'block';
- updateSizeStockFields();
- } else {
- stockGroup.style.display = 'block';
- sizesGroup.style.display = 'none';
- sizeStockGroup.style.display = 'none';
- }
- }
-
- function updateSizeStockFields() {
- const sizesInput = document.getElementById('sizes');
- const sizeStockGroup = document.getElementById('size-stock-group');
- const sizes = sizesInput.value.split(',').map(s => s.trim()).filter(s => s);
-
- let html = '';
- sizes.forEach(size => {
- const safeName = size.replace(/[ ,]/g, '_');
- html += '<div class="form-group">';
- html += '<label>Lagerbestand für Größe "' + size + '" *</label>';
- html += '<input type="number" name="stock_' + safeName + '" min="0" value="0" required>';
- html += '</div>';
- });
- sizeStockGroup.innerHTML = html;
- }
- </script>
- <button type="submit" name="update_product" class="btn">Produkt aktualisieren</button>
- <a href="products.php" class="btn btn-secondary">Abbrechen</a>
- </form>
- </div>
- <?php else: ?>
- <div class="panel" style="padding: 2rem;">
- <h3>Neues Produkt hinzufügen</h3>
- <form method="POST">
- <div class="form-group">
- <label for="name">Name *</label>
- <input type="text" id="name" name="name" required>
- </div>
- <div class="form-group">
- <label for="description">Beschreibung</label>
- <textarea id="description" name="description" rows="4"></textarea>
- </div>
- <div class="form-group">
- <label for="price">Preis (€) *</label>
- <input type="number" id="price" name="price" step="0.01" min="0" required>
- </div>
- <div class="form-group">
- <label for="category">Kategorie *</label>
- <select id="category" name="category" required onchange="toggleStockFields()">
- <option value="apparel">Bekleidung</option>
- <option value="merch">Merchandise</option>
- </select>
- </div>
- <div class="form-group" id="stock-group" style="display: none;">
- <label for="stock">Lagerbestand *</label>
- <input type="number" id="stock" name="stock" min="0" value="0">
- </div>
- <div class="form-group" id="sizes-group" style="display: none;">
- <label for="sizes">Größen (kommagetrennt, z.B. S,M,L,XL,XXL)</label>
- <input type="text" id="sizes" name="sizes" placeholder="S,M,L,XL" onchange="updateSizeStockFields()">
- </div>
- <div id="size-stock-group" style="display: none;"></div>
- <div class="form-group">
- <label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
- <input type="text" id="image" name="image">
- </div>
- <script>
- function toggleStockFields() {
- const category = document.getElementById('category').value;
- const stockGroup = document.getElementById('stock-group');
- const sizesGroup = document.getElementById('sizes-group');
- const sizeStockGroup = document.getElementById('size-stock-group');
-
- if (category === 'apparel') {
- stockGroup.style.display = 'none';
- sizesGroup.style.display = 'block';
- sizeStockGroup.style.display = 'block';
- updateSizeStockFields();
- } else {
- stockGroup.style.display = 'block';
- sizesGroup.style.display = 'none';
- sizeStockGroup.style.display = 'none';
- }
- }
-
- function updateSizeStockFields() {
- const sizesInput = document.getElementById('sizes');
- const sizeStockGroup = document.getElementById('size-stock-group');
- const sizes = sizesInput.value.split(',').map(s => s.trim()).filter(s => s);
-
- let html = '';
- sizes.forEach(size => {
- const safeName = size.replace(/[ ,]/g, '_');
- html += '<div class="form-group">';
- html += '<label>Lagerbestand für Größe "' + size + '" *</label>';
- html += '<input type="number" name="stock_' + safeName + '" min="0" value="0" required>';
- html += '</div>';
- });
- sizeStockGroup.innerHTML = html;
- }
- </script>
- <button type="submit" name="add_product" class="btn">Produkt hinzufügen</button>
- </form>
- </div>
- <?php endif; ?>
- <h3>Alle Produkte</h3>
- <?php if (empty($products)): ?>
- <p>Keine Produkte vorhanden.</p>
- <?php else: ?>
- <table class="responsive-table">
- <thead>
- <tr>
- <th>ID</th>
- <th>Name</th>
- <th>Kategorie</th>
- <th>Preis</th>
- <th>Lagerbestand</th>
- <th>Aktionen</th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($products as $product): ?>
- <tr>
- <td data-label="ID"><?php echo $product['id']; ?></td>
- <td data-label="Name"><?php echo htmlspecialchars($product['name']); ?></td>
- <td data-label="Kategorie"><?php echo $product['category'] === 'apparel' ? 'Bekleidung' : 'Merchandise'; ?></td>
- <td data-label="Preis"><?php echo formatPrice($product['price']); ?></td>
- <td data-label="Lagerbestand">
- <?php
- if ($product['category'] === 'apparel' && isset($product['stock_by_size']) && is_array($product['stock_by_size'])) {
- $stockInfo = [];
- foreach ($product['stock_by_size'] as $size => $stock) {
- $stockInfo[] = "$size: $stock";
- }
- echo implode(', ', $stockInfo) ?: '0';
- } else {
- echo isset($product['stock']) ? (int)$product['stock'] : 0;
- }
- ?>
- </td>
- <td data-label="Aktionen">
- <a href="?edit=<?php echo $product['id']; ?>" class="btn btn-small">Bearbeiten</a>
- <form method="POST" style="display: inline;" onsubmit="return confirm('Möchten Sie dieses Produkt wirklich löschen?');">
- <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
- <button type="submit" name="delete_product" class="btn btn-secondary btn-small">Löschen</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php endif; ?>
- <?php include __DIR__ . '/../includes/footer.php'; ?>
|