diff --git a/README.md b/README.md index 716baca..eaf4f12 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ Or clone from GitHub and put in the `user/plugins/comments` folder. # TODO -- Validate comment, name and email on the frontend form - Add language file for the frontend - Allow to moderate comments from the admin - Email the comment to the site admins (default to all with admin.super, could be configured) diff --git a/admin/templates/comments.html.twig b/admin/templates/comments.html.twig index 103cb60..8a54619 100644 --- a/admin/templates/comments.html.twig +++ b/admin/templates/comments.html.twig @@ -26,61 +26,75 @@ .pages-list .row p.page-route { margin: 0px 0 10px 0px; } + + th { background: #d9d9d9; } + + .comment { flex: 3 } + .details { flex: 1.5 } + + .center { + margin: 0 auto; + text-align: center; + display: block; + } + + .button:active { margin: 0 auto;} +

Latest comments

+
- - -
{% endblock %} diff --git a/comments.php b/comments.php index 7e83366..e5998a0 100644 --- a/comments.php +++ b/comments.php @@ -53,7 +53,15 @@ class CommentsPlugin extends Plugin 'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0], ]); - $this->grav['twig']->files = $this->getFilesOrderedByModifiedDate(); + $page = $this->grav['uri']->param('page'); + $comments = $this->getLastComments($page); + + if ($page > 0) { + echo json_encode($comments); + exit(); + } + + $this->grav['twig']->comments = $comments; } } @@ -152,6 +160,48 @@ class CommentsPlugin extends Plugin return $files; } + private function getLastComments($page = 0) { + $number = 10; + + $files = []; + $files = $this->getFilesOrderedByModifiedDate(); + + $comments = []; + + foreach($files as $file) { + $data = Yaml::parse(file_get_contents($file->filePath)); + for($i = 0; $i < count($data['comments']); $i++) { + $data['comments'][$i]['pageTitle'] = $data['title']; + $data['comments'][$i]['filePath'] = $file->filePath; + + } + $comments = array_merge($comments, $data['comments']); + } + + // Order comments by date + usort($comments, function($a, $b) { + return !($a['date'] > $b['date']); + }); + + $totalAvailable = count($comments); + + $comments = array_slice($comments, $page * $number, $number); + + $totalRetrieved = ($page + 1) * $number; + $hasMore = false; + + if ($totalAvailable > $totalRetrieved) { + $hasMore = true; + } + + return (object)array( + "comments" => $comments, + "page" => $page, + "totalAvailable" => $totalAvailable, + "totalRetrieved" => $totalRetrieved + ); + } + /** * Return the comments associated to the current route */