widget/reply: lazy image loading in html5
[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 $imagecount = 0;
90 while ($row = $query->fetch()) {
91         $rowuser = new User("profile/{$row->author}");
92         printf('<li id="%d">', $row->id);
93         printf('<strong>%s</strong> <small class=date>%s</small>',
94                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
95         );
96         if ($html = $row->message) {
97                 $html = preg_replace('/(?<=<img )/',
98                         $imagecount > 2 ? 'loading="lazy" ' : '', $html, -1, $found);
99                 $imagecount += $found;
100                 printf("<blockquote>\n%s</blockquote>\n", $html);
101         }
102         if ($changes = json_decode($row->journal)) {
103                 print '<ul>';
104                 foreach ($changes as $change) {
105                         print '<li>';
106                         if ($change->col == 'closed') {
107                                 printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
108                         }
109                         else {
110                                 printf("<em>%s</em> %s",
111                                         $journalcol[$change->col], sprintf(
112                                                 !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
113                                                 (!isset($change->value) ? 'verwijderd (<s>%s</s>)' :
114                                                 'gewijzigd van <q>%s</q> naar <q>%s</q>'),
115                                                 $change->old_value, $change->value
116                                         )
117                                 );
118                         }
119                         print "</li>\n";
120                 }
121                 print "</ul>\n";
122         }
123         print "</li>\n";
124 }
125
126 if ($User->login) {
127         print '<li>';
128         print '<form method="post" action="" enctype="multipart/form-data">';
129         if (isset($Issue) and $User->admin("edit {$Page->link}")) {
130                 print '<p>';
131                 printf(
132                         '<label for="%s">%s:</label> '
133                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
134                         'assign',
135                         $journalcol['assign'],
136                         htmlspecialchars($Issue->assign ?? '')
137                 );
138                 printf(
139                         '<input type="hidden" name="%s" value="" />' .
140                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
141                         . '<label for="%1$s"> %s</label>'."\n",
142                         'status',
143                         'resolved',
144                         isset($Issue->closed) ? ' checked' : '',
145                         'Gesloten'
146                 );
147                 print "</p>\n";
148         }
149         if (isset($Issue)) {
150                 printf(
151                         '<p><label for="%s">%s:</label> '
152                         . '<input id="%1$s" name="%1$s" value=""%s /></p>'."\n",
153                         'image', 'Beeldmateriaal', ' type="file" accept="image/*"'
154                 );
155         }
156         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
157                 'reply',
158                 "Bericht van {$User->login}",
159                 ''
160         );
161         print '<input type="submit" value="Plaatsen" />'."\n";
162         print "</form>";
163         print '<script src="/upload/progress.js"></script>';
164         print "</li>\n";
165 }
166
167 print "</ul>\n\n";