Dashboard: show own premade in a read-only roster widget

Adds a "Meine Premade" panel to the dashboard when the user belongs to
one, reusing the same 5-slot roster markup as the Premades page (read
only, links to /premades.php for management). Extracted the premade
name/avatar/slot-building helpers that premades.php already had into
includes/raids_api.php so both pages can share them instead of
duplicating the logic.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Maaxxs 2026-09-20 01:31:34 +02:00
parent 8d82065220
commit c8d9fb6baf
3 changed files with 135 additions and 36 deletions

View file

@ -7,7 +7,8 @@ $user = current_user();
$canViewUsers = user_has_permission('users.view');
$canManageRoles = user_has_permission('roles.manage');
$canViewRaids = user_has_permission('raids.view');
$discordId = $user['discord_id'] ?? null;
$canViewPremades = user_has_permission('premades.view');
$discordId = $user['discord_id'] ? (int) $user['discord_id'] : null;
$roleStmt = db()->prepare(
"SELECT COALESCE(NULLIF(r.display_name, ''), r.name) AS name
@ -44,6 +45,39 @@ $nextRaids = array_slice($upcomingRaids, 0, 3);
$memberCount = $canViewUsers ? (int) db()->query("SELECT COUNT(*) FROM users WHERE status = 'active'")->fetchColumn() : null;
$roleCount = $canManageRoles ? (int) db()->query('SELECT COUNT(*) FROM roles')->fetchColumn() : null;
// Eigene Premade fuer die Dashboard-Kurzuebersicht laden (nur Anzeige, keine Verwaltung -
// dafuer verweist das Widget auf /premades.php). Faellt bei Bot-Ausfall o.ae. still weg,
// das restliche Dashboard soll trotzdem laden.
$myPremade = null;
$myPremadeNamesByDiscordId = [];
$myPremadeLinkedUsersByDiscordId = [];
if ($canViewPremades && $discordId) {
try {
$myRelation = find_my_premade(fetch_premades(), $discordId);
if ($myRelation !== null) {
$myPremade = $myRelation['premade'];
$premadeDiscordIds = [(int) ($myPremade['owner_id'] ?? 0)];
foreach ($myPremade['members'] ?? [] as $m) {
$premadeDiscordIds[] = (int) ($m['user_id'] ?? 0);
}
$premadeDiscordIds = array_values(array_unique(array_filter($premadeDiscordIds)));
if (!empty($premadeDiscordIds)) {
$placeholders = implode(',', array_fill(0, count($premadeDiscordIds), '?'));
$stmt = db()->prepare("SELECT id, discord_id, discord_username, display_name, avatar_filename FROM users WHERE discord_id IN ($placeholders)");
$stmt->execute($premadeDiscordIds);
foreach ($stmt->fetchAll() as $row) {
$name = $row['display_name'] !== '' ? $row['display_name'] : $row['discord_username'];
$myPremadeNamesByDiscordId[(int) $row['discord_id']] = $name;
$myPremadeLinkedUsersByDiscordId[(int) $row['discord_id']] = $row;
}
}
}
} catch (Throwable $e) {
$myPremade = null;
}
}
$pageTitle = 'Dashboard';
include __DIR__ . '/includes/header.php';
?>
@ -157,4 +191,48 @@ include __DIR__ . '/includes/header.php';
</div>
<?php endif; ?>
<?php if ($myPremade): ?>
<div class="panel">
<div class="raid-header">
<h2>⚔️ Meine Premade: <?= e((string) ($myPremade['name'] ?? '')) ?></h2>
<a href="/premades.php">Zu den Premades &rarr;</a>
</div>
<div class="premade-roster">
<?php foreach (build_premade_slots($myPremade, $myPremadeNamesByDiscordId) as $slot): ?>
<?php if ($slot['type'] === 'empty'): ?>
<div class="premade-slot premade-slot-empty">
<div class="premade-slot-inner">
<span class="premade-slot-placeholder">+</span>
</div>
</div>
<?php else: ?>
<?php $slotUser = premade_avatar_user((int) $slot['id'], $myPremadeLinkedUsersByDiscordId, $slot['name']); ?>
<div class="premade-slot premade-slot-<?= e($slot['type']) ?>">
<div class="premade-slot-inner">
<?= avatar_html($slotUser, 40) ?>
<div class="premade-slot-name">
<strong><?= $slot['type'] === 'pending' ? '⏳ ' : '' ?><?= e($slot['name']) ?></strong>
<?php if ($slot['type'] === 'pending'): ?>
<span class="text-muted">Einladung offen</span>
<?php elseif (!empty($slot['class'])): ?>
<?php $slotClassIcon = class_icon_url($slot['class']); ?>
<span class="premade-slot-class">
<?php if ($slotClassIcon): ?>
<img class="premade-slot-class-icon" src="<?= e($slotClassIcon) ?>" alt="">
<?php endif; ?>
<span class="text-muted"><?= e($slot['class']) ?></span>
</span>
<?php endif; ?>
</div>
<?php if ($slot['type'] === 'lead'): ?>
<span class="premade-crown" title="Lead der Premade"><?= icon('crown') ?></span>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<?php include __DIR__ . '/includes/footer.php'; ?>

View file

@ -390,6 +390,61 @@ function find_my_pending_invites(array $premades, int $discordId): array
return $result;
}
// Bevorzugt den von der Bot-API aufgeloesten Discord-Anzeigenamen (die API kennt Discord direkt,
// die Website nicht); faellt auf den verknuepften Website-Account und zuletzt auf die rohe
// Discord-ID zurueck, falls beides fehlt (z.B. Bot offline beim API-Aufruf).
// Wird sowohl von premades.php als auch vom Dashboard-Widget genutzt.
function premade_name_for(int $discordId, array $namesByDiscordId, ?string $apiName = null): string
{
if ($apiName !== null && $apiName !== '') {
return $apiName;
}
return $namesByDiscordId[$discordId] ?? ('Discord-Nutzer ' . $discordId);
}
// Baut ein fuer avatar_html() nutzbares Nutzer-Array: das echte, hochgeladene Profilbild,
// falls die Discord-ID zu einem verknuepften Website-Account gehoert, sonst ein Platzhalter
// mit dem Anfangsbuchstaben des (von der API aufgeloesten) Namens.
function premade_avatar_user(int $discordId, array $linkedUsersByDiscordId, string $displayName): array
{
if (isset($linkedUsersByDiscordId[$discordId])) {
return $linkedUsersByDiscordId[$discordId];
}
return ['id' => $discordId, 'display_name' => $displayName, 'username' => $displayName, 'avatar_filename' => null];
}
/**
* Baut die feste 5er-Slot-Darstellung einer Premade (Lead + bis zu 4 weitere,
* mit ausgegrauten Platzhaltern aufgefuellt) - genutzt sowohl von premades.php
* als auch vom "Meine Premade"-Widget im Dashboard.
*
* @param array<string, mixed> $premade
* @param array<int, string> $namesByDiscordId
* @return array<int, array<string, mixed>>
*/
function build_premade_slots(array $premade, array $namesByDiscordId): array
{
$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');
$ownerName = premade_name_for($ownerId, $namesByDiscordId, $premade['owner_name'] ?? null);
$slots = [['type' => 'lead', 'id' => $ownerId, 'name' => $ownerName, 'class' => $premade['owner_class'] ?? null]];
foreach ($accepted as $member) {
$mid = (int) ($member['user_id'] ?? 0);
$slots[] = ['type' => 'accepted', 'id' => $mid, 'name' => premade_name_for($mid, $namesByDiscordId, $member['name'] ?? null), 'class' => $member['class'] ?? null];
}
foreach ($pending as $member) {
$mid = (int) ($member['user_id'] ?? 0);
$slots[] = ['type' => 'pending', 'id' => $mid, 'name' => premade_name_for($mid, $namesByDiscordId, $member['name'] ?? null), 'class' => $member['class'] ?? null];
}
while (count($slots) < 5) {
$slots[] = ['type' => 'empty'];
}
return $slots;
}
/**
* Sucht einen einzelnen Raid (inkl. aktueller Signups) anhand seiner ID.
*

View file

@ -39,28 +39,6 @@ if (!empty($allDiscordIds)) {
}
}
// Bevorzugt den von der Bot-API aufgeloesten Discord-Anzeigenamen (die API kennt Discord direkt,
// die Website nicht); faellt auf den verknuepften Website-Account und zuletzt auf die rohe
// Discord-ID zurueck, falls beides fehlt (z.B. Bot offline beim API-Aufruf).
function premade_name_for(int $discordId, array $namesByDiscordId, ?string $apiName = null): string
{
if ($apiName !== null && $apiName !== '') {
return $apiName;
}
return $namesByDiscordId[$discordId] ?? ('Discord-Nutzer ' . $discordId);
}
// Baut ein fuer avatar_html() nutzbares Nutzer-Array: das echte, hochgeladene Profilbild,
// falls die Discord-ID zu einem verknuepften Website-Account gehoert, sonst ein Platzhalter
// mit dem Anfangsbuchstaben des (von der API aufgeloesten) Namens.
function premade_avatar_user(int $discordId, array $linkedUsersByDiscordId, string $displayName): array
{
if (isset($linkedUsersByDiscordId[$discordId])) {
return $linkedUsersByDiscordId[$discordId];
}
return ['id' => $discordId, 'display_name' => $displayName, 'username' => $displayName, 'avatar_filename' => null];
}
$myRelation = $discordId ? find_my_premade($premades, $discordId) : null;
$myPendingInvites = $discordId ? find_my_pending_invites($premades, $discordId) : [];
@ -180,19 +158,7 @@ include __DIR__ . '/includes/header.php';
<?php
// Premades bilden eine volle 5er-Gruppe ab (Lead + bis zu 4 weitere) - freie
// Plaetze werden mit ausgegrauten Platzhaltern aufgefuellt.
$ownerName = premade_name_for($ownerId, $namesByDiscordId, $premade['owner_name'] ?? null);
$slots = [['type' => 'lead', 'id' => $ownerId, 'name' => $ownerName, 'class' => $premade['owner_class'] ?? null]];
foreach ($accepted as $member) {
$mid = (int) ($member['user_id'] ?? 0);
$slots[] = ['type' => 'accepted', 'id' => $mid, 'name' => premade_name_for($mid, $namesByDiscordId, $member['name'] ?? null), 'class' => $member['class'] ?? null];
}
foreach ($pending as $member) {
$mid = (int) ($member['user_id'] ?? 0);
$slots[] = ['type' => 'pending', 'id' => $mid, 'name' => premade_name_for($mid, $namesByDiscordId, $member['name'] ?? null), 'class' => $member['class'] ?? null];
}
while (count($slots) < 5) {
$slots[] = ['type' => 'empty'];
}
$slots = build_premade_slots($premade, $namesByDiscordId);
$isMemberHere = false;
$myClass = null;
?>