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