|
@@ -1,44 +1,58 @@
|
|
|
<?php
|
|
<?php
|
|
|
-require_once __DIR__ . '/../config.php';
|
|
|
|
|
-require_once __DIR__ . '/../includes/functions.php';
|
|
|
|
|
|
|
+require_once __DIR__ . "/../config.php";
|
|
|
|
|
+require_once __DIR__ . "/../includes/functions.php";
|
|
|
|
|
|
|
|
-if (empty($_SESSION['admin_logged_in'])) {
|
|
|
|
|
- header('Location: login.php');
|
|
|
|
|
- exit;
|
|
|
|
|
|
|
+if (empty($_SESSION["admin_logged_in"])) {
|
|
|
|
|
+ header("Location: login.php");
|
|
|
|
|
+ exit();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-$pageTitle = 'Produkte verwalten';
|
|
|
|
|
-$message = '';
|
|
|
|
|
-$messageType = '';
|
|
|
|
|
|
|
+$pageTitle = "Produkte verwalten";
|
|
|
|
|
+$message = "";
|
|
|
|
|
+$messageType = "";
|
|
|
$categories = getCategories();
|
|
$categories = getCategories();
|
|
|
|
|
|
|
|
-function handleImageUpload($fileInputName = 'image_file') {
|
|
|
|
|
- if (!isset($_FILES[$fileInputName]) || $_FILES[$fileInputName]['error'] === UPLOAD_ERR_NO_FILE) {
|
|
|
|
|
- return ['success' => true, 'filename' => null];
|
|
|
|
|
|
|
+function handleImageUpload($fileInputName = "image_file")
|
|
|
|
|
+{
|
|
|
|
|
+ if (
|
|
|
|
|
+ !isset($_FILES[$fileInputName]) ||
|
|
|
|
|
+ $_FILES[$fileInputName]["error"] === UPLOAD_ERR_NO_FILE
|
|
|
|
|
+ ) {
|
|
|
|
|
+ return ["success" => true, "filename" => null];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$file = $_FILES[$fileInputName];
|
|
$file = $_FILES[$fileInputName];
|
|
|
- if ($file['error'] !== UPLOAD_ERR_OK) {
|
|
|
|
|
- return ['success' => false, 'message' => 'Upload fehlgeschlagen.'];
|
|
|
|
|
|
|
+ if ($file["error"] !== UPLOAD_ERR_OK) {
|
|
|
|
|
+ return ["success" => false, "message" => "Upload fehlgeschlagen."];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $allowedExtensions = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
|
|
|
|
|
- $originalName = basename($file['name']);
|
|
|
|
|
|
|
+ $allowedExtensions = ["jpg", "jpeg", "png", "webp", "gif"];
|
|
|
|
|
+ $originalName = basename($file["name"]);
|
|
|
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
|
|
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
|
|
|
if (!in_array($extension, $allowedExtensions, true)) {
|
|
if (!in_array($extension, $allowedExtensions, true)) {
|
|
|
- return ['success' => false, 'message' => 'Ungültiger Dateityp.'];
|
|
|
|
|
|
|
+ return ["success" => false, "message" => "Ungültiger Dateityp."];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
|
- $mimeType = $finfo->file($file['tmp_name']);
|
|
|
|
|
- $allowedMimes = ['image/jpeg', 'image/png', 'image/webp', 'image/gif'];
|
|
|
|
|
|
|
+ $mimeType = $finfo->file($file["tmp_name"]);
|
|
|
|
|
+ $allowedMimes = ["image/jpeg", "image/png", "image/webp", "image/gif"];
|
|
|
if (!in_array($mimeType, $allowedMimes, true)) {
|
|
if (!in_array($mimeType, $allowedMimes, true)) {
|
|
|
- return ['success' => false, 'message' => 'Die Datei ist kein gültiges Bild.'];
|
|
|
|
|
|
|
+ return [
|
|
|
|
|
+ "success" => false,
|
|
|
|
|
+ "message" => "Die Datei ist kein gültiges Bild.",
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $uploadsDir = rtrim(UPLOADS_DIR, '/\\');
|
|
|
|
|
- if (!is_dir($uploadsDir) && !mkdir($uploadsDir, 02775, true) && !is_dir($uploadsDir)) {
|
|
|
|
|
- return ['success' => false, 'message' => 'Upload-Verzeichnis konnte nicht erstellt werden.'];
|
|
|
|
|
|
|
+ $uploadsDir = rtrim(UPLOADS_DIR, "/\\");
|
|
|
|
|
+ if (
|
|
|
|
|
+ !is_dir($uploadsDir) &&
|
|
|
|
|
+ !mkdir($uploadsDir, 02775, true) &&
|
|
|
|
|
+ !is_dir($uploadsDir)
|
|
|
|
|
+ ) {
|
|
|
|
|
+ return [
|
|
|
|
|
+ "success" => false,
|
|
|
|
|
+ "message" => "Upload-Verzeichnis konnte nicht erstellt werden.",
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (is_dir($uploadsDir)) {
|
|
if (is_dir($uploadsDir)) {
|
|
@@ -46,55 +60,77 @@ function handleImageUpload($fileInputName = 'image_file') {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!is_writable($uploadsDir)) {
|
|
if (!is_writable($uploadsDir)) {
|
|
|
- return ['success' => false, 'message' => 'Upload-Verzeichnis ist nicht beschreibbar: ' . $uploadsDir];
|
|
|
|
|
|
|
+ return [
|
|
|
|
|
+ "success" => false,
|
|
|
|
|
+ "message" =>
|
|
|
|
|
+ "Upload-Verzeichnis ist nicht beschreibbar: " . $uploadsDir,
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $safeBaseName = preg_replace('/[^a-zA-Z0-9_-]/', '-', pathinfo($originalName, PATHINFO_FILENAME));
|
|
|
|
|
- $safeBaseName = trim((string) $safeBaseName, '-');
|
|
|
|
|
- if ($safeBaseName === '') {
|
|
|
|
|
- $safeBaseName = 'bild';
|
|
|
|
|
|
|
+ $safeBaseName = preg_replace(
|
|
|
|
|
+ "/[^a-zA-Z0-9_-]/",
|
|
|
|
|
+ "-",
|
|
|
|
|
+ pathinfo($originalName, PATHINFO_FILENAME),
|
|
|
|
|
+ );
|
|
|
|
|
+ $safeBaseName = trim((string) $safeBaseName, "-");
|
|
|
|
|
+ if ($safeBaseName === "") {
|
|
|
|
|
+ $safeBaseName = "bild";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $targetFilename = $safeBaseName . '.' . $extension;
|
|
|
|
|
- $targetPath = $uploadsDir . '/' . $targetFilename;
|
|
|
|
|
|
|
+ $targetFilename = $safeBaseName . "." . $extension;
|
|
|
|
|
+ $targetPath = $uploadsDir . "/" . $targetFilename;
|
|
|
$counter = 1;
|
|
$counter = 1;
|
|
|
while (file_exists($targetPath)) {
|
|
while (file_exists($targetPath)) {
|
|
|
- $targetFilename = $safeBaseName . '-' . $counter . '.' . $extension;
|
|
|
|
|
- $targetPath = $uploadsDir . '/' . $targetFilename;
|
|
|
|
|
|
|
+ $targetFilename = $safeBaseName . "-" . $counter . "." . $extension;
|
|
|
|
|
+ $targetPath = $uploadsDir . "/" . $targetFilename;
|
|
|
$counter++;
|
|
$counter++;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
|
|
|
|
|
- return ['success' => false, 'message' => 'Bild konnte nicht gespeichert werden.'];
|
|
|
|
|
|
|
+ if (!move_uploaded_file($file["tmp_name"], $targetPath)) {
|
|
|
|
|
+ return [
|
|
|
|
|
+ "success" => false,
|
|
|
|
|
+ "message" => "Bild konnte nicht gespeichert werden.",
|
|
|
|
|
+ ];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return ['success' => true, 'filename' => $targetFilename];
|
|
|
|
|
|
|
+ return ["success" => true, "filename" => $targetFilename];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function buildProductAvailabilityFields($sizesInput, $submittedValues = [], $existingValues = []) {
|
|
|
|
|
- $sizes = getProductSizes(['sizes' => (string) $sizesInput]);
|
|
|
|
|
|
|
+function buildProductAvailabilityFields(
|
|
|
|
|
+ $sizesInput,
|
|
|
|
|
+ $submittedValues = [],
|
|
|
|
|
+ $existingValues = [],
|
|
|
|
|
+) {
|
|
|
|
|
+ $sizes = getProductSizes(["sizes" => (string) $sizesInput]);
|
|
|
if (empty($sizes)) {
|
|
if (empty($sizes)) {
|
|
|
- $sizes = ['Standard'];
|
|
|
|
|
|
|
+ $sizes = ["Standard"];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$availabilityLabels = [];
|
|
$availabilityLabels = [];
|
|
|
foreach ($sizes as $size) {
|
|
foreach ($sizes as $size) {
|
|
|
- $fieldName = 'availability_' . str_replace([' ', ','], '_', $size);
|
|
|
|
|
|
|
+ $fieldName = "availability_" . str_replace([" ", ","], "_", $size);
|
|
|
if (isset($submittedValues[$fieldName])) {
|
|
if (isset($submittedValues[$fieldName])) {
|
|
|
- $availabilityLabels[$size] = trim((string) $submittedValues[$fieldName]);
|
|
|
|
|
|
|
+ $availabilityLabels[$size] = trim(
|
|
|
|
|
+ (string) $submittedValues[$fieldName],
|
|
|
|
|
+ );
|
|
|
} else {
|
|
} else {
|
|
|
- $availabilityLabels[$size] = trim((string) ($existingValues[$size] ?? ''));
|
|
|
|
|
|
|
+ $availabilityLabels[$size] = trim(
|
|
|
|
|
+ (string) ($existingValues[$size] ?? ""),
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
return [
|
|
|
- 'sizes' => implode(',', $sizes),
|
|
|
|
|
- 'availability_labels' => $availabilityLabels,
|
|
|
|
|
|
|
+ "sizes" => implode(",", $sizes),
|
|
|
|
|
+ "availability_labels" => $availabilityLabels,
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function getSubmittedProductCategoryIds($submittedValues) {
|
|
|
|
|
- $selectedCategoryIds = normalizeProductCategoryIds($submittedValues['categories'] ?? []);
|
|
|
|
|
|
|
+function getSubmittedProductCategoryIds($submittedValues)
|
|
|
|
|
+{
|
|
|
|
|
+ $selectedCategoryIds = normalizeProductCategoryIds(
|
|
|
|
|
+ $submittedValues["categories"] ?? [],
|
|
|
|
|
+ );
|
|
|
$validCategoryIds = [];
|
|
$validCategoryIds = [];
|
|
|
|
|
|
|
|
foreach ($selectedCategoryIds as $categoryId) {
|
|
foreach ($selectedCategoryIds as $categoryId) {
|
|
@@ -106,96 +142,145 @@ function getSubmittedProductCategoryIds($submittedValues) {
|
|
|
return $validCategoryIds;
|
|
return $validCategoryIds;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
|
- $products = getProducts();
|
|
|
|
|
-
|
|
|
|
|
- if (empty($categories)) {
|
|
|
|
|
- $message = 'Bitte zuerst mindestens eine Kategorie anlegen.';
|
|
|
|
|
- $messageType = 'error';
|
|
|
|
|
- } elseif (isset($_POST['add_product']) || isset($_POST['update_product'])) {
|
|
|
|
|
- $uploadResult = handleImageUpload();
|
|
|
|
|
- if (!$uploadResult['success']) {
|
|
|
|
|
- $message = $uploadResult['message'];
|
|
|
|
|
- $messageType = 'error';
|
|
|
|
|
- } else {
|
|
|
|
|
- $categoryIds = getSubmittedProductCategoryIds($_POST);
|
|
|
|
|
- $existingLabels = [];
|
|
|
|
|
- $productId = isset($_POST['product_id']) ? (int) $_POST['product_id'] : 0;
|
|
|
|
|
- foreach ($products as $product) {
|
|
|
|
|
- if ((int) $product['id'] === $productId) {
|
|
|
|
|
- $existingLabels = $product['availability_labels'] ?? [];
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|
|
|
|
+ // Validate CSRF token
|
|
|
|
|
+ if (!validateCsrfToken($_POST["csrf_token"] ?? "")) {
|
|
|
|
|
+ $message = "Ungültiges Token. Bitte versuchen Sie es erneut.";
|
|
|
|
|
+ $messageType = "error";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $products = getProducts();
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($categories)) {
|
|
|
|
|
+ $message = "Bitte zuerst mindestens eine Kategorie anlegen.";
|
|
|
|
|
+ $messageType = "error";
|
|
|
|
|
+ } elseif (
|
|
|
|
|
+ isset($_POST["add_product"]) ||
|
|
|
|
|
+ isset($_POST["update_product"])
|
|
|
|
|
+ ) {
|
|
|
|
|
+ $uploadResult = handleImageUpload();
|
|
|
|
|
+ if (!$uploadResult["success"]) {
|
|
|
|
|
+ $message = $uploadResult["message"];
|
|
|
|
|
+ $messageType = "error";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $categoryIds = getSubmittedProductCategoryIds($_POST);
|
|
|
|
|
+ $existingLabels = [];
|
|
|
|
|
+ $productId = isset($_POST["product_id"])
|
|
|
|
|
+ ? (int) $_POST["product_id"]
|
|
|
|
|
+ : 0;
|
|
|
|
|
+ foreach ($products as $product) {
|
|
|
|
|
+ if ((int) $product["id"] === $productId) {
|
|
|
|
|
+ $existingLabels = $product["availability_labels"] ?? [];
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- $sizeData = buildProductAvailabilityFields($_POST['sizes'] ?? '', $_POST, $existingLabels);
|
|
|
|
|
|
|
+ $sizeData = buildProductAvailabilityFields(
|
|
|
|
|
+ $_POST["sizes"] ?? "",
|
|
|
|
|
+ $_POST,
|
|
|
|
|
+ $existingLabels,
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
- if (empty($categoryIds)) {
|
|
|
|
|
- $message = 'Bitte mindestens eine gültige Kategorie auswählen.';
|
|
|
|
|
- $messageType = 'error';
|
|
|
|
|
- } else {
|
|
|
|
|
- $record = [
|
|
|
|
|
- 'name' => sanitize($_POST['name'] ?? ''),
|
|
|
|
|
- 'description' => trim((string) ($_POST['description'] ?? '')),
|
|
|
|
|
- 'categories' => $categoryIds,
|
|
|
|
|
- 'image' => $uploadResult['filename'] !== null ? $uploadResult['filename'] : trim((string) ($_POST['image'] ?? '')),
|
|
|
|
|
- 'sizes' => $sizeData['sizes'],
|
|
|
|
|
- 'availability_labels' => $sizeData['availability_labels'],
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
- if ($record['name'] === '') {
|
|
|
|
|
- $message = 'Bitte einen Produktnamen eingeben.';
|
|
|
|
|
- $messageType = 'error';
|
|
|
|
|
- } elseif (isset($_POST['add_product'])) {
|
|
|
|
|
- $newId = empty($products) ? 1 : (max(array_map(function ($product) {
|
|
|
|
|
- return (int) $product['id'];
|
|
|
|
|
- }, $products)) + 1);
|
|
|
|
|
- $record['id'] = $newId;
|
|
|
|
|
- $products[] = $record;
|
|
|
|
|
- saveProducts($products);
|
|
|
|
|
- $message = 'Produkt wurde angelegt.';
|
|
|
|
|
- $messageType = 'success';
|
|
|
|
|
|
|
+ if (empty($categoryIds)) {
|
|
|
|
|
+ $message =
|
|
|
|
|
+ "Bitte mindestens eine gültige Kategorie auswählen.";
|
|
|
|
|
+ $messageType = "error";
|
|
|
} else {
|
|
} else {
|
|
|
- $updated = false;
|
|
|
|
|
- foreach ($products as &$product) {
|
|
|
|
|
- if ((int) $product['id'] === $productId) {
|
|
|
|
|
- $record['id'] = $productId;
|
|
|
|
|
- $product = $record;
|
|
|
|
|
- $updated = true;
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- unset($product);
|
|
|
|
|
-
|
|
|
|
|
- if ($updated) {
|
|
|
|
|
|
|
+ $record = [
|
|
|
|
|
+ "name" => sanitize($_POST["name"] ?? ""),
|
|
|
|
|
+ "description" => trim(
|
|
|
|
|
+ (string) ($_POST["description"] ?? ""),
|
|
|
|
|
+ ),
|
|
|
|
|
+ "categories" => $categoryIds,
|
|
|
|
|
+ "image" =>
|
|
|
|
|
+ $uploadResult["filename"] !== null
|
|
|
|
|
+ ? $uploadResult["filename"]
|
|
|
|
|
+ : trim((string) ($_POST["image"] ?? "")),
|
|
|
|
|
+ "sizes" => $sizeData["sizes"],
|
|
|
|
|
+ "availability_labels" =>
|
|
|
|
|
+ $sizeData["availability_labels"],
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ if ($record["name"] === "") {
|
|
|
|
|
+ $message = "Bitte einen Produktnamen eingeben.";
|
|
|
|
|
+ $messageType = "error";
|
|
|
|
|
+ } elseif (isset($_POST["add_product"])) {
|
|
|
|
|
+ $newId = empty($products)
|
|
|
|
|
+ ? 1
|
|
|
|
|
+ : max(
|
|
|
|
|
+ array_map(function ($product) {
|
|
|
|
|
+ return (int) $product["id"];
|
|
|
|
|
+ }, $products),
|
|
|
|
|
+ ) + 1;
|
|
|
|
|
+ $record["id"] = $newId;
|
|
|
|
|
+ $products[] = $record;
|
|
|
saveProducts($products);
|
|
saveProducts($products);
|
|
|
- $message = 'Produkt wurde aktualisiert.';
|
|
|
|
|
- $messageType = 'success';
|
|
|
|
|
|
|
+ logAccess("Admin added product", [
|
|
|
|
|
+ "product_id" => $newId,
|
|
|
|
|
+ "product_name" => $record["name"],
|
|
|
|
|
+ ]);
|
|
|
|
|
+ $message = "Produkt wurde angelegt.";
|
|
|
|
|
+ $messageType = "success";
|
|
|
} else {
|
|
} else {
|
|
|
- $message = 'Produkt nicht gefunden.';
|
|
|
|
|
- $messageType = 'error';
|
|
|
|
|
|
|
+ $updated = false;
|
|
|
|
|
+ foreach ($products as &$product) {
|
|
|
|
|
+ if ((int) $product["id"] === $productId) {
|
|
|
|
|
+ $record["id"] = $productId;
|
|
|
|
|
+ $product = $record;
|
|
|
|
|
+ $updated = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ unset($product);
|
|
|
|
|
+
|
|
|
|
|
+ if ($updated) {
|
|
|
|
|
+ saveProducts($products);
|
|
|
|
|
+ logAccess("Admin updated product", [
|
|
|
|
|
+ "product_id" => $productId,
|
|
|
|
|
+ "product_name" => $record["name"],
|
|
|
|
|
+ ]);
|
|
|
|
|
+ $message = "Produkt wurde aktualisiert.";
|
|
|
|
|
+ $messageType = "success";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $message = "Produkt nicht gefunden.";
|
|
|
|
|
+ $messageType = "error";
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- if (isset($_POST['delete_product'])) {
|
|
|
|
|
- $productId = (int) ($_POST['product_id'] ?? 0);
|
|
|
|
|
- $products = array_values(array_filter($products, function ($product) use ($productId) {
|
|
|
|
|
- return (int) $product['id'] !== $productId;
|
|
|
|
|
- }));
|
|
|
|
|
- saveProducts($products);
|
|
|
|
|
- $message = 'Produkt wurde gelöscht.';
|
|
|
|
|
- $messageType = 'success';
|
|
|
|
|
|
|
+ if (isset($_POST["delete_product"])) {
|
|
|
|
|
+ $productId = (int) ($_POST["product_id"] ?? 0);
|
|
|
|
|
+ $productName = "";
|
|
|
|
|
+ foreach ($products as $product) {
|
|
|
|
|
+ if ((int) $product["id"] === $productId) {
|
|
|
|
|
+ $productName = $product["name"];
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ $products = array_values(
|
|
|
|
|
+ array_filter($products, function ($product) use ($productId) {
|
|
|
|
|
+ return (int) $product["id"] !== $productId;
|
|
|
|
|
+ }),
|
|
|
|
|
+ );
|
|
|
|
|
+ saveProducts($products);
|
|
|
|
|
+ logAccess("Admin deleted product", [
|
|
|
|
|
+ "product_id" => $productId,
|
|
|
|
|
+ "product_name" => $productName,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ $message = "Produkt wurde gelöscht.";
|
|
|
|
|
+ $messageType = "success";
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$products = getProducts();
|
|
$products = getProducts();
|
|
|
-$editingProduct = isset($_GET['edit']) ? getProductById((int) $_GET['edit']) : null;
|
|
|
|
|
|
|
+$editingProduct = isset($_GET["edit"])
|
|
|
|
|
+ ? getProductById((int) $_GET["edit"])
|
|
|
|
|
+ : null;
|
|
|
|
|
|
|
|
-$bodyClass = 'admin-page';
|
|
|
|
|
-include __DIR__ . '/../includes/header.php';
|
|
|
|
|
|
|
+$bodyClass = "admin-page";
|
|
|
|
|
+include __DIR__ . "/../includes/header.php";
|
|
|
?>
|
|
?>
|
|
|
|
|
|
|
|
<div class="admin-header">
|
|
<div class="admin-header">
|
|
@@ -205,7 +290,7 @@ include __DIR__ . '/../includes/header.php';
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
-<?php if ($message !== ''): ?>
|
|
|
|
|
|
|
+<?php if ($message !== ""): ?>
|
|
|
<div class="alert alert-<?php echo escape($messageType); ?>">
|
|
<div class="alert alert-<?php echo escape($messageType); ?>">
|
|
|
<?php echo escape($message); ?>
|
|
<?php echo escape($message); ?>
|
|
|
</div>
|
|
</div>
|
|
@@ -213,43 +298,63 @@ include __DIR__ . '/../includes/header.php';
|
|
|
|
|
|
|
|
<?php
|
|
<?php
|
|
|
$currentProduct = $editingProduct ?: [
|
|
$currentProduct = $editingProduct ?: [
|
|
|
- 'id' => '',
|
|
|
|
|
- 'name' => '',
|
|
|
|
|
- 'description' => '',
|
|
|
|
|
- 'categories' => [],
|
|
|
|
|
- 'sizes' => 'Standard',
|
|
|
|
|
- 'availability_labels' => ['Standard' => ''],
|
|
|
|
|
- 'image' => '',
|
|
|
|
|
|
|
+ "id" => "",
|
|
|
|
|
+ "name" => "",
|
|
|
|
|
+ "description" => "",
|
|
|
|
|
+ "categories" => [],
|
|
|
|
|
+ "sizes" => "Standard",
|
|
|
|
|
+ "availability_labels" => ["Standard" => ""],
|
|
|
|
|
+ "image" => "",
|
|
|
];
|
|
];
|
|
|
$currentSizes = getProductSizes($currentProduct);
|
|
$currentSizes = getProductSizes($currentProduct);
|
|
|
if (empty($currentSizes)) {
|
|
if (empty($currentSizes)) {
|
|
|
- $currentSizes = ['Standard'];
|
|
|
|
|
|
|
+ $currentSizes = ["Standard"];
|
|
|
}
|
|
}
|
|
|
?>
|
|
?>
|
|
|
|
|
|
|
|
<div class="panel panel-lg">
|
|
<div class="panel panel-lg">
|
|
|
- <h3><?php echo $editingProduct ? 'Produkt bearbeiten' : 'Neues Produkt anlegen'; ?></h3>
|
|
|
|
|
|
|
+ <h3><?php echo $editingProduct
|
|
|
|
|
+ ? "Produkt bearbeiten"
|
|
|
|
|
+ : "Neues Produkt anlegen"; ?></h3>
|
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<form method="POST" enctype="multipart/form-data">
|
|
|
|
|
+ <?php echo csrfField(); ?>
|
|
|
<?php if ($editingProduct): ?>
|
|
<?php if ($editingProduct): ?>
|
|
|
- <input type="hidden" name="product_id" value="<?php echo (int) $editingProduct['id']; ?>">
|
|
|
|
|
|
|
+ <input type="hidden" name="product_id" value="<?php echo (int) $editingProduct[
|
|
|
|
|
+ "id"
|
|
|
|
|
+ ]; ?>">
|
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
|
<label for="name">Name *</label>
|
|
<label for="name">Name *</label>
|
|
|
- <input type="text" id="name" name="name" required value="<?php echo escape($currentProduct['name']); ?>">
|
|
|
|
|
|
|
+ <input type="text" id="name" name="name" required value="<?php echo escape(
|
|
|
|
|
+ $currentProduct["name"],
|
|
|
|
|
+ ); ?>">
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
|
<label for="description">Beschreibung</label>
|
|
<label for="description">Beschreibung</label>
|
|
|
- <textarea id="description" name="description" rows="4"><?php echo escape($currentProduct['description']); ?></textarea>
|
|
|
|
|
|
|
+ <textarea id="description" name="description" rows="4"><?php echo escape(
|
|
|
|
|
+ $currentProduct["description"],
|
|
|
|
|
+ ); ?></textarea>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
|
<label for="categories">Kategorien *</label>
|
|
<label for="categories">Kategorien *</label>
|
|
|
- <select id="categories" name="categories[]" multiple size="<?php echo max(3, min(8, count($categories))); ?>" required>
|
|
|
|
|
|
|
+ <select id="categories" name="categories[]" multiple size="<?php echo max(
|
|
|
|
|
+ 3,
|
|
|
|
|
+ min(8, count($categories)),
|
|
|
|
|
+ ); ?>" required>
|
|
|
<?php foreach ($categories as $category): ?>
|
|
<?php foreach ($categories as $category): ?>
|
|
|
- <option value="<?php echo escape($category['id']); ?>" <?php echo in_array($category['id'], getProductCategoryIds($currentProduct), true) ? 'selected' : ''; ?>>
|
|
|
|
|
- <?php echo escape($category['label']); ?>
|
|
|
|
|
|
|
+ <option value="<?php echo escape(
|
|
|
|
|
+ $category["id"],
|
|
|
|
|
+ ); ?>" <?php echo in_array(
|
|
|
|
|
+ $category["id"],
|
|
|
|
|
+ getProductCategoryIds($currentProduct),
|
|
|
|
|
+ true,
|
|
|
|
|
+)
|
|
|
|
|
+ ? "selected"
|
|
|
|
|
+ : ""; ?>>
|
|
|
|
|
+ <?php echo escape($category["label"]); ?>
|
|
|
</option>
|
|
</option>
|
|
|
<?php endforeach; ?>
|
|
<?php endforeach; ?>
|
|
|
</select>
|
|
</select>
|
|
@@ -257,22 +362,32 @@ if (empty($currentSizes)) {
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
|
<label for="sizes">Größen (kommagetrennt) *</label>
|
|
<label for="sizes">Größen (kommagetrennt) *</label>
|
|
|
- <input type="text" id="sizes" name="sizes" required value="<?php echo escape($currentProduct['sizes']); ?>" placeholder="Standard" oninput="updateAvailabilityFields()">
|
|
|
|
|
|
|
+ <input type="text" id="sizes" name="sizes" required value="<?php echo escape(
|
|
|
|
|
+ $currentProduct["sizes"],
|
|
|
|
|
+ ); ?>" placeholder="Standard" oninput="updateAvailabilityFields()">
|
|
|
<small>Für Artikel ohne Varianten z. B. <code>Standard</code> oder <code>Einheitsgröße</code>.</small>
|
|
<small>Für Artikel ohne Varianten z. B. <code>Standard</code> oder <code>Einheitsgröße</code>.</small>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div id="availability-group">
|
|
<div id="availability-group">
|
|
|
<?php foreach ($currentSizes as $size): ?>
|
|
<?php foreach ($currentSizes as $size): ?>
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
|
- <label>Lieferhinweis für Größe "<?php echo escape($size); ?>"</label>
|
|
|
|
|
- <textarea name="availability_<?php echo escape(str_replace([' ', ','], '_', $size)); ?>" rows="2" placeholder="Optionaler Hinweis"><?php echo escape($currentProduct['availability_labels'][$size] ?? ''); ?></textarea>
|
|
|
|
|
|
|
+ <label>Lieferhinweis für Größe "<?php echo escape(
|
|
|
|
|
+ $size,
|
|
|
|
|
+ ); ?>"</label>
|
|
|
|
|
+ <textarea name="availability_<?php echo escape(
|
|
|
|
|
+ str_replace([" ", ","], "_", $size),
|
|
|
|
|
+ ); ?>" rows="2" placeholder="Optionaler Hinweis"><?php echo escape(
|
|
|
|
|
+ $currentProduct["availability_labels"][$size] ?? "",
|
|
|
|
|
+); ?></textarea>
|
|
|
</div>
|
|
</div>
|
|
|
<?php endforeach; ?>
|
|
<?php endforeach; ?>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
|
<label for="image">Bilddateiname</label>
|
|
<label for="image">Bilddateiname</label>
|
|
|
- <input type="text" id="image" name="image" value="<?php echo escape($currentProduct['image']); ?>">
|
|
|
|
|
|
|
+ <input type="text" id="image" name="image" value="<?php echo escape(
|
|
|
|
|
+ $currentProduct["image"],
|
|
|
|
|
+ ); ?>">
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<div class="form-group">
|
|
<div class="form-group">
|
|
@@ -280,8 +395,12 @@ if (empty($currentSizes)) {
|
|
|
<input type="file" id="image_file" name="image_file" accept=".jpg,.jpeg,.png,.webp,.gif,image/*">
|
|
<input type="file" id="image_file" name="image_file" accept=".jpg,.jpeg,.png,.webp,.gif,image/*">
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- <button type="submit" name="<?php echo $editingProduct ? 'update_product' : 'add_product'; ?>" class="btn">
|
|
|
|
|
- <?php echo $editingProduct ? 'Produkt aktualisieren' : 'Produkt anlegen'; ?>
|
|
|
|
|
|
|
+ <button type="submit" name="<?php echo $editingProduct
|
|
|
|
|
+ ? "update_product"
|
|
|
|
|
+ : "add_product"; ?>" class="btn">
|
|
|
|
|
+ <?php echo $editingProduct
|
|
|
|
|
+ ? "Produkt aktualisieren"
|
|
|
|
|
+ : "Produkt anlegen"; ?>
|
|
|
</button>
|
|
</button>
|
|
|
<?php if ($editingProduct): ?>
|
|
<?php if ($editingProduct): ?>
|
|
|
<a href="products.php" class="btn btn-secondary">Abbrechen</a>
|
|
<a href="products.php" class="btn btn-secondary">Abbrechen</a>
|
|
@@ -340,25 +459,48 @@ function updateAvailabilityFields() {
|
|
|
<tbody>
|
|
<tbody>
|
|
|
<?php foreach ($products as $product): ?>
|
|
<?php foreach ($products as $product): ?>
|
|
|
<tr>
|
|
<tr>
|
|
|
- <td data-label="ID"><?php echo (int) $product['id']; ?></td>
|
|
|
|
|
- <td data-label="Name"><?php echo escape($product['name']); ?></td>
|
|
|
|
|
- <td data-label="Kategorien"><?php echo escape(implode(', ', getCategoryLabels(getProductCategoryIds($product)))); ?></td>
|
|
|
|
|
- <td data-label="Größen"><?php echo escape(implode(', ', getProductSizes($product))); ?></td>
|
|
|
|
|
|
|
+ <td data-label="ID"><?php echo (int) $product[
|
|
|
|
|
+ "id"
|
|
|
|
|
+ ]; ?></td>
|
|
|
|
|
+ <td data-label="Name"><?php echo escape(
|
|
|
|
|
+ $product["name"],
|
|
|
|
|
+ ); ?></td>
|
|
|
|
|
+ <td data-label="Kategorien"><?php echo escape(
|
|
|
|
|
+ implode(
|
|
|
|
|
+ ", ",
|
|
|
|
|
+ getCategoryLabels(
|
|
|
|
|
+ getProductCategoryIds($product),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ),
|
|
|
|
|
+ ); ?></td>
|
|
|
|
|
+ <td data-label="Größen"><?php echo escape(
|
|
|
|
|
+ implode(", ", getProductSizes($product)),
|
|
|
|
|
+ ); ?></td>
|
|
|
<td data-label="Lieferhinweise">
|
|
<td data-label="Lieferhinweise">
|
|
|
<?php
|
|
<?php
|
|
|
$labels = [];
|
|
$labels = [];
|
|
|
- foreach (($product['availability_labels'] ?? []) as $size => $label) {
|
|
|
|
|
- if (trim((string) $label) !== '') {
|
|
|
|
|
- $labels[] = $size . ': ' . $label;
|
|
|
|
|
|
|
+ foreach (
|
|
|
|
|
+ $product["availability_labels"] ?? []
|
|
|
|
|
+ as $size => $label
|
|
|
|
|
+ ) {
|
|
|
|
|
+ if (trim((string) $label) !== "") {
|
|
|
|
|
+ $labels[] = $size . ": " . $label;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- echo empty($labels) ? 'Keine' : escape(implode(' | ', $labels));
|
|
|
|
|
|
|
+ echo empty($labels)
|
|
|
|
|
+ ? "Keine"
|
|
|
|
|
+ : escape(implode(" | ", $labels));
|
|
|
?>
|
|
?>
|
|
|
</td>
|
|
</td>
|
|
|
<td data-label="Aktionen">
|
|
<td data-label="Aktionen">
|
|
|
- <a href="?edit=<?php echo (int) $product['id']; ?>" class="btn btn-small">Bearbeiten</a>
|
|
|
|
|
|
|
+ <a href="?edit=<?php echo (int) $product[
|
|
|
|
|
+ "id"
|
|
|
|
|
+ ]; ?>" class="btn btn-small">Bearbeiten</a>
|
|
|
<form method="POST" class="inline-form" onsubmit="return confirm('Produkt wirklich löschen?');">
|
|
<form method="POST" class="inline-form" onsubmit="return confirm('Produkt wirklich löschen?');">
|
|
|
- <input type="hidden" name="product_id" value="<?php echo (int) $product['id']; ?>">
|
|
|
|
|
|
|
+ <?php echo csrfField(); ?>
|
|
|
|
|
+ <input type="hidden" name="product_id" value="<?php echo (int) $product[
|
|
|
|
|
+ "id"
|
|
|
|
|
+ ]; ?>">
|
|
|
<button type="submit" name="delete_product" class="btn btn-secondary btn-small">Löschen</button>
|
|
<button type="submit" name="delete_product" class="btn btn-secondary btn-small">Löschen</button>
|
|
|
</form>
|
|
</form>
|
|
|
</td>
|
|
</td>
|
|
@@ -369,4 +511,4 @@ function updateAvailabilityFields() {
|
|
|
</div>
|
|
</div>
|
|
|
<?php endif; ?>
|
|
<?php endif; ?>
|
|
|
|
|
|
|
|
-<?php include __DIR__ . '/../includes/footer.php'; ?>
|
|
|
|
|
|
|
+<?php include __DIR__ . "/../includes/footer.php"; ?>
|