issue: include reply image in api metadata
[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         $Page->body = $replies;  # find image
19         if ($Page->api) return;
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 ($Page->api) return;
46 if ($_POST and isset($_POST['subject'])) {
47                 require_once 'upload.inc.php';
48                 if (strlen($_POST['subject']) < 2) {
49                         throw new Exception('Een minimaal onderwerp is verplicht om een issue aan te maken.');
50                 }
51                 $query = $Db->set('issues', [
52                         'page'    => $Page->handler,
53                         'subject' => $_POST['subject'],
54                         'body'    => messagehtml($_POST['body']),
55                         'author'  => $User->login,
56                 ]);
57                 if (!$query->rowCount()) {
58                         throw new Exception('Issue niet opgeslagen.');
59                 }
60                 $_POST = [];
61 }
62
63 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
64 $cols = "*, ($subsql AND message IS NOT NULL) AS replycount";
65 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
66 $sql = "SELECT $cols FROM issues i WHERE page = ?";
67 if (isset($_GET['open'])) {
68         $sql .= ' AND closed IS NULL';
69 }
70 $sql .= ' ORDER BY closed IS NOT NULL, updated DESC';
71 $query = $Db->query($sql, [$Page->handler]);
72
73 if ($id == 'feed') {
74         require 'issue/feed.inc.php';
75 }
76
77 ob_start();
78 print '<ul>';
79 while ($row = $query->fetch()) {
80         printf('<li%s><div><a href="%s">',
81                 $row->closed ? ' class="disabled"' : '',
82                 "/{$Page->handler}/{$row->id}/{$row->link}"
83         );
84         printf($row->closed ? '<s>%s</s>' : '%s', htmlspecialchars($row->subject));
85         {
86                 printf(' <small class="date">%s</small>',
87                         showdate(array_slice(preg_split('/\D/', $row->updated), 0, 3))
88                 );
89         }
90         if ($row->replycount) {
91                 printf(' <span class=right>%s %d</span>',
92                         '<span class="icon icon-comment" title="reacties">&#x1F5E8;</span>',
93                         $row->replycount
94                 );
95         }
96         if ($row->imagecount) {
97                 print ' <span class="right icon icon-camera" title="afbeeldingen">&#x1F4F7;</span>';
98         }
99         if (isset($row->assign)) {
100                 print ' <em class="right">'.$row->assign.'</em>';
101         }
102         print "</a></div></li>\n";
103 }
104 print "</ul>\n";
105 $Page->place['issuelist'] = ob_get_clean();