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