page: replace global variables by $Page object
[minimedit.git] / widget / reply.php
index 879b53d248317faa662e9ae66184260fa84188ce..ca38ce970d1885d964e216924e69b6a3e1feb1b8 100644 (file)
@@ -7,11 +7,21 @@ $journalcol = [
 ];
 
 if ($_POST) {
+       require_once 'upload.inc.php';
        try {
-               $html = nl2br(htmlspecialchars($_POST['reply']));
-               $html = "<p>$html</p>";
+               $html = messagehtml($_POST['reply']);
+               if ($_FILES and !empty($_FILES['image'])) {
+                       $target = 'data/upload';
+                       if (!file_exists($target)) {
+                               throw new Exception("er is geen uploadmap aanwezig op $target");
+                       }
+                       $target .= '/' . $User->login;
+                       if ($result = userupload($_FILES['image'], $target)) {
+                               $html .= sprintf('<p><img src="/thumb/640x/%s" /></p>', $result);
+                       }
+               }
                $query = $Db->set('comments', [
-                       'page'    => $Page,
+                       'page'    => $Page->handler,
                        'message' => $html,
                        'author'  => $User->login,
                ]);
@@ -47,6 +57,7 @@ if ($_POST) {
                                                'value'      => $updated->$col,
                                        ]);
                                }
+                               $Issue = $updated;
                        }
                }
                $_POST['reply'] = NULL;
@@ -56,7 +67,8 @@ if ($_POST) {
        }
 }
 
-$query = $Db->query('SELECT * FROM comments WHERE page = ? ORDER BY created', [$Page]);
+$cols = '*, (SELECT json_agg(journal.*) FROM journal WHERE comment_id = comments.id) AS journal';
+$query = $Db->query("SELECT $cols FROM comments WHERE page = ? ORDER BY created", [$Page->handler]);
 
 print '<ul class="replies">';
 
@@ -66,14 +78,35 @@ while ($row = $query->fetch()) {
        printf('<strong>%s</strong> <small class=date>%s</small>',
                $rowuser->html, showdate(preg_split('/\D/', $row->created))
        );
-       printf("<blockquote>%s</blockquote>\n", $row->message);
+       printf("<blockquote>\n%s</blockquote>\n", $row->message);
+       if ($changes = json_decode($row->journal)) {
+               print '<ul>';
+               foreach ($changes as $change) {
+                       print '<li>';
+                       if ($change->col == 'closed') {
+                               printf('<em>%s</em>', isset($change->value) ? 'Gesloten' : 'Heropend');
+                       }
+                       else {
+                               printf("<em>%s</em> %s",
+                                       $journalcol[$change->col], sprintf(
+                                               !isset($change->old_value) ? 'gewijzigd naar <q>%2$s</q>' :
+                                               (!isset($change->value) ? 'verwijderd (<s>%s</s>)' :
+                                               'gewijzigd van <q>%s</q> naar <q>%s</q>'),
+                                               $change->old_value, $change->value
+                                       )
+                               );
+                       }
+                       print "</li>\n";
+               }
+               print "</ul>\n";
+       }
        print "</li>\n";
 }
 
-if ($User) {
+if ($User->login) {
        print '<li>';
-       print '<form method="post" action="">';
-       if (isset($Issue) and $User->admin("edit $Page")) {
+       print '<form method="post" action="" enctype="multipart/form-data">';
+       if (isset($Issue) and $User->admin("edit {$Page->handler}")) {
                print '<p>';
                printf(
                        '<label for="%s">%s:</label> '
@@ -93,6 +126,13 @@ if ($User) {
                );
                print "</p>\n";
        }
+       if (isset($Issue)) {
+               printf(
+                       '<p><label for="%s">%s:</label> '
+                       . '<input id="%1$s" name="%1$s" value=""%s /></p>'."\n",
+                       'image', 'Beeldmateriaal', ' type="file" accept="image/*"'
+               );
+       }
        printf('<textarea id="%s" name="%1$s" cols=60 rows=3 placeholder="%s">%s</textarea>'."\n",
                'reply',
                "Bericht van {$User->login}",