diff options
author | Jozef Selesi <jozefs@users.sourceforge.net> | 2008-11-18 15:48:08 +0000 |
---|---|---|
committer | Jozef Selesi <jozefs@users.sourceforge.net> | 2008-11-18 15:48:08 +0000 |
commit | b63ea2cdbf995d4648bc3c4a5f4eb51ed8da3a8d (patch) | |
tree | 63515fed283da26948282acb635dd3186f203204 | |
parent | d4fc15f76c383be2ac77679ad2fcaa11bba93c05 (diff) |
- All comments of an item can now be seen /comments?item_id=
- Return proper Content-Type header for GET /comments requests
- Got rid of the query processing for index() in REST_Controller()
- Small misc fixes
-rw-r--r-- | core/config/routes.php | 1 | ||||
-rw-r--r-- | core/controllers/item.php | 4 | ||||
-rw-r--r-- | core/controllers/rest.php | 28 | ||||
-rw-r--r-- | modules/comment/controllers/comments.php | 18 | ||||
-rw-r--r-- | modules/comment/helpers/comment.php | 12 | ||||
-rw-r--r-- | modules/user/controllers/users.php | 8 |
6 files changed, 45 insertions, 26 deletions
diff --git a/core/config/routes.php b/core/config/routes.php index 567e999e..ea177eda 100644 --- a/core/config/routes.php +++ b/core/config/routes.php @@ -24,7 +24,6 @@ $config['^rest'] = null; $config['^rest/.*'] = null; $config['^(\w+)/(\d+)$'] = '$1/dispatch/$2'; -// @todo The following will need to support query strings. $config['^(\w+)$'] = '$1/index'; $config['^form/(\w+)/(\w+)/(.*)$'] = '$2/form/$3/$1'; diff --git a/core/controllers/item.php b/core/controllers/item.php index 7ee3674b..1113d00f 100644 --- a/core/controllers/item.php +++ b/core/controllers/item.php @@ -21,9 +21,9 @@ class Item_Controller extends REST_Controller { protected $resource_type = "item"; /** - * @see Rest_Controller::_index($query) + * @see Rest_Controller::_index() */ - public function _index($query) { + public function _index() { throw new Exception("@todo Item_Controller::_index NOT IMPLEMENTED"); } diff --git a/core/controllers/rest.php b/core/controllers/rest.php index b2097aaf..30a92b24 100644 --- a/core/controllers/rest.php +++ b/core/controllers/rest.php @@ -24,7 +24,7 @@ * class Comment_Controller extends REST_Controller { * protected $resource_type = "comment"; // this tells REST which model to use * - * public function _index($query) { + * public function _index() { * // Handle GET request to controller root * } * @@ -67,13 +67,9 @@ abstract class REST_Controller extends Controller { if (!$resource->loaded && !$this->request_method() == "post") { return Kohana::show_404(); } - /** - * We're expecting to run in an environment that only supports GET/POST, so expect to tunnel - * PUT/DELETE through POST. - */ - $output_format = $this->input->get("_format", $this->input->post("_format", "html")); + if ($this->request_method() == "get") { - $this->_show($resource, $output_format); + $this->_show($resource, $this->get_output_format()); if (Session::instance()->get("use_profiler", false)) { $profiler = new Profiler(); @@ -115,16 +111,18 @@ abstract class REST_Controller extends Controller { } } - public function index($query_string=null) { - // @todo Convert query string to an array and pass it along to _index() + public function index() { if (request::method() == "post") { return $this->dispatch(null); } - return $this->_index(array()); + return $this->_index(); } /** - * Return HTTP request method taking into consideration PUT and DELETE tunneling through POST. + * We're expecting to run in an environment that only supports GET/POST, so expect to tunnel + * PUT and DELETE through POST. + * + * Returns the HTTP request method taking into consideration PUT/DELETE tunneling. * @todo Move this to a MY_request helper? * @return string HTTP request method */ @@ -140,12 +138,16 @@ abstract class REST_Controller extends Controller { } } + // @todo Figure out a good consistent method of passing the output format to controller methods. + protected function get_output_format() { + return $this->input->get("_format", $this->input->post("_format", "html")); + } + /** * Perform a GET request on the controller root * (e.g. http://www.example.com/gallery3/comments) - * @param array $query name-value pairs from the query string, if any */ - abstract public function _index($query); + abstract public function _index(); /** * Perform a POST request on this resource diff --git a/modules/comment/controllers/comments.php b/modules/comment/controllers/comments.php index b51e7e4e..34557a83 100644 --- a/modules/comment/controllers/comments.php +++ b/modules/comment/controllers/comments.php @@ -22,10 +22,17 @@ class Comments_Controller extends REST_Controller { /** * Display comments based on criteria. - * @see Rest_Controller::_delete($resource) + * @see Rest_Controller::_index() */ - public function _index($query) { - throw new Exception("@todo Comment_Controller::_index NOT IMPLEMENTED"); + public function _index() { + $item_id = $this->input->get('item_id'); + + if (empty($item_id)) { + /* We currently do not support getting all comments from the entire gallery. */ + header("HTTP/1.1 400 Bad Request"); + return; + } + print comment::get_comments($item_id, $this->get_output_format()); } /** @@ -51,15 +58,18 @@ class Comments_Controller extends REST_Controller { /** * Display an existing comment. - * @see Rest_Controller::_show($resource, $format) + * @todo Set proper Content-Type in a central place (REST_Controller::dispatch?). + * @see Rest_Controller::_show($resource, $output_format) */ public function _show($comment, $output_format) { switch ($output_format) { case "xml": + header("Content-Type: application/xml"); print xml::to_xml($comment->as_array(), array("comment")); break; case "json": + header("Content-Type: application/json"); print json_encode($comment->as_array()); break; diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index e474bf65..e9628a01 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -105,17 +105,25 @@ class Comment_Core { return $block; } - static function get_comments($item, $output_format) { - $comments = ORM::factory('comment')->where('item_id', $item->id) + // @todo Set proper Content-Type in a central place (REST_Controller::dispatch?). + static function get_comments($item_id, $output_format) { + $comments = ORM::factory('comment')->where('item_id', $item_id) ->orderby('datetime', 'asc') ->find_all(); + if (!$comments->count()) { + header("HTTP/1.1 400 Bad Request"); + return; + } + switch ($output_format) { case "xml": + header("Content-Type: application/xml"); return xml::to_xml($comments, array("comments", "comment")); break; case "json": + header("Content-Type: application/json"); foreach ($comments as $comment) { $data[] = $comment->as_array(); } diff --git a/modules/user/controllers/users.php b/modules/user/controllers/users.php index ea3a4c8b..d381d399 100644 --- a/modules/user/controllers/users.php +++ b/modules/user/controllers/users.php @@ -22,9 +22,9 @@ class Users_Controller extends REST_Controller { /** * Display comments based on criteria. - * @see Rest_Controller::_delete($resource) + * @see Rest_Controller::_index() */ - public function _index($query) { + public function _index() { throw new Exception("@todo Comment_Controller::_index NOT IMPLEMENTED"); } @@ -36,9 +36,9 @@ class Users_Controller extends REST_Controller { } /** - * @see Rest_Controller::_show($resource, $format) + * @see Rest_Controller::_show($resource, $output_format) */ - public function _show($user, $format) { + public function _show($user, $output_format) { throw new Exception("@todo User_Controller::_show NOT IMPLEMENTED"); } |