issue: populate ticket data before reply include
[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         require_once 'upload.inc.php';
11         try {
12                 $query = $Db->set('comments', [
13                         'page'    => $Page,
14                         'message' => messagehtml($_POST['reply']),
15                         'author'  => $User->login,
16                 ]);
17                 if (!$query->rowCount()) {
18                         throw new Exception('Fout bij opslaan');
19                 }
20                 $newcomment = $Db->dbh->lastInsertId('comments_id_seq');
21
22                 if (isset($Issue)) {
23                         $row = [];
24                         foreach (array_keys($journalcol) as $col) {
25                                 if (!isset($_POST[$col])) continue;
26                                 $row[$col] = $_POST[$col] ?: NULL;
27                         }
28                         if (isset($_POST['status'])) {
29                                 $reset = !empty($_POST['status']);
30                                 if (isset($Issue->closed) !== $reset) {
31                                         $row['closed'] = $reset ? ['now()'] : NULL;
32                                 }
33                         }
34                         $derived = ['updated' => ['now()']];
35                         $filter = ['id = ? RETURNING *', $Issue->id];
36                         $subquery = $Db->set('issues', $row + $derived, $filter);
37
38                         if ($updated = $subquery->fetch()) {
39                                 foreach (array_keys($row) as $col) {
40                                         if ($updated->$col === $Issue->$col) continue; # unaltered
41                                         $Db->set('journal', [
42                                                 'comment_id' => $newcomment,
43                                                 'property'   => 'attr',
44                                                 'col'        => $col,
45                                                 'old_value'  => $Issue->$col,
46                                                 'value'      => $updated->$col,
47                                         ]);
48                                 }
49                                 $Issue = $updated;
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 $cols = '*, (SELECT json_agg(journal.*) FROM journal WHERE comment_id = comments.id) AS journal';
60 $query = $Db->query("SELECT $cols FROM comments WHERE page = ? ORDER BY created", [$Page]);
61
62 print '<ul class="replies">';
63
64 while ($row = $query->fetch()) {
65         $rowuser = new User("profile/{$row->author}");
66         print '<li>';
67         printf('<strong>%s</strong> <small class=date>%s</small>',
68                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
69         );
70         printf("<blockquote>%s</blockquote>\n", $row->message);
71         if ($changes = json_decode($row->journal)) {
72                 print '<ul>';
73                 foreach ($changes as $change) {
74                         print '<li>';
75                         if ($change->col == 'closed') {
76                                 printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
77                         }
78                         else {
79                                 printf("<em>%s</em> %s",
80                                         $journalcol[$change->col], sprintf(
81                                                 !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
82                                                 (!isset($change->value) ? 'verwijderd (<strike>%s</strike>)' :
83                                                 'gewijzigd van <q>%s</q> naar <q>%s</q>'),
84                                                 $change->old_value, $change->value
85                                         )
86                                 );
87                         }
88                         print "</li>\n";
89                 }
90                 print "</ul>\n";
91         }
92         print "</li>\n";
93 }
94
95 if ($User) {
96         print '<li>';
97         print '<form method="post" action="">';
98         if (isset($Issue) and $User->admin("edit $Page")) {
99                 print '<p>';
100                 printf(
101                         '<label for="%s">%s:</label> '
102                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
103                         'assign',
104                         $journalcol['assign'],
105                         htmlspecialchars($Issue->assign ?? '')
106                 );
107                 printf(
108                         '<input type="hidden" name="%s" value="" />' .
109                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
110                         . '<label for="%1$s"> %s</label>'."\n",
111                         'status',
112                         'resolved',
113                         isset($Issue->closed) ? ' checked' : '',
114                         'Gesloten'
115                 );
116                 print "</p>\n";
117         }
118         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
119                 'reply',
120                 "Bericht van {$User->login}",
121                 ''
122         );
123         print '<input type="submit" value="Plaatsen" />'."\n";
124         print "</form></li>\n";
125 }
126
127 print "</ul>\n\n";