LuminaWebsite/premades.php
Maaxxs 4d4a030b95 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>
2026-09-19 08:00:45 +02:00

238 lines
12 KiB
PHP

<?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'; ?>