diff options
author | Bharat Mediratta <bharat@menalto.com> | 2008-11-27 05:00:50 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2008-11-27 05:00:50 +0000 |
commit | 5447b8321080afd09387d837a4acfa9c90ddfa16 (patch) | |
tree | 1fbaf7cc855066aa8d337282a648d809c921f5dc | |
parent | 8e33f5d7b90335236e14b283ccb6d6ee5db4762d (diff) |
Clean up REST pattern in tags:
1) Generate the form in Tags_Controller::_form_add()
2) Process the form submit in Tags_Controller::_create()
3) Create the tag properly
This required me to limit our scope to adding one tag at a time, which
I think is fine if we're doing Ajax style tag addition.
-rw-r--r-- | modules/tag/controllers/tags.php | 32 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 18 | ||||
-rw-r--r-- | modules/tag/helpers/tag_block.php | 9 | ||||
-rw-r--r-- | modules/tag/views/tag_block.html.php | 10 |
4 files changed, 39 insertions, 30 deletions
diff --git a/modules/tag/controllers/tags.php b/modules/tag/controllers/tags.php index 95d7145d..9a5422cf 100644 --- a/modules/tag/controllers/tags.php +++ b/modules/tag/controllers/tags.php @@ -46,16 +46,13 @@ class Tags_Controller extends REST_Controller { throw new Exception("@todo Tag_Controller::_index NOT IMPLEMENTED"); } - public function _form_add($parameters) { - $item_id = is_array($parameters) ? $parameters[0] : $parameters; - $form = tag::get_add_form($item_id); - if ($form->validate()) { - $tags = explode(",", $form->inputs["tags"]->value); - -// $item = ORM::factory("item", $item_id); - $form->inputs["tags"]->value("add new tags..."); - } - print $form->render(); + public function _form_add($item_id) { + $form = new Forge(url::site("tags"), "", "post", array("id" => "gAddTag")); + $form->input("tag_name")->value(_("add new tags..."))->id("gNewTags"); + $form->hidden("item_id")->value($item_id); + $form->submit(_("Add")); + $form->add_rules_from(ORM::factory("tag")); + return $form; } public function _form_edit($tag) { @@ -63,7 +60,20 @@ class Tags_Controller extends REST_Controller { } public function _create($tag) { - throw new Exception("@todo Tag_Controller::_create NOT IMPLEMENTED"); + // @todo: check permissions + $form = self::form_add($this->input->post('item_id')); + if ($form->validate()) { + $item = ORM::factory("item", $this->input->post("item_id")); + if ($item->loaded) { + tag::add($item, $this->input->post("tag_name")); + } + + rest::http_status(rest::CREATED); + rest::http_location(url::site("tags/{$tag->id}")); + } + + // @todo Return appropriate HTTP status code indicating error. + print $form; } public function _delete($tag) { diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 09109cd5..a1939df7 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -23,8 +23,9 @@ class tag_Core { * * @todo Write test. * - * @param ORM $item an item - * @param string $tag_name a tag name + * @param Item_Model $item an item + * @param string $tag_name a tag name + * @return Tag_Model * @throws Exception("@todo {$tag_name} WAS_NOT_ADDED_TO {$item->id}") */ public static function add($item, $tag_name) { @@ -46,6 +47,7 @@ class tag_Core { $tag->count++; $tag->save(); } + return $tag; } /** @@ -59,16 +61,4 @@ class tag_Core { ->limit($count) ->find_all(); } - - public static function get_add_form($item_id) { - $form = new Forge(url::site("form/add/tags/$item_id"), "", "post", array("id" => "gAddTag")); - $form->input("tags") - ->label(_("(Enter tags separated by commas)")) - ->id("gNewTags") - ->class("text") - ->value("add new tags...") - ->rules("required"); - $form->submit(_("Add")); - return $form; - } } diff --git a/modules/tag/helpers/tag_block.php b/modules/tag/helpers/tag_block.php index 5cf6aa75..c735ee04 100644 --- a/modules/tag/helpers/tag_block.php +++ b/modules/tag/helpers/tag_block.php @@ -17,7 +17,6 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ - class tag_block_Core { public static function head($theme) { $url = url::file("modules/tag/js/tag.js"); @@ -39,7 +38,15 @@ class tag_block_Core { if ($block->content->page_type != "tag") { $block->content->item = $theme->item(); } + } else { + $block->content->tags = array(); + } + + if ($theme->page_type() != "tag") { + $controller = new Tags_Controller(); + $block->content->form = $controller->form_add($theme->item()); } + return $block; } diff --git a/modules/tag/views/tag_block.html.php b/modules/tag/views/tag_block.html.php index bd507571..6ce67e9d 100644 --- a/modules/tag/views/tag_block.html.php +++ b/modules/tag/views/tag_block.html.php @@ -1,4 +1,5 @@ <? defined("SYSPATH") or die("No direct script access."); ?> +<? if ($tags): ?> <ul> <? foreach ($tags as $tag): ?> <li class="size<?=(int)(($tag->count / $max_count) * 7) ?>"> @@ -7,10 +8,11 @@ </li> <? endforeach ?> </ul> +<? endif ?> -<? if ($page_type != "tag"): ?> - <div id="gTagFormContainer"> - <?= tag::get_add_form($item->id) ?> - </div> +<? if (isset($form)): ?> +<div id="gTagFormContainer"> + <?= $form ?> +</div> <? endif; ?> |