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