Add a Premades management page mirroring the Discord bot's /premade
Ports the bot's premade feature set (cogs/premades.py) to the website using the raid API's existing, already-deployed premade endpoints - no bot/API changes needed: - Overview of all premades: lead, accepted members, open invites - Found/rename/dissolve your own premade - Invite a linked website member (sends the same Discord DM with Accept/Decline buttons the bot itself sends), remove a member, transfer leadership - "Meine Einladungen": accept/decline invites addressed to you - premades.manage lets admins manage any premade, not just their own, mirroring the bot's admin-only "Admin-Verwaltung" flow New permissions premades.view/premades.manage (view granted to the default Mitglied role on fresh installs; existing installs need to add them manually, documented in README.md). Every mutation is re-validated server-side against the current premade list (ownership, one-premade-per-person, can't remove the lead, accept/decline only for an invite that's actually pending for that user) since the UI's button visibility alone isn't an authorization check. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
70f050dc19
commit
4d4a030b95
8 changed files with 593 additions and 30 deletions
11
README.md
11
README.md
|
|
@ -139,6 +139,17 @@ das schon unterstützt. Zusätzlich gibt es die neue Berechtigung `raids.manage`
|
|||
`raids.view` nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden muss, falls dein
|
||||
Account vor diesem Update installiert wurde.
|
||||
|
||||
### Premades
|
||||
|
||||
Bildet die Premade-Verwaltung des Discord-Bots (`/premade ...`) auf der Website nach: Gründen,
|
||||
Umbenennen, Mitglieder einladen (per Discord-PN mit Annehmen/Ablehnen, genau wie im Bot) bzw.
|
||||
entfernen, Leitung übertragen, Auflösen. Nutzt ausschließlich bereits vorhandene Endpunkte der
|
||||
Bot-API (`/api/premades*`), es sind keine Änderungen an der Bot-API nötig.
|
||||
|
||||
Neue Berechtigungen `premades.view` (Premades einsehen, eigene verwalten) und `premades.manage`
|
||||
(alle Premades verwalten, nicht nur die eigene) müssen bei einem vor diesem Update installierten
|
||||
Account nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden.
|
||||
|
||||
## Berechtigungskonzept
|
||||
|
||||
- **Berechtigungen** sind einzelne Schlüssel wie `users.view`, `roles.manage`, `dashboard.view`.
|
||||
|
|
|
|||
|
|
@ -748,6 +748,7 @@ input:focus, select:focus, textarea:focus {
|
|||
|
||||
.raid-premade-row {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
align-items: center;
|
||||
|
|
@ -755,6 +756,46 @@ input:focus, select:focus, textarea:focus {
|
|||
|
||||
.raid-premade-row select { width: auto; }
|
||||
|
||||
.premade-member-list {
|
||||
list-style: none;
|
||||
margin: 0.75rem 0 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.premade-member-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 0.5rem;
|
||||
padding: 0.4rem 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.premade-member-list li:last-child { border-bottom: none; }
|
||||
|
||||
.premade-member-pending { color: var(--text-muted); }
|
||||
|
||||
.premade-invite-row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 0.75rem;
|
||||
padding: 0.6rem 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.premade-invite-row:last-child { border-bottom: none; }
|
||||
|
||||
.premade-manage {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
margin-top: 1rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px dashed var(--border);
|
||||
}
|
||||
|
||||
.raid-empty-hint { margin: 0; }
|
||||
|
||||
/* Drag & Drop nur optisch aktiv, wenn die Karte im Editor-Modus ist */
|
||||
|
|
|
|||
|
|
@ -4,12 +4,14 @@ $currentUser = current_user();
|
|||
$canViewUsers = $currentUser && user_has_permission('users.view');
|
||||
$canManageRoles = $currentUser && user_has_permission('roles.manage');
|
||||
$canViewRaids = $currentUser && user_has_permission('raids.view');
|
||||
$canViewPremades = $currentUser && user_has_permission('premades.view');
|
||||
$showAdminMenu = $canViewUsers || $canManageRoles;
|
||||
|
||||
$currentScript = basename((string) ($_SERVER['SCRIPT_NAME'] ?? ''));
|
||||
$isDashboardActive = $currentScript === 'dashboard.php';
|
||||
$isProfileActive = $currentScript === 'profile.php';
|
||||
$isRaidsActive = $currentScript === 'raids.php';
|
||||
$isPremadesActive = $currentScript === 'premades.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);
|
||||
|
||||
|
|
@ -18,6 +20,7 @@ $pageBackgrounds = [
|
|||
'dashboard.php' => 'bg-dashboard',
|
||||
'profile.php' => 'bg-profile',
|
||||
'raids.php' => 'bg-raids',
|
||||
'premades.php' => 'bg-raids',
|
||||
'users.php' => 'bg-admin',
|
||||
'user_form.php' => 'bg-admin',
|
||||
'user_delete.php' => 'bg-admin',
|
||||
|
|
@ -53,6 +56,11 @@ $pageBackgroundClass = $pageBackgrounds[$currentScript] ?? '';
|
|||
<?= icon('calendar') ?><span class="label">Raids</span>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<?php if ($canViewPremades): ?>
|
||||
<a href="/premades.php" class="sidebar-link<?= $isPremadesActive ? ' active' : '' ?>" title="Premades">
|
||||
<?= icon('shield') ?><span class="label">Premades</span>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
<?php if ($showAdminMenu): ?>
|
||||
<div class="sidebar-section-label">Admin</div>
|
||||
<?php if ($canViewUsers): ?>
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ const CORE_PERMISSIONS = [
|
|||
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
|
||||
['key' => 'raids.view', 'description' => 'Raids einsehen'],
|
||||
['key' => 'raids.manage', 'description' => 'Raids archivieren/wiederherstellen'],
|
||||
['key' => 'premades.view', 'description' => 'Premade-Gruppen einsehen'],
|
||||
['key' => 'premades.manage', 'description' => 'Alle Premade-Gruppen verwalten (nicht nur die eigene)'],
|
||||
];
|
||||
|
||||
function user_has_permission(string $key): bool
|
||||
|
|
|
|||
|
|
@ -182,49 +182,177 @@ function invite_premade(int $raidId, int $premadeId, int $group): int
|
|||
return (int) ($data['invited_count'] ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fuehrt einen Request gegen die Premades-API aus (liegt unter derselben Basis-URL
|
||||
* wie die Raid-API, eine Ebene ueber "/raids") und liefert die dekodierte
|
||||
* JSON-Antwort zurueck. Analog zu raids_api_request(), nur mit eigener Basis-URL.
|
||||
*
|
||||
* @param array<string, mixed>|null $jsonBody
|
||||
* @return array<string, mixed>
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
function premades_api_request(string $method, string $path = '', ?array $jsonBody = null): array
|
||||
{
|
||||
if (!function_exists('curl_init')) {
|
||||
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
|
||||
}
|
||||
|
||||
$config = raids_api_config();
|
||||
// /api/premades liegt eine Ebene ueber /api/raids.
|
||||
$base = preg_replace('#/raids$#', '', $config['url']);
|
||||
$url = $base . '/premades' . $path;
|
||||
|
||||
$headers = ['X-API-Key: ' . $config['key']];
|
||||
|
||||
$ch = curl_init($url);
|
||||
$options = [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_CUSTOMREQUEST => $method,
|
||||
CURLOPT_TIMEOUT => 10,
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
];
|
||||
|
||||
if ($jsonBody !== null) {
|
||||
$headers[] = 'Content-Type: application/json';
|
||||
$options[CURLOPT_POSTFIELDS] = json_encode($jsonBody, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
$options[CURLOPT_HTTPHEADER] = $headers;
|
||||
curl_setopt_array($ch, $options);
|
||||
|
||||
$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 Premades-API fehlgeschlagen: ' . $curlError);
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
|
||||
if ($httpCode < 200 || $httpCode >= 300) {
|
||||
$detail = is_array($data) ? ($data['detail'] ?? '') : '';
|
||||
$message = 'Premades-API antwortete mit Statuscode ' . $httpCode . '.';
|
||||
if ($detail !== '') {
|
||||
$message .= ' (' . $detail . ')';
|
||||
}
|
||||
throw new RuntimeException($message);
|
||||
}
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException('Unerwartetes Antwortformat der Premades-API.');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
function fetch_premades(): 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.');
|
||||
}
|
||||
$data = premades_api_request('GET');
|
||||
|
||||
// /api/premades liegt eine Ebene ueber /api/raids.
|
||||
$base = preg_replace('#/raids$#', '', rtrim($apiUrl, '/'));
|
||||
|
||||
$ch = curl_init($base . '/premades');
|
||||
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);
|
||||
curl_close($ch);
|
||||
|
||||
if ($response === false || $httpCode !== 200) {
|
||||
throw new RuntimeException('Premades konnten nicht geladen werden.');
|
||||
}
|
||||
|
||||
$data = json_decode($response, true);
|
||||
if (!is_array($data) || ($data['status'] ?? '') !== 'success' || !is_array($data['premades'] ?? null)) {
|
||||
if (($data['status'] ?? '') !== 'success' || !is_array($data['premades'] ?? null)) {
|
||||
throw new RuntimeException('Unerwartetes Antwortformat der Premades-API.');
|
||||
}
|
||||
|
||||
return $data['premades'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{id: int, name: string, owner_id: int}
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
function create_premade(string $name, int $ownerId): array
|
||||
{
|
||||
$data = premades_api_request('POST', '', ['name' => $name, 'owner_id' => $ownerId]);
|
||||
|
||||
return [
|
||||
'id' => (int) ($data['premade_id'] ?? 0),
|
||||
'name' => (string) ($data['name'] ?? $name),
|
||||
'owner_id' => (int) ($data['owner_id'] ?? $ownerId),
|
||||
];
|
||||
}
|
||||
|
||||
/** @throws RuntimeException */
|
||||
function rename_premade(int $premadeId, string $name): void
|
||||
{
|
||||
premades_api_request('PATCH', '/' . $premadeId, ['name' => $name]);
|
||||
}
|
||||
|
||||
/** @throws RuntimeException */
|
||||
function delete_premade(int $premadeId): void
|
||||
{
|
||||
premades_api_request('DELETE', '/' . $premadeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $status 'accepted' fuegt direkt hinzu, 'pending' verschickt eine
|
||||
* Beitritts-PN mit Annehmen-/Ablehnen-Buttons ueber den Bot.
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
function add_premade_member(int $premadeId, int $userId, string $status = 'accepted'): void
|
||||
{
|
||||
premades_api_request('POST', '/' . $premadeId . '/members', ['user_id' => $userId, 'status' => $status]);
|
||||
}
|
||||
|
||||
/** @throws RuntimeException */
|
||||
function remove_premade_member(int $premadeId, int $userId): void
|
||||
{
|
||||
premades_api_request('DELETE', '/' . $premadeId . '/members/' . $userId);
|
||||
}
|
||||
|
||||
/** @throws RuntimeException */
|
||||
function transfer_premade_owner(int $premadeId, int $newOwnerId): void
|
||||
{
|
||||
premades_api_request('POST', '/' . $premadeId . '/transfer', ['new_owner_id' => $newOwnerId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt die Beziehung eines Discord-Nutzers zu einer Liste von Premades: 'owner'
|
||||
* (Lead) oder 'accepted' (Mitglied). Laut Bot-Logik kann ein Nutzer nie mehr als einer
|
||||
* Premade gleichzeitig als Owner/Mitglied angehoeren (siehe is_user_in_any_premade im Bot).
|
||||
*
|
||||
* @param array<int, array<string, mixed>> $premades
|
||||
* @return array{premade: array<string, mixed>, status: string}|null
|
||||
*/
|
||||
function find_my_premade(array $premades, int $discordId): ?array
|
||||
{
|
||||
foreach ($premades as $premade) {
|
||||
if ((int) ($premade['owner_id'] ?? 0) === $discordId) {
|
||||
return ['premade' => $premade, 'status' => 'owner'];
|
||||
}
|
||||
foreach ($premade['members'] ?? [] as $member) {
|
||||
if ((int) ($member['user_id'] ?? 0) === $discordId && ($member['status'] ?? '') === 'accepted') {
|
||||
return ['premade' => $premade, 'status' => 'accepted'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $premades
|
||||
* @return array<int, array<string, mixed>> Alle Premades, bei denen der Nutzer eine offene
|
||||
* (noch nicht angenommene) Einladung hat.
|
||||
*/
|
||||
function find_my_pending_invites(array $premades, int $discordId): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($premades as $premade) {
|
||||
foreach ($premade['members'] ?? [] as $member) {
|
||||
if ((int) ($member['user_id'] ?? 0) === $discordId && ($member['status'] ?? '') === 'pending') {
|
||||
$result[] = $premade;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sucht einen einzelnen Raid (inkl. aktueller Signups) anhand seiner ID.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ function seed_defaults(PDO $pdo): array
|
|||
->execute([$memberRoleId, $permissionIds['dashboard.view']]);
|
||||
$pdo->prepare('INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)')
|
||||
->execute([$memberRoleId, $permissionIds['raids.view']]);
|
||||
$pdo->prepare('INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)')
|
||||
->execute([$memberRoleId, $permissionIds['premades.view']]);
|
||||
|
||||
return ['admin_role_id' => $adminRoleId];
|
||||
}
|
||||
|
|
|
|||
133
premade_action.php
Normal file
133
premade_action.php
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/includes/bootstrap.php';
|
||||
require_once __DIR__ . '/includes/raids_api.php';
|
||||
require_permission('premades.view');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
redirect('/premades.php');
|
||||
}
|
||||
verify_csrf();
|
||||
|
||||
$user = current_user();
|
||||
$discordId = $user['discord_id'] ? (int) $user['discord_id'] : null;
|
||||
$canManage = user_has_permission('premades.manage');
|
||||
$action = (string) ($_POST['action'] ?? '');
|
||||
$premadeId = (int) ($_POST['premade_id'] ?? 0);
|
||||
|
||||
/**
|
||||
* Prueft serverseitig, ob der aktuelle Nutzer diese Premade verwalten darf (Lead
|
||||
* oder premades.manage) - die Sichtbarkeit der Buttons im UI allein ist keine
|
||||
* Berechtigungspruefung.
|
||||
*/
|
||||
function require_premade_access(array $premades, int $premadeId, ?int $discordId, bool $canManage): array
|
||||
{
|
||||
foreach ($premades as $premade) {
|
||||
if ((int) ($premade['id'] ?? 0) === $premadeId) {
|
||||
if ($canManage || ($discordId !== null && (int) ($premade['owner_id'] ?? 0) === $discordId)) {
|
||||
return $premade;
|
||||
}
|
||||
set_flash('error', 'Du darfst diese Premade nicht verwalten.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
}
|
||||
set_flash('error', 'Premade wurde nicht gefunden.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
|
||||
try {
|
||||
if ($action === 'create') {
|
||||
if (!$discordId) {
|
||||
set_flash('error', 'Bitte verknüpfe zuerst deinen Discord-Account in "Mein Profil".');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
$name = trim((string) ($_POST['name'] ?? ''));
|
||||
if ($name === '') {
|
||||
set_flash('error', 'Bitte einen Namen für die Premade angeben.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
|
||||
$premades = fetch_premades();
|
||||
if (find_my_premade($premades, $discordId) !== null) {
|
||||
set_flash('error', 'Du bist bereits Lead oder Mitglied einer Premade-Gruppe.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
|
||||
create_premade($name, $discordId);
|
||||
set_flash('success', 'Premade "' . $name . '" wurde gegründet.');
|
||||
} elseif ($action === 'rename') {
|
||||
$name = trim((string) ($_POST['name'] ?? ''));
|
||||
if ($name === '') {
|
||||
set_flash('error', 'Bitte einen Namen angeben.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
require_premade_access(fetch_premades(), $premadeId, $discordId, $canManage);
|
||||
rename_premade($premadeId, $name);
|
||||
set_flash('success', 'Premade wurde umbenannt.');
|
||||
} elseif ($action === 'delete') {
|
||||
require_premade_access(fetch_premades(), $premadeId, $discordId, $canManage);
|
||||
delete_premade($premadeId);
|
||||
set_flash('success', 'Premade wurde aufgelöst.');
|
||||
} elseif ($action === 'invite') {
|
||||
$premades = fetch_premades();
|
||||
require_premade_access($premades, $premadeId, $discordId, $canManage);
|
||||
$targetId = (int) ($_POST['user_id'] ?? 0);
|
||||
if ($targetId <= 0) {
|
||||
set_flash('error', 'Bitte ein Mitglied zum Einladen auswählen.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
if (find_my_premade($premades, $targetId) !== null) {
|
||||
set_flash('error', 'Dieser Spieler ist bereits Lead oder Mitglied einer Premade-Gruppe.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
add_premade_member($premadeId, $targetId, 'pending');
|
||||
set_flash('success', 'Einladung per Discord-PN verschickt.');
|
||||
} elseif ($action === 'remove_member') {
|
||||
$premade = require_premade_access(fetch_premades(), $premadeId, $discordId, $canManage);
|
||||
$targetId = (int) ($_POST['user_id'] ?? 0);
|
||||
if ($targetId === (int) ($premade['owner_id'] ?? 0)) {
|
||||
set_flash('error', 'Der Lead kann nicht als Mitglied entfernt werden. Übertrage die Leitung oder löse die Gruppe auf.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
remove_premade_member($premadeId, $targetId);
|
||||
set_flash('success', 'Mitglied wurde entfernt.');
|
||||
} elseif ($action === 'transfer') {
|
||||
require_premade_access(fetch_premades(), $premadeId, $discordId, $canManage);
|
||||
$newOwnerId = (int) ($_POST['new_owner_id'] ?? 0);
|
||||
if ($newOwnerId <= 0) {
|
||||
set_flash('error', 'Bitte ein Mitglied für die neue Leitung auswählen.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
transfer_premade_owner($premadeId, $newOwnerId);
|
||||
set_flash('success', 'Leitung wurde übertragen.');
|
||||
} elseif ($action === 'accept' || $action === 'decline') {
|
||||
if (!$discordId) {
|
||||
redirect('/premades.php');
|
||||
}
|
||||
// Nur eine tatsaechlich offene Einladung fuer diesen Nutzer darf angenommen/abgelehnt
|
||||
// werden - sonst koennte man per manipuliertem Request jeder beliebigen Premade beitreten.
|
||||
$myPending = find_my_pending_invites(fetch_premades(), $discordId);
|
||||
$hasPendingInvite = false;
|
||||
foreach ($myPending as $invite) {
|
||||
if ((int) ($invite['id'] ?? 0) === $premadeId) {
|
||||
$hasPendingInvite = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$hasPendingInvite) {
|
||||
set_flash('error', 'Keine offene Einladung für diese Premade gefunden.');
|
||||
redirect('/premades.php');
|
||||
}
|
||||
|
||||
if ($action === 'accept') {
|
||||
add_premade_member($premadeId, $discordId, 'accepted');
|
||||
set_flash('success', 'Du bist der Premade beigetreten.');
|
||||
} else {
|
||||
remove_premade_member($premadeId, $discordId);
|
||||
set_flash('success', 'Einladung abgelehnt.');
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
set_flash('error', 'Aktion fehlgeschlagen: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
redirect('/premades.php');
|
||||
238
premades.php
Normal file
238
premades.php
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/includes/bootstrap.php';
|
||||
require_once __DIR__ . '/includes/raids_api.php';
|
||||
require_permission('premades.view');
|
||||
|
||||
$user = current_user();
|
||||
$discordId = $user['discord_id'] ? (int) $user['discord_id'] : null;
|
||||
$canManage = user_has_permission('premades.manage');
|
||||
|
||||
$premades = [];
|
||||
$apiError = null;
|
||||
try {
|
||||
$premades = fetch_premades();
|
||||
} catch (Throwable $e) {
|
||||
$apiError = $e->getMessage();
|
||||
}
|
||||
|
||||
// Alle vorkommenden Discord-IDs (Owner + Mitglieder) auf Website-Accounts abbilden,
|
||||
// damit echte Anzeigenamen statt roher Discord-IDs gezeigt werden koennen.
|
||||
$allDiscordIds = [];
|
||||
foreach ($premades as $p) {
|
||||
$allDiscordIds[] = (int) ($p['owner_id'] ?? 0);
|
||||
foreach ($p['members'] ?? [] as $m) {
|
||||
$allDiscordIds[] = (int) ($m['user_id'] ?? 0);
|
||||
}
|
||||
}
|
||||
$allDiscordIds = array_values(array_unique(array_filter($allDiscordIds)));
|
||||
|
||||
$namesByDiscordId = [];
|
||||
if (!empty($allDiscordIds)) {
|
||||
$placeholders = implode(',', array_fill(0, count($allDiscordIds), '?'));
|
||||
$stmt = db()->prepare("SELECT discord_id, discord_username, display_name FROM users WHERE discord_id IN ($placeholders)");
|
||||
$stmt->execute($allDiscordIds);
|
||||
foreach ($stmt->fetchAll() as $row) {
|
||||
$name = $row['display_name'] !== '' ? $row['display_name'] : $row['discord_username'];
|
||||
$namesByDiscordId[(int) $row['discord_id']] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
function premade_name_for(int $discordId, array $namesByDiscordId): string
|
||||
{
|
||||
return $namesByDiscordId[$discordId] ?? ('Discord-Nutzer ' . $discordId);
|
||||
}
|
||||
|
||||
$myRelation = $discordId ? find_my_premade($premades, $discordId) : null;
|
||||
$myPendingInvites = $discordId ? find_my_pending_invites($premades, $discordId) : [];
|
||||
|
||||
// Kandidaten fuer "Mitglied einladen": verknuepfte Website-Nutzer, die aktuell weder
|
||||
// Lead noch akzeptiertes Mitglied einer Premade sind (identisch zur Regel im Bot).
|
||||
$busyDiscordIds = [];
|
||||
foreach ($premades as $p) {
|
||||
$busyDiscordIds[] = (int) ($p['owner_id'] ?? 0);
|
||||
foreach ($p['members'] ?? [] as $m) {
|
||||
if (($m['status'] ?? '') === 'accepted') {
|
||||
$busyDiscordIds[] = (int) ($m['user_id'] ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
$inviteCandidates = [];
|
||||
foreach (db()->query("SELECT discord_id, discord_username, display_name FROM users WHERE discord_id IS NOT NULL AND status = 'active' ORDER BY display_name, username")->fetchAll() as $row) {
|
||||
if (in_array((int) $row['discord_id'], $busyDiscordIds, true)) {
|
||||
continue;
|
||||
}
|
||||
$inviteCandidates[] = $row;
|
||||
}
|
||||
|
||||
$pageTitle = 'Premades';
|
||||
include __DIR__ . '/includes/header.php';
|
||||
?>
|
||||
<div class="panel">
|
||||
<h1>Premades</h1>
|
||||
<p class="text-muted">Feste Gruppen für Raids. Jeder kann höchstens einer Premade gleichzeitig angehören.</p>
|
||||
</div>
|
||||
|
||||
<?php if ($apiError): ?>
|
||||
<div class="panel">
|
||||
<div class="flash flash-error">Premades konnten nicht geladen werden: <?= e($apiError) ?></div>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
|
||||
<?php if (!empty($myPendingInvites)): ?>
|
||||
<div class="panel">
|
||||
<h2>Meine Einladungen</h2>
|
||||
<?php foreach ($myPendingInvites as $invite): ?>
|
||||
<div class="premade-invite-row">
|
||||
<div>
|
||||
Einladung zu <strong><?= e((string) ($invite['name'] ?? '')) ?></strong>
|
||||
von <?= e(premade_name_for((int) ($invite['owner_id'] ?? 0), $namesByDiscordId)) ?>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<form method="post" action="/premade_action.php" class="inline-form">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="accept">
|
||||
<input type="hidden" name="premade_id" value="<?= (int) ($invite['id'] ?? 0) ?>">
|
||||
<button type="submit">Annehmen</button>
|
||||
</form>
|
||||
<form method="post" action="/premade_action.php" class="inline-form">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="decline">
|
||||
<input type="hidden" name="premade_id" value="<?= (int) ($invite['id'] ?? 0) ?>">
|
||||
<button type="submit" class="button-secondary">Ablehnen</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($discordId && !$myRelation): ?>
|
||||
<div class="panel">
|
||||
<h2>Neue Premade gründen</h2>
|
||||
<form method="post" action="/premade_action.php" class="raid-premade-row">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="create">
|
||||
<input type="text" name="name" placeholder="Name der Premade-Gruppe" maxlength="50" required style="width:auto;flex:1;min-width:200px;">
|
||||
<button type="submit">Gründen</button>
|
||||
</form>
|
||||
</div>
|
||||
<?php elseif (!$discordId): ?>
|
||||
<div class="panel">
|
||||
<p class="text-muted">Verknüpfe deinen <a href="/profile.php">Discord-Account</a>, um eine Premade zu gründen oder beizutreten.</p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (empty($premades)): ?>
|
||||
<div class="panel">
|
||||
<p class="text-muted">Aktuell gibt es keine Premade-Gruppen.</p>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<?php foreach ($premades as $premade): ?>
|
||||
<?php
|
||||
$premadeId = (int) ($premade['id'] ?? 0);
|
||||
$premadeName = (string) ($premade['name'] ?? '');
|
||||
$ownerId = (int) ($premade['owner_id'] ?? 0);
|
||||
$members = is_array($premade['members'] ?? null) ? $premade['members'] : [];
|
||||
$accepted = array_filter($members, fn ($m) => ($m['status'] ?? '') === 'accepted' && (int) ($m['user_id'] ?? 0) !== $ownerId);
|
||||
$pending = array_filter($members, fn ($m) => ($m['status'] ?? '') === 'pending');
|
||||
$canManageThis = $canManage || ($discordId !== null && $ownerId === $discordId);
|
||||
?>
|
||||
<div class="panel">
|
||||
<div class="raid-header">
|
||||
<h2>⚔️ <?= e($premadeName) ?></h2>
|
||||
<div class="badge-group">
|
||||
<span class="badge">👑 <?= e(premade_name_for($ownerId, $namesByDiscordId)) ?></span>
|
||||
<span class="badge"><?= count($accepted) + 1 ?> Mitglieder</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ul class="premade-member-list">
|
||||
<?php foreach ($accepted as $member): ?>
|
||||
<li>
|
||||
<span><?= e(premade_name_for((int) ($member['user_id'] ?? 0), $namesByDiscordId)) ?></span>
|
||||
<?php if ($canManageThis): ?>
|
||||
<form method="post" action="/premade_action.php" class="inline-form">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="remove_member">
|
||||
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
|
||||
<input type="hidden" name="user_id" value="<?= (int) ($member['user_id'] ?? 0) ?>">
|
||||
<button type="submit" class="icon-button icon-button-danger" title="Aus der Premade entfernen"><?= icon('x') ?></button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
<?php foreach ($pending as $member): ?>
|
||||
<li class="premade-member-pending">
|
||||
<span>⏳ <?= e(premade_name_for((int) ($member['user_id'] ?? 0), $namesByDiscordId)) ?> <span class="text-muted">(Einladung offen)</span></span>
|
||||
<?php if ($canManageThis): ?>
|
||||
<form method="post" action="/premade_action.php" class="inline-form">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="remove_member">
|
||||
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
|
||||
<input type="hidden" name="user_id" value="<?= (int) ($member['user_id'] ?? 0) ?>">
|
||||
<button type="submit" class="icon-button icon-button-danger" title="Einladung zurückziehen"><?= icon('x') ?></button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
<?php if (empty($accepted) && empty($pending)): ?>
|
||||
<li class="text-muted">Noch keine weiteren Mitglieder.</li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
|
||||
<?php if ($canManageThis): ?>
|
||||
<div class="premade-manage">
|
||||
<form method="post" action="/premade_action.php" class="raid-premade-row">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="rename">
|
||||
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
|
||||
<input type="text" name="name" value="<?= e($premadeName) ?>" style="width:auto;flex:1;min-width:160px;">
|
||||
<button type="submit" class="button-secondary">Umbenennen</button>
|
||||
</form>
|
||||
|
||||
<?php if (!empty($inviteCandidates)): ?>
|
||||
<form method="post" action="/premade_action.php" class="raid-premade-row">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="invite">
|
||||
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
|
||||
<select name="user_id">
|
||||
<?php foreach ($inviteCandidates as $candidate): ?>
|
||||
<option value="<?= (int) $candidate['discord_id'] ?>">
|
||||
<?= e($candidate['display_name'] !== '' ? $candidate['display_name'] : $candidate['discord_username']) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<button type="submit" class="button-secondary">Einladen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($accepted)): ?>
|
||||
<form method="post" action="/premade_action.php" class="raid-premade-row">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="transfer">
|
||||
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
|
||||
<select name="new_owner_id">
|
||||
<?php foreach ($accepted as $candidate): ?>
|
||||
<option value="<?= (int) ($candidate['user_id'] ?? 0) ?>">
|
||||
<?= e(premade_name_for((int) ($candidate['user_id'] ?? 0), $namesByDiscordId)) ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<button type="submit" class="button-secondary">Leitung übertragen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<form method="post" action="/premade_action.php" class="inline-form" onsubmit="return confirm('Diese Premade wirklich auflösen?');">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
|
||||
<button type="submit" class="button-danger">Gruppe auflösen</button>
|
||||
</form>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php include __DIR__ . '/includes/footer.php'; ?>
|
||||
Loading…
Reference in a new issue