product_attributes.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. require_once 'admin_auth.php';
  3. require_once '../config/database.php';
  4. session_start();
  5. if (!isset($_SESSION['user_id'])) {
  6. header("Location: login.php");
  7. exit;
  8. }
  9. require_once '../config/database.php';
  10. header('Content-Type: text/html; charset=utf-8');
  11. $message = '';
  12. $product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
  13. if ($product_id === 0) {
  14. die("Δεν επιλέχθηκε προϊόν.");
  15. }
  16. // 1. Φέρνουμε τα στοιχεία του προϊόντος για να δούμε σε ποιο Group ανήκει
  17. $stmt = $pdo->prepare("SELECT title, attribute_group_id FROM products WHERE id = ?");
  18. $stmt->execute([$product_id]);
  19. $product = $stmt->fetch();
  20. if (!$product) {
  21. die("Το προϊόν δεν βρέθηκε.");
  22. }
  23. $group_id = $product['attribute_group_id'];
  24. // Αν το προϊόν δεν έχει Attribute Group, δεν μπορούμε να βάλουμε χαρακτηριστικά
  25. if (!$group_id) {
  26. $no_group_msg = true;
  27. } else {
  28. $no_group_msg = false;
  29. // Χειρισμός Αποθήκευσης Φόρμας
  30. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['attributes'])) {
  31. try {
  32. $pdo->beginTransaction();
  33. // Διαγράφουμε τις παλιές τιμές για αυτό το προϊόν (ώστε να κάνουμε καθαρό update)
  34. $delStmt = $pdo->prepare("DELETE FROM product_attribute_values WHERE product_id = ?");
  35. $delStmt->execute([$product_id]);
  36. // Εισάγουμε τις νέες τιμές
  37. $insStmt = $pdo->prepare("INSERT INTO product_attribute_values (product_id, attribute_id, value) VALUES (?, ?, ?)");
  38. foreach ($_POST['attributes'] as $attr_id => $value) {
  39. $value = trim($value);
  40. if ($value !== '') { // Αποθηκεύουμε μόνο όσα δεν είναι κενά
  41. $insStmt->execute([$product_id, $attr_id, $value]);
  42. }
  43. }
  44. $pdo->commit();
  45. $message = "<div class='alert success'>Τα χαρακτηριστικά αποθηκεύτηκαν επιτυχώς!</div>";
  46. } catch (Exception $e) {
  47. $pdo->rollBack();
  48. $message = "<div class='alert error'>Σφάλμα αποθήκευσης: " . $e->getMessage() . "</div>";
  49. }
  50. }
  51. // 2. Φέρνουμε τα Attributes της ομάδας + Τις τρέχουσες τιμές (αν υπάρχουν ήδη καταχωρημένες)
  52. // Χρησιμοποιούμε LEFT JOIN για να ενώσουμε τα χαρακτηριστικά με τις τιμές που έχει βάλει ο χρήστης
  53. $attrStmt = $pdo->prepare("
  54. SELECT a.id as attr_id, a.name as attr_name, a.unit, pav.value
  55. FROM attributes a
  56. LEFT JOIN product_attribute_values pav ON a.id = pav.attribute_id AND pav.product_id = ?
  57. WHERE a.group_id = ?
  58. ORDER BY a.name
  59. ");
  60. $attrStmt->execute([$product_id, $group_id]);
  61. $product_attributes = $attrStmt->fetchAll();
  62. }
  63. ?>
  64. <!DOCTYPE html>
  65. <html lang="el">
  66. <head>
  67. <meta charset="UTF-8">
  68. <title>Χαρακτηριστικά: <?php echo htmlspecialchars($product['title']); ?></title>
  69. <style>
  70. body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f6f9; margin: 0; }
  71. .sidebar { width: 250px; background: #343a40; color: white; position: fixed; height: 100vh; padding-top: 20px; }
  72. .sidebar a { display: block; color: white; padding: 15px; text-decoration: none; }
  73. .sidebar a:hover { background: #495057; }
  74. .content { margin-left: 250px; padding: 20px; }
  75. .card { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); max-width: 600px; margin-top: 20px; }
  76. .form-group { margin-bottom: 20px; }
  77. .form-group label { display: block; margin-bottom: 8px; font-weight: bold; color: #333; }
  78. .input-group { display: flex; align-items: center; }
  79. .input-group input { flex: 1; padding: 10px; border: 1px solid #ccc; border-radius: 4px 0 0 4px; box-sizing: border-box; }
  80. .input-group .unit { background: #e9ecef; padding: 10px 15px; border: 1px solid #ccc; border-left: none; border-radius: 0 4px 4px 0; color: #555; }
  81. .btn { padding: 10px 20px; background: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
  82. .btn:hover { background: #218838; }
  83. .btn-back { padding: 10px 20px; background: #6c757d; color: white; border: none; border-radius: 4px; cursor: pointer; text-decoration: none; display: inline-block; margin-right: 10px; }
  84. .alert { padding: 15px; border-radius: 4px; margin-bottom: 20px; }
  85. .success { background: #d1e7dd; color: #0f5132; border: 1px solid #badbcc; }
  86. .error { background: #f8d7da; color: #842029; border: 1px solid #f5c2c7; }
  87. .info { background: #fff3cd; color: #856404; border: 1px solid #ffeeba; }
  88. </style>
  89. </head>
  90. <body>
  91. <?php include 'sidebar.php'; ?>
  92. <div class="content">
  93. <h2>Συμπλήρωση Χαρακτηριστικών</h2>
  94. <h4 style="color: #666; margin-top: -15px;">Προϊόν: <?php echo htmlspecialchars($product['title']); ?></h4>
  95. <?php echo $message; ?>
  96. <div class="card">
  97. <?php if ($no_group_msg): ?>
  98. <div class="alert info">
  99. Αυτό το προϊόν δημιουργήθηκε χωρίς "Ομάδα Χαρακτηριστικών".
  100. Μπορείτε να του αναθέσετε μια ομάδα επεξεργάζοντας το προϊόν.
  101. </div>
  102. <a href="products.php" class="btn-back">Επιστροφή στα Προϊόντα</a>
  103. <?php else: ?>
  104. <form method="POST" action="product_attributes.php?id=<?php echo $product_id; ?>">
  105. <?php foreach($product_attributes as $attr): ?>
  106. <div class="form-group">
  107. <label><?php echo htmlspecialchars($attr['attr_name']); ?></label>
  108. <?php if (!empty($attr['unit'])): ?>
  109. <div class="input-group">
  110. <input type="text" name="attributes[<?php echo $attr['attr_id']; ?>]" value="<?php echo htmlspecialchars($attr['value'] ?? ''); ?>">
  111. <span class="unit"><?php echo htmlspecialchars($attr['unit']); ?></span>
  112. </div>
  113. <?php else: ?>
  114. <input type="text" name="attributes[<?php echo $attr['attr_id']; ?>]" value="<?php echo htmlspecialchars($attr['value'] ?? ''); ?>" style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box;">
  115. <?php endif; ?>
  116. </div>
  117. <?php endforeach; ?>
  118. <div style="margin-top: 30px;">
  119. <a href="products.php" class="btn-back">Επιστροφή</a>
  120. <button type="submit" class="btn">Αποθήκευση Χαρακτηριστικών</button>
  121. </div>
  122. </form>
  123. <?php endif; ?>
  124. </div>
  125. </div>
  126. </body>
  127. </html>