127 lines
4.6 KiB
PHP
Executable File
127 lines
4.6 KiB
PHP
Executable File
<?php
|
|
declare(strict_types=1);
|
|
|
|
$app = app();
|
|
$pdo = $app->pdo();
|
|
$userId = $_SESSION['user_id'] ?? null;
|
|
$error = '';
|
|
$threadId = (int)($_GET['id'] ?? 0);
|
|
|
|
$communityCfg = require __DIR__ . '/../../../config/community.php';
|
|
$community = $pdo ? new \App\Community($pdo, $communityCfg) : null;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reply') {
|
|
if (!$userId) {
|
|
$error = 'Bitte einloggen, um zu antworten.';
|
|
} elseif ($community) {
|
|
$body = trim((string)($_POST['body'] ?? ''));
|
|
if ($body === '') {
|
|
$error = 'Antwort darf nicht leer sein.';
|
|
} else {
|
|
$community->createPost((int)$userId, $threadId, $body);
|
|
redirect('/community_thread?id=' . $threadId);
|
|
}
|
|
}
|
|
}
|
|
|
|
$thread = $community ? $community->getThread($threadId) : null;
|
|
$posts = $community ? $community->listPosts($threadId) : [];
|
|
|
|
if (!$thread) {
|
|
http_response_code(404);
|
|
echo "<p>Thread nicht gefunden.</p>";
|
|
return;
|
|
}
|
|
|
|
$threadAuthor = (string)($thread['display_name'] ?: 'Mitglied');
|
|
$threadPoints = ($community && $pdo) ? $community->computePoints((int)$thread['user_id']) : 0.0;
|
|
$threadLevel = $community ? $community->membershipLevel($threadPoints) : ['label' => '', 'icon' => ''];
|
|
?>
|
|
<main class="section">
|
|
<div class="container">
|
|
<div class="forum-breadcrumbs">
|
|
<a href="/">Home</a>
|
|
<span>/</span>
|
|
<a href="/community">Community</a>
|
|
<span>/</span>
|
|
<span><?= htmlspecialchars($thread['title'], ENT_QUOTES) ?></span>
|
|
</div>
|
|
|
|
<div class="forum-thread-head">
|
|
<div>
|
|
<h1><?= htmlspecialchars($thread['title'], ENT_QUOTES) ?></h1>
|
|
<p class="forum-thread-head__meta">Erstellt von <?= htmlspecialchars($threadAuthor, ENT_QUOTES) ?> am <?= htmlspecialchars($thread['created_at'], ENT_QUOTES) ?></p>
|
|
</div>
|
|
<div class="forum-thread-head__meta-box">
|
|
<strong><?= count($posts) ?></strong>
|
|
<span>Antworten</span>
|
|
</div>
|
|
</div>
|
|
|
|
<article class="forum-post forum-post--lead">
|
|
<aside class="forum-post__author">
|
|
<div class="forum-post__avatar" aria-hidden="true"><?= strtoupper(mb_substr($threadAuthor, 0, 1)) ?></div>
|
|
<strong><?= htmlspecialchars($threadAuthor, ENT_QUOTES) ?></strong>
|
|
<span><?= htmlspecialchars($threadLevel['icon'] ?? '', ENT_QUOTES) ?> <?= htmlspecialchars($threadLevel['label'] ?? '', ENT_QUOTES) ?></span>
|
|
<span><?= number_format($threadPoints, 1) ?> Punkte</span>
|
|
</aside>
|
|
<div class="forum-post__body">
|
|
<div class="forum-post__head">
|
|
<span><?= htmlspecialchars($thread['created_at'], ENT_QUOTES) ?></span>
|
|
<span>#1</span>
|
|
</div>
|
|
<div class="forum-post__content">
|
|
<?= nl2br(htmlspecialchars($thread['body'], ENT_QUOTES)) ?>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
|
|
<div class="forum-replies-head">
|
|
<h2>Antworten</h2>
|
|
<span><?= count($posts) ?> Beitrag/Beiträge</span>
|
|
</div>
|
|
|
|
<div class="forum-replies">
|
|
<?php foreach ($posts as $p): ?>
|
|
<?php
|
|
$postAuthor = (string)($p['display_name'] ?: 'Mitglied');
|
|
?>
|
|
<article class="forum-post">
|
|
<aside class="forum-post__author">
|
|
<div class="forum-post__avatar" aria-hidden="true"><?= strtoupper(mb_substr($postAuthor, 0, 1)) ?></div>
|
|
<strong><?= htmlspecialchars($postAuthor, ENT_QUOTES) ?></strong>
|
|
</aside>
|
|
<div class="forum-post__body">
|
|
<div class="forum-post__head">
|
|
<span><?= htmlspecialchars($p['created_at'], ENT_QUOTES) ?></span>
|
|
</div>
|
|
<div class="forum-post__content">
|
|
<?= nl2br(htmlspecialchars($p['body'], ENT_QUOTES)) ?>
|
|
</div>
|
|
</div>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
<?php if (!$posts): ?>
|
|
<div class="forum-empty">Noch keine Antworten.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="toast-bar" style="margin-top:12px; border-color:#f87171; color:#991b1b;"><?= htmlspecialchars($error, ENT_QUOTES) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($userId): ?>
|
|
<form method="post" class="forum-reply-form">
|
|
<input type="hidden" name="action" value="reply">
|
|
<div class="stack gap-6">
|
|
<label class="label" for="replyBody">Antwort</label>
|
|
<textarea id="replyBody" name="body" class="textarea" rows="4" required></textarea>
|
|
</div>
|
|
<button class="btn" type="submit">Antwort senden</button>
|
|
</form>
|
|
<?php else: ?>
|
|
<div class="forum-login-note">Bitte <a href="/login">einloggen</a>, um zu antworten.</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</main>
|