39029-vm/thread.php
2026-03-06 17:54:56 +00:00

147 lines
4.8 KiB
PHP

<?php
require_once __DIR__ . '/includes/app.php';
ensure_tables();
$pageTitle = 'Thread Detail';
$active = 'forums';
$pdo = db();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$author = trim($_POST['author'] ?? '');
$body = trim($_POST['body'] ?? '');
if ($author === '') {
$errors[] = 'Name is required for a reply.';
}
if ($body === '' || strlen($body) < 3) {
$errors[] = 'Reply must be at least 3 characters.';
}
if (!$errors && $id) {
$stmt = $pdo->prepare("INSERT INTO forum_posts (thread_id, author, body) VALUES (:thread_id, :author, :body)");
$stmt->execute([
':thread_id' => $id,
':author' => $author,
':body' => $body,
]);
header('Location: thread.php?id=' . $id . '&posted=1');
exit;
}
}
$stmt = $pdo->prepare("SELECT * FROM forum_threads WHERE id = :id");
$stmt->execute([':id' => $id]);
$thread = $stmt->fetch();
if (!$thread) {
$pageTitle = 'Thread Not Found';
}
$posts = [];
if ($thread) {
$stmt = $pdo->prepare("SELECT * FROM forum_posts WHERE thread_id = :id ORDER BY created_at DESC");
$stmt->execute([':id' => $id]);
$posts = $stmt->fetchAll();
}
include __DIR__ . '/includes/header.php';
?>
<?php if (!$thread): ?>
<div class="app-card">
<h1 class="h4">Thread not found</h1>
<p class="muted">This thread may have been removed or never existed.</p>
<a class="btn btn-outline-light" href="forums.php">Back to forums</a>
</div>
<?php else: ?>
<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-3">
<div>
<span class="badge badge-soft mb-2"><?= h($thread['game']) ?></span>
<?php if (!empty($thread['tag'])): ?>
<span class="badge badge-soft mb-2"><?= h($thread['tag']) ?></span>
<?php endif; ?>
<h1 class="h3 mb-1"><?= h($thread['title']) ?></h1>
<p class="muted mb-0">Started by <?= h($thread['author']) ?> · <?= h(format_date($thread['created_at'])) ?></p>
</div>
<a class="btn btn-outline-light" href="create_thread.php">Start new thread</a>
</div>
<?php if (!empty($_GET['created'])): ?>
<div class="alert alert-success alert-dismissible show" role="alert" data-autohide="true">
Thread published. Invite others to join the discussion.
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if (!empty($_GET['posted'])): ?>
<div class="alert alert-success alert-dismissible show" role="alert" data-autohide="true">
Reply posted. Keep the conversation moving.
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>
<?php if ($errors): ?>
<div class="alert alert-warning" role="alert">
<?= h(implode(' ', $errors)) ?>
</div>
<?php endif; ?>
<div class="row g-3 mb-4">
<div class="col-lg-8">
<div class="app-card">
<h2 class="h5">Opening post</h2>
<p><?= nl2br(h($thread['body'])) ?></p>
</div>
</div>
<div class="col-lg-4">
<div class="app-card">
<h2 class="h6">Thread focus</h2>
<ul class="list-unstyled mb-0 muted">
<li class="mb-2">Game: <?= h($thread['game']) ?></li>
<li class="mb-2">Tag: <?= h($thread['tag'] ?: 'General') ?></li>
<li>Replies: <?= count($posts) ?></li>
</ul>
</div>
</div>
</div>
<div class="app-card">
<div class="d-flex justify-content-between align-items-center mb-3">
<h2 class="h5 mb-0">Replies</h2>
<span class="muted"><?= count($posts) ?> replies</span>
</div>
<form method="post" class="mb-4">
<div class="row g-3">
<div class="col-md-4">
<label class="form-label">Name</label>
<input class="form-control" type="text" name="author" required>
</div>
<div class="col-md-8">
<label class="form-label">Reply</label>
<input class="form-control" type="text" name="body" maxlength="1000" required>
</div>
</div>
<button class="btn btn-primary mt-3" type="submit">Post reply</button>
</form>
<?php if (!$posts): ?>
<p class="muted mb-0">No replies yet. Be the first to respond.</p>
<?php else: ?>
<div class="d-grid gap-3">
<?php foreach ($posts as $post): ?>
<div class="app-card">
<div class="d-flex justify-content-between">
<strong><?= h($post['author']) ?></strong>
<span class="muted"><?= h(format_date($post['created_at'])) ?></span>
</div>
<p class="mb-0"><?= h($post['body']) ?></p>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php include __DIR__ . '/includes/footer.php'; ?>