Files
domili/history.php

178 lines
6.9 KiB
PHP
Executable File

<?php
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 is_completed = 1
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);
include('inc/header.php');
?>
<div class="container mt-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2>Termin-History</h2>
</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 bg-secondary bg-opacity-25 text-dark align-items-center">
<div class="mb-0 fs-6 fs-md-5 fw-bold">
<span class="fw-normal me-2">am</span><?= date('d.m.y', strtotime($meeting['date'])) ?>
</div>
<?php
if ($_SESSION['role'] == 'admin') {
?>
<div>
<a href="admin/participant.php?id=<?= $meeting_id ?>&source=history" style="text-decoration: none; border: none; outline: none;">
<span class="material-symbols-outlined text-dark">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>
<?php
}
?>
</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'); ?>