product.php 4.4 KB

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