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