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