summaryrefslogtreecommitdiff
path: root/modules/comment/controllers/comments.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-25 00:47:40 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-25 00:47:40 +0000
commit0bb82b76216d033742afab91ce9e9a20dd607ad8 (patch)
tree7e662d7a71040d5fa770798449bf27fc93d0994c /modules/comment/controllers/comments.php
parent7a82beb752d51e2d0a1c16446a5e27738192aaf3 (diff)
Gut the comment module and simplify it. Stop trying to support Atom
and XML for now, we have no driver for those technologies so anything we implement is not going to be sufficiently tested and therefore it'll be broken. Change all comment functions to return JSON and update the JS to deal purely with JSON. This is our new protocol for talking to the browser and it should be flexible and portable. Create comments.html.php. This duplicates comment.html.php, but will be more efficient for rendering comments since we won't be creating a new View for every comment we render.
Diffstat (limited to 'modules/comment/controllers/comments.php')
-rw-r--r--modules/comment/controllers/comments.php97
1 files changed, 56 insertions, 41 deletions
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);
}
}