true, 'filename' => null]; } $file = $_FILES[$fileInputName]; if ($file['error'] !== UPLOAD_ERR_OK) { return ['success' => false, 'message' => 'Upload fehlgeschlagen. Bitte erneut versuchen.']; } $allowedExtensions = ['jpg', 'jpeg', 'png', 'webp', 'gif']; $originalName = basename($file['name']); $extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION)); if (!in_array($extension, $allowedExtensions, true)) { return ['success' => false, 'message' => 'Ungültiger Dateityp. Erlaubt: JPG, PNG, WEBP, GIF.']; } $finfo = new finfo(FILEINFO_MIME_TYPE); $mimeType = $finfo->file($file['tmp_name']); $allowedMimes = ['image/jpeg', 'image/png', 'image/webp', 'image/gif']; if (!in_array($mimeType, $allowedMimes, true)) { return ['success' => false, 'message' => 'Die hochgeladene Datei ist kein gültiges Bild.']; } $imagesDir = __DIR__ . '/../assets/images'; if (!is_dir($imagesDir)) { mkdir($imagesDir, 0755, true); } $safeBaseName = preg_replace('/[^a-zA-Z0-9_-]/', '-', pathinfo($originalName, PATHINFO_FILENAME)); $safeBaseName = trim((string) $safeBaseName, '-'); if ($safeBaseName === '') { $safeBaseName = 'bild'; } $targetFilename = $safeBaseName . '.' . $extension; $targetPath = $imagesDir . '/' . $targetFilename; $counter = 1; while (file_exists($targetPath)) { $targetFilename = $safeBaseName . '-' . $counter . '.' . $extension; $targetPath = $imagesDir . '/' . $targetFilename; $counter++; } if (!move_uploaded_file($file['tmp_name'], $targetPath)) { return ['success' => false, 'message' => 'Bild konnte nicht gespeichert werden.']; } 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 (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 { $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'; } } } if (isset($_POST['update_product'])) { $uploadResult = handleImageUpload(); if (!$uploadResult['success']) { $message = $uploadResult['message']; $messageType = 'error'; } else { $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; } } unset($product); saveProducts($products); $message = 'Produkt erfolgreich aktualisiert.'; $messageType = 'success'; } } } if (isset($_POST['delete_product'])) { $productId = (int) ($_POST['product_id'] ?? 0); $products = array_filter($products, function ($product) use ($productId) { return $product['id'] !== $productId; }); $products = array_values($products); 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'; ?>
Keine Produkte vorhanden.
| ID | Name | Kategorien | Preis | Lagerbestand | Aktionen |
|---|---|---|---|---|---|
| $stock) { $stockInfo[] = $size . ': ' . (int) $stock; } echo !empty($stockInfo) ? htmlspecialchars(implode(', ', $stockInfo)) : '0'; ?> | Bearbeiten |