get('/forum/thread/:x/:x', 'Forum\showthread');
$r->form('/forum/new', 'Forum\newthread');
$r->post('/forum/reply', 'Forum\reply');
$r->get('/forum/list/:x', 'Forum\donothing');
$r->get('/forum', 'Forum\donothing');
return $r;
}
public static function donothing($start = 0)
{
$query = db()->query('SELECT * FROM forum WHERE parent=0 ORDER BY newpostdate DESC LIMIT 20 OFFSET ?;', [20 * $start]);
$page = <<
New Thread
|
Thread |
Replies |
Last Post |
HTML;
$hasRows = false;
while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
$hasRows = true;
$page .= <<
{$row['title']} |
{$row['replies']} |
{$row['newpostdate']} |
HTML;
}
if (! $hasRows) {
$page .= 'No threads in forum. | ';
}
$page .= ' |
';
page_title('Forum');
return $page;
}
public static function showthread($id, $start)
{
$posts = db()->query('SELECT * FROM forum WHERE id=? OR parent=? ORDER BY id LIMIT 15 OFFSET ?;', [$id, $id, $start * 15]);
$title = db()->query('SELECT title FROM forum WHERE id=? LIMIT 1;', [$id])->fetchArray(SQLITE3_ASSOC);
$page = 'Forum :: '.$title['title']." | \n";
while ($row = $posts->fetchArray(SQLITE3_ASSOC)) {
$page .= ''.$row['author'].'
'.pretty_date($row['postdate']).' | '.nl2br($row['content'])." | \n";
}
$page .= '
|
';
$page .= "';
page_title('Forum: '.$title['title']);
return $page;
}
public static function reply()
{
$form = validate($_POST, [
'title' => [],
'content' => [],
]);
if (! $form['valid']) {
exit(ul_from_validate_errors($form['errors']));
}
$form = $form['data'];
db()->query('INSERT INTO forum (author, title, content, parent) VALUES (?, ?, ?, ?);', [
user()->username, $form['title'], $form['content'], $form['parent'],
]);
db()->query('UPDATE forum SET newpostdate=CURRENT_TIMESTAMP, replies=replies + 1 WHERE id=?;', [$form['parent']]);
return self::showthread($form['parent'], 0);
}
public static function newthread()
{
if (isset($_POST['submit'])) {
$form = validate($_POST, [
'title' => ['length:2-30'],
'content' => [],
]);
if (! $form['valid']) {
exit(ul_from_validate_errors($form['errors']));
}
$form = $form['data'];
db()->query('INSERT INTO forum (author, title, content) VALUES (?, ?, ?);', [
user()->username, $form['title'], $form['content'],
]);
redirect('/forum/thread/'.db()->lastInsertRowID().'/0');
}
page_title('Form: New Thread');
return '';
}
}