issue: require message contents
[minimedit.git] / issue / index.php
index 6aebf8e2ff5032317adcab4e6b5322b86392d0f8..df1aa6869c0511e57fafe1c7c3b5053cd3fbeac4 100644 (file)
@@ -1,24 +1,46 @@
 <?php
-global $User, $Db;
+global $User, $Db, $Issue;
 require_once 'database.inc.php';
-@list ($id, $title) = explode('/', ltrim($Args, '/'));
+@list ($id, $title) = explode('/', ltrim($Page->path, '/'));
 
 if ($id and ctype_digit($id)) {
-       $Article->title = "Issue #$id";
-       $Args = "/$id";  # minimal reference
+       $Page->title = "Issue #$id";
+       $Page->link = $Page->handler . ($Page->path = "/$id");  # minimal reference
        $Issue = $Db->query(
-               'SELECT * FROM issues WHERE page = ? AND id = ?', [$Page, $id]
+               'SELECT * FROM issues WHERE page = ? AND id = ?', [$Page->handler, $id]
        )->fetch();
        if (!$Issue) throw new Exception('Issuenummer niet gevonden');
+       $Page->title .= ': '.htmlspecialchars($Issue->subject);
 
-       $replies = placeholder_include('reply');  # handle updates
+       if ($title and ctype_digit($title)) {
+               $Page->title = "Antwoord op {$Page->title}";
+               $Page->handler = $Page->link;
+               $Page->link .= "/$title";
+               $row = $Db->query(
+                       'SELECT * FROM comments WHERE id = ?', [$title]
+               )->fetch();
+               if (!$row) throw new Exception('Antwoordnummer niet gevonden');
 
-       $Article->title .= ': '.htmlspecialchars($Issue->subject);
-       $Article->teaser = $Issue->body;
-       $Article->body = $replies;  # find image
+               print "<h2>{$Page->title}</h2>\n";
+               printf('<form method="post" action="%s" enctype="multipart/form-data">',
+                       $Page->handler
+               );
+               printf('<input type="hidden" name="%s" value="%s" />'."\n", 'id', $row->id);
+               printf('<textarea id="%s" name="%1$s" cols=60 rows=3>%s</textarea>'."\n",
+                       'reply',
+                       htmlspecialchars($row->raw)
+               );
+               print '<input type="submit" value="Aanpassen" />'."\n";
+               print "</form>\n";
+               return;
+       }
 
-       print "<h2>{$Article->title}</h2>\n";
-       print "<dl class=\"aside right sidebar\">\n";
+       $replies = $Page->widget('reply');  # handle updates
+       $Page->body = $replies;  # find image
+       if ($Page->api) return;
+
+       print "<h2>{$Page->title}</h2>\n";
+       print '<aside class="metadata"><dl>'."\n";
        print '<dt>Geplaatst</dt>';
        printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->created)));
        if ($Issue->author and $author = new User('profile/'.$Issue->author, FALSE)) {
@@ -32,49 +54,71 @@ if ($id and ctype_digit($id)) {
                print '<dt>Opgelost</dt>';
                printf('<dd>%s</dd>'."\n", showdate(preg_split('/\D/', $Issue->closed)));
        }
-       print "</dl>\n\n";
+       print "</dl></aside>\n\n";
 
        print '<div>';
-       print $Issue->body;
        print $replies;
        print "</div>\n";
        return;
 }
 
-if ($_POST) {
+if ($Page->api) return;
+if ($_POST and isset($_POST['subject'])) {
                require_once 'upload.inc.php';
+               if (strlen($_POST['subject']) < 2) {
+                       throw new Exception('Vul een onderwerp in om de issue te kunnen benoemen.');
+               }
+               if (!preg_match('/\S/', $_POST['reply'])) {
+                       throw new Exception('Een korte beschrijving is verplicht om een issue aan te maken.');
+               }
                $query = $Db->set('issues', [
-                       'page'    => $Page,
+                       'page'    => $Page->handler,
                        'subject' => $_POST['subject'],
-                       'body'    => messagehtml($_POST['body']),
+                       'link'    => preg_replace('/\b(?:de|het|een)\s+|\W+/', '-', strtolower($_POST['subject'])),
                        'author'  => $User->login,
                ]);
                if (!$query->rowCount()) {
                        throw new Exception('Issue niet opgeslagen.');
                }
+               $row = $query->fetch();
+               if (!$row->id) {
+                       throw new Exception('Issue niet goed opgeslagen.');
+               }
+               try {
+                       createcomment($_POST, $row);
+               }
+               catch (Exception $e) {
+                       throw new Exception("Issueinhoud niet opgeslagen: {$e->getMessage()}.");
+               }
                $_POST = [];
 }
 
 $subsql = "SELECT count(*) FROM comments WHERE page=i.page||'/'||i.id";
-$cols = "*, ($subsql AND message IS NOT NULL) AS replycount";
+$cols = "*, ($subsql AND message IS NOT NULL) - 1 AS replycount";
 $cols .= ", ($subsql AND message ~ '<img') AS imagecount";
 $sql = "SELECT $cols FROM issues i WHERE page = ?";
 if (isset($_GET['open'])) {
        $sql .= ' AND closed IS NULL';
 }
-$sql .= ' ORDER BY closed IS NOT NULL, updated DESC';
-$query = $Db->query($sql, [$Page]);
+$sql .= ' ORDER BY updated DESC';
+$query = $Db->query($sql, [$Page->handler]);
 
 if ($id == 'feed') {
        require 'issue/feed.inc.php';
 }
 
 ob_start();
+$stats = $Db->query(
+       "SELECT count(*) AS total, count(closed) AS closed FROM issues"
+)->fetch();
+printf("<h4>%d lopende zaken, %s opgelost</h4>\n",
+       $stats->total - $stats->closed, $stats->closed
+);
 print '<ul>';
 while ($row = $query->fetch()) {
        printf('<li%s><div><a href="%s">',
                $row->closed ? ' class="disabled"' : '',
-               "/$Page/{$row->id}/{$row->link}"
+               "/{$Page->handler}/{$row->id}/{$row->link}"
        );
        printf($row->closed ? '<s>%s</s>' : '%s', htmlspecialchars($row->subject));
        {
@@ -97,4 +141,4 @@ while ($row = $query->fetch()) {
        print "</a></div></li>\n";
 }
 print "</ul>\n";
-$Place['issuelist'] = ob_get_clean();
+$Page->place['issuelist'] = ob_get_clean();