Finish updating forum to use HTMX

This commit is contained in:
Sky Johnson 2024-12-19 12:20:59 -06:00
parent b8c3c96f8d
commit d45d3f74e5
4 changed files with 41 additions and 22 deletions

View File

@ -13,6 +13,7 @@ html {
body { body {
background-image: url('/img/backgrounds/classic.jpg'); background-image: url('/img/backgrounds/classic.jpg');
scrollbar-gutter: stable both-edges;
&.skin-1 { &.skin-1 {
background-image: url('/img/backgrounds/snowstorm.jpg'); background-image: url('/img/backgrounds/snowstorm.jpg');

View File

@ -19,22 +19,44 @@ function register_routes(Router $r): Router
function donothing($start = 0) function donothing($start = 0)
{ {
$query = db()->query('SELECT * FROM forum WHERE parent=0 ORDER BY newpostdate DESC LIMIT 20 OFFSET ?;', [20 * $start]); $query = db()->query('SELECT * FROM forum WHERE parent=0 ORDER BY newpostdate DESC LIMIT 20 OFFSET ?;', [20 * $start]);
$page = "<table width=\"100%\"><tr><td style=\"padding:1px; background-color:black;\"><table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\"><tr><th colspan=\"3\" style=\"background-color:#dddddd;\"><center><a href=\"/forum/new\">New Thread</a></center></th></tr><tr><th width=\"50%\" style=\"background-color:#dddddd;\">Thread</th><th width=\"10%\" style=\"background-color:#dddddd;\">Replies</th><th style=\"background-color:#dddddd;\">Last Post</th></tr>\n"; $page = <<<HTML
<table width="100%">
<tr>
<td style="padding: 1px; background-color: black;">
<table width="100%" style="margins: 0px;" cellspacing="1" cellpadding="3">
<tr>
<th colspan="3" style="background-color: #ddd;">
<center><a href="/forum/new" hx-get="/forum/new" hx-target="#middle">New Thread</a></center>
</th>
</tr>
<tr>
<th width="50%" style="background-color:#dddddd;">Thread</th>
<th width="10%" style="background-color:#dddddd;">Replies</th>
<th style="background-color:#dddddd;">Last Post</th>
</tr>
HTML;
$hasRows = false; $hasRows = false;
while ($row = $query->fetchArray(SQLITE3_ASSOC)) { while ($row = $query->fetchArray(SQLITE3_ASSOC)) {
$hasRows = true; $hasRows = true;
$page .= "<tr><td style=\"background-color:#ffffff;\"><a href=\"/forum/thread/".$row["id"]."/0\">".$row["title"]."</a></td><td style=\"background-color:#ffffff;\">".$row["replies"]."</td><td style=\"background-color:#ffffff;\">".$row["newpostdate"]."</td></tr>\n"; $page .= <<<HTML
<tr>
<td style="background-color: white;"><a href="/forum/thread/{$row['id']}/0" hx-get="/forum/thread/{$row['id']}/0" hx-target="#middle">{$row['title']}</a></td>
<td style="background-color: white;">{$row['replies']}</td>
<td style="background-color: white;">{$row['newpostdate']}</td>
</tr>
HTML;
} }
if (!$hasRows) { if (!$hasRows) {
$page .= "<tr><td style=\"background-color:#ffffff;\" colspan=\"3\"><b>No threads in forum.</b></td></tr>\n"; $page .= '<tr><td style="background-color:#ffffff;" colspan="3"><b>No threads in forum.</b></td></tr>';
} }
$page .= "</table></td></tr></table>"; $page .= '</table></td></tr></table>';
return display($page, "Forum"); page_title('Forum');
return is_htmx() ? $page : display($page);
} }
function showthread($id, $start) function showthread($id, $start)
@ -42,28 +64,25 @@ function showthread($id, $start)
$posts = db()->query('SELECT * FROM forum WHERE id=? OR parent=? ORDER BY id LIMIT 15 OFFSET ?;', [$id, $id, $start * 15]); $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); $title = db()->query('SELECT title FROM forum WHERE id=? LIMIT 1;', [$id])->fetchArray(SQLITE3_ASSOC);
$page = "<table width=\"100%\"><tr><td style=\"padding:1px; background-color:black;\"><table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\"><tr><td colspan=\"2\" style=\"background-color:#dddddd;\"><b><a href=\"/forum\">Forum</a> :: ".$title['title']."</b></td></tr>\n"; $page = "<table width=\"100%\"><tr><td style=\"padding:1px; background-color:black;\"><table width=\"100%\" style=\"margins:0px;\" cellspacing=\"1\" cellpadding=\"3\"><tr><td colspan=\"2\" style=\"background-color:#dddddd;\"><b><a href=\"/forum\" hx-get=\"/forum\" hx-target=\"#middle\">Forum</a> :: ".$title['title']."</b></td></tr>\n";
while ($row = $posts->fetchArray(SQLITE3_ASSOC)) { while ($row = $posts->fetchArray(SQLITE3_ASSOC)) {
$page .= "<tr><td width=\"25%\" style=\"background-color:#ffffff; vertical-align:top;\"><span class=\"small\"><b>".$row["author"]."</b><br><br>".pretty_date($row["postdate"])."</td><td style=\"background-color:#ffffff; vertical-align:top;\">".nl2br($row["content"])."</td></tr>\n"; $page .= "<tr><td width=\"25%\" style=\"background-color:#ffffff; vertical-align:top;\"><span class=\"small\"><b>".$row["author"]."</b><br><br>".pretty_date($row["postdate"])."</td><td style=\"background-color:#ffffff; vertical-align:top;\">".nl2br($row["content"])."</td></tr>\n";
} }
$page .= "</table></td></tr></table><br>"; $page .= "</table></td></tr></table><br>";
$page .= "<table width=\"100%\"><tr><td><b>Reply To This Thread:</b><br><form action=\"/forum/reply\" method=\"post\"><input type=\"hidden\" name=\"parent\" value=\"$id\" /><input type=\"hidden\" name=\"title\" value=\"Re: ".$title["title"]."\" /><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>"; $page .= "<table width=\"100%\"><tr><td><b>Reply To This Thread:</b><br><form action=\"/forum/reply\" method=\"post\" hx-post=\"/forum/reply\" hx-target=\"#middle\"><input type=\"hidden\" name=\"parent\" value=\"$id\" /><input type=\"hidden\" name=\"title\" value=\"Re: ".$title["title"]."\" /><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>";
return display($page, "Forum"); page_title('Forum: '.$title['title']);
return is_htmx() ? $page : display($page);
} }
function reply() function reply()
{ {
global $userrow;
$form = validate($_POST, [ $form = validate($_POST, [
'title' => [], 'title' => [],
'content' => [] 'content' => []
]); ]);
if (!$form['valid']) { if (!$form['valid']) exit(ul_from_validate_errors($form['errors']));
exit(ul_from_validate_errors($form['errors']));
}
$form = $form['data']; $form = $form['data'];
@ -71,7 +90,7 @@ function reply()
user()->username, $form['title'], $form['content'], $form['parent'] user()->username, $form['title'], $form['content'], $form['parent']
]); ]);
db()->query('UPDATE forum SET newpostdate=CURRENT_TIMESTAMP, replies=replies + 1 WHERE id=?;', [$form['parent']]); db()->query('UPDATE forum SET newpostdate=CURRENT_TIMESTAMP, replies=replies + 1 WHERE id=?;', [$form['parent']]);
redirect("/forum/thread/{$form['parent']}/0"); return showthread($form['parent'], 0);
} }
function newthread() function newthread()
@ -82,17 +101,16 @@ function newthread()
'content' => [] 'content' => []
]); ]);
if (!$form['valid']) { if (!$form['valid']) exit(ul_from_validate_errors($form['errors']));
exit(ul_from_validate_errors($form['errors']));
}
$form = $form['data']; $form = $form['data'];
db()->query('INSERT INTO forum (author, title, content) VALUES (?, ?, ?);', [ db()->query('INSERT INTO forum (author, title, content) VALUES (?, ?, ?);', [
user()->username, $form['title'], $form['content'] user()->username, $form['title'], $form['content']
]); ]);
redirect('/forum'); redirect('/forum/thread/'.db()->lastInsertRowID().'/0');
} }
$page = "<table width=\"100%\"><tr><td><b>Make A New Post:</b><br><br/ ><form action=\"/forum/new\" method=\"post\">Title:<br><input type=\"text\" name=\"title\" size=\"50\" maxlength=\"50\" /><br><br>Message:<br><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>"; $page = "<table width=\"100%\"><tr><td><b>Make A New Post:</b><br><br/ ><form action=\"/forum/new\" method=\"post\" hx-post=\"/forum/new\" hx-target=\"#middle\">Title:<br><input type=\"text\" name=\"title\" size=\"50\" maxlength=\"50\" /><br><br>Message:<br><textarea name=\"content\" rows=\"7\" cols=\"40\"></textarea><br><br><input type=\"submit\" name=\"submit\" value=\"Submit\" /> <input type=\"reset\" name=\"reset\" value=\"Reset\" /></form></td></tr></table>";
return display($page, "Forum"); page_title('Form: New Thread');
return is_htmx() ? $page : display($page);
} }

View File

@ -104,7 +104,7 @@ function display_admin($content, $title)
/** /**
* Finalize page and output to browser. * Finalize page and output to browser.
*/ */
function display($content, $title, bool $topnav = true, bool $leftnav = true, bool $rightnav = true): string function display($content, $title = '', bool $topnav = true, bool $leftnav = true, bool $rightnav = true): string
{ {
global $controlrow; global $controlrow;

View File

@ -20,7 +20,7 @@ class Model
public function __set(string $key, mixed $value): void public function __set(string $key, mixed $value): void
{ {
if (array_key_exists($key, $this->original_data)) { if (array_key_exists($key, $this->original_data)) {
$this->changes[$key] = $value; if ($value !== $this->original_data[$key]) $this->changes[$key] = $value;
} else { } else {
throw new InvalidArgumentException("Attempted to write to $key, which doesn't exist in the data for this model."); throw new InvalidArgumentException("Attempted to write to $key, which doesn't exist in the data for this model.");
} }