| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- require_once 'admin_auth.php';
- require_once '../config/database.php';
- session_start();
- if (!isset($_SESSION['user_id'])) {
- header("Location: login.php");
- exit;
- }
- require_once '../config/database.php';
- header('Content-Type: text/html; charset=utf-8');
- $message = '';
- $product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
- if ($product_id === 0) {
- die("Δεν επιλέχθηκε προϊόν.");
- }
- // 1. Φέρνουμε τα στοιχεία του προϊόντος για να δούμε σε ποιο Group ανήκει
- $stmt = $pdo->prepare("SELECT title, attribute_group_id FROM products WHERE id = ?");
- $stmt->execute([$product_id]);
- $product = $stmt->fetch();
- if (!$product) {
- die("Το προϊόν δεν βρέθηκε.");
- }
- $group_id = $product['attribute_group_id'];
- // Αν το προϊόν δεν έχει Attribute Group, δεν μπορούμε να βάλουμε χαρακτηριστικά
- if (!$group_id) {
- $no_group_msg = true;
- } else {
- $no_group_msg = false;
-
- // Χειρισμός Αποθήκευσης Φόρμας
- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['attributes'])) {
- try {
- $pdo->beginTransaction();
-
- // Διαγράφουμε τις παλιές τιμές για αυτό το προϊόν (ώστε να κάνουμε καθαρό update)
- $delStmt = $pdo->prepare("DELETE FROM product_attribute_values WHERE product_id = ?");
- $delStmt->execute([$product_id]);
-
- // Εισάγουμε τις νέες τιμές
- $insStmt = $pdo->prepare("INSERT INTO product_attribute_values (product_id, attribute_id, value) VALUES (?, ?, ?)");
-
- foreach ($_POST['attributes'] as $attr_id => $value) {
- $value = trim($value);
- if ($value !== '') { // Αποθηκεύουμε μόνο όσα δεν είναι κενά
- $insStmt->execute([$product_id, $attr_id, $value]);
- }
- }
-
- $pdo->commit();
- $message = "<div class='alert success'>Τα χαρακτηριστικά αποθηκεύτηκαν επιτυχώς!</div>";
- } catch (Exception $e) {
- $pdo->rollBack();
- $message = "<div class='alert error'>Σφάλμα αποθήκευσης: " . $e->getMessage() . "</div>";
- }
- }
- // 2. Φέρνουμε τα Attributes της ομάδας + Τις τρέχουσες τιμές (αν υπάρχουν ήδη καταχωρημένες)
- // Χρησιμοποιούμε LEFT JOIN για να ενώσουμε τα χαρακτηριστικά με τις τιμές που έχει βάλει ο χρήστης
- $attrStmt = $pdo->prepare("
- SELECT a.id as attr_id, a.name as attr_name, a.unit, pav.value
- FROM attributes a
- LEFT JOIN product_attribute_values pav ON a.id = pav.attribute_id AND pav.product_id = ?
- WHERE a.group_id = ?
- ORDER BY a.name
- ");
- $attrStmt->execute([$product_id, $group_id]);
- $product_attributes = $attrStmt->fetchAll();
- }
- ?>
- <!DOCTYPE html>
- <html lang="el">
- <head>
- <meta charset="UTF-8">
- <title>Χαρακτηριστικά: <?php echo htmlspecialchars($product['title']); ?></title>
- <style>
- body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f6f9; margin: 0; }
- .sidebar { width: 250px; background: #343a40; color: white; position: fixed; height: 100vh; padding-top: 20px; }
- .sidebar a { display: block; color: white; padding: 15px; text-decoration: none; }
- .sidebar a:hover { background: #495057; }
- .content { margin-left: 250px; padding: 20px; }
- .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; }
- .form-group { margin-bottom: 20px; }
- .form-group label { display: block; margin-bottom: 8px; font-weight: bold; color: #333; }
- .input-group { display: flex; align-items: center; }
- .input-group input { flex: 1; padding: 10px; border: 1px solid #ccc; border-radius: 4px 0 0 4px; box-sizing: border-box; }
- .input-group .unit { background: #e9ecef; padding: 10px 15px; border: 1px solid #ccc; border-left: none; border-radius: 0 4px 4px 0; color: #555; }
- .btn { padding: 10px 20px; background: #28a745; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
- .btn:hover { background: #218838; }
- .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; }
- .alert { padding: 15px; border-radius: 4px; margin-bottom: 20px; }
- .success { background: #d1e7dd; color: #0f5132; border: 1px solid #badbcc; }
- .error { background: #f8d7da; color: #842029; border: 1px solid #f5c2c7; }
- .info { background: #fff3cd; color: #856404; border: 1px solid #ffeeba; }
- </style>
- </head>
- <body>
-
- <?php include 'sidebar.php'; ?>
- <div class="content">
- <h2>Συμπλήρωση Χαρακτηριστικών</h2>
- <h4 style="color: #666; margin-top: -15px;">Προϊόν: <?php echo htmlspecialchars($product['title']); ?></h4>
-
- <?php echo $message; ?>
- <div class="card">
- <?php if ($no_group_msg): ?>
- <div class="alert info">
- Αυτό το προϊόν δημιουργήθηκε χωρίς "Ομάδα Χαρακτηριστικών".
- Μπορείτε να του αναθέσετε μια ομάδα επεξεργάζοντας το προϊόν.
- </div>
- <a href="products.php" class="btn-back">Επιστροφή στα Προϊόντα</a>
- <?php else: ?>
-
- <form method="POST" action="product_attributes.php?id=<?php echo $product_id; ?>">
-
- <?php foreach($product_attributes as $attr): ?>
- <div class="form-group">
- <label><?php echo htmlspecialchars($attr['attr_name']); ?></label>
-
- <?php if (!empty($attr['unit'])): ?>
- <div class="input-group">
- <input type="text" name="attributes[<?php echo $attr['attr_id']; ?>]" value="<?php echo htmlspecialchars($attr['value'] ?? ''); ?>">
- <span class="unit"><?php echo htmlspecialchars($attr['unit']); ?></span>
- </div>
- <?php else: ?>
- <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;">
- <?php endif; ?>
- </div>
- <?php endforeach; ?>
-
- <div style="margin-top: 30px;">
- <a href="products.php" class="btn-back">Επιστροφή</a>
- <button type="submit" class="btn">Αποθήκευση Χαρακτηριστικών</button>
- </div>
- </form>
- <?php endif; ?>
- </div>
- </div>
- </body>
- </html>
|