66e958746b6f71bb330b4165ff138446c6178021
[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         'subject' => 'Onderwerp',
8 ];
9
10 if ($_POST) {
11         require_once 'upload.inc.php';
12         try {
13                 $reply = [];
14                 if (isset($_POST['reply']) and $body = $_POST['reply']) {
15                         $reply['raw'] = $body;
16                         $reply['message'] = messagehtml($body);
17                 }
18                 if ($_FILES and !empty($_FILES['image'])) {
19                         $target = 'data/upload';
20                         if (!file_exists($target)) {
21                                 throw new Exception("er is geen uploadmap aanwezig op $target");
22                         }
23                         $target .= '/' . $User->login;
24                         if ($result = userupload($_FILES['image'], $target)) {
25                                 $reply['raw'] .= "/$result";
26                                 if (preg_match('(^image/)', $_FILES['image']['type'])) {
27                                         $reply['message'] .= sprintf('<p><img src="/thumb/640x/%s" /></p>', $result);
28                                 }
29                                 else {
30                                         $reply['message'] .= sprintf('<p>Bijgevoegd bestand: <a href="/%s" />%s</a></p>',
31                                                 $result, basename($result)
32                                         );
33                                 }
34                         }
35                 }
36                 $query = $Db->set('comments', $reply + [
37                         'page'    => $Page->link,
38                         'author'  => $User->login,
39                 ]);
40                 if (!$query->rowCount()) {
41                         throw new Exception('Fout bij opslaan');
42                 }
43                 $newcomment = $Db->dbh->lastInsertId('comments_id_seq');
44
45                 if (isset($Issue)) {
46                         $row = [];
47                         foreach (array_keys($journalcol) as $col) {
48                                 if (!isset($_POST[$col])) continue;
49                                 $row[$col] = $_POST[$col] ?: NULL;
50                         }
51                         if (isset($_POST['status'])) {
52                                 $reset = !empty($_POST['status']);
53                                 if (isset($Issue->closed) !== $reset) {
54                                         $row['closed'] = $reset ? ['now()'] : NULL;
55                                 }
56                         }
57                         $derived = ['updated' => ['now()']];
58                         $filter = ['id = ? RETURNING *', $Issue->id];
59                         $subquery = $Db->set('issues', $row + $derived, $filter);
60
61                         if ($updated = $subquery->fetch()) {
62                                 foreach (array_keys($row) as $col) {
63                                         if ($updated->$col === $Issue->$col) continue; # unaltered
64                                         $Db->set('journal', [
65                                                 'comment_id' => $newcomment,
66                                                 'property'   => 'attr',
67                                                 'col'        => $col,
68                                                 'old_value'  => $Issue->$col,
69                                                 'value'      => $updated->$col,
70                                         ]);
71                                 }
72                                 $Issue = $updated;
73                         }
74                 }
75
76                 $target = "/{$Page->link}/$newcomment#$newcomment";
77                 abort($target, ($Page->api ? 200 : 303) . ' reply success');
78                 $_POST['reply'] = NULL;
79         }
80         catch (Exception $e) {
81                 if ($Page->api) {
82                         abort(ucfirst($e->getMessage()), '500 reply error');
83                 }
84                 print "<p class=warn>Antwoord niet opgeslagen: {$e->getMessage()}.</p>\n\n";
85         }
86 }
87
88 $cols = '*, (SELECT json_agg(journal.*) FROM journal WHERE comment_id = comments.id) AS journal';
89 $query = $Db->query("SELECT $cols FROM comments WHERE page = ? ORDER BY created", [$Page->link]);
90
91 if ($row = $query->fetch()) {
92         print $row->message;
93         $Page->teaser = $row->raw;
94 }
95
96 print '<ul class="replies">';
97
98 $imagecount = 0;
99 while ($row = $query->fetch()) {
100         $rowuser = new User("profile/{$row->author}");
101         printf('<li id="%d">', $row->id);
102         printf('<strong>%s</strong> <small class=date>%s</small>',
103                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
104         );
105         if ($html = $row->message) {
106                 $html = preg_replace('/(?<=<img )/',
107                         $imagecount > 2 ? 'loading="lazy" ' : '', $html, -1, $found);
108                 $imagecount += $found;
109                 printf("<blockquote>\n%s</blockquote>\n", $html);
110         }
111         if ($changes = json_decode($row->journal)) {
112                 print '<ul>';
113                 foreach ($changes as $change) {
114                         print '<li>';
115                         if ($change->col == 'closed') {
116                                 printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
117                         }
118                         else {
119                                 printf("<em>%s</em> %s",
120                                         $journalcol[$change->col], sprintf(
121                                                 !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
122                                                 (!isset($change->value) ? 'verwijderd (<s>%s</s>)' :
123                                                 'gewijzigd van <q>%s</q> naar <q>%s</q>'),
124                                                 $change->old_value, $change->value
125                                         )
126                                 );
127                         }
128                         print "</li>\n";
129                 }
130                 print "</ul>\n";
131         }
132         print "</li>\n";
133 }
134
135 if ($User->login) {
136         print '<li>';
137         print '<form method="post" action="" enctype="multipart/form-data">';
138         if (isset($Issue) and $User->admin("edit {$Page->link}")) {
139                 print '<p>';
140                 printf(
141                         '<label for="%s">%s:</label> '
142                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
143                         'assign',
144                         $journalcol['assign'],
145                         htmlspecialchars($Issue->assign ?? '')
146                 );
147                 printf(
148                         '<input type="hidden" name="%s" value="" />' .
149                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
150                         . '<label for="%1$s"> %s</label>'."\n",
151                         'status',
152                         'resolved',
153                         isset($Issue->closed) ? ' checked' : '',
154                         'Gesloten'
155                 );
156                 print "</p>\n";
157         }
158         if (isset($Issue)) {
159                 printf(
160                         '<p><label for="%s">%s:</label> '
161                         . '<input id="%1$s" name="%1$s" value=""%s /></p>'."\n",
162                         'image', 'Beeldmateriaal', ' type="file" accept="image/*"'
163                 );
164         }
165         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
166                 'reply',
167                 "Bericht van {$User->login}",
168                 ''
169         );
170         print '<input type="submit" value="Plaatsen" />'."\n";
171         print "</form>";
172         print '<script src="/upload/progress.js"></script>';
173         print "</li>\n";
174 }
175
176 print "</ul>\n\n";