Files
domili/admin/colors.php
2025-08-11 15:51:21 +02:00

169 lines
7.3 KiB
PHP
Executable File

<?php
require_once '../inc/check_login.php';
require_once '../inc/db.php';
$message = '';
$message_type = '';
$edit_mode = false;
$edit_color = null;
// --- Logik zum Hinzufügen, Bearbeiten und Löschen von Farben ---
// Aktion Löschen
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = mysqli_prepare($conn, "DELETE FROM colors WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)) {
$message = "Farbe erfolgreich gelöscht!";
$message_type = 'success';
} else {
$message = "Fehler beim Löschen der Farbe.";
$message_type = 'danger';
}
mysqli_stmt_close($stmt);
}
// Aktion Bearbeiten (Formular laden)
if (isset($_GET['action']) && $_GET['action'] == 'edit' && isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = mysqli_prepare($conn, "SELECT id, name, hex_code FROM colors WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$edit_color = mysqli_fetch_assoc($result);
mysqli_stmt_close($stmt);
$edit_mode = true;
}
// Aktion Hinzufügen oder Speichern (POST)
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$hex_code = $_POST['hex_code'];
$id = $_POST['id'] ?? null;
if ($id) { // Update-Logik
$stmt = mysqli_prepare($conn, "UPDATE colors SET name = ?, hex_code = ? WHERE id = ?");
mysqli_stmt_bind_param($stmt, "ssi", $name, $hex_code, $id);
if (mysqli_stmt_execute($stmt)) {
$message = "Farbe erfolgreich aktualisiert!";
$message_type = 'success';
} else {
$message = "Fehler beim Aktualisieren der Farbe.";
$message_type = 'danger';
}
mysqli_stmt_close($stmt);
} else { // Insert-Logik
$stmt = mysqli_prepare($conn, "INSERT INTO colors (name, hex_code) VALUES (?, ?)");
mysqli_stmt_bind_param($stmt, "ss", $name, $hex_code);
if (mysqli_stmt_execute($stmt)) {
$message = "Neue Farbe erfolgreich hinzugefügt!";
$message_type = 'success';
} else {
$message = "Fehler beim Hinzufügen der Farbe.";
$message_type = 'danger';
}
mysqli_stmt_close($stmt);
}
}
// --- Logik zum Auslesen aller Farben ---
$colors = [];
$result = mysqli_query($conn, "SELECT id, name, hex_code FROM colors ORDER BY name");
while ($row = mysqli_fetch_assoc($result)) {
$colors[] = $row;
}
require_once '../inc/header.php';
?>
<div class="container mt-5">
<h2 class="mb-4">Farbverwaltung</h2>
<?php if ($message) : ?>
<div id="status-message" class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
<?php echo htmlspecialchars($message); ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<div class="card shadow mb-4">
<div class="card-header bg-primary text-white">
<h4 class="mb-0"><?php echo $edit_mode ? 'Farbe bearbeiten' : 'Neue Farbe hinzufügen'; ?></h4>
</div>
<div class="card-body">
<form action="colors.php" method="post">
<?php if ($edit_mode): ?>
<input type="hidden" name="id" value="<?php echo htmlspecialchars($edit_color['id']); ?>">
<?php endif; ?>
<div class="row g-3 align-items-end">
<div class="col-md-5">
<label for="name" class="form-label">Name der Farbe</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($edit_color['name'] ?? ''); ?>" required>
<div class="form-text" style="visibility: hidden;">&nbsp;</div>
</div>
<div class="col-md-5">
<label for="hex_code" class="form-label">Hex-Code</label>
<input type="color" class="form-control form-control-color" id="hex_code" name="hex_code" value="<?php echo htmlspecialchars($edit_color['hex_code'] ?? '#'); ?>">
</div>
<div class="col-md-2 d-flex flex-column justify-content-end">
<div class="d-flex w-100">
<button type="submit" class="btn btn-<?php echo $edit_mode ? 'success' : 'primary'; ?> w-100 me-2">
<?php echo $edit_mode ? 'Speichern' : 'Hinzufügen'; ?>
</button>
<?php if ($edit_mode): ?>
<a href="colors.php" class="btn btn-secondary w-100">Abbrechen</a>
<?php endif; ?>
</div>
</div>
</div>
</form>
</div>
</div>
<div class="card shadow">
<div class="card-header bg-secondary text-white">
<h4 class="mb-0">Aktuelle Farben</h4>
</div>
<div class="card-body">
<?php if (empty($colors)): ?>
<p class="text-muted text-center">Es sind noch keine Farben vorhanden.</p>
<?php else: ?>
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Name</th>
<th>Hex-Code</th>
<th>Vorschau</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($colors as $color): ?>
<tr>
<td><?php echo htmlspecialchars($color['name']); ?></td>
<td><?php echo htmlspecialchars($color['hex_code']); ?></td>
<td>
<div class="color-preview" style="background-color: <?php echo htmlspecialchars($color['hex_code']); ?>"></div>
</td>
<td>
<a href="colors.php?action=edit&id=<?php echo htmlspecialchars($color['id']); ?>" class="text-dark me-1 text-decoration-none">
<span class="material-icons">mode_edit_outline</span>
</a>
<a href="colors.php?action=delete&id=<?php echo htmlspecialchars($color['id']); ?>" class="text-danger text-decoration-none" onclick="return confirm('Sind Sie sicher, dass Sie diese Farbe löschen möchten?');">
<span class="material-icons">delete_outline</span>
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?php include('../inc/footer.php'); ?>