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