|
|
@@ -11,6 +11,7 @@ if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
|
|
|
$pageTitle = 'Produkte verwalten';
|
|
|
$message = '';
|
|
|
$messageType = '';
|
|
|
+$categories = getCategories();
|
|
|
|
|
|
function handleImageUpload($fileInputName = 'image_file') {
|
|
|
if (!isset($_FILES[$fileInputName]) || $_FILES[$fileInputName]['error'] === UPLOAD_ERR_NO_FILE) {
|
|
|
@@ -42,10 +43,11 @@ function handleImageUpload($fileInputName = 'image_file') {
|
|
|
}
|
|
|
|
|
|
$safeBaseName = preg_replace('/[^a-zA-Z0-9_-]/', '-', pathinfo($originalName, PATHINFO_FILENAME));
|
|
|
- $safeBaseName = trim($safeBaseName, '-');
|
|
|
+ $safeBaseName = trim((string) $safeBaseName, '-');
|
|
|
if ($safeBaseName === '') {
|
|
|
$safeBaseName = 'bild';
|
|
|
}
|
|
|
+
|
|
|
$targetFilename = $safeBaseName . '.' . $extension;
|
|
|
$targetPath = $imagesDir . '/' . $targetFilename;
|
|
|
$counter = 1;
|
|
|
@@ -62,111 +64,151 @@ function handleImageUpload($fileInputName = 'image_file') {
|
|
|
return ['success' => true, 'filename' => $targetFilename];
|
|
|
}
|
|
|
|
|
|
+function isValidProductCategoryInput($categoryId) {
|
|
|
+ return getCategoryById($categoryId) !== null;
|
|
|
+}
|
|
|
+
|
|
|
+function getSubmittedProductCategoryIds($submittedValues) {
|
|
|
+ $selectedCategoryIds = normalizeProductCategoryIds($submittedValues['categories'] ?? []);
|
|
|
+ $validCategoryIds = [];
|
|
|
+
|
|
|
+ foreach ($selectedCategoryIds as $categoryId) {
|
|
|
+ if (isValidProductCategoryInput($categoryId)) {
|
|
|
+ $validCategoryIds[] = $categoryId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $validCategoryIds;
|
|
|
+}
|
|
|
+
|
|
|
+function buildProductSizeStock($sizesInput, $submittedValues = [], $existingValues = []) {
|
|
|
+ $sizes = getProductSizes(['sizes' => (string) $sizesInput]);
|
|
|
+ $stockBySize = [];
|
|
|
+
|
|
|
+ foreach ($sizes as $size) {
|
|
|
+ $stockKey = 'stock_' . str_replace([' ', ','], '_', $size);
|
|
|
+ if (isset($submittedValues[$stockKey])) {
|
|
|
+ $stockBySize[$size] = max(0, (int) $submittedValues[$stockKey]);
|
|
|
+ } elseif (isset($existingValues[$size])) {
|
|
|
+ $stockBySize[$size] = max(0, (int) $existingValues[$size]);
|
|
|
+ } else {
|
|
|
+ $stockBySize[$size] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'sizes' => implode(',', $sizes),
|
|
|
+ 'stock_by_size' => $stockBySize
|
|
|
+ ];
|
|
|
+}
|
|
|
+
|
|
|
// Handle product operations
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
$products = getProducts();
|
|
|
-
|
|
|
- if (isset($_POST['add_product'])) {
|
|
|
+
|
|
|
+ if (empty($categories)) {
|
|
|
+ $message = 'Es ist keine Kategorie vorhanden. Bitte zuerst Kategorien anlegen.';
|
|
|
+ $messageType = 'error';
|
|
|
+ } elseif (isset($_POST['add_product'])) {
|
|
|
$uploadResult = handleImageUpload();
|
|
|
if (!$uploadResult['success']) {
|
|
|
$message = $uploadResult['message'];
|
|
|
$messageType = 'error';
|
|
|
} else {
|
|
|
- // 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' => $uploadResult['filename'] !== null ? $uploadResult['filename'] : 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;
|
|
|
+ $categoryIds = getSubmittedProductCategoryIds($_POST);
|
|
|
+ $sizeData = buildProductSizeStock($_POST['sizes'] ?? '', $_POST);
|
|
|
+
|
|
|
+ if (empty($categoryIds)) {
|
|
|
+ $message = 'Bitte wählen Sie mindestens eine gültige Kategorie aus.';
|
|
|
+ $messageType = 'error';
|
|
|
+ } elseif ($sizeData['sizes'] === '') {
|
|
|
+ $message = 'Bitte geben Sie mindestens eine Größe ein.';
|
|
|
+ $messageType = 'error';
|
|
|
+ } else {
|
|
|
+ $newId = 1;
|
|
|
+ if (!empty($products)) {
|
|
|
+ $ids = array_column($products, 'id');
|
|
|
+ $newId = max($ids) + 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ $products[] = [
|
|
|
+ 'id' => $newId,
|
|
|
+ 'name' => sanitize($_POST['name']),
|
|
|
+ 'description' => sanitize($_POST['description']),
|
|
|
+ 'price' => (float) ($_POST['price'] ?? 0),
|
|
|
+ 'categories' => $categoryIds,
|
|
|
+ 'image' => $uploadResult['filename'] !== null ? $uploadResult['filename'] : sanitize($_POST['image']),
|
|
|
+ 'sizes' => $sizeData['sizes'],
|
|
|
+ 'stock_by_size' => $sizeData['stock_by_size']
|
|
|
+ ];
|
|
|
+
|
|
|
+ saveProducts($products);
|
|
|
+ $message = 'Produkt erfolgreich hinzugefügt.';
|
|
|
+ $messageType = 'success';
|
|
|
}
|
|
|
- } else {
|
|
|
- $newProduct['stock'] = (int)$_POST['stock'];
|
|
|
- }
|
|
|
-
|
|
|
- $products[] = $newProduct;
|
|
|
- saveProducts($products);
|
|
|
- $message = 'Produkt erfolgreich hinzugefügt.';
|
|
|
- $messageType = 'success';
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (isset($_POST['update_product'])) {
|
|
|
$uploadResult = handleImageUpload();
|
|
|
if (!$uploadResult['success']) {
|
|
|
$message = $uploadResult['message'];
|
|
|
$messageType = 'error';
|
|
|
} else {
|
|
|
- $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'] = $uploadResult['filename'] !== null ? $uploadResult['filename'] : 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']);
|
|
|
+ $productId = (int) ($_POST['product_id'] ?? 0);
|
|
|
+ $categoryIds = getSubmittedProductCategoryIds($_POST);
|
|
|
+
|
|
|
+ $existingProduct = null;
|
|
|
+ foreach ($products as $product) {
|
|
|
+ if ($product['id'] === $productId) {
|
|
|
+ $existingProduct = $product;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $existingStockBySize = isset($existingProduct['stock_by_size']) && is_array($existingProduct['stock_by_size'])
|
|
|
+ ? $existingProduct['stock_by_size']
|
|
|
+ : [];
|
|
|
+ $sizeData = buildProductSizeStock($_POST['sizes'] ?? '', $_POST, $existingStockBySize);
|
|
|
+
|
|
|
+ if (empty($categoryIds)) {
|
|
|
+ $message = 'Bitte wählen Sie mindestens eine gültige Kategorie aus.';
|
|
|
+ $messageType = 'error';
|
|
|
+ } elseif ($existingProduct === null) {
|
|
|
+ $message = 'Produkt nicht gefunden.';
|
|
|
+ $messageType = 'error';
|
|
|
+ } elseif ($sizeData['sizes'] === '') {
|
|
|
+ $message = 'Bitte geben Sie mindestens eine Größe ein.';
|
|
|
+ $messageType = 'error';
|
|
|
+ } else {
|
|
|
+ foreach ($products as &$product) {
|
|
|
+ if ($product['id'] === $productId) {
|
|
|
+ $product['name'] = sanitize($_POST['name']);
|
|
|
+ $product['description'] = sanitize($_POST['description']);
|
|
|
+ $product['price'] = (float) ($_POST['price'] ?? 0);
|
|
|
+ $product['categories'] = $categoryIds;
|
|
|
+ $product['image'] = $uploadResult['filename'] !== null ? $uploadResult['filename'] : sanitize($_POST['image']);
|
|
|
+ $product['sizes'] = $sizeData['sizes'];
|
|
|
+ $product['stock_by_size'] = $sizeData['stock_by_size'];
|
|
|
+ unset($product['stock']);
|
|
|
+ break;
|
|
|
}
|
|
|
- } else {
|
|
|
- $product['stock'] = (int)$_POST['stock'];
|
|
|
- unset($product['sizes']);
|
|
|
- unset($product['stock_by_size']);
|
|
|
}
|
|
|
- break;
|
|
|
+ unset($product);
|
|
|
+
|
|
|
+ saveProducts($products);
|
|
|
+ $message = 'Produkt erfolgreich aktualisiert.';
|
|
|
+ $messageType = 'success';
|
|
|
}
|
|
|
}
|
|
|
- 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;
|
|
|
+ $productId = (int) ($_POST['product_id'] ?? 0);
|
|
|
+ $products = array_filter($products, function ($product) use ($productId) {
|
|
|
+ return $product['id'] !== $productId;
|
|
|
});
|
|
|
- $products = array_values($products); // Re-index
|
|
|
+ $products = array_values($products);
|
|
|
saveProducts($products);
|
|
|
$message = 'Produkt erfolgreich gelöscht.';
|
|
|
$messageType = 'success';
|
|
|
@@ -177,7 +219,7 @@ $products = getProducts();
|
|
|
$editingProduct = null;
|
|
|
|
|
|
if (isset($_GET['edit'])) {
|
|
|
- $editingProduct = getProductById((int)$_GET['edit']);
|
|
|
+ $editingProduct = getProductById((int) $_GET['edit']);
|
|
|
}
|
|
|
|
|
|
$bodyClass = 'admin-page';
|
|
|
@@ -191,7 +233,7 @@ include __DIR__ . '/../includes/header.php';
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
-<?php if ($message): ?>
|
|
|
+<?php if ($message !== ''): ?>
|
|
|
<div class="alert alert-<?php echo $messageType; ?>">
|
|
|
<?php echo htmlspecialchars($message); ?>
|
|
|
</div>
|
|
|
@@ -215,33 +257,32 @@ include __DIR__ . '/../includes/header.php';
|
|
|
<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>
|
|
|
+ <label for="categories">Kategorien *</label>
|
|
|
+ <select id="categories" name="categories[]" multiple size="<?php echo max(3, min(8, count($categories))); ?>" required>
|
|
|
+ <?php foreach ($categories as $category): ?>
|
|
|
+ <option value="<?php echo htmlspecialchars($category['id']); ?>" <?php echo in_array($category['id'], getProductCategoryIds($editingProduct), true) ? 'selected' : ''; ?>>
|
|
|
+ <?php echo htmlspecialchars($category['label']); ?>
|
|
|
+ </option>
|
|
|
+ <?php endforeach; ?>
|
|
|
</select>
|
|
|
+ <small>Mehrfachauswahl mit Strg/Cmd oder Umschalt. Auf Touch-Geräten können mehrere Einträge nacheinander markiert werden.</small>
|
|
|
</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 class="form-group">
|
|
|
+ <label for="sizes">Größen (kommagetrennt, z.B. Standard oder S,M,L,XL) *</label>
|
|
|
+ <input type="text" id="sizes" name="sizes" required value="<?php echo htmlspecialchars($editingProduct['sizes'] ?? ''); ?>" placeholder="Standard" oninput="updateSizeStockFields()">
|
|
|
+ <small>Alle Produkte nutzen größenbasierten Bestand. Für Artikel ohne Varianten z.B. <code>Standard</code> oder <code>Einheitsgröße</code> verwenden.</small>
|
|
|
</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 id="size-stock-group">
|
|
|
+ <?php
|
|
|
+ $sizes = getProductSizes($editingProduct);
|
|
|
+ $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; ?>
|
|
|
</div>
|
|
|
<div class="form-group">
|
|
|
<label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
|
|
|
@@ -253,37 +294,28 @@ include __DIR__ . '/../includes/header.php';
|
|
|
<small>Upload nach <code>assets/images</code>; ersetzt den Dateinamen oben automatisch.</small>
|
|
|
</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);
|
|
|
-
|
|
|
+ const currentValues = {};
|
|
|
+
|
|
|
+ sizeStockGroup.querySelectorAll('input[type="number"]').forEach((input) => {
|
|
|
+ currentValues[input.name] = input.value;
|
|
|
+ });
|
|
|
+
|
|
|
+ const sizes = sizesInput.value.split(',').map((size) => size.trim()).filter((size, index, all) => size && all.indexOf(size) === index);
|
|
|
let html = '';
|
|
|
- sizes.forEach(size => {
|
|
|
+
|
|
|
+ sizes.forEach((size) => {
|
|
|
const safeName = size.replace(/[ ,]/g, '_');
|
|
|
+ const fieldName = 'stock_' + safeName;
|
|
|
+ const currentValue = Object.prototype.hasOwnProperty.call(currentValues, fieldName) ? currentValues[fieldName] : '0';
|
|
|
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 += '<label>Lagerbestand für Größe "' + size.replace(/"/g, '"') + '" *</label>';
|
|
|
+ html += '<input type="number" name="' + fieldName + '" min="0" value="' + currentValue + '" required>';
|
|
|
html += '</div>';
|
|
|
});
|
|
|
+
|
|
|
sizeStockGroup.innerHTML = html;
|
|
|
}
|
|
|
</script>
|
|
|
@@ -308,21 +340,22 @@ include __DIR__ . '/../includes/header.php';
|
|
|
<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>
|
|
|
+ <label for="categories">Kategorien *</label>
|
|
|
+ <select id="categories" name="categories[]" multiple size="<?php echo max(3, min(8, count($categories))); ?>" required>
|
|
|
+ <?php foreach ($categories as $category): ?>
|
|
|
+ <option value="<?php echo htmlspecialchars($category['id']); ?>">
|
|
|
+ <?php echo htmlspecialchars($category['label']); ?>
|
|
|
+ </option>
|
|
|
+ <?php endforeach; ?>
|
|
|
</select>
|
|
|
+ <small>Mehrfachauswahl mit Strg/Cmd oder Umschalt. Auf Touch-Geräten können mehrere Einträge nacheinander markiert werden.</small>
|
|
|
</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 class="form-group">
|
|
|
+ <label for="sizes">Größen (kommagetrennt, z.B. Standard oder S,M,L,XL) *</label>
|
|
|
+ <input type="text" id="sizes" name="sizes" required placeholder="Standard" oninput="updateSizeStockFields()">
|
|
|
+ <small>Alle Produkte nutzen größenbasierten Bestand. Für Artikel ohne Varianten z.B. <code>Standard</code> oder <code>Einheitsgröße</code> verwenden.</small>
|
|
|
</div>
|
|
|
- <div id="size-stock-group" style="display: none;"></div>
|
|
|
+ <div id="size-stock-group"></div>
|
|
|
<div class="form-group">
|
|
|
<label for="image">Bilddateiname (z.B. tshirt.jpg)</label>
|
|
|
<input type="text" id="image" name="image">
|
|
|
@@ -333,37 +366,28 @@ include __DIR__ . '/../includes/header.php';
|
|
|
<small>Upload nach <code>assets/images</code>; Dateiname wird automatisch übernommen.</small>
|
|
|
</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);
|
|
|
-
|
|
|
+ const currentValues = {};
|
|
|
+
|
|
|
+ sizeStockGroup.querySelectorAll('input[type="number"]').forEach((input) => {
|
|
|
+ currentValues[input.name] = input.value;
|
|
|
+ });
|
|
|
+
|
|
|
+ const sizes = sizesInput.value.split(',').map((size) => size.trim()).filter((size, index, all) => size && all.indexOf(size) === index);
|
|
|
let html = '';
|
|
|
- sizes.forEach(size => {
|
|
|
+
|
|
|
+ sizes.forEach((size) => {
|
|
|
const safeName = size.replace(/[ ,]/g, '_');
|
|
|
+ const fieldName = 'stock_' + safeName;
|
|
|
+ const currentValue = Object.prototype.hasOwnProperty.call(currentValues, fieldName) ? currentValues[fieldName] : '0';
|
|
|
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 += '<label>Lagerbestand für Größe "' + size.replace(/"/g, '"') + '" *</label>';
|
|
|
+ html += '<input type="number" name="' + fieldName + '" min="0" value="' + currentValue + '" required>';
|
|
|
html += '</div>';
|
|
|
});
|
|
|
+
|
|
|
sizeStockGroup.innerHTML = html;
|
|
|
}
|
|
|
</script>
|
|
|
@@ -376,48 +400,46 @@ include __DIR__ . '/../includes/header.php';
|
|
|
<?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): ?>
|
|
|
+ <div class="table-responsive">
|
|
|
+ <table class="responsive-table">
|
|
|
+ <thead>
|
|
|
<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>
|
|
|
+ <th>ID</th>
|
|
|
+ <th>Name</th>
|
|
|
+ <th>Kategorien</th>
|
|
|
+ <th>Preis</th>
|
|
|
+ <th>Lagerbestand</th>
|
|
|
+ <th>Aktionen</th>
|
|
|
</tr>
|
|
|
- <?php endforeach; ?>
|
|
|
- </tbody>
|
|
|
- </table>
|
|
|
+ </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="Kategorien"><?php echo htmlspecialchars(implode(', ', getCategoryLabels(getProductCategoryIds($product)))); ?></td>
|
|
|
+ <td data-label="Preis"><?php echo formatPrice($product['price']); ?></td>
|
|
|
+ <td data-label="Lagerbestand">
|
|
|
+ <?php
|
|
|
+ $stockInfo = [];
|
|
|
+ foreach (($product['stock_by_size'] ?? []) as $size => $stock) {
|
|
|
+ $stockInfo[] = $size . ': ' . (int) $stock;
|
|
|
+ }
|
|
|
+ echo !empty($stockInfo) ? htmlspecialchars(implode(', ', $stockInfo)) : '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>
|
|
|
+ </div>
|
|
|
<?php endif; ?>
|
|
|
|
|
|
<?php include __DIR__ . '/../includes/footer.php'; ?>
|