page: call handler through index method
[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
14         $replies = $Page->widget('reply');  # handle updates
15
16         $Page->title .= ': '.htmlspecialchars($Issue->subject);
17         $Page->teaser = $Issue->body;
18         if ($Page->api) return;
19         $Page->body = $replies;  # find image
20
21         print "<h2>{$Page->title}</h2>\n";
22         print '<aside class="metadata"><dl>'."\n";
23         print '<dt>Geplaatst</dt>';
24         printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->created)));
25         if ($Issue->author and $author = new User('profile/'.$Issue->author, FALSE)) {
26                 printf('<dd>%s</dd>'."\n", $author->html);
27         }
28         if ($Issue->assign) {
29                 print '<dt>Toegewezen aan</dt>';
30                 printf('<dd>%s</dd>'."\n", htmlspecialchars($Issue->assign));
31         }
32         if ($Issue->closed) {
33                 print '<dt>Opgelost</dt>';
34                 printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->closed)));
35         }
36         print "</dl></aside>\n\n";
37
38         print '<div>';
39         print $Issue->body;
40         print $replies;
41         print "</div>\n";
42         return;
43 }
44
45 if ($_POST) {
46                 require_once 'upload.inc.php';
47                 $query = $Db->set('issues', [
48                         'page'    => $Page->handler,
49                         'subject' => $_POST['subject'],
50                         'body'    => messagehtml($_POST['body']),
51                         'author'  => $User->login,
52                 ]);
53                 if (!$query->rowCount()) {
54                         throw new Exception('Issue niet opgeslagen.');
55                 }
56                 $_POST = [];
57 }
58 if ($Page->api) return;
59
60 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
61 $cols = "*, ($subsql AND message IS NOT NULL) AS replycount";
62 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
63 $sql = "SELECT $cols FROM issues i WHERE page = ?";
64 if (isset($_GET['open'])) {
65         $sql .= ' AND closed IS NULL';
66 }
67 $sql .= ' ORDER BY closed IS NOT NULL, updated DESC';
68 $query = $Db->query($sql, [$Page->handler]);
69
70 if ($id == 'feed') {
71         require 'issue/feed.inc.php';
72 }
73
74 ob_start();
75 print '<ul>';
76 while ($row = $query->fetch()) {
77         printf('<li%s><div><a href="%s">',
78                 $row->closed ? ' class="disabled"' : '',
79                 "/{$Page->handler}/{$row->id}/{$row->link}"
80         );
81         printf($row->closed ? '<s>%s</s>' : '%s', htmlspecialchars($row->subject));
82         {
83                 printf(' <small class="date">%s</small>',
84                         showdate(array_slice(preg_split('/\D/', $row->updated), 0, 3))
85                 );
86         }
87         if ($row->replycount) {
88                 printf(' <span class=right>%s %d</span>',
89                         '<span class="icon icon-comment" title="reacties">&#x1F5E8;</span>',
90                         $row->replycount
91                 );
92         }
93         if ($row->imagecount) {
94                 print ' <span class="right icon icon-camera" title="afbeeldingen">&#x1F4F7;</span>';
95         }
96         if (isset($row->assign)) {
97                 print ' <em class="right">'.$row->assign.'</em>';
98         }
99         print "</a></div></li>\n";
100 }
101 print "</ul>\n";
102 $Page->place['issuelist'] = ob_get_clean();