This commit is contained in:
2025-12-29 02:10:04 +01:00
parent b99239ca66
commit 33482a0291
4 changed files with 228 additions and 2 deletions

View File

@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
$app = app();
$pdo = $app->pdo();
$userId = $_SESSION['user_id'] ?? null;
$id = (int)($_GET['id'] ?? 0);
$error = '';
if (!$id) {
http_response_code(404);
exit('<p>Thread nicht gefunden.</p>');
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reply') {
if (!$userId) {
$error = 'Bitte einloggen, um zu antworten.';
} elseif ($pdo) {
$body = trim((string)($_POST['body'] ?? ''));
if ($body === '') {
$error = 'Antwort darf nicht leer sein.';
} else {
$pdo->prepare('INSERT INTO forum_posts (thread_id, user_id, body) VALUES (:tid, :uid, :body)')
->execute(['tid' => $id, 'uid' => $userId, 'body' => $body]);
header('Location: /community_thread?id=' . $id);
exit;
}
}
}
$thread = null;
$posts = [];
if ($pdo) {
$stmt = $pdo->prepare('SELECT ft.*, p.display_name FROM forum_threads ft LEFT JOIN user_profiles p ON p.user_id = ft.user_id WHERE ft.id = :id');
$stmt->execute(['id' => $id]);
$thread = $stmt->fetch(PDO::FETCH_ASSOC);
if ($thread) {
$stmt = $pdo->prepare('SELECT fp.*, p.display_name FROM forum_posts fp LEFT JOIN user_profiles p ON p.user_id = fp.user_id WHERE fp.thread_id = :id ORDER BY fp.created_at ASC');
$stmt->execute(['id' => $id]);
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC) ?: [];
}
}
if (!$thread) {
http_response_code(404);
exit('<p>Thread nicht gefunden.</p>');
}
?>
<main class="section">
<div class="container">
<p class="eyebrow">Community</p>
<h1><?= htmlspecialchars($thread['title'], ENT_QUOTES) ?></h1>
<p class="muted">Von <?= htmlspecialchars($thread['display_name'] ?: 'Mitglied', ENT_QUOTES) ?> · <?= htmlspecialchars($thread['created_at'], ENT_QUOTES) ?></p>
<article class="card" style="margin-top:12px;">
<div class="event__body">
<p><?= nl2br(htmlspecialchars($thread['body'], ENT_QUOTES)) ?></p>
</div>
</article>
<h3 style="margin-top:16px;">Antworten (<?= count($posts) ?>)</h3>
<div class="stack gap-12" style="margin-top:10px;">
<?php foreach ($posts as $p): ?>
<article class="card">
<div class="event__body">
<div class="event__meta">
<span><?= htmlspecialchars($p['created_at'], ENT_QUOTES) ?></span>
<span><?= htmlspecialchars($p['display_name'] ?: 'Mitglied', ENT_QUOTES) ?></span>
</div>
<p><?= nl2br(htmlspecialchars($p['body'], ENT_QUOTES)) ?></p>
</div>
</article>
<?php endforeach; ?>
<?php if (!$posts): ?>
<p class="muted">Noch keine Antworten.</p>
<?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="stack gap-12 card" style="margin-top:14px; padding:16px;">
<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: ?>
<p class="muted" style="margin-top:12px;">Bitte <a href=\"/login\">einloggen</a>, um zu antworten.</p>
<?php endif; ?>
</div>
</main>