55ea273285b71128ccaa24e7f501d3707b600406
[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('Een minimaal onderwerp is verplicht om een issue aan te maken.');
70                 }
71                 $query = $Db->set('issues', [
72                         'page'    => $Page->handler,
73                         'subject' => $_POST['subject'],
74                         'link'    => preg_replace('/\b(?:de|het|een)\s+|\W+/', '-', strtolower($_POST['subject'])),
75                         'author'  => $User->login,
76                 ]);
77                 if (!$query->rowCount()) {
78                         throw new Exception('Issue niet opgeslagen.');
79                 }
80                 $row = $query->fetch();
81                 if (!$row->id) {
82                         throw new Exception('Issue niet goed opgeslagen.');
83                 }
84                 try {
85                         createcomment($_POST, $row);
86                 }
87                 catch (Exception $e) {
88                         throw new Exception("Issueinhoud niet opgeslagen: {$e->getMessage()}.");
89                 }
90                 $_POST = [];
91 }
92
93 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
94 $cols = "*, ($subsql AND message IS NOT NULL) - 1 AS replycount";
95 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
96 $sql = "SELECT $cols FROM issues i WHERE page = ?";
97 if (isset($_GET['open'])) {
98         $sql .= ' AND closed IS NULL';
99 }
100 $sql .= ' ORDER BY updated DESC';
101 $query = $Db->query($sql, [$Page->handler]);
102
103 if ($id == 'feed') {
104         require 'issue/feed.inc.php';
105 }
106
107 ob_start();
108 $stats = $Db->query(
109         "SELECT count(*) AS total, count(closed) AS closed FROM issues"
110 )->fetch();
111 printf("<h4>%d lopende zaken, %s opgelost</h4>\n",
112         $stats->total - $stats->closed, $stats->closed
113 );
114 print '<ul>';
115 while ($row = $query->fetch()) {
116         printf('<li%s><div><a href="%s">',
117                 $row->closed ? ' class="disabled"' : '',
118                 "/{$Page->handler}/{$row->id}/{$row->link}"
119         );
120         printf($row->closed ? '<s>%s</s>' : '%s', htmlspecialchars($row->subject));
121         {
122                 printf(' <small class="date">%s</small>',
123                         showdate(array_slice(preg_split('/\D/', $row->updated), 0, 3))
124                 );
125         }
126         if ($row->replycount) {
127                 printf(' <span class=right>%s %d</span>',
128                         '<span class="icon icon-comment" title="reacties">&#x1F5E8;</span>',
129                         $row->replycount
130                 );
131         }
132         if ($row->imagecount) {
133                 print ' <span class="right icon icon-camera" title="afbeeldingen">&#x1F4F7;</span>';
134         }
135         if (isset($row->assign)) {
136                 print ' <em class="right">'.$row->assign.'</em>';
137         }
138         print "</a></div></li>\n";
139 }
140 print "</ul>\n";
141 $Page->place['issuelist'] = ob_get_clean();