issue: header counting open and closed rows
[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->body = $replies;  # find image
18         if ($Page->api) return;
19
20         print "<h2>{$Page->title}</h2>\n";
21         print '<aside class="metadata"><dl>'."\n";
22         print '<dt>Geplaatst</dt>';
23         printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->created)));
24         if ($Issue->author and $author = new User('profile/'.$Issue->author, FALSE)) {
25                 printf('<dd>%s</dd>'."\n", $author->html);
26         }
27         if ($Issue->assign) {
28                 print '<dt>Toegewezen aan</dt>';
29                 printf('<dd>%s</dd>'."\n", htmlspecialchars($Issue->assign));
30         }
31         if ($Issue->closed) {
32                 print '<dt>Opgelost</dt>';
33                 printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->closed)));
34         }
35         print "</dl></aside>\n\n";
36
37         print '<div>';
38         print $replies;
39         print "</div>\n";
40         return;
41 }
42
43 if ($Page->api) return;
44 if ($_POST and isset($_POST['subject'])) {
45                 require_once 'upload.inc.php';
46                 if (strlen($_POST['subject']) < 2) {
47                         throw new Exception('Een minimaal onderwerp is verplicht om een issue aan te maken.');
48                 }
49                 $query = $Db->set('issues', [
50                         'page'    => $Page->handler,
51                         'subject' => $_POST['subject'],
52                         'link'    => preg_replace('/\b(?:de|het|een)\s+|\W+/', '-', strtolower($_POST['subject'])),
53                         'author'  => $User->login,
54                 ]);
55                 if (!$query->rowCount()) {
56                         throw new Exception('Issue niet opgeslagen.');
57                 }
58                 $row = $query->fetch();
59                 if (!$row->id) {
60                         throw new Exception('Issue niet goed opgeslagen.');
61                 }
62                 try {
63                         createcomment($_POST, $row);
64                 }
65                 catch (Exception $e) {
66                         throw new Exception("Issueinhoud niet opgeslagen: {$e->getMessage()}.");
67                 }
68                 $_POST = [];
69 }
70
71 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
72 $cols = "*, ($subsql AND message IS NOT NULL) - 1 AS replycount";
73 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
74 $sql = "SELECT $cols FROM issues i WHERE page = ?";
75 if (isset($_GET['open'])) {
76         $sql .= ' AND closed IS NULL';
77 }
78 $sql .= ' ORDER BY updated DESC';
79 $query = $Db->query($sql, [$Page->handler]);
80
81 if ($id == 'feed') {
82         require 'issue/feed.inc.php';
83 }
84
85 ob_start();
86 $stats = $Db->query(
87         "SELECT count(*) AS total, count(closed) AS closed FROM issues"
88 )->fetch();
89 printf("<h4>%d lopende zaken, %s opgelost</h4>\n",
90         $stats->total - $stats->closed, $stats->closed
91 );
92 print '<ul>';
93 while ($row = $query->fetch()) {
94         printf('<li%s><div><a href="%s">',
95                 $row->closed ? ' class="disabled"' : '',
96                 "/{$Page->handler}/{$row->id}/{$row->link}"
97         );
98         printf($row->closed ? '<s>%s</s>' : '%s', htmlspecialchars($row->subject));
99         {
100                 printf(' <small class="date">%s</small>',
101                         showdate(array_slice(preg_split('/\D/', $row->updated), 0, 3))
102                 );
103         }
104         if ($row->replycount) {
105                 printf(' <span class=right>%s %d</span>',
106                         '<span class="icon icon-comment" title="reacties">&#x1F5E8;</span>',
107                         $row->replycount
108                 );
109         }
110         if ($row->imagecount) {
111                 print ' <span class="right icon icon-camera" title="afbeeldingen">&#x1F4F7;</span>';
112         }
113         if (isset($row->assign)) {
114                 print ' <em class="right">'.$row->assign.'</em>';
115         }
116         print "</a></div></li>\n";
117 }
118 print "</ul>\n";
119 $Page->place['issuelist'] = ob_get_clean();