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