Files
domili/history.php
2025-08-20 17:53:55 +02:00

223 lines
9.8 KiB
PHP
Executable File

<?php
// PHP-Logik muss VOR dem HTML kommen!
include('inc/check_login.php');
include('inc/db.php');
include('inc/helpers.php');
// PHP-Logik für die Löschfunktion
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
$id = $_GET['id'];
$stmt = mysqli_prepare($conn, "DELETE FROM meetings WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
if (mysqli_stmt_execute($stmt)) {
// Erfolgreiche Weiterleitung
header("Location: history.php?status=deleted");
exit;
} else {
// Fehler-Meldung, falls etwas schiefgeht
$error_message = "Fehler beim Löschen des Termins.";
}
mysqli_stmt_close($stmt);
}
// Funktion zum Abrufen aller Meeting-Details
function get_all_meeting_details($conn)
{
$sql = "
SELECT
m.id AS meeting_id,
m.meeting_date,
m.reason,
c.name AS color_name,
c.hex_code,
u.username,
mt.attended,
mt.wore_color,
mt.paid
FROM meetings m
JOIN colors c ON m.color_id = c.id
LEFT JOIN meeting_teilnehmer mt ON m.id = mt.meeting_id
LEFT JOIN users u ON mt.user_id = u.id
WHERE m.meeting_date < CURDATE()
ORDER BY m.meeting_date DESC, u.username ASC
";
$result = mysqli_query($conn, $sql);
if (!$result) {
die("Fehler in der SQL-Abfrage: " . mysqli_error($conn));
}
$meetings = [];
while ($row = mysqli_fetch_assoc($result)) {
$meeting_id = $row['meeting_id'];
if (!isset($meetings[$meeting_id])) {
$meetings[$meeting_id] = [
'date' => $row['meeting_date'],
'reason' => $row['reason'],
'color_name' => $row['color_name'],
'hex_code' => $row['hex_code'],
'participants' => []
];
}
if ($row['username']) {
$meetings[$meeting_id]['participants'][] = [
'username' => $row['username'],
'attended' => $row['attended'],
'wore_color' => $row['wore_color'],
'paid' => $row['paid']
];
}
}
return $meetings;
}
// PHP-Logik zur Verarbeitung des Formulars
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit_meeting'])) {
$meeting_date = $_POST['meeting_date'];
$reason = $_POST['reason'];
$color_id = $_POST['color_id'];
if (!empty($meeting_date) && !empty($color_id)) {
$stmt = mysqli_prepare($conn, "INSERT INTO meetings (meeting_date, reason, color_id) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($stmt, "ssi", $meeting_date, $reason, $color_id);
if (mysqli_stmt_execute($stmt)) {
header("Location: history.php?status=success");
exit;
} else {
$error_message = "Fehler beim Hinzufügen des Treffens: " . mysqli_error($conn);
}
mysqli_stmt_close($stmt);
} else {
$error_message = "Datum und Farbe sind Pflichtfelder.";
}
}
// Farben für das Formular abrufen
$colors_result = mysqli_query($conn, "SELECT id, name, hex_code FROM colors ORDER BY name");
$colors = mysqli_fetch_all($colors_result, MYSQLI_ASSOC);
// Hier wird die Funktion aufgerufen, nachdem sie definiert wurde
$all_meetings = get_all_meeting_details($conn);
?>
<?php
// Erst jetzt wird der Header inkludiert und HTML gesendet
include('inc/header.php');
?>
<div class="container mt-5">
<div class="collapse mt-3" id="addMeetingForm">
<div class="card shadow mb-4">
<div class="card-body">
<?php if (isset($_GET['status']) && $_GET['status'] == 'success'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
Treffen erfolgreich hinzugefügt.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php elseif (isset($_GET['status']) && $_GET['status'] == 'deleted'): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
Treffen erfolgreich gelöscht.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php elseif (isset($error_message)): ?>
<div class="alert alert-danger" role="alert">
<?= $error_message ?>
</div>
<?php endif; ?>
<form action="history.php" method="POST">
<div class="row">
<div class="col-md-4 mb-3">
<label for="meeting_date" class="form-label">Datum des Treffens</label>
<input type="date" class="form-control" id="meeting_date" name="meeting_date" required>
</div>
<div class="col-md-4 mb-3">
<label for="color_id" class="form-label">Farbvorgabe</label>
<select class="form-select" id="color_id" name="color_id" required>
<option value="">Wähle eine Farbe...</option>
<?php foreach ($colors as $color): ?>
<option value="<?= $color['id'] ?>"><?= htmlspecialchars($color['name']) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-4 mb-3">
<label for="reason" class="form-label">Grund (optional)</label>
<input type="text" class="form-control" id="reason" name="reason">
</div>
</div>
<button type="submit" name="submit_meeting" class="btn btn-primary">Treffen hinzufügen</button>
</form>
</div>
</div>
<hr class="mt-4 mb-4">
</div>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>Termin-History</h2>
<a class="btn btn-sm btn-outline-primary" data-bs-toggle="collapse" href="#addMeetingForm" role="button" aria-expanded="false" aria-controls="addMeetingForm">
<span class="material-symbols-outlined">add</span>
</a>
</div>
<?php if (empty($all_meetings)): ?>
<div class="alert alert-info text-center" role="alert">
Bisher wurden keine Treffen erfasst.
</div>
<?php else: ?>
<?php foreach ($all_meetings as $meeting_id => $meeting): ?>
<div class="card shadow mb-4">
<div class="card-header d-flex justify-content-between align-items-center" style="background-color: rgba(<?= hexToRgb($meeting['hex_code']) ?>, 0.25);">
<div class="mb-0 fs-6 fs-md-5 fw-bold" style="color: <?= get_readable_text_color($meeting['hex_code']) ?>;">
<span class="fw-normal me-2">am</span>
<?= date('d.m.y', strtotime($meeting['date'])) ?>
</div>
<div>
<a href="admin/participant.php?id=<?= $meeting_id ?>" style="text-decoration: none; border: none; outline: none;">
<span class="material-symbols-outlined" style="color: <?= get_readable_text_color($meeting['hex_code']) ?>;">edit_calendar</span>
</a>
<a href="history.php?action=delete&id=<?= $meeting_id ?>" class="ms-1" style="text-decoration: none; border: none; outline: none;" onclick="return confirm('Möchtest du diesen Termin wirklich löschen?');">
<span class="material-symbols-outlined text-danger">delete_outline</span>
</a>
</div>
</div>
<div class="card-body">
<div class="d-flex flex-column text-muted fst-italic mb-2">
<span>Farbe: <span class="fw-bold"><?= htmlspecialchars($meeting['color_name']) ?></span></span>
<span>Grund: <?= htmlspecialchars($meeting['reason']) ?></span>
</div>
<h6 class="mb-2">Teilnehmer:</h6>
<?php if (empty($meeting['participants'])): ?>
<p class="text-muted">Noch keine Teilnehmer erfasst.</p>
<?php else: ?>
<ul class="list-unstyled">
<?php foreach ($meeting['participants'] as $participant): ?>
<li>
<?php
$status_icon = '❌';
$status_text = 'Nicht dabei';
if ($participant['attended']) {
$status_icon = $participant['wore_color'] ? '✅' : '🔴';
$status_text = $participant['wore_color'] ? 'Farbe getragen' : 'Falsche Farbe';
}
$paid_icon = $participant['paid'] ? '💰' : '';
?>
<?= $status_icon ?>
<span class="fw-bold"><?= htmlspecialchars($participant['username']) ?></span>
<?= $paid_icon ?>
<small class="text-muted ms-2"><?= $status_text ?></small>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php include('inc/footer.php'); ?>