diff options
author | Bharat Mediratta <bharat@menalto.com> | 2008-11-17 00:30:18 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2008-11-17 00:30:18 +0000 |
commit | c91e90406bbc81f8311a1d2707dd141d150f99a4 (patch) | |
tree | ddda2c6bacd4b0e0e30f4c8a62d13b2842b864cc /modules | |
parent | 0975e702a99dad701f66c606de6597048338f247 (diff) |
Add output formats to our REST controllers. Add support for JSON and
XML to the comment controllers as a proof of concept. It's not fully
baked; we should examine ways to create helpers to make this process
easier.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/comment/controllers/comment.php | 23 | ||||
-rw-r--r-- | modules/comment/controllers/comments.php | 6 | ||||
-rw-r--r-- | modules/comment/helpers/comment.php | 36 | ||||
-rw-r--r-- | modules/user/controllers/user.php | 4 |
4 files changed, 47 insertions, 22 deletions
diff --git a/modules/comment/controllers/comment.php b/modules/comment/controllers/comment.php index 9a9ce84e..546f3d67 100644 --- a/modules/comment/controllers/comment.php +++ b/modules/comment/controllers/comment.php @@ -31,14 +31,26 @@ class Comment_Controller extends REST_Controller { /** * Get an existing comment. - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $output_format) */ - public function _get($comment) { - $v = new View("comment.html"); - $v->comment = $comment; - print $v; + public function _get($comment, $output_format) { + switch ($output_format) { + case "xml": + print xml::to_xml($comment->as_array(), array("comment")); + break; + + case "json": + print json_encode($comment->as_array()); + break; + + default: + $v = new View("comment.$output_format"); + $v->comment = $comment; + print $v; + } } + /** * Update existing comment. * @see Rest_Controller::_put($resource) @@ -46,7 +58,6 @@ class Comment_Controller extends REST_Controller { public function _put($comment) { $form = comment::get_edit_form($comment); if ($form->validate()) { - $comment = ORM::factory('comment'); $comment->author = $this->input->post('author'); $comment->email = $this->input->post('email'); $comment->text = $this->input->post('text'); diff --git a/modules/comment/controllers/comments.php b/modules/comment/controllers/comments.php index 60ab9b5d..ab3be76f 100644 --- a/modules/comment/controllers/comments.php +++ b/modules/comment/controllers/comments.php @@ -31,10 +31,10 @@ class Comments_Controller extends REST_Controller { /** * Show the comment collection - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $format) */ - public function _get($item) { - print comment::get_comments($item); + public function _get($item, $output_format) { + print comment::get_comments($item, $output_format); } /** diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index b5d57829..0bfd08d0 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -96,7 +96,7 @@ class Comment_Core { $block = new Block; $block->id = "gComment"; $block->title = _("Comments"); - $block->content = comment::get_comments($theme->item()); + $block->content = comment::get_comments($theme->item(), "html"); if ($show_add_form) { $block->content .= comment::get_add_form($theme->item())->render("form.html"); @@ -104,17 +104,31 @@ class Comment_Core { return $block; } - static function get_comments($item) { - $comments = array("<ul>"); - foreach (ORM::factory('comment')->where('item_id', $item->id) - ->orderby('datetime', 'asc') - ->find_all() as $comment) { - $v = new View("comment.html"); - $v->comment = $comment; - $comments[] = $v; + static function get_comments($item, $output_format) { + $comments = ORM::factory('comment')->where('item_id', $item->id) + ->orderby('datetime', 'asc') + ->find_all(); + + switch ($output_format) { + case "xml": + return xml::to_xml($comments, array("comments", "comment")); + break; + + case "json": + foreach ($comments as $comment) { + $data[] = $comment->as_array(); + } + return json_encode($data); + + default: + $html = array("<ul>"); + foreach ($comments as $comment) { + $v = new View("comment.html"); + $v->comment = $comment; + $html[] = $v; + } + return "<ul>\n" . implode("\n", $html) . "</ul>\n"; } - $comments[] = "</ul>"; - return implode("\n", $comments); } /** diff --git a/modules/user/controllers/user.php b/modules/user/controllers/user.php index 35498a04..bc7fb6ff 100644 --- a/modules/user/controllers/user.php +++ b/modules/user/controllers/user.php @@ -30,9 +30,9 @@ class User_Controller extends REST_Controller { } /** - * @see Rest_Controller::_get($resource) + * @see Rest_Controller::_get($resource, $format) */ - public function _get($user) { + public function _get($user, $format) { throw new Exception("@todo User_Controller::_get NOT IMPLEMENTED"); } |