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