Files
lootjes/admin.php
2025-11-12 15:28:40 +01:00

285 lines
8.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
session_start();
require __DIR__ . '/data/db.php';
require __DIR__ . '/functions/logging.php';
require __DIR__ . '/functions/ldap_groups.php';
$config = require __DIR__ . '/config/config.php';
// --- Alleen admin toegang ---
if (!isset($_SESSION['user']) || !in_array($_SESSION['user']['username'], $config['admin_usernames'])) {
die("Toegang geweigerd. Alleen admin.");
}
$message = '';
$members = get_group_members('APP_LootjesTrekken');
// --- Uitloggen ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['logout'])) {
log_action($pdo, $_SESSION['user']['username'], 'Uitloggen via admin', 'Admin pagina');
session_unset();
session_destroy();
header('Location: login.php');
exit;
}
// --- Nieuwe ronde starten ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['start_round'])) {
if (count($members) < 2) {
$message = "Niet genoeg deelnemers om een ronde te starten.";
} else {
try {
$rondeNaam = "Ronde " . date('Y-m-d H:i:s');
$pdo->beginTransaction();
$stmt = $pdo->prepare("INSERT INTO lootjes_rondes (naam) VALUES (?)");
$stmt->execute([$rondeNaam]);
$ronde_id = $pdo->lastInsertId();
$gevers = $members;
$ontvangers = $members;
$maxRetries = 1000;
$retry = 0;
do {
shuffle($ontvangers);
$valid = true;
for ($i = 0; $i < count($gevers); $i++) {
if ($gevers[$i] === $ontvangers[$i]) {
$valid = false;
break;
}
}
$retry++;
if ($retry > $maxRetries) {
throw new Exception("Kon geen geldige trekking genereren na $maxRetries pogingen.");
}
} while (!$valid);
$stmtInsert = $pdo->prepare("INSERT INTO lootjes (ronde_id, gever, ontvanger) VALUES (?, ?, ?)");
for ($i = 0; $i < count($gevers); $i++) {
$stmtInsert->execute([$ronde_id, $gevers[$i], $ontvangers[$i]]);
}
$pdo->commit();
log_action($pdo, $_SESSION['user']['username'], "Nieuwe ronde gestart: $rondeNaam", "Admin pagina");
$message = "✅ Nieuwe ronde '$rondeNaam' succesvol gestart!";
} catch (Exception $e) {
$pdo->rollBack();
$message = "❌ Fout bij starten van nieuwe ronde: " . $e->getMessage();
}
}
}
// --- Ronde afronden ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['close_round'])) {
$ronde_id = $_POST['round_id'] ?? 0;
if ($ronde_id) {
$stmt = $pdo->prepare("UPDATE lootjes_rondes SET status='afgerond' WHERE id=?");
$stmt->execute([$ronde_id]);
log_action($pdo, $_SESSION['user']['username'], "Ronde $ronde_id afgerond", "Admin pagina");
$message = "Ronde afgerond!";
}
}
// --- Ronde hernoemen ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rename_round'])) {
$ronde_id = $_POST['round_id'] ?? 0;
$new_name = trim($_POST['new_name'] ?? '');
if ($ronde_id && $new_name !== '') {
$stmt = $pdo->prepare("UPDATE lootjes_rondes SET naam=? WHERE id=?");
$stmt->execute([$new_name, $ronde_id]);
log_action($pdo, $_SESSION['user']['username'], "Ronde $ronde_id hernoemd naar '$new_name'", "Admin pagina");
$message = "Naam van ronde aangepast naar: " . htmlspecialchars($new_name);
} else {
$message = "❌ Ongeldige naam opgegeven.";
}
}
// --- Alle rondes ophalen ---
try {
$stmt = $pdo->query("SELECT * FROM lootjes_rondes ORDER BY created_at DESC");
$rondes = $stmt->fetchAll();
} catch (PDOException $e) {
die("Fout bij ophalen van rondes: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin - Lootjesbeheer</title>
<style>
:root {
--primary: #4c8bf5;
--accent: #2e6ae3;
--background: #f7f9fc;
--card-bg: #fff;
--border: #ddd;
--text: #333;
}
body {
font-family: "Segoe UI", Roboto, sans-serif;
background: var(--background);
margin: 0;
color: var(--text);
}
header {
background: var(--primary);
color: white;
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
}
header h1 {
margin: 0;
font-size: 1.3em;
}
nav a {
color: white;
margin-left: 20px;
text-decoration: none;
font-weight: 600;
}
nav a:hover {
text-decoration: underline;
}
main {
max-width: 1000px;
margin: 30px auto;
background: var(--card-bg);
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
padding: 30px 40px;
}
h2, h3 {
color: var(--accent);
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
th {
background: #f4f4f4;
}
button {
padding: 6px 12px;
border: none;
border-radius: 6px;
cursor: pointer;
}
button:hover {
opacity: 0.9;
}
button.logout { background: white; color: var(--primary); }
button.close { background: #e67e22; color: white; }
button.start { background: var(--accent); color: white; }
button.rename { background: #8e44ad; color: white; }
.message {
margin: 15px 0;
padding: 10px;
border-radius: 6px;
background: #eef3ff;
border: 1px solid #cdd7fb;
}
footer {
text-align: center;
font-size: 0.8em;
color: #777;
margin-top: 30px;
padding-bottom: 20px;
}
input.rename-input {
padding: 4px 6px;
width: 200px;
font-size: 0.9em;
}
</style>
</head>
<body>
<header>
<h1>🎩 Adminpaneel - Lootjesbeheer</h1>
<nav>
<a href="index.php">🏠 Terug naar overzicht</a>
<a href="show_log.php">📜 Bekijk log</a>
<form method="post" style="display:inline;">
<button type="submit" name="logout" class="logout">🚪 Afmelden</button>
</form>
</nav>
</header>
<main>
<?php if ($message): ?>
<div class="message"><?= htmlspecialchars($message) ?></div>
<?php endif; ?>
<form method="post" style="margin-bottom:20px;">
<button type="submit" name="start_round" class="start"> Nieuwe ronde starten</button>
</form>
<h3>📅 Rondes</h3>
<table>
<tr><th>ID</th><th>Naam</th><th>Status</th><th>Acties</th><th>Lootjes</th></tr>
<?php foreach ($rondes as $r): ?>
<tr>
<td><?= $r['id'] ?></td>
<td>
<form method="post" style="display:flex; gap:6px; align-items:center;">
<input type="hidden" name="round_id" value="<?= $r['id'] ?>">
<input type="text" name="new_name" class="rename-input" value="<?= htmlspecialchars($r['naam']) ?>">
<button type="submit" name="rename_round" class="rename">💾 Opslaan</button>
</form>
</td>
<td><?= htmlspecialchars($r['status']) ?></td>
<td>
<?php if ($r['status'] === 'open'): ?>
<form method="post" style="display:inline;">
<input type="hidden" name="round_id" value="<?= $r['id'] ?>">
<button type="submit" name="close_round" class="close">Ronde afronden</button>
</form>
<?php else: ?>
✅ Afgerond
<?php endif; ?>
</td>
<td>
<?php
// Toon lootjes alleen als ronde is afgerond
if ($r['status'] === 'afgerond') {
$stmtLootjes = $pdo->prepare("SELECT gever, ontvanger FROM lootjes WHERE ronde_id=?");
$stmtLootjes->execute([$r['id']]);
$lootjes = $stmtLootjes->fetchAll();
if ($lootjes):
?>
<ul style="margin:0; padding-left:15px;">
<?php foreach ($lootjes as $l): ?>
<li><?= htmlspecialchars($l['gever']) ?> → <?= htmlspecialchars($l['ontvanger']) ?></li>
<?php endforeach; ?>
</ul>
<?php
else:
echo "Geen lootjes gevonden";
endif;
} else {
echo "<em>🔒 Verborgen tot de ronde is afgerond</em>";
}
?>
</td>
</tr>
<?php endforeach; ?>
</table>
</main>
<footer>
&copy; <?= date('Y') ?> Lootjes Trekking — Beheerderspaneel 🎁
</footer>
</body>
</html>