reply: set method to abstract update queries
[minimedit.git] / widget / reply.php
1 <?php
2 global $User, $Db;
3 require_once 'database.inc.php';
4
5 print '<h3>Reacties</h3>'."\n";
6
7 if ($_POST) {
8         try {
9                 $html = nl2br(htmlspecialchars($_POST['reply']));
10                 $html = "<p>$html</p>";
11                 $query = $Db->set('comments', [
12                         'page'    => $Page,
13                         'message' => $html,
14                         'author'  => $User->login,
15                 ]);
16                 if (!$query->rowCount()) {
17                         throw new Exception('Fout bij opslaan');
18                 }
19                 if (@list ($cat, $issue) = explode('/', $Page) and ctype_digit($issue)) {
20                         $row = ['updated' => ['now()']];
21                         $Db->set('issues', $row, ['page = ? AND id = ?', $cat, $issue]);
22                 }
23                 $_POST['reply'] = NULL;
24         }
25         catch (Exception $e) {
26                 print "<p class=warn>Antwoord niet opgeslagen: {$e->getMessage()}.</p>\n\n";
27         }
28 }
29
30 $query = $Db->query('SELECT * FROM comments WHERE page = ? ORDER BY created', [$Page]);
31
32 print '<ul class="replies">';
33
34 while ($row = $query->fetch()) {
35         $rowuser = new User("profile/{$row->author}");
36         print '<li>';
37         printf('<strong>%s</strong> <small class=date>%s</small>',
38                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
39         );
40         printf("<blockquote>%s</blockquote>\n", $row->message);
41         print "</li>\n";
42 }
43
44 if ($User) {
45         print '<li>';
46         print '<form method="post" action="">';
47         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
48                 'reply',
49                 "Bericht van {$User->login}",
50                 ''
51         );
52         print '<input type="submit" value="Plaatsen" />'."\n";
53         print "</form></li>\n";
54 }
55
56 print "</ul>\n\n";