diff --git a/modules/comment/controllers/comments.php b/modules/comment/controllers/comments.php index 7dce9d01..8c3f8428 100644 --- a/modules/comment/controllers/comments.php +++ b/modules/comment/controllers/comments.php @@ -28,7 +28,26 @@ class Comments_Controller extends REST_Controller { $item = ORM::factory("item", $this->input->get('item_id')); access::required("view", $item); - print comment::get_comments($item->id); + $comments = ORM::factory("comment") + ->where("item_id", $item->id) + ->orderby("created", "desc") + ->find_all(); + + switch (rest::output_format()) { + case "json": + rest::http_content_type(rest::JSON); + foreach ($comments as $comment) { + $data[] = $comment->as_array(); + } + print json_encode($data); + break; + + case "html": + $view = new View("comments.html"); + $view->comments = $comments; + print $view; + break; + } } /** @@ -36,22 +55,28 @@ class Comments_Controller extends REST_Controller { * @see Rest_Controller::_create($resource) */ public function _create($comment) { - $form = comment::get_add_form($this->input->post('item_id')); + rest::http_content_type(rest::JSON); + + $form = comment::get_add_form($this->input->post("item_id")); if ($form->validate()) { - $comment->author = $this->input->post('author'); - $comment->email = $this->input->post('email'); - $comment->text = $this->input->post('text'); + $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->item_id = $this->input->post("item_id"); $comment->save(); module::event("comment_created", $comment); - rest::http_status(rest::CREATED); - rest::http_location(url::site("comments/{$comment->id}")); + print json_encode( + array("result" => "success", + "resource" => url::site("comments/{$comment->id}"), + "form" => comment::get_add_form($this->input->post("item_id"))->__toString())); + } else { + print json_encode( + array("result" => "error", + "form" => $form->__toString())); } - // @todo Return appropriate HTTP status code indicating error. - print $form; } /** @@ -60,31 +85,12 @@ class Comments_Controller extends REST_Controller { * @see Rest_Controller::_show($resource) */ public function _show($comment) { - $output_format = rest::output_format(); - switch ($output_format) { - case "xml": - rest::http_content_type(rest::XML); - print xml::to_xml($comment->as_array(), array("comment")); - break; - - case "json": - rest::http_content_type(rest::JSON); - print json_encode($comment->as_array()); - break; - - case "atom": - rest::http_content_type(rest::XML); - print comment::get_atom_entry($comment); - break; - - case "html": - $view = new View("comment.$output_format"); + if (rest::output_format() == "json") { + print json_encode(array("result" => "success", "resource" => $comment)); + } else { + $view = new View("comment.html"); $view->comment = $comment; print $view; - break; - - default: - kohana::show_404(); } } @@ -93,19 +99,25 @@ class Comments_Controller extends REST_Controller { * @see Rest_Controller::_update($resource) */ public function _update($comment) { + rest::http_content_type(rest::JSON); + $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->author = $this->input->post("author"); + $comment->email = $this->input->post("email"); + $comment->text = $this->input->post("text"); $comment->save(); module::event("comment_updated", $comment); - return; + print json_encode( + array("result" => "success", + "resource" => url::site("comments/{$comment->id}"))); + } else { + print json_encode( + array("result" => "error", + "html" => $form)); } - // @todo Return appropriate HTTP status code indicating error. - print $form; } /** @@ -113,8 +125,11 @@ class Comments_Controller extends REST_Controller { * @see Rest_Controller::_delete($resource) */ public function _delete($comment) { + rest::http_content_type(rest::JSON); + $comment->delete(); - rest::http_status(rest::OK); + print json_encode( + array("result" => "success")); } /** @@ -130,6 +145,6 @@ class Comments_Controller extends REST_Controller { * @see Rest_Controller::form_edit($resource) */ public function _form_edit($comment) { - print $form = comment::get_edit_form($comment); + print comment::get_edit_form($comment); } } diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index 7695cadb..51246444 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -75,157 +75,5 @@ class comment_Core { $form->add_rules_from($comment); return $form; } - - /** - * @todo Refactor this into a more generic location - */ - private static function _add_validation_rules($model_name, $form) { - $rules = ORM::factory($model_name)->validation_rules; - foreach ($form->inputs as $name => $input) { - if (isset($input->inputs)) { - comment::_add_validation_rules($model_name, $input); - } - if (isset($rules[$name])) { - $input->rules($rules[$name]); - } - } - } - - static function block($theme, $show_add_form=true) { - $block = new Block; - $block->id = "gComments"; - $block->title = _("Comments"); - $block->content = comment::get_comments($theme->item(), "html"); - - if ($show_add_form) { - $block->content .= comment::get_add_form($theme->item())->render("form.html"); - } - return $block; - } - - // @todo Set proper Content-Type in a central place (REST_Controller::dispatch?). - static function get_comments($item_id) { - $comments = ORM::factory('comment')->where('item_id', $item_id) - ->orderby('created', 'asc') - ->find_all(); - - if (!$comments->count()) { - rest::http_status(rest::BAD_REQUEST); - return; - } - - switch (rest::output_format()) { - case "atom": - rest::http_content_type(rest::XML); - print comment::get_atom_feed($comments); - break; - - case "xml": - rest::http_content_type(rest::XML); - return xml::to_xml($comments, array("comments", "comment")); - break; - - case "json": - rest::http_content_type(rest::JSON); - foreach ($comments as $comment) { - $data[] = $comment->as_array(); - } - return json_encode($data); - - default: - foreach ($comments as $comment) { - $v = new View("comment.html"); - $v->comment = $comment; - $html[] = $v; - } - if (!empty($html)) { - return "
= $comment->author ?> - = comment::format_elapsed_time($comment->created) ?>, - = strftime('%c', $comment->created) ?> + = date("Y-M-d H:i:s", $comment->created) ?>
+ = $comment->author ?> + = date("Y-M-d H:i:s", $comment->created) ?> +
+