nieuws/replies: read contents from database
[minimedit.git] / widget / nieuws / replies.php
1 <?php
2 global $User;
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->prepare('INSERT INTO comments (page, message, author) VALUES (?, ?, ?)');
12                 $query->execute([ $Page, $html, $User->login ]);
13                 if (!$query->rowCount()) {
14                         throw new Exception('Fout bij opslaan');
15                 }
16                 $_POST['reply'] = NULL;
17         }
18         catch (Exception $e) {
19                 print '<p class=warn>Antwoord niet opgeslagen.</p>'."\n\n";
20         }
21 }
22
23 $query = $Db->prepare('SELECT * FROM comments WHERE page = ? ORDER BY created');
24 $query->execute([$Page]);
25
26 print '<ul class="replies">';
27
28 while ($row = $query->fetch(PDO::FETCH_OBJ)) {
29         $rowuser = new User("profile/{$row->author}");
30         print '<li>';
31         printf('<strong>%s</strong> <small class=date>%s</small>',
32                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
33         );
34         printf("<blockquote>%s</blockquote>\n", $row->message);
35         print "</li>\n";
36 }
37
38 print '<li>';
39 print '<form method="post" action="">';
40 printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
41         'reply',
42         "Bericht van {$User->login}",
43         ''
44 );
45 print '<input type="submit" value="Plaatsen" />'."\n";
46 print "</form></li>\n";
47
48 print "</ul>\n\n";