resource_type == null) { throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); } // @todo this needs security checks $resource = ORM::factory($this->resource_type, $id); 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); if (Session::instance()->get("use_profiler", false)) { $profiler = new Profiler(); $profiler->render(); } return; } switch ($this->request_method()) { case "put": return $this->_update($resource); case "delete": return $this->_delete($resource); case "post": return $this->_create($resource); } } // @todo Get rid of $form_type, move to add_form() and edit_form(). public function form($data, $form_type) { if ($this->resource_type == null) { throw new Exception("@todo ERROR_MISSING_RESOURCE_TYPE"); } // @todo this needs security checks if ($form_type == "edit") { /* We're editing an existing item, load it from the database. */ $resource = ORM::factory($this->resource_type, $data); if (!$resource->loaded) { return Kohana::show_404(); } return $this->_form($resource, $form_type); } else { /* We're adding a new item, pass along any additional parameters. */ return $this->_form($data, $form_type); } } public function index($query_string=null) { // @todo Convert query string to an array and pass it along to _index() if (request::method() == "post") { return $this->dispatch(null); } return $this->_index(array()); } /** * Return HTTP request method taking into consideration PUT and DELETE tunneling through POST. * @todo Move this to a MY_request helper? * @return string HTTP request method */ protected function request_method() { if (request::method() == "get") { return "get"; } else { switch ($this->input->post("_method", $this->input->get("_method"))) { case "put": return "put"; case "delete": return "delete"; default: return "post"; } } } /** * 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); /** * Perform a POST request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _create($resource); /** * Perform a GET request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _show($resource, $output_format); /** * Perform a PUT request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _update($resource); /** * Perform a DELETE request on this resource * @param ORM $resource the instance of this resource type */ abstract public function _delete($resource); /** * Present a form for adding a new resource * @param ORM $resource the resource container for instances of this resource type */ abstract public function _form($resource, $form_type); }