From de3a683cbe162aea20e542b4ff62d801b8f4f671 Mon Sep 17 00:00:00 2001 From: Thorsten Witteler Date: Fri, 20 Oct 2017 15:08:26 +0200 Subject: [PATCH] prepare ajax --- assets/comments.js | 9 ++-- comments.php | 50 ++++++++++++++++++++-- templates/partials/comments.form.html.twig | 2 +- templates/partials/comments.html.twig | 3 +- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/assets/comments.js b/assets/comments.js index bd499bd..ab811ff 100644 --- a/assets/comments.js +++ b/assets/comments.js @@ -11,7 +11,7 @@ $(document).ready(function () { } PhpComment.prototype.setupVariables = function () { - this.commentForm = this.element.find(".comment-form"); + this.commentForm = this.element.find(".comments-form"); this.titleField = this.element.find("#comment_title"); this.bodyField = this.element.find("#comment_body"); } @@ -35,8 +35,8 @@ $(document).ready(function () { title = phpComment.titleField.val(), body = phpComment.bodyField.val(); - if(phpComment.commentForm.parents(".media").length > 0){ - parentId = phpComment.commentForm.closest(".media").attr("data-Id"); + if(phpComment.commentForm.parents(".comment").length > 0){ + parentId = phpComment.commentForm.closest(".comment").attr("data-Id"); } $.ajax({ @@ -63,8 +63,7 @@ $(document).ready(function () { $(document).on("click", ".comment-add-new", function (e) { e.preventDefault(); - var media = $(this).closest(".comments"); - media.find(">.comment-body>.comment-text").after(phpComment.commentForm); + $(this).find(".comments").before(phpComment.commentForm); }); $(document).on("click", ".comment-add-reply", function (e) { e.preventDefault(); diff --git a/comments.php b/comments.php index 832e6a9..a6fc7ec 100644 --- a/comments.php +++ b/comments.php @@ -56,6 +56,13 @@ class CommentsPlugin extends Plugin public function onTwigSiteVariables() { $this->grav['twig']->enable_comments_plugin = $this->enable; $this->grav['twig']->comments = $this->fetchComments(); + if ($this->config->get('plugins.comments.built_in_css')) { + $this->grav['assets'] + ->addCss('plugin://comments/assets/comments.css'); + } + $this->grav['assets'] + ->add('jquery', 101) + ->addJs('plugin://comments/assets/comments.js'); } /** @@ -166,15 +173,52 @@ class CommentsPlugin extends Plugin // $this->total_stars = $this->config->get('plugins.star-ratings.total_stars'); // $this->only_full_stars = $this->config->get('plugins.star-ratings.only_full_stars'); - // Process vote if required + // Process comment if required if ($this->callback === $this->grav['uri']->path()) { - // try to add the vote - $result = $this->addVote(); + // try to add the comment + $result = $this->addComment(); echo json_encode(['status' => $result[0], 'message' => $result[1], 'data' => ['score' => $result[2][0], 'count' => $result[2][1]]]); exit(); } } + public function addComment() + { + $nonce = $this->grav['uri']->param('nonce'); + if (!Utils::verifyNonce($nonce, 'comments')) { + return [false, 'Invalid security nonce', [0, 0]]; + } + $language = $this->grav['language']; + // get and filter the data + $parent_id = filter_input(INPUT_POST, 'parent_id', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); + $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING); + $text = filter_input(INPUT_POST, 'text', FILTER_SANITIZE_STRING); + $title = filter_input(INPUT_POST, 'title', FILTER_SANITIZE_STRING); + $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING); + //$data = $this->getStars($id); + $data = array( + ['parent_id'] => $parent_id, + ['email'] => $email, + ['text'] => $text, + ['title'] => $title, + ['name'] => $name, + ); + // ensure both values are sent + if (is_null($title) || is_null($text)) { + return [false, 'missing either text or title', [0, 0]]; + //return [false, $language->translate('PLUGIN_COMMENTS.FAIL'), $data]; + } + // sanity checks for parents + if ($parent_id < 0) { + $parent_id = 0; + } elseif ($parent_id > 999 ) { //TODO: Change to 'exists in list of comment ids + $parent_id = 0; + } + //$this->saveVoteData($id, $rating); + //$data = $this->getStars($id); + return [true, $language->translate('PLUGIN_COMMENTS.SUCCESS'), $data]; + } + /** * Handle form processing instructions. diff --git a/templates/partials/comments.form.html.twig b/templates/partials/comments.form.html.twig index dab77dc..78a2a15 100644 --- a/templates/partials/comments.form.html.twig +++ b/templates/partials/comments.form.html.twig @@ -1,5 +1,5 @@

{{'PLUGIN_COMMENTS.ADD_COMMENT'|t}}

-
diff --git a/templates/partials/comments.html.twig b/templates/partials/comments.html.twig index a481604..5f11eec 100644 --- a/templates/partials/comments.html.twig +++ b/templates/partials/comments.html.twig @@ -6,6 +6,7 @@ {% if grav.twig.comments|length %}

{{'PLUGIN_COMMENTS.COMMENTS'|t}}

+ {{'PLUGIN_COMMENTS.ADD_NEW'|t}}
{% for comment in grav.twig.comments|array_reverse %}
@@ -17,7 +18,7 @@

{{comment.title}}

- +
{{'PLUGIN_COMMENTS.WRITTEN_ON'|t}} {{comment.date|e}} {{'PLUGIN_COMMENTS.BY'|t}} {{comment.author}}