issue: reply posts in article elements
[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 '<section class="replies">';
32
33 $imagecount = 0;
34 while ($row = $query->fetch()) {
35         $rowuser = new User("profile/{$row->author}");
36         printf('<article id="%d">', $row->id);
37         printf('<strong>%s</strong>', $rowuser->html);
38         $rowdate = showdate(preg_split('/\D/', $row->created));
39         if ($User->admin('beheer') and $row->updated) {
40                 $rowdate = "<s>$rowdate</s> " . showdate(preg_split('/\D/', $row->updated));
41         }
42         printf(' <small class=date>%s</small>', $rowdate);
43         if ($User->admin('beheer') and $User->admin('user') || $User->login === $row->author) {
44                 printf(' <a href="%s" title="%s" class=icon>%s</a>',
45                         "/{$Page->link}/{$row->id}", 'reactie aanpassen', "\u{270D}");
46         }
47         if ($html = $row->message) {
48                 $html = preg_replace('/(?<=<img )/',
49                         $imagecount > 2 ? 'loading="lazy" ' : '', $html, -1, $found);
50                 $imagecount += $found;
51                 printf("<blockquote>\n%s</blockquote>\n", $html);
52         }
53         if ($changes = json_decode($row->journal)) {
54                 print '<ul>';
55                 foreach ($changes as $change) {
56                         print '<li>';
57                         if ($change->col == 'closed') {
58                                 printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
59                         }
60                         else {
61                                 printf("<em>%s</em> %s",
62                                         $journalcol[$change->col], sprintf(
63                                                 !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
64                                                 (!isset($change->value) ? 'verwijderd (<s>%s</s>)' :
65                                                 'gewijzigd van <q>%s</q> naar <q>%s</q>'),
66                                                 $change->old_value, $change->value
67                                         )
68                                 );
69                         }
70                         print "</li>\n";
71                 }
72                 print "</ul>\n";
73         }
74         print "</article>\n";
75 }
76
77 if ($User->login) {
78         print '<article><hr />';
79         print '<form method="post" action="" enctype="multipart/form-data">';
80         if (isset($Issue) and $User->admin("edit {$Page->link}")) {
81                 print "<aside>\n";
82                 print '<p>';
83                 printf(
84                         '<label for="%s">%s:</label> '
85                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
86                         'subject',
87                         $journalcol['subject'],
88                         htmlspecialchars($Issue->subject ?? '')
89                 );
90                 print "</p>\n";
91
92                 print '<p>';
93                 printf(
94                         '<label for="%s">%s:</label> '
95                         . '<input id="%1$s" name="%1$s" value="%s" />'."\n",
96                         'assign',
97                         $journalcol['assign'],
98                         htmlspecialchars($Issue->assign ?? '')
99                 );
100                 printf(
101                         '<input type="hidden" name="%s" value="" />' .
102                         '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s />'
103                         . '<label for="%1$s"> %s</label>'."\n",
104                         'status',
105                         'resolved',
106                         isset($Issue->closed) ? ' checked' : '',
107                         'Gesloten'
108                 );
109                 print "</p>\n";
110                 print "</aside>\n";
111         }
112         {
113                 print '<p>';
114                 printf(
115                         '<input type="hidden" name="%s" value="" />'
116                         . "Geplaatste berichten zijn hier direct zichtbaar voor bewoners.\n"
117                         . '<input type="checkbox" id="%1$s" name="%1$s" value="%s"%s%s />'
118                         . '<label for="%1$s"> %s</label>'."\n",
119                         'announce',
120                         '1',
121                         ($_POST['announce'] ?? TRUE) ? ' checked' : '',
122                         ' onclick="this.nextSibling.hidden = !this.checked"',
123                         "De eerste regel wordt ook weergegeven op het scherm in de hal."
124                 );
125                 print "</p>\n";
126         }
127         if (isset($Issue)) {
128                 printf(
129                         '<p><label for="%s">%s:</label> '
130                         . '<input id="%1$s" name="%1$s" value=""%s /></p>'."\n",
131                         'image', 'Beeldmateriaal', ' type="file" accept="image/*"'
132                 );
133         }
134         printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
135                 'reply',
136                 "Bericht van {$User->login}",
137                 ''
138         );
139         print '<input type="submit" value="Plaatsen" />'."\n";
140         print "</form>";
141         print '<script src="/upload/progress.js"></script>';
142         print "</article>\n";
143 }
144
145 print "</section>\n\n";