f9248512105bda05860bf691d212bbf7d1e0e3b3
[minimedit.git] / widget / reply.php
1 <?php
2 global $User, $Db, $Issue;
3 require_once 'database.inc.php';
4 require_once 'upload.inc.php';
5
6 if ($_POST) {
7         try {
8                 $newcomment = createcomment($_POST, $Issue);
9                 $target = "/{$Page->link}?last=$newcomment#$newcomment";
10                 abort($target, ($Page->api ? 200 : 303) . ' reply success');
11                 $_POST['reply'] = NULL;
12         }
13         catch (Exception $e) {
14                 if ($Page->api) {
15                         abort(ucfirst($e->getMessage()), '500 reply error');
16                 }
17                 printf("<p class=warn>Antwoord niet opgeslagen: %s.</p>\n\n",
18                         nl2br(htmlspecialchars($e->getMessage()))
19                 );
20         }
21 }
22
23 $cols = "*, (SELECT json_agg(journal.*) FROM journal WHERE comment_id = comments.id AND property = 'attr') AS journal";
24 $query = $Db->query("SELECT $cols FROM comments WHERE page = ? ORDER BY created", [$Page->link]);
25
26 if ($row = $query->fetch()) {
27         print $row->message;
28         $Page->teaser = $row->raw;
29 }
30
31 print '<ul class="replies">';
32
33 $imagecount = 0;
34 while ($row = $query->fetch()) {
35         $rowuser = new User("profile/{$row->author}");
36         printf('<li id="%d">', $row->id);
37         printf('<strong>%s</strong> <small class=date>%s</small>',
38                 $rowuser->html, showdate(preg_split('/\D/', $row->created))
39         );
40         if ($html = $row->message) {
41                 $html = preg_replace('/(?<=<img )/',
42                         $imagecount > 2 ? 'loading="lazy" ' : '', $html, -1, $found);
43                 $imagecount += $found;
44                 printf("<blockquote>\n%s</blockquote>\n", $html);
45         }
46         if ($changes = json_decode($row->journal)) {
47                 print '<ul>';
48                 foreach ($changes as $change) {
49                         print '<li>';
50                         if ($change->col == 'closed') {
51                                 printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
52                         }
53                         else {
54                                 printf("<em>%s</em> %s",
55                                         $journalcol[$change->col], sprintf(
56                                                 !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
57                                                 (!isset($change->value) ? 'verwijderd (<s>%s</s>)' :
58                                                 'gewijzigd van <q>%s</q> naar <q>%s</q>'),
59                                                 $change->old_value, $change->value
60                                         )
61                                 );
62                         }
63                         print "</li>\n";
64                 }
65                 print "</ul>\n";
66         }
67         print "</li>\n";
68 }
69
70 if ($User->login) {
71         print '<li>';
72         print '<form method="post" action="" enctype="multipart/form-data">';
73         if (isset($Issue) and $User->admin("edit {$Page->link}")) {
74                 print "<aside>\n";
75                 print '<p>';
76                 printf(
77                         '<label for="%s">%s:</label> '
78                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
79                         'subject',
80                         $journalcol['subject'],
81                         htmlspecialchars($Issue->subject ?? '')
82                 );
83                 print "</p>\n";
84
85                 print '<p>';
86                 printf(
87                         '<label for="%s">%s:</label> '
88                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
89                         'assign',
90                         $journalcol['assign'],
91                         htmlspecialchars($Issue->assign ?? '')
92                 );
93                 printf(
94                         '<input type="hidden" name="%s" value="" />' .
95                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
96                         . '<label for="%1$s"> %s</label>'."\n",
97                         'status',
98                         'resolved',
99                         isset($Issue->closed) ? ' checked' : '',
100                         'Gesloten'
101                 );
102                 print "</p>\n";
103                 print "</aside>\n";
104         }
105         {
106                 print '<p>';
107                 printf(
108                         '<input type="hidden" name="%s" value="" />'
109                         . "Geplaatste berichten zijn hier direct zichtbaar voor bewoners.\n"
110                         . '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s%s />'
111                         . '<label for="%1$s"> %s</label>'."\n",
112                         'announce',
113                         '1',
114                         ($_POST['announce'] ?? TRUE) ? ' checked' : '',
115                         ' onclick="this.nextSibling.hidden = !this.checked"',
116                         "De eerste regel wordt ook weergegeven op het scherm in de hal."
117                 );
118                 print "</p>\n";
119         }
120         if (isset($Issue)) {
121                 printf(
122                         '<p><label for="%s">%s:</label> '
123                         . '<input id="%1$s" name="%1$s" value=""%s /></p>'."\n",
124                         'image', 'Beeldmateriaal', ' type="file" accept="image/*"'
125                 );
126         }
127         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
128                 'reply',
129                 "Bericht van {$User->login}",
130                 ''
131         );
132         print '<input type="submit" value="Plaatsen" />'."\n";
133         print "</form>";
134         print '<script src="/upload/progress.js"></script>';
135         print "</li>\n";
136 }
137
138 print "</ul>\n\n";