145 lines
8.7 KiB
PHP
145 lines
8.7 KiB
PHP
<?php
|
||
/**
|
||
* Persoenliche Nachricht an ein Mitglied (Recht users.message, includes/user_notices.php): erscheint beim
|
||
* naechsten Besuch als Popup in der Farbe der gewaehlten Dringlichkeit. Darunter alle bisherigen
|
||
* Nachrichten an diese Person mit "gelesen am".
|
||
*/
|
||
require_once __DIR__ . '/../includes/bootstrap.php';
|
||
require_permission('users.message');
|
||
|
||
$id = (int) ($_GET['id'] ?? 0);
|
||
$stmt = db()->prepare('SELECT id, username, display_name, status, deleted_at FROM users WHERE id = ?');
|
||
$stmt->execute([$id]);
|
||
$target = $stmt->fetch();
|
||
if (!$target || !empty($target['deleted_at'])) {
|
||
show_not_found();
|
||
}
|
||
$name = $target['display_name'] !== '' ? (string) $target['display_name'] : (string) $target['username'];
|
||
|
||
$historyStmt = db()->prepare(
|
||
"SELECT n.*, COALESCE(NULLIF(a.display_name, ''), a.username) AS author_name
|
||
FROM user_notices n LEFT JOIN users a ON a.id = n.created_by
|
||
WHERE n.user_id = ? ORDER BY n.read_at IS NOT NULL, n.created_at DESC, n.id DESC"
|
||
);
|
||
$historyStmt->execute([$id]);
|
||
$history = $historyStmt->fetchAll();
|
||
$fmt = static fn (?string $dt): string => $dt ? date('d.m.Y, H:i', (int) strtotime($dt)) . ' Uhr' : '-';
|
||
$me = current_user();
|
||
$myName = $me['display_name'] !== '' ? (string) $me['display_name'] : (string) $me['username'];
|
||
|
||
$pageTitle = 'Nachricht an ' . $name;
|
||
include __DIR__ . '/../includes/header.php';
|
||
?>
|
||
<div class="panel">
|
||
<p class="text-muted data-admin-back"><a href="/admin/users.php">← Benutzerverwaltung</a></p>
|
||
<h1><?= icon('message') ?> Nachricht an <?= e($name) ?></h1>
|
||
<p class="text-muted">
|
||
Die Nachricht erscheint als Popup, sobald <?= e($name) ?> die Website das nächste Mal öffnet, und bleibt auf jeder Seite stehen,
|
||
bis sie mit „Gelesen“ bestätigt wird. Hier siehst du danach, ob und wann das passiert ist.
|
||
<?php if ($target['status'] !== 'active'): ?>
|
||
<br><strong>Hinweis:</strong> Das Konto ist gerade <?= $target['status'] === 'pending' ? 'noch nicht freigeschaltet' : 'deaktiviert' ?> –
|
||
<?= $target['status'] === 'pending' ? 'das Popup erscheint trotzdem nach der Anmeldung (auf der Warteseite).' : 'die Nachricht erscheint erst, wenn es wieder aktiv ist.' ?>
|
||
<?php endif; ?>
|
||
</p>
|
||
</div>
|
||
|
||
<div class="panel site-admin-section">
|
||
<h2>Neue Nachricht</h2>
|
||
<form method="post" action="/admin/user_notice_action.php" class="site-admin-form user-notice-form" id="user-notice-form">
|
||
<?= csrf_field() ?>
|
||
<input type="hidden" name="id" value="<?= $id ?>">
|
||
<input type="hidden" name="action" value="send">
|
||
|
||
<div class="user-notice-templates">
|
||
<span class="text-muted">Vorlagen:</span>
|
||
<?php foreach (USER_NOTICE_TEMPLATES as [$button, $level, $title, $text]): ?>
|
||
<button type="button" class="button-secondary button-small" data-level="<?= e($level) ?>" data-title="<?= e($title) ?>" data-text="<?= e($text) ?>"><?= e($button) ?></button>
|
||
<?php endforeach; ?>
|
||
</div>
|
||
|
||
<fieldset class="user-notice-levels">
|
||
<legend>Dringlichkeit</legend>
|
||
<?php foreach (USER_NOTICE_LEVELS as $key => [$label, $symbol, $hint]): ?>
|
||
<label class="user-notice-level user-notice-<?= e($key) ?>">
|
||
<input type="radio" name="level" value="<?= e($key) ?>" <?= $key === 'info' ? 'checked' : '' ?>>
|
||
<span class="user-notice-level-name"><span aria-hidden="true"><?= $symbol ?></span> <?= e($label) ?></span>
|
||
<span class="user-notice-level-hint"><?= e($hint) ?></span>
|
||
</label>
|
||
<?php endforeach; ?>
|
||
</fieldset>
|
||
|
||
<label for="notice-title">Überschrift <span class="text-muted">(optional)</span></label>
|
||
<input type="text" id="notice-title" name="title" maxlength="<?= USER_NOTICE_TITLE_MAX ?>" placeholder="z. B. Bitte melde dich bei uns">
|
||
|
||
<label for="notice-text">Nachricht</label>
|
||
<textarea id="notice-text" name="message" rows="5" maxlength="<?= USER_NOTICE_TEXT_MAX ?>" required
|
||
placeholder="z. B. Bitte melde dich umgehend bei der Legionsleitung."></textarea>
|
||
<p class="field-hint user-notice-hint">Zeilenumbrüche bleiben erhalten, Links (https://…) werden klickbar.</p>
|
||
|
||
<label class="site-admin-check">
|
||
<input type="checkbox" name="show_author" value="1" checked id="notice-author">
|
||
Meinen Namen dazuschreiben (sonst nur „Nachricht der Legionsleitung“)
|
||
</label>
|
||
|
||
<h3 class="user-notice-preview-title">Vorschau</h3>
|
||
<div class="user-notice-preview" aria-hidden="true">
|
||
<section class="user-notice user-notice-info" id="user-notice-preview" data-author="<?= e($myName) ?>" data-date="<?= e(date('d.m.Y')) ?>">
|
||
<div class="user-notice-head">
|
||
<span class="user-notice-icon"><?= USER_NOTICE_LEVELS['info'][1] ?></span>
|
||
<div>
|
||
<div class="user-notice-label"><?= e(USER_NOTICE_LEVELS['info'][0]) ?></div>
|
||
<div class="user-notice-from">Nachricht der Legionsleitung</div>
|
||
</div>
|
||
</div>
|
||
<h2 hidden></h2>
|
||
<p class="user-notice-text"><span class="text-muted">Hier erscheint deine Nachricht.</span></p>
|
||
<div class="user-notice-meta"></div>
|
||
<div class="user-notice-actions"><span class="button">Gelesen</span></div>
|
||
</section>
|
||
</div>
|
||
<script type="application/json" id="user-notice-levels"><?= json_encode(array_map(static fn (array $l): array => [$l[0], $l[1]], USER_NOTICE_LEVELS), JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP) ?></script>
|
||
|
||
<div class="site-admin-actions">
|
||
<button type="submit" class="button"><?= icon('message') ?> Nachricht senden</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
|
||
<div class="panel site-admin-section">
|
||
<h2>Bisherige Nachrichten</h2>
|
||
<?php if (!$history): ?>
|
||
<p class="text-muted">Noch keine Nachrichten an <?= e($name) ?>.</p>
|
||
<?php else: ?>
|
||
<ul class="user-notice-history">
|
||
<?php foreach ($history as $n): ?>
|
||
<?php $level = isset(USER_NOTICE_LEVELS[$n['level']]) ? $n['level'] : 'info'; ?>
|
||
<li class="user-notice-history-item user-notice-<?= e($level) ?>">
|
||
<div class="user-notice-history-head">
|
||
<span class="user-notice-chip"><?= USER_NOTICE_LEVELS[$level][1] ?> <?= e(USER_NOTICE_LEVELS[$level][0]) ?></span>
|
||
<?php if ($n['read_at'] === null): ?>
|
||
<span class="badge badge-pending">Ungelesen</span>
|
||
<?php else: ?>
|
||
<span class="badge badge-active" title="Mit „Gelesen“ bestätigt">Gelesen am <?= e($fmt($n['read_at'])) ?></span>
|
||
<?php endif; ?>
|
||
<form method="post" action="/admin/user_notice_action.php" class="inline-form user-notice-history-delete"
|
||
data-confirm="<?= $n['read_at'] === null ? 'Die Nachricht zurückziehen? Sie erscheint dann nicht mehr als Popup.' : 'Diese Nachricht aus dem Verlauf löschen?' ?>"
|
||
data-confirm-title="<?= $n['read_at'] === null ? 'Nachricht zurückziehen' : 'Nachricht löschen' ?>" data-confirm-danger data-confirm-ok="<?= $n['read_at'] === null ? 'Zurückziehen' : 'Löschen' ?>">
|
||
<?= csrf_field() ?>
|
||
<input type="hidden" name="id" value="<?= $id ?>">
|
||
<input type="hidden" name="notice_id" value="<?= (int) $n['id'] ?>">
|
||
<input type="hidden" name="action" value="delete">
|
||
<button type="submit" class="icon-button icon-button-danger" title="<?= $n['read_at'] === null ? 'Zurückziehen' : 'Löschen' ?>" aria-label="<?= $n['read_at'] === null ? 'Zurückziehen' : 'Löschen' ?>"><?= icon('trash') ?></button>
|
||
</form>
|
||
</div>
|
||
<?php if ($n['title'] !== ''): ?><strong><?= e($n['title']) ?></strong><?php endif; ?>
|
||
<p><?= announcement_html((string) $n['message']) ?></p>
|
||
<div class="text-muted user-notice-history-meta">
|
||
Gesendet am <?= e($fmt($n['created_at'])) ?> von <?= e((string) ($n['author_name'] ?? 'unbekannt')) ?><?= empty($n['show_author']) ? ' (Name im Popup ausgeblendet)' : '' ?>
|
||
</div>
|
||
</li>
|
||
<?php endforeach; ?>
|
||
</ul>
|
||
<?php endif; ?>
|
||
</div>
|
||
<script src="<?= e(asset_url('/assets/js/user-notice-form.js')) ?>"></script>
|
||
<?php include __DIR__ . '/../includes/footer.php'; ?>
|