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

96 lines
3.0 KiB
PHP

<?php
require_once __DIR__ . '/includes/app.php';
ensure_tables();
$pageTitle = 'Start Thread';
$active = 'create-thread';
$errors = [];
$values = [
'title' => '',
'game' => '',
'tag' => '',
'body' => '',
'author' => '',
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
foreach ($values as $key => $value) {
$values[$key] = trim($_POST[$key] ?? '');
}
if ($values['title'] === '') {
$errors[] = 'Title is required.';
}
if ($values['game'] === '') {
$errors[] = 'Game is required.';
}
if ($values['body'] === '' || strlen($values['body']) < 10) {
$errors[] = 'Opening post must be at least 10 characters.';
}
if ($values['author'] === '') {
$errors[] = 'Author name is required.';
}
if (!$errors) {
$stmt = db()->prepare(
"INSERT INTO forum_threads (title, game, tag, body, author)
VALUES (:title, :game, :tag, :body, :author)"
);
$stmt->execute([
':title' => $values['title'],
':game' => $values['game'],
':tag' => $values['tag'] ?: null,
':body' => $values['body'],
':author' => $values['author'],
]);
$id = (int)db()->lastInsertId();
header('Location: thread.php?id=' . $id . '&created=1');
exit;
}
}
include __DIR__ . '/includes/header.php';
?>
<div class="d-flex flex-wrap justify-content-between align-items-center mb-4 gap-2">
<div>
<h1 class="h3 mb-1">Start a Thread</h1>
<p class="muted mb-0">Lead the conversation on builds, patches, or loot.</p>
</div>
<a class="btn btn-outline-light" href="forums.php">Back to forums</a>
</div>
<?php if ($errors): ?>
<div class="alert alert-warning" role="alert">
<?= h(implode(' ', $errors)) ?>
</div>
<?php endif; ?>
<form method="post" class="app-card">
<div class="row g-3">
<div class="col-md-8">
<label class="form-label">Thread title</label>
<input class="form-control" type="text" name="title" maxlength="160" required value="<?= h($values['title']) ?>">
</div>
<div class="col-md-4">
<label class="form-label">Tag</label>
<input class="form-control" type="text" name="tag" maxlength="40" placeholder="Patch, Meta, Guide" value="<?= h($values['tag']) ?>">
</div>
<div class="col-md-6">
<label class="form-label">Game</label>
<input class="form-control" type="text" name="game" required value="<?= h($values['game']) ?>">
</div>
<div class="col-md-6">
<label class="form-label">Author</label>
<input class="form-control" type="text" name="author" required value="<?= h($values['author']) ?>">
</div>
<div class="col-12">
<label class="form-label">Opening post</label>
<textarea class="form-control" name="body" rows="5" required><?= h($values['body']) ?></textarea>
</div>
</div>
<button class="btn btn-primary mt-3" type="submit">Publish thread</button>
</form>
<?php include __DIR__ . '/includes/footer.php'; ?>