diff options
-rw-r--r-- | modules/comment/controllers/comments.php | 26 | ||||
-rw-r--r-- | modules/comment/helpers/comment.php | 41 | ||||
-rw-r--r-- | modules/comment/helpers/comment_block.php | 1 | ||||
-rw-r--r-- | modules/comment/helpers/comment_installer.php | 6 | ||||
-rw-r--r-- | modules/comment/models/comment.php | 1 | ||||
-rw-r--r-- | modules/comment/tests/Comment_Helper_Test.php | 42 |
6 files changed, 100 insertions, 17 deletions
diff --git a/modules/comment/controllers/comments.php b/modules/comment/controllers/comments.php index e6ade267..293c456d 100644 --- a/modules/comment/controllers/comments.php +++ b/modules/comment/controllers/comments.php @@ -61,18 +61,15 @@ class Comments_Controller extends REST_Controller { $form = comment::get_add_form($item); if ($form->validate()) { - $comment->author = $this->input->post("author"); - $comment->email = $this->input->post("email"); - $comment->text = $this->input->post("text"); - $comment->created = time(); - $comment->item_id = $this->input->post("item_id"); - $comment->save(); - - module::event("comment_created", $comment); + $comment = comment::create($this->input->post("author"), + $this->input->post("email"), + $this->input->post("text"), + $this->input->post("item_id"), + $this->input->post("url")); print json_encode( array("result" => "success", - "resource" => url::site("comments/{$comment->id}"), + "resource" => $comment->visible ? url::site("comments/{$comment->id}") : NULL, "form" => comment::get_add_form($item)->__toString())); } else { print json_encode( @@ -107,12 +104,11 @@ class Comments_Controller extends REST_Controller { $form = comment::get_edit_form($comment); if ($form->validate()) { - $comment->author = $this->input->post("author"); - $comment->email = $this->input->post("email"); - $comment->text = $this->input->post("text"); - $comment->save(); - - module::event("comment_updated", $comment); + $comment = comment::update($comment, + $this->input->post("author"), + $this->input->post("email"), + $this->input->post("text"), + $this->input->post("url")); print json_encode( array("result" => "success", diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index 3006d73d..92a9e54c 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -36,27 +36,65 @@ class comment_Core { * @param string $email author's email * @param string $text comment body * @param integer $item_id id of parent item + * @param string $url author's url * @return Comment_Model */ - static function create($author, $email, $text, $item_id) { + static function create($author, $email, $text, $item_id, $url) { $comment = ORM::factory("comment"); $comment->author = $author; $comment->email = $email; $comment->text = $text; $comment->item_id = $item_id; + $comment->url = $url; $comment->created = time(); + // @todo Figure out how to mock up the test of the spam_filter + if (module::is_installed("spam_filter") && !TEST_MODE) { + spam_filter::verify_comment($comment); + } else { + $comment->visible = true; + } + $comment->save(); module::event("comment_created", $comment); return $comment; } + /** + * Update an existing comment. + * @param Comment_Model $comment + * @param string $author author's name + * @param string $email author's email + * @param string $text comment body + * @param string $url author's url + * @return Comment_Model + */ + static function update($comment, $author, $email, $text, $url) { + $comment->author = $author; + $comment->email = $email; + $comment->text = $text; + $comment->url = $url; + + // @todo Figure out how to mock up the test of the spam_filter + if (module::is_installed("spam_filter") && !TEST_MODE) { + spam_filter::verify_comment($comment); + } + + $comment->save(); + if ($comment->saved) { + module::event("comment_updated", $comment); + } + + return $comment; + } + static function get_add_form($item) { $form = new Forge("comments", "", "post"); $group = $form->group("add_comment")->label(_("Add comment")); $group->input("author") ->label(_("Author")) ->id("gAuthor"); $group->input("email") ->label(_("Email")) ->id("gEmail"); + $group->input("url") ->label(_("Website")) ->id("gUrl"); $group->textarea("text")->label(_("Text")) ->id("gText"); $group->hidden("item_id")->value($item->id); $group->submit(_("Add")); @@ -69,6 +107,7 @@ class comment_Core { $group = $form->group("edit_comment")->label(_("Edit comment")); $group->input("author") ->label(_("Author")) ->id("gAuthor") ->value($comment->author); $group->input("email") ->label(_("Email")) ->id("gEmail") ->value($comment->email); + $group->input("url") ->label(_("Website")) ->id("gUrl") ->value($comment->url); $group->textarea("text")->label(_("Text")) ->id("gText") ->value($comment->text); $group->submit(_("Edit")); $form->add_rules_from($comment); diff --git a/modules/comment/helpers/comment_block.php b/modules/comment/helpers/comment_block.php index 7d132646..9231dcd6 100644 --- a/modules/comment/helpers/comment_block.php +++ b/modules/comment/helpers/comment_block.php @@ -32,6 +32,7 @@ class comment_block_Core { $view = new View("comments.html"); $view->comments = ORM::factory("comment") ->where("item_id", $theme->item()->id) + ->where("visible", 1) ->orderby("created", "ASC") ->find_all(); diff --git a/modules/comment/helpers/comment_installer.php b/modules/comment/helpers/comment_installer.php index be8b2a14..f203d940 100644 --- a/modules/comment/helpers/comment_installer.php +++ b/modules/comment/helpers/comment_installer.php @@ -30,9 +30,15 @@ class comment_installer { `text` text, `created` int(9) NOT NULL, `item_id` int(9) NOT NULL, + `url` varchar(255) default NULL, + `visible` tinyint(1) default 1, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + if (module::is_installed("spam_filter")) { + spam_filter_installer::add_fields(); + } + module::set_version("comment", 1); } } diff --git a/modules/comment/models/comment.php b/modules/comment/models/comment.php index b1eb37b7..4c4a8729 100644 --- a/modules/comment/models/comment.php +++ b/modules/comment/models/comment.php @@ -21,5 +21,6 @@ class Comment_Model extends ORM { var $rules = array( "author" => "required", "email" => "required|valid_email", + "url" => "valid_url", "text" => "required"); } diff --git a/modules/comment/tests/Comment_Helper_Test.php b/modules/comment/tests/Comment_Helper_Test.php index d9b8d3ab..81ec12a8 100644 --- a/modules/comment/tests/Comment_Helper_Test.php +++ b/modules/comment/tests/Comment_Helper_Test.php @@ -20,12 +20,52 @@ class Comment_Helper_Test extends Unit_Test_Case { public function create_comment_test() { $rand = rand(); - $comment = comment::create($rand, $rand, $rand, $rand, $rand); + $comment = comment::create($rand, $rand, $rand, $rand, $rand, $rand); $this->assert_equal($rand, $comment->author); $this->assert_equal($rand, $comment->email); $this->assert_equal($rand, $comment->text); $this->assert_equal($rand, $comment->item_id); + $this->assert_equal($rand, $comment->url); $this->assert_true(!empty($comment->created)); } + + public function update_comment_test() { + $rand = rand(); + $comment = comment::create($rand, $rand, $rand, $rand, $rand, $rand); + + $this->assert_equal($rand, $comment->author); + $this->assert_equal($rand, $comment->email); + $this->assert_equal($rand, $comment->text); + $this->assert_equal($rand, $comment->item_id); + $this->assert_equal($rand, $comment->url); + $this->assert_true(!empty($comment->created)); + + $rand2 = rand(); + comment::update($comment, $rand2, $rand2, $rand2, $rand2, $rand2); + $this->assert_equal($rand2, $comment->author); + $this->assert_equal($rand2, $comment->email); + $this->assert_equal($rand2, $comment->text); + $this->assert_equal($rand, $comment->item_id); + $this->assert_equal($rand2, $comment->url); + } + + public function update_comment_no_change_test() { + $rand = rand(); + $comment = comment::create($rand, $rand, $rand, $rand, $rand, $rand); + + $this->assert_equal($rand, $comment->author); + $this->assert_equal($rand, $comment->email); + $this->assert_equal($rand, $comment->text); + $this->assert_equal($rand, $comment->item_id); + $this->assert_equal($rand, $comment->url); + $this->assert_true(!empty($comment->created)); + + comment::update($comment, $rand, $rand, $rand, $rand, $rand); + $this->assert_equal($rand, $comment->author); + $this->assert_equal($rand, $comment->email); + $this->assert_equal($rand, $comment->text); + $this->assert_equal($rand, $comment->item_id); + $this->assert_equal($rand, $comment->url); + } } |