Add a Raids page fed by the guild's external raids API

New raids.php shows upcoming raids (date, requirements, gearscore,
groups and their signups) pulled from https://api.maaxxs-coding.de/api/raids
via includes/raids_api.php (cURL + X-API-Key header), gated behind a
new raids.view permission so it fits the existing permission-group
model. Fresh installs seed raids.view onto Mitglied automatically;
existing installs need to add it once via the Berechtigungsgruppen UI
(documented in the README). API/network failures show a friendly
error instead of crashing the page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Maaxxs 2026-09-18 12:44:17 +02:00
parent 00e52f2308
commit 620ef68015
10 changed files with 220 additions and 0 deletions

View file

@ -16,6 +16,8 @@ und wird später ausgebaut. Fokus dieser Version ist der Mitglieder-/Admin-Berei
- Berechtigungen: feste Kern-Berechtigungen + eigene, frei anlegbare Berechtigungs-Schlüssel
für zukünftige Funktionen
- Jeder Benutzer kann mehreren Berechtigungsgruppen angehören
- Raids-Seite: zeigt anstehende Raid-Termine, Anforderungen, Gruppen und Anmeldungen,
bezogen von einer externen API (`config.php: raids_api`)
## Ordnerstruktur
@ -59,6 +61,23 @@ Spalte `must_change_password`), muss sie nachträglich ergänzt werden:
ALTER TABLE users ADD COLUMN must_change_password TINYINT(1) NOT NULL DEFAULT 0 AFTER status;
```
### Raids-API einrichten
In `config.php` einen Abschnitt `raids_api` mit URL und Schlüssel deiner API ergänzen:
```php
'raids_api' => [
'url' => 'https://api.maaxxs-coding.de/api/raids',
'key' => 'dein-api-key',
],
```
Falls dein Account bereits vor diesem Update installiert wurde, kennt deine Datenbank die
Berechtigung `raids.view` noch nicht automatisch. Leg sie einmalig unter **Berechtigungsgruppen
-> Neue Berechtigung anlegen** mit dem Schlüssel `raids.view` an und hake sie bei den Gruppen ab,
die Raids sehen sollen (z.B. Administrator und Mitglied). Bei einer frischen Installation über
`install.php` passiert das automatisch.
## Berechtigungskonzept
- **Berechtigungen** sind einzelne Schlüssel wie `users.view`, `roles.manage`, `dashboard.view`.

View file

@ -361,6 +361,34 @@ input:focus, select:focus, textarea:focus {
.stat-card h3 { margin: 0 0 0.15rem; color: var(--text); font-size: 1rem; }
.stat-card p { margin: 0; }
.raid-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
flex-wrap: wrap;
}
.raid-header h2 { margin: 0; }
.raid-groups {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.raid-group {
background: var(--bg-panel-alt);
border: 1px solid var(--border);
border-radius: 8px;
padding: 1rem;
}
.raid-group h3 { margin: 0 0 0.5rem; font-size: 0.95rem; color: var(--text); }
.raid-group ul { margin: 0; padding-left: 1.1rem; }
.raid-group li { margin-bottom: 0.25rem; }
@media (max-width: 860px) {
.app-shell {
flex-direction: column;

View file

@ -13,4 +13,8 @@ return [
'app' => [
'name' => 'Aion2 Gilde',
],
'raids_api' => [
'url' => 'https://api.maaxxs-coding.de/api/raids',
'key' => 'dein-api-key',
],
];

View file

@ -5,6 +5,7 @@ require_permission('dashboard.view');
$user = current_user();
$canViewUsers = user_has_permission('users.view');
$canManageRoles = user_has_permission('roles.manage');
$canViewRaids = user_has_permission('raids.view');
$pageTitle = 'Dashboard';
include __DIR__ . '/includes/header.php';
@ -14,6 +15,20 @@ include __DIR__ . '/includes/header.php';
<p class="text-muted">Dies ist dein Mitgliederbereich. Die eigentliche Gilden-Homepage folgt hier später.</p>
</div>
<?php if ($canViewRaids): ?>
<div class="panel">
<div class="card-grid">
<div class="panel stat-card">
<div class="icon-badge"><?= icon('calendar') ?></div>
<div>
<h3>Raids</h3>
<p class="text-muted"><a href="/raids.php">Anstehende Raid-Termine ansehen</a></p>
</div>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($canViewUsers || $canManageRoles): ?>
<div class="panel">
<h2>Administration</h2>

View file

@ -69,6 +69,7 @@ function icon(string $name): string
'shield' => '<path d="M12 21c4.5-1.8 7-5.2 7-9.5V6l-7-3-7 3v5.5c0 4.3 2.5 7.7 7 9.5z"/>',
'log-out' => '<path d="M9 20H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h3"/><path d="m14 16 4-4-4-4"/><path d="M18 12H9"/>',
'lock' => '<rect x="4" y="11" width="16" height="9" rx="2"/><path d="M7.5 11V7.5a4.5 4.5 0 0 1 9 0V11"/>',
'calendar' => '<rect x="3" y="5" width="18" height="16" rx="2"/><path d="M16 3v4"/><path d="M8 3v4"/><path d="M3 10h18"/>',
];
$path = $paths[$name] ?? '';

View file

@ -3,10 +3,12 @@
$currentUser = current_user();
$canViewUsers = $currentUser && user_has_permission('users.view');
$canManageRoles = $currentUser && user_has_permission('roles.manage');
$canViewRaids = $currentUser && user_has_permission('raids.view');
$showAdminMenu = $canViewUsers || $canManageRoles;
$currentScript = basename((string) ($_SERVER['SCRIPT_NAME'] ?? ''));
$isDashboardActive = $currentScript === 'dashboard.php';
$isRaidsActive = $currentScript === 'raids.php';
$isUsersActive = in_array($currentScript, ['users.php', 'user_form.php', 'user_delete.php'], true);
$isRolesActive = in_array($currentScript, ['roles.php', 'role_form.php', 'role_delete.php'], true);
?>
@ -27,6 +29,11 @@ $isRolesActive = in_array($currentScript, ['roles.php', 'role_form.php', 'role_d
<a href="/dashboard.php" class="sidebar-link<?= $isDashboardActive ? ' active' : '' ?>" title="Dashboard">
<?= icon('home') ?><span class="label">Dashboard</span>
</a>
<?php if ($canViewRaids): ?>
<a href="/raids.php" class="sidebar-link<?= $isRaidsActive ? ' active' : '' ?>" title="Raids">
<?= icon('calendar') ?><span class="label">Raids</span>
</a>
<?php endif; ?>
<?php if ($showAdminMenu): ?>
<div class="sidebar-section-label">Admin</div>
<?php if ($canViewUsers): ?>

View file

@ -12,6 +12,7 @@ const CORE_PERMISSIONS = [
['key' => 'users.edit', 'description' => 'Benutzer bearbeiten'],
['key' => 'users.delete', 'description' => 'Benutzer löschen'],
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
['key' => 'raids.view', 'description' => 'Raids einsehen'],
];
function user_has_permission(string $key): bool

52
includes/raids_api.php Normal file
View file

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/**
* @return array<int, array<string, mixed>>
* @throws RuntimeException
*/
function fetch_raids(): array
{
$configPath = __DIR__ . '/../config.php';
if (!file_exists($configPath)) {
throw new RuntimeException('Konfigurationsdatei fehlt.');
}
$config = require $configPath;
$apiUrl = $config['raids_api']['url'] ?? '';
$apiKey = $config['raids_api']['key'] ?? '';
if ($apiUrl === '' || $apiKey === '') {
throw new RuntimeException('Raid-API ist nicht konfiguriert (config.php: raids_api.url / raids_api.key fehlt).');
}
if (!function_exists('curl_init')) {
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
}
$ch = curl_init($apiUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: ' . $apiKey],
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => true,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($response === false) {
throw new RuntimeException('Verbindung zur Raid-API fehlgeschlagen: ' . $curlError);
}
if ($httpCode !== 200) {
throw new RuntimeException('Raid-API antwortete mit Statuscode ' . $httpCode . '.');
}
$data = json_decode($response, true);
if (!is_array($data) || ($data['status'] ?? '') !== 'success' || !isset($data['raids']) || !is_array($data['raids'])) {
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
}
return $data['raids'];
}

View file

@ -51,6 +51,8 @@ function seed_defaults(PDO $pdo): array
$pdo->prepare('INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)')
->execute([$memberRoleId, $permissionIds['dashboard.view']]);
$pdo->prepare('INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)')
->execute([$memberRoleId, $permissionIds['raids.view']]);
return ['admin_role_id' => $adminRoleId];
}

91
raids.php Normal file
View file

@ -0,0 +1,91 @@
<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raids_api.php';
require_permission('raids.view');
$raids = [];
$apiError = null;
try {
$raids = fetch_raids();
} catch (Throwable $e) {
$apiError = $e->getMessage();
}
$pageTitle = 'Raids';
include __DIR__ . '/includes/header.php';
?>
<div class="panel">
<h1>Raids</h1>
<p class="text-muted">Anstehende Raid-Termine deiner Gilde.</p>
</div>
<?php if ($apiError): ?>
<div class="panel">
<div class="flash flash-error">Raid-Daten konnten nicht geladen werden: <?= e($apiError) ?></div>
</div>
<?php elseif (empty($raids)): ?>
<div class="panel">
<p class="text-muted">Aktuell sind keine Raids geplant.</p>
</div>
<?php else: ?>
<?php foreach ($raids as $raid): ?>
<?php
$title = (string) ($raid['title'] ?? 'Raid');
$dateLabel = (string) ($raid['date'] ?? '');
if (!empty($raid['timestamp'])) {
$dateLabel = date('d.m.Y H:i', (int) $raid['timestamp']) . ' Uhr';
}
$requirements = trim((string) ($raid['requirements'] ?? ''));
$gearscore = trim((string) ($raid['gearscore'] ?? ''));
$maxSlots = (int) ($raid['max_slots'] ?? 0);
$signups = is_array($raid['signups'] ?? null) ? $raid['signups'] : [];
$groupNames = array_values(array_filter(array_map('trim', explode(',', (string) ($raid['groups'] ?? '')))));
$signupsByGroup = [];
foreach ($signups as $signup) {
$groupNumber = (int) ($signup['group'] ?? 0);
$signupsByGroup[$groupNumber][] = $signup;
}
?>
<div class="panel">
<div class="raid-header">
<h2><?= e($title) ?></h2>
<span class="badge"><?= count($signups) ?> / <?= $maxSlots ?> angemeldet</span>
</div>
<p class="text-muted"><?= e($dateLabel) ?></p>
<?php if ($requirements !== ''): ?>
<p><strong>Anforderungen:</strong> <?= e($requirements) ?></p>
<?php endif; ?>
<?php if ($gearscore !== ''): ?>
<p><strong>Gearscore:</strong> <?= e($gearscore) ?></p>
<?php endif; ?>
<?php if (!empty($groupNames)): ?>
<div class="raid-groups">
<?php foreach ($groupNames as $index => $groupName): ?>
<?php $members = $signupsByGroup[$index + 1] ?? []; ?>
<div class="raid-group">
<h3><?= e($groupName) ?></h3>
<?php if (empty($members)): ?>
<p class="text-muted">Keine Anmeldungen</p>
<?php else: ?>
<ul>
<?php foreach ($members as $member): ?>
<li>
<?= e((string) ($member['name'] ?? '?')) ?>
<span class="text-muted">(<?= e((string) ($member['class'] ?? '')) ?>)</span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php include __DIR__ . '/includes/footer.php'; ?>