0935567e82ecb51ba2aeb4ff0a6717b50e313abe
[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                 $target = "/{$Page->link}/$newcomment#$newcomment";
65                 abort($target, ($Page->api ? 200 : 303) . ' reply success');
66                 $_POST['reply'] = NULL;
67         }
68         catch (Exception $e) {
69                 if ($Page->api) {
70                         abort(ucfirst($e->getMessage()), '500 reply error');
71                 }
72                 print "<p class=warn>Antwoord niet opgeslagen: {$e->getMessage()}.</p>\n\n";
73         }
74 }
75
76 $cols = '*, (SELECT json_agg(journal.*) FROM journal WHERE comment_id = comments.id) AS journal';
77 $query = $Db->query("SELECT $cols FROM comments WHERE page = ? ORDER BY created", [$Page->link]);
78
79 print '<ul class="replies">';
80
81 while ($row = $query->fetch()) {
82         $rowuser = new User("profile/{$row->author}");
83         printf('<li id="%d">', $row->id);
84         printf('<strong>%s</strong> <small class=date>%s</small>',
85                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
86         );
87         printf("<blockquote>\n%s</blockquote>\n", $row->message);
88         if ($changes = json_decode($row->journal)) {
89                 print '<ul>';
90                 foreach ($changes as $change) {
91                         print '<li>';
92                         if ($change->col == 'closed') {
93                                 printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
94                         }
95                         else {
96                                 printf("<em>%s</em> %s",
97                                         $journalcol[$change->col], sprintf(
98                                                 !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
99                                                 (!isset($change->value) ? 'verwijderd (<s>%s</s>)' :
100                                                 'gewijzigd van <q>%s</q> naar <q>%s</q>'),
101                                                 $change->old_value, $change->value
102                                         )
103                                 );
104                         }
105                         print "</li>\n";
106                 }
107                 print "</ul>\n";
108         }
109         print "</li>\n";
110 }
111
112 if ($User->login) {
113         print '<li>';
114         print '<form method="post" action="" enctype="multipart/form-data">';
115         if (isset($Issue) and $User->admin("edit {$Page->link}")) {
116                 print '<p>';
117                 printf(
118                         '<label for="%s">%s:</label> '
119                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
120                         'assign',
121                         $journalcol['assign'],
122                         htmlspecialchars($Issue->assign ?? '')
123                 );
124                 printf(
125                         '<input type="hidden" name="%s" value="" />' .
126                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
127                         . '<label for="%1$s"> %s</label>'."\n",
128                         'status',
129                         'resolved',
130                         isset($Issue->closed) ? ' checked' : '',
131                         'Gesloten'
132                 );
133                 print "</p>\n";
134         }
135         if (isset($Issue)) {
136                 printf(
137                         '<p><label for="%s">%s:</label> '
138                         . '<input id="%1$s" name="%1$s" value=""%s /></p>'."\n",
139                         'image', 'Beeldmateriaal', ' type="file" accept="image/*"'
140                 );
141         }
142         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
143                 'reply',
144                 "Bericht van {$User->login}",
145                 ''
146         );
147         print '<input type="submit" value="Plaatsen" />'."\n";
148         print "</form>";
149         print '<script src="/upload/progress.js"></script>';
150         print "</li>\n";
151 }
152
153 print "</ul>\n\n";