login/pass: error messages below page title
[minimedit.git] / issue / index.php
1 <?php
2 global $User, $Db, $Issue;
3 require_once 'database.inc.php';
4 @list ($id, $title) = explode('/', ltrim($Page->path, '/'));
5
6 if ($id and ctype_digit($id)) {
7         $Page->title = "Issue #$id";
8         $Page->link = $Page->handler . ($Page->path = "/$id");  # minimal reference
9         $Issue = $Db->query(
10                 'SELECT * FROM issues WHERE page = ? AND id = ?', [$Page->handler, $id]
11         )->fetch();
12         if (!$Issue) throw new Exception('Issuenummer niet gevonden');
13         $Page->title .= ': '.htmlspecialchars($Issue->subject);
14
15         if ($title and ctype_digit($title)) {
16                 $Page->title = "Antwoord op {$Page->title}";
17                 $Page->handler = $Page->link;
18                 $Page->link .= "/$title";
19                 $row = $Db->query(
20                         'SELECT * FROM comments WHERE id = ?', [$title]
21                 )->fetch();
22                 if (!$row) throw new Exception('Antwoordnummer niet gevonden');
23
24                 printf('<form method="post" action="%s" enctype="multipart/form-data">',
25                         $Page->handler
26                 );
27                 print "<h2>{$Page->title}</h2>\n";
28                 printf('<input type="hidden" name="%s" value="%s" />'."\n", 'id', $row->id);
29                 printf(
30                         '<input type="hidden" name="%s" value="" />'
31                         . '<input type="checkbox" id="%1$s" name="%1$s" value="1"%s />'
32                         . '<label for="%1$s"> %s</label>'."\n<p>",
33                         'announce', $row->announced ? ' checked' : '', 'Aangekondigd'
34                 );
35                 printf('<input type="text" name="%s" value="%s" /><p>'."\n", 'page', $row->page);
36                 printf('<textarea id="%s" name="%1$s" cols=60 rows=3>%s</textarea>'."\n",
37                         'reply',
38                         htmlspecialchars($row->raw)
39                 );
40                 print '<input type="submit" value="Aanpassen" />'."\n";
41                 print "</form>\n";
42                 return;
43         }
44
45         $replies = $Page->widget('reply');  # handle updates
46         $Page->body = $replies;  # find image
47         if ($Page->api) return;
48
49         print "<h2>{$Page->title}</h2>\n";
50         print '<aside class="metadata"><dl>'."\n";
51         print '<dt>Geplaatst</dt>';
52         printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->created)));
53         if ($Issue->author and $author = new User('profile/'.$Issue->author, FALSE)) {
54                 printf('<dd>%s</dd>'."\n", $author->html);
55         }
56         if ($Issue->assign) {
57                 print '<dt>Toegewezen aan</dt>';
58                 printf('<dd>%s</dd>'."\n", htmlspecialchars($Issue->assign));
59         }
60         if ($Issue->closed) {
61                 print '<dt>Opgelost</dt>';
62                 printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->closed)));
63         }
64         print "</dl></aside>\n\n";
65
66         print '<div>';
67         print $replies;
68         print "</div>\n";
69         return;
70 }
71
72 if ($Page->api) return;
73 if ($_POST and isset($_POST['subject'])) {
74                 require_once 'upload.inc.php';
75                 if (strlen($_POST['subject']) < 2) {
76                         throw new Exception('Vul een onderwerp in om de issue te kunnen benoemen.');
77                 }
78                 if (!preg_match('/\S/', $_POST['reply'])) {
79                         throw new Exception('Een korte beschrijving is verplicht om een issue aan te maken.');
80                 }
81                 $query = $Db->set('issues', [
82                         'page'    => $Page->handler,
83                         'subject' => $_POST['subject'],
84                         'link'    => preg_replace('/\b(?:de|het|een)\s+|\W+/', '-', strtolower($_POST['subject'])),
85                         'author'  => $User->login,
86                 ]);
87                 if (!$query->rowCount()) {
88                         throw new Exception('Issue niet opgeslagen.');
89                 }
90                 $row = $query->fetch();
91                 if (!$row->id) {
92                         throw new Exception('Issue niet goed opgeslagen.');
93                 }
94                 try {
95                         createcomment($_POST, $row);
96                 }
97                 catch (Exception $e) {
98                         throw new Exception("Issueinhoud niet opgeslagen: {$e->getMessage()}.");
99                 }
100                 $_POST = [];
101 }
102
103 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
104 $cols = "*, ($subsql AND message IS NOT NULL) - 1 AS replycount";
105 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
106 $sql = "SELECT $cols FROM issues i WHERE page = ?";
107 if (isset($_GET['open'])) {
108         $sql .= ' AND closed IS NULL';
109 }
110 $sql .= ' ORDER BY updated DESC';
111 $query = $Db->query($sql, [$Page->handler]);
112
113 if ($id == 'feed') {
114         require 'issue/feed.inc.php';
115 }
116
117 ob_start();
118 $stats = $Db->query(
119         "SELECT count(*) AS total, count(closed) AS closed FROM issues"
120 )->fetch();
121 printf("<h4>%s, %s opgelost</h4>\n",
122         showlink(
123                 sprintf('%d lopende zaken', $stats->total - $stats->closed),
124                 isset($_GET['open']) ? FALSE : '?open'
125         ),
126         $stats->closed
127 );
128 print '<ul>';
129 while ($row = $query->fetch()) {
130         printf('<li%s><div><a href="%s">',
131                 $row->closed ? ' class="disabled"' : '',
132                 "/{$Page->handler}/{$row->id}/{$row->link}"
133         );
134         printf($row->closed ? '<s>%s</s>' : '%s', htmlspecialchars($row->subject));
135         {
136                 printf(' <small class="date">%s</small>',
137                         showdate(array_slice(preg_split('/\D/', $row->updated), 0, 3))
138                 );
139         }
140         if ($row->replycount) {
141                 printf(' <span class=right>%s %d</span>',
142                         '<span class="icon icon-comment" title="reacties">&#x1F5E8;</span>',
143                         $row->replycount
144                 );
145         }
146         if ($row->imagecount) {
147                 print ' <span class="right icon icon-camera" title="afbeeldingen">&#x1F4F7;</span>';
148         }
149         if (isset($row->assign)) {
150                 print ' <em class="right">'.$row->assign.'</em>';
151         }
152         print "</a></div></li>\n";
153 }
154 print "</ul>\n";
155 $Page->place['issuelist'] = ob_get_clean();