issue: admin option to close or reopen
[minimedit.git] / widget / reply.php
1 <?php
2 global $User, $Db, $Issue;
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
20                 if (isset($Issue)) {
21                         $row = ['updated' => ['now()']];
22                         foreach (['assign'] as $col) {
23                                 if (!isset($_POST[$col])) continue;
24                                 $row[$col] = $_POST[$col] ?: NULL;
25                         }
26                         if (isset($_POST['status'])) {
27                                 $reset = !empty($_POST['status']);
28                                 if (isset($Issue->closed) !== $reset) {
29                                         $row['closed'] = $reset ? ['now()'] : NULL;
30                                 }
31                         }
32                         $Db->set('issues', $row, ['id = ?', $Issue->id]);
33                 }
34                 $_POST['reply'] = NULL;
35         }
36         catch (Exception $e) {
37                 print "<p class=warn>Antwoord niet opgeslagen: {$e->getMessage()}.</p>\n\n";
38         }
39 }
40
41 $query = $Db->query('SELECT * FROM comments WHERE page = ? ORDER BY created', [$Page]);
42
43 print '<ul class="replies">';
44
45 while ($row = $query->fetch()) {
46         $rowuser = new User("profile/{$row->author}");
47         print '<li>';
48         printf('<strong>%s</strong> <small class=date>%s</small>',
49                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
50         );
51         printf("<blockquote>%s</blockquote>\n", $row->message);
52         print "</li>\n";
53 }
54
55 if ($User) {
56         print '<li>';
57         print '<form method="post" action="">';
58         if (isset($Issue) and $User->admin("edit $Page")) {
59                 print '<p>';
60                 printf(
61                         '<label for="%s">%s:</label> '
62                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
63                         'assign',
64                         'Toegewezen aan',
65                         htmlspecialchars($Issue->assign ?? '')
66                 );
67                 printf(
68                         '<input type="hidden" name="%s" value="" />' .
69                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
70                         . '<label for="%1$s"> %s</label>'."\n",
71                         'status',
72                         'resolved',
73                         isset($Issue->closed) ? ' checked' : '',
74                         'Gesloten'
75                 );
76                 print "</p>\n";
77         }
78         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
79                 'reply',
80                 "Bericht van {$User->login}",
81                 ''
82         );
83         print '<input type="submit" value="Plaatsen" />'."\n";
84         print "</form></li>\n";
85 }
86
87 print "</ul>\n\n";