product.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. } elseif (!addCartItem($product['id'], $size)) {
  17. $error = 'Der Artikel konnte nicht in den Warenkorb gelegt werden.';
  18. } else {
  19. header('Location: cart.php');
  20. exit;
  21. }
  22. }
  23. include __DIR__ . '/includes/header.php';
  24. ?>
  25. <?php if (isset($error)): ?>
  26. <div class="alert alert-error">
  27. <?php echo escape($error); ?>
  28. </div>
  29. <?php endif; ?>
  30. <div class="product-detail-grid">
  31. <div>
  32. <?php if (!empty($product['image']) && file_exists(__DIR__ . '/assets/images/' . $product['image'])): ?>
  33. <img src="<?php echo escape(SITE_URL); ?>/assets/images/<?php echo escape($product['image']); ?>" alt="<?php echo escape($product['name']); ?>" style="width: 100%; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">
  34. <?php else: ?>
  35. <div class="product-placeholder">Kein Bild verfügbar</div>
  36. <?php endif; ?>
  37. </div>
  38. <div>
  39. <h1><?php echo escape($product['name']); ?></h1>
  40. <div style="margin: 1.5rem 0 2rem;">
  41. <h3>Beschreibung</h3>
  42. <p style="margin-top: 0.5rem; line-height: 1.8;"><?php echo nl2br(escape($product['description'])); ?></p>
  43. </div>
  44. <form method="POST" style="margin-top: 2rem;">
  45. <?php if (!empty($sizes)): ?>
  46. <div class="form-group">
  47. <label for="size">Größe *</label>
  48. <select id="size" name="size" required style="width: 100%;" onchange="updateAvailabilityNotice()">
  49. <option value="">Bitte wählen</option>
  50. <?php foreach ($sizes as $sizeOption): ?>
  51. <?php $label = getAvailabilityLabel($product, $sizeOption); ?>
  52. <option value="<?php echo escape($sizeOption); ?>" data-label="<?php echo escape($label); ?>">
  53. <?php echo escape($sizeOption); ?><?php echo $label !== '' ? ' - ' . escape($label) : ''; ?>
  54. </option>
  55. <?php endforeach; ?>
  56. </select>
  57. </div>
  58. <div id="availabilityNotice" class="alert alert-warning" style="display: none;"></div>
  59. <?php endif; ?>
  60. <div class="alert alert-info">
  61. Jeder Artikel wird mit Menge 1 in den Warenkorb gelegt.
  62. </div>
  63. <button type="submit" name="add_to_cart" class="btn" style="width: 100%;">In den Warenkorb</button>
  64. </form>
  65. <?php if (!empty($sizes)): ?>
  66. <script>
  67. function updateAvailabilityNotice() {
  68. const sizeSelect = document.getElementById('size');
  69. const notice = document.getElementById('availabilityNotice');
  70. const selectedOption = sizeSelect.options[sizeSelect.selectedIndex];
  71. const text = selectedOption ? selectedOption.getAttribute('data-label') : '';
  72. if (text) {
  73. notice.textContent = text;
  74. notice.style.display = 'block';
  75. } else {
  76. notice.textContent = '';
  77. notice.style.display = 'none';
  78. }
  79. }
  80. </script>
  81. <?php endif; ?>
  82. <div style="margin-top: 2rem;">
  83. <a href="index.php" class="btn btn-secondary">Zurück zur Übersicht</a>
  84. </div>
  85. </div>
  86. </div>
  87. <?php include __DIR__ . '/includes/footer.php'; ?>