product.php 5.3 KB

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