LuminaWebsite/includes/user_notices.php

102 lines
5.2 KiB
PHP
Raw 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
declare(strict_types=1);
/**
* Persoenliche Nachrichten der Legionsleitung an einzelne Mitglieder (Benutzerverwaltung ->
* Briefumschlag, admin/user_notice.php). Die Nachricht erscheint beim naechsten Besuch als Popup
* und bleibt, bis das Mitglied sie mit "Gelesen" bestaetigt - die Leitung sieht, ob und wann.
*
* Die Dringlichkeit bestimmt Farbe und Rand des Popups (CSS: .user-notice-<level>).
*/
// Stufe => [Beschriftung, Symbol, Erklaerung fuer die Leitung]; nach Dringlichkeit aufsteigend
const USER_NOTICE_LEVELS = [
'info' => ['Hinweis', '💬', 'Zur Info, keine Eile'],
'soon' => ['Bald', '⏳', 'Bitte in den nächsten Tagen erledigen'],
'important' => ['Wichtig', '⚠️', 'Bitte zeitnah darum kümmern'],
'urgent' => ['Dringend', '🚨', 'Sofort – auffälliger, pulsierender Rand'],
];
// Textvorlagen zum Einfuegen auf der Verwaltungsseite: [Knopf, Stufe, Titel, Text]
const USER_NOTICE_TEMPLATES = [
['Umgehend melden', 'urgent', 'Bitte melde dich bei uns', 'Bitte melde dich umgehend bei der Legionsleitung – am besten per Discord-Nachricht.'],
['Bei Gelegenheit melden', 'soon', 'Kurze Rückfrage', 'Bitte melde dich in den nächsten Tagen bei der Legionsleitung, wir haben eine kurze Frage an dich.'],
['Profil aktualisieren', 'soon', 'Profil aktualisieren', 'Bitte bring dein Profil auf den aktuellen Stand (Charaktere, Klasse und Stufe), damit wir die Raids gut planen können.'],
['Danke', 'info', 'Danke!', 'Danke für deinen Einsatz in der Legion – wir freuen uns, dass du dabei bist!'],
];
const USER_NOTICE_TITLE_MAX = 120;
const USER_NOTICE_TEXT_MAX = 2000;
/** SQL-Ausdruck: hoehere Zahl = dringender (zum Sortieren). */
function user_notice_level_sql(string $column = 'level'): string
{
return "FIELD($column, 'info', 'soon', 'important', 'urgent')";
}
/**
* Ungelesene Nachrichten eines Kontos, die dringendste zuerst (bei gleicher Stufe die aelteste).
*
* @return list<array{id: int, level: string, title: string, message: string, author: string, created_at: string}>
*/
function user_notices_unread(int $userId): array
{
try {
$stmt = db()->prepare(
"SELECT n.id, n.level, n.title, n.message, n.created_at,
IF(n.show_author, COALESCE(NULLIF(a.display_name, ''), a.username), '') AS author
FROM user_notices n LEFT JOIN users a ON a.id = n.created_by
WHERE n.user_id = ? AND n.read_at IS NULL
ORDER BY " . user_notice_level_sql('n.level') . " DESC, n.created_at, n.id"
);
$stmt->execute([$userId]);
} catch (PDOException $e) {
return []; // Tabelle noch nicht angelegt (Update laeuft beim naechsten Seitenaufruf)
}
return array_map(static fn (array $row): array => [
'id' => (int) $row['id'],
'level' => isset(USER_NOTICE_LEVELS[$row['level']]) ? (string) $row['level'] : 'info',
'title' => (string) $row['title'],
'message' => (string) $row['message'],
'author' => (string) ($row['author'] ?? ''),
'created_at' => (string) $row['created_at'],
], $stmt->fetchAll());
}
/**
* Popup fuer den Seitenkopf: die dringendste ungelesene Nachricht. Nach "Gelesen" laedt die Seite
* neu und zeigt die naechste. $total = Anzahl ungelesener Nachrichten.
*/
function user_notice_popup_html(array $notice, int $total): string
{
[$label, $symbol] = USER_NOTICE_LEVELS[$notice['level']];
ob_start();
?>
<div class="user-notice-overlay" role="presentation">
<section class="user-notice user-notice-<?= e($notice['level']) ?>" role="alertdialog" aria-modal="true"
aria-labelledby="user-notice-heading" aria-describedby="user-notice-text">
<div class="user-notice-head">
<span class="user-notice-icon" aria-hidden="true"><?= $symbol ?></span>
<div>
<div class="user-notice-label"><?= e($label) ?><?= $total > 1 ? ' · Nachricht 1 von ' . $total : '' ?></div>
<div class="user-notice-from"<?= $notice['title'] === '' ? ' id="user-notice-heading"' : '' ?>>Nachricht der Legionsleitung</div>
</div>
</div>
<?php if ($notice['title'] !== ''): ?>
<h2 id="user-notice-heading"><?= e($notice['title']) ?></h2>
<?php endif; ?>
<p id="user-notice-text" class="user-notice-text"><?= announcement_html($notice['message']) ?></p>
<div class="user-notice-meta">
<?= $notice['author'] !== '' ? '– ' . e($notice['author']) . ', ' : '' ?><?= e(date('d.m.Y', (int) strtotime($notice['created_at']))) ?>
</div>
<form method="post" action="/user_notice_read.php" class="user-notice-actions">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int) $notice['id'] ?>">
<input type="hidden" name="back" value="<?= e((string) ($_SERVER['REQUEST_URI'] ?? '/dashboard.php')) ?>">
<button type="submit" class="button" autofocus><?= $notice['level'] === 'urgent' || $notice['level'] === 'important' ? 'Verstanden' : 'Gelesen' ?></button>
</form>
</section>
</div>
<?php
return (string) ob_get_clean();
}