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