product.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. require_once __DIR__ . "/config.php";
  3. require_once __DIR__ . "/includes/functions.php";
  4. $productId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  5. $product = getProductById($productId);
  6. if ($product === null) {
  7. header("Location: index.php");
  8. exit();
  9. }
  10. $pageTitle = $product["name"];
  11. $sizes = getProductSizes($product);
  12. $productCategoryIds = getProductCategoryIds($product);
  13. if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['add_to_cart'])) {
  14. // Validate CSRF token
  15. if (!validateCsrfToken($_POST['csrf_token'] ?? "")) {
  16. $error = "Ungültiges Token. Bitte versuchen Sie es erneut.";
  17. } else {
  18. $size = trim((string) ($_POST['size'] ?? ""));
  19. if (
  20. !empty($sizes) &&
  21. ($size === "" || !in_array($size, $sizes, true))
  22. ) {
  23. $error = "Bitte wählen Sie eine Größe aus.";
  24. } else {
  25. $result = addCartItem($product["id"], $size);
  26. if (!$result["success"]) {
  27. $error =
  28. "Der Artikel konnte nicht in den Warenkorb gelegt werden.";
  29. } elseif ($result["status"] === "replaced") {
  30. setFlashMessage(
  31. "cart_notice",
  32. "success",
  33. "Die Größe für diesen Artikel wurde im Warenkorb aktualisiert.",
  34. );
  35. header("Location: cart.php");
  36. exit();
  37. } elseif ($result["status"] === "unchanged") {
  38. setFlashMessage(
  39. "cart_notice",
  40. "info",
  41. "Dieser Artikel ist bereits mit der gewählten Größe im Warenkorb.",
  42. );
  43. header("Location: cart.php");
  44. exit();
  45. } else {
  46. setFlashMessage(
  47. "cart_notice",
  48. "success",
  49. "Der Artikel wurde zum Warenkorb hinzugefügt.",
  50. );
  51. header("Location: cart.php");
  52. exit();
  53. }
  54. }
  55. }
  56. }
  57. include __DIR__ . "/includes/header.php";
  58. ?>
  59. <?php if (isset($error)): ?>
  60. <div class="alert alert-error">
  61. <?php echo escape($error); ?>
  62. </div>
  63. <?php endif; ?>
  64. <div class="product-detail-grid">
  65. <div>
  66. <?php $imagePath = getUploadPath($product["image"] ?? ""); ?>
  67. <?php $imageUrl = getUploadUrl($product["image"] ?? ""); ?>
  68. <?php if (
  69. $imagePath !== null &&
  70. $imageUrl !== null &&
  71. file_exists($imagePath)
  72. ): ?>
  73. <img class="product-image" src="<?php echo escape(
  74. $imageUrl,
  75. ); ?>" alt="<?php echo escape($product["name"]); ?>">
  76. <?php else: ?>
  77. <img class="product-image" src="assets/no-image.jpg" alt="Kein Bild verfügbar">
  78. <?php endif; ?>
  79. </div>
  80. <div class="product-copy">
  81. <h1><?php echo escape($product["name"]); ?></h1>
  82. <?php if (!empty($productCategoryIds)): ?>
  83. <div class="product-category-list" aria-label="Kategorien">
  84. <?php foreach ($productCategoryIds as $productCategoryId): ?>
  85. <?php $chipPalette = getCategoryChipPalette($productCategoryId); ?>
  86. <span class="category-chip" style="background-color: <?php echo escape(
  87. $chipPalette["background"],
  88. ); ?>; border-color: <?php echo escape(
  89. $chipPalette["border"],
  90. ); ?>; color: <?php echo escape($chipPalette["text"]); ?>;">
  91. <?php echo escape(getCategoryLabel($productCategoryId)); ?>
  92. </span>
  93. <?php endforeach; ?>
  94. </div>
  95. <?php endif; ?>
  96. <div class="product-description-block">
  97. <h3>Beschreibung</h3>
  98. <p class="product-description"><?php echo nl2br(
  99. escape($product["description"]),
  100. ); ?></p>
  101. </div>
  102. <form method="POST" class="product-form">
  103. <?php echo csrfField(); ?>
  104. <?php if (!empty($sizes)): ?>
  105. <div class="form-group">
  106. <label for="size">Größe *</label>
  107. <select id="size" name="size" required onchange="updateAvailabilityNotice()">
  108. <option value="">Bitte wählen</option>
  109. <?php foreach ($sizes as $sizeOption): ?>
  110. <?php $label = getAvailabilityLabel(
  111. $product,
  112. $sizeOption,
  113. ); ?>
  114. <option value="<?php echo escape(
  115. $sizeOption,
  116. ); ?>" data-label="<?php echo escape($label); ?>">
  117. <?php echo escape($sizeOption); ?>
  118. </option>
  119. <?php endforeach; ?>
  120. </select>
  121. </div>
  122. <div id="availabilityNotice" class="alert alert-warning is-hidden"></div>
  123. <?php endif; ?>
  124. <button type="submit" name="add_to_cart" class="btn btn-block">In den Warenkorb</button>
  125. </form>
  126. <?php if (!empty($sizes)): ?>
  127. <script>
  128. function updateAvailabilityNotice() {
  129. const sizeSelect = document.getElementById('size');
  130. const notice = document.getElementById('availabilityNotice');
  131. const selectedOption = sizeSelect.options[sizeSelect.selectedIndex];
  132. const text = selectedOption ? selectedOption.getAttribute('data-label') : '';
  133. if (text) {
  134. notice.textContent = text;
  135. notice.classList.remove('is-hidden');
  136. } else {
  137. notice.textContent = '';
  138. notice.classList.add('is-hidden');
  139. }
  140. }
  141. </script>
  142. <?php endif; ?>
  143. <div class="mt-4">
  144. <a href="index.php" class="btn btn-secondary">Zurück zur Übersicht</a>
  145. </div>
  146. </div>
  147. </div>
  148. <?php include __DIR__ . "/includes/footer.php"; ?>