From 140736a1e49d47376ebc893aa2da250ba3d836a3 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 16 Nov 2008 07:14:12 +0000 Subject: Several large changes: 1) Changed the way that we get forms. Now, if you want to get a form for a REST resource you prefix /form to the resource id. So: /form/photo/1 : returns a form for editing photo id 1 /form/comments/1 : returns a form for adding a comment to photo id 1 /form/comment/1 : returns a form for editing comment id 1 2) Changed the comment module to have two controllers: comment: deals with a single comment resource comments: deal with collections of comments attached to an item Related stuff: - Moved the comments js into the theme - Reworked Comment_Helper for clarity - Moved form generation code down into Comment_Helper - Cleaned up routes (eliminating new comment ones added in recent rev) - Added form() function to all REST controllers - Changed comment module to use a block instead of an arbitrary helper call from the theme - Comment controller only returns HTML currently, but returns a 201 Created status code when a new comment is added, which the Ajax code can catch and act upon. - Got rid of a lot of extra views in comment module --- core/controllers/album.php | 10 ++++++++++ core/controllers/item.php | 7 +++++++ core/controllers/photo.php | 10 ++++++++++ core/controllers/rest.php | 24 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+) (limited to 'core/controllers') diff --git a/core/controllers/album.php b/core/controllers/album.php index 63290853..87e27ac5 100644 --- a/core/controllers/album.php +++ b/core/controllers/album.php @@ -19,6 +19,16 @@ */ class Album_Controller extends Item_Controller { + /** + * @see Rest_Controller::_form($resource) + */ + public function _form($comment) { + throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + } + + /** + * @see Rest_Controller::_get($resource) + */ public function _get($item) { // @todo: these need to be pulled from the database $theme_name = "default"; diff --git a/core/controllers/item.php b/core/controllers/item.php index e9ff03e6..027dcada 100644 --- a/core/controllers/item.php +++ b/core/controllers/item.php @@ -20,6 +20,13 @@ class Item_Controller extends REST_Controller { protected $resource_type = "item"; + /** + * @see Rest_Controller::_form($resource) + */ + public function _form($comment) { + throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + } + public function _get($item) { // Redirect to the more specific resource type, since it will render // differently. We could also just delegate here, but it feels more appropriate diff --git a/core/controllers/photo.php b/core/controllers/photo.php index 78287afa..4c3154bd 100644 --- a/core/controllers/photo.php +++ b/core/controllers/photo.php @@ -19,6 +19,16 @@ */ class Photo_Controller extends Item_Controller { + /** + * @see Rest_Controller::_form($resource) + */ + public function _form($comment) { + throw new Exception("@todo Comment_Controller::_get NOT IMPLEMENTED"); + } + + /** + * @see Rest_Controller::_get($resource) + */ public function _get($item) { $template = new View("page.html"); diff --git a/core/controllers/rest.php b/core/controllers/rest.php index 09c85653..4a6c4eb4 100644 --- a/core/controllers/rest.php +++ b/core/controllers/rest.php @@ -39,6 +39,10 @@ * public function _delete(ORM $comment) { * // Handle DELETE request * } + * + * public function form(ORM $comment) { + * // Show a form for creating a new comment + * } * } * * A request to http://example.com/gallery3/comment/3 will result in a call to @@ -85,6 +89,20 @@ abstract class REST_Controller extends Controller { } } + public function form($id) { + if ($this->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) { + return Kohana::show_404(); + } + + return $this->_form($resource); + } + /** * Perform a GET request on this resource * @param ORM $resource the instance of this resource type @@ -108,4 +126,10 @@ abstract class REST_Controller extends Controller { * @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); } -- cgit v1.2.3