reply: omit header from mandatory output
[minimedit.git] / widget / reply.php
1 <?php
2 global $User, $Db, $Issue;
3 require_once 'database.inc.php';
4
5 if ($_POST) {
6         try {
7                 $html = nl2br(htmlspecialchars($_POST['reply']));
8                 $html = "<p>$html</p>";
9                 $query = $Db->set('comments', [
10                         'page'    => $Page,
11                         'message' => $html,
12                         'author'  => $User->login,
13                 ]);
14                 if (!$query->rowCount()) {
15                         throw new Exception('Fout bij opslaan');
16                 }
17                 $newcomment = $Db->dbh->lastInsertId('comments_id_seq');
18
19                 if (isset($Issue)) {
20                         $row = [];
21                         foreach (['assign'] as $col) {
22                                 if (!isset($_POST[$col])) continue;
23                                 $row[$col] = $_POST[$col] ?: NULL;
24                         }
25                         if (isset($_POST['status'])) {
26                                 $reset = !empty($_POST['status']);
27                                 if (isset($Issue->closed) !== $reset) {
28                                         $row['closed'] = $reset ? ['now()'] : NULL;
29                                 }
30                         }
31                         $derived = ['updated' => ['now()']];
32                         $filter = ['id = ? RETURNING *', $Issue->id];
33                         $subquery = $Db->set('issues', $row + $derived, $filter);
34
35                         if ($updated = $subquery->fetch()) {
36                                 foreach (array_keys($row) as $col) {
37                                         if ($updated->$col === $Issue->$col) continue; # unaltered
38                                         $Db->set('journal', [
39                                                 'comment_id' => $newcomment,
40                                                 'property'   => 'attr',
41                                                 'col'        => $col,
42                                                 'old_value'  => $Issue->$col,
43                                                 'value'      => $updated->$col,
44                                         ]);
45                                 }
46                         }
47                 }
48                 $_POST['reply'] = NULL;
49         }
50         catch (Exception $e) {
51                 print "<p class=warn>Antwoord niet opgeslagen: {$e->getMessage()}.</p>\n\n";
52         }
53 }
54
55 $query = $Db->query('SELECT * FROM comments WHERE page = ? ORDER BY created', [$Page]);
56
57 print '<ul class="replies">';
58
59 while ($row = $query->fetch()) {
60         $rowuser = new User("profile/{$row->author}");
61         print '<li>';
62         printf('<strong>%s</strong> <small class=date>%s</small>',
63                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
64         );
65         printf("<blockquote>%s</blockquote>\n", $row->message);
66         print "</li>\n";
67 }
68
69 if ($User) {
70         print '<li>';
71         print '<form method="post" action="">';
72         if (isset($Issue) and $User->admin("edit $Page")) {
73                 print '<p>';
74                 printf(
75                         '<label for="%s">%s:</label> '
76                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
77                         'assign',
78                         'Toegewezen aan',
79                         htmlspecialchars($Issue->assign ?? '')
80                 );
81                 printf(
82                         '<input type="hidden" name="%s" value="" />' .
83                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
84                         . '<label for="%1$s"> %s</label>'."\n",
85                         'status',
86                         'resolved',
87                         isset($Issue->closed) ? ' checked' : '',
88                         'Gesloten'
89                 );
90                 print "</p>\n";
91         }
92         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
93                 'reply',
94                 "Bericht van {$User->login}",
95                 ''
96         );
97         print '<input type="submit" value="Plaatsen" />'."\n";
98         print "</form></li>\n";
99 }
100
101 print "</ul>\n\n";