diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-11-05 12:59:37 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-11-05 13:01:01 -0800 |
commit | 72a8ce696e37a74ed8bbea31364b39f9e0776bbc (patch) | |
tree | ed19098efa52d1869457308c2d485a4e9e21387a /modules | |
parent | 03408f3e39bbb0b407b2949721090dd6b07913ac (diff) |
Refactor out the in place editting and use the new gallery.in_place_edit widget to manage the tag renames. Part the fix for ticket #750.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/tag/controllers/admin_tags.php | 46 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 15 | ||||
-rw-r--r-- | modules/tag/helpers/tag_theme.php | 8 | ||||
-rw-r--r-- | modules/tag/js/tag.js | 71 | ||||
-rw-r--r-- | modules/tag/views/admin_tags.html.php | 9 |
5 files changed, 30 insertions, 119 deletions
diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php index 63f7957c..d79f697c 100644 --- a/modules/tag/controllers/admin_tags.php +++ b/modules/tag/controllers/admin_tags.php @@ -69,7 +69,9 @@ class Admin_Tags_Controller extends Admin_Controller { public function form_rename($id) { $tag = ORM::factory("tag", $id); if ($tag->loaded) { - print tag::get_rename_form($tag); + print InPlaceEdit::factory($tag->name) + ->add_action("admin/tags/rename/$id") + ->render(); } } @@ -81,25 +83,15 @@ class Admin_Tags_Controller extends Admin_Controller { kohana::show_404(); } - // Don't use a form as the form is dynamically created in the js - $post = new Validation($_POST); - $post->add_rules("name", "required", "length[1,64]"); - $valid = $post->validate(); - if ($valid) { - $new_name = $this->input->post("name"); - $new_tag = ORM::factory("tag")->where("name", $new_name)->find(); - if ($new_tag->loaded) { - $error_msg = t("There is already a tag with that name"); - $valid = false; - } - } else { - $error_msg = $post->errors(); - $error_msg = $error_msg[0]; - } + $inplaceedit = InPlaceEdit::factory($tag->name) + ->add_action("admin/tags/rename/$tag->id") + ->add_rules(array("required", "length[1,64]")) + ->add_messages(array("in_use" => t("There is already a tag with that name"))) + ->add_callback(array($this, "check_for_duplicate")); - if ($valid) { + if ($inplaceedit->validate()) { $old_name = $tag->name; - $tag->name = $new_name; + $tag->name = $inplaceedit->value(); $tag->save(); $message = t("Renamed tag %old_name to %new_name", @@ -107,16 +99,18 @@ class Admin_Tags_Controller extends Admin_Controller { message::success($message); log::success("tags", $message); - print json_encode( - array("result" => "success", - "location" => url::site("admin/tags"), - "tag_id" => $tag->id, - "new_tagname" => html::clean($tag->name))); + print json_encode(array("result" => "success")); } else { - print json_encode( - array("result" => "error", - "message" => (string) $error_msg)); + print json_encode(array("result" => "error", "form" => $inplaceedit->render())); } } + + public function check_for_duplicate(Validation $post_data, $field) { + $tag_exists = ORM::factory("tag")->where("name", $post_data[$field])->count_all(); + if ($tag_exists) { + $post_data->add_error($field, "in_use"); + } + } + } diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 01972a65..feaf40c5 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -73,12 +73,16 @@ class tag_Core { if ($tags) { $cloud = new View("tag_cloud.html"); $cloud->max_count = $tags[0]->count; - usort($tags, array("tag_theme", "sort_by_name")); + usort($tags, array("tag", "sort_by_name")); $cloud->tags = $tags; return $cloud; } } + static function sort_by_name($tag1, $tag2) { + return strcasecmp($tag1->name, $tag2->name); + } + /** * Return all the tags for a given item. * @return array @@ -109,15 +113,6 @@ class tag_Core { return $form; } - static function get_rename_form($tag) { - $form = new Forge("admin/tags/rename/$tag->id", "", "post", array("id" => "g-rename-tag-form", "class" => "g-short-form")); - $group = $form->group("rename_tag")->label(t("Rename Tag")); - $group->input("name")->label(t("Tag name"))->value($tag->name)->rules("required|length[1,64]"); - $group->inputs["name"]->error_messages("in_use", t("There is already a tag with that name")); - $group->submit("")->value(t("Save")); - return $form; - } - static function get_delete_form($tag) { $form = new Forge("admin/tags/delete/$tag->id", "", "post", array("id" => "g-delete-tag-form")); $group = $form->group("delete_tag") diff --git a/modules/tag/helpers/tag_theme.php b/modules/tag/helpers/tag_theme.php index ac0dd016..76c0ea6b 100644 --- a/modules/tag/helpers/tag_theme.php +++ b/modules/tag/helpers/tag_theme.php @@ -21,16 +21,12 @@ class tag_theme_Core { static function head($theme) { $theme->css("jquery.autocomplete.css"); $theme->script("jquery.autocomplete.js"); - $theme->css("tag.css"); $theme->script("tag.js"); + $theme->css("tag.css"); } static function admin_head($theme) { $theme->css("tag.css"); - $theme->script("tag.js"); - } - - static function sort_by_name($tag1, $tag2) { - return strcasecmp($tag1->name, $tag2->name); + $theme->script("gallery.in_place_edit.js"); } }
\ No newline at end of file diff --git a/modules/tag/js/tag.js b/modules/tag/js/tag.js index 8cb4e571..4760084d 100644 --- a/modules/tag/js/tag.js +++ b/modules/tag/js/tag.js @@ -1,7 +1,3 @@ -$("document").ready(function() { - ajaxify_tag_form(); -}); - function ajaxify_tag_form() { $("#g-tag form").ajaxForm({ dataType: "json", @@ -15,70 +11,3 @@ function ajaxify_tag_form() { } }); } - -function closeEditInPlaceForms() { - // closes currently open inplace edit forms - if ($("#g-rename-tag-form").length) { - $("#g-action-status").remove(); - var li = $("#g-rename-tag-form").parent(); - $("#g-rename-tag-form").parent().html($("#g-rename-tag-form").parent().data("revert")); - li.height(""); - $(".g-editable", li).bind("click", editInPlace); - $(".g-dialog-link", li).gallery_dialog(); - } -} - -function str_replace(search_term, replacement, string) { - var temp = string.split(search_term); - return temp.join(replacement); -} - -function editInPlace(element) { - closeEditInPlaceForms(); - - // create edit form - var tag_id = $(this).attr('rel'); - var tag_name = $(this).html(); - var tag_width = $(this).width(); - $(this).parent().data("revert", $(this).parent().html()); - var form = '<form id="g-rename-tag-form" method="post" class="g-short-form" '; - form += 'action="' + TAG_RENAME_URL.replace('__ID__', tag_id) + '">'; - form += '<input name="csrf" type="hidden" value="' + csrf_token + '" />'; - form += '<ul>'; - form += '<li><input id="name" name="name" type="text" class="textbox" value="' + - str_replace('"', """, tag_name) + '" /></li>'; - form += '<li><input type="submit" class="submit ui-state-default" value="' + save_i18n + '" /></li>'; - form += '<li><a href="#" class="g-cancel">' + cancel_i18n + '</a></li>'; - form += '</ul>'; - form += '</form>'; - - // add edit form - $(this).parent().html(form); - $("#g-rename-tag-form #name") - .width(tag_width) - .focus(); - $(".g-short-form").gallery_short_form(); - $("#g-rename-tag-form .g-cancel").bind("click", closeEditInPlaceForms); - - ajaxify_editInPlaceForm = function() { - $("#g-rename-tag-form").ajaxForm({ - dataType: "json", - success: function(data) { - if (data.result == "success") { - closeEditInPlaceForms(); // close form - $(".g-tag[rel=" + data.tag_id + "]").text(data.new_tagname); // update tagname - window.location.reload(); - } else if (data.result == "error") { - $("#g-rename-tag-form #name") - .addClass("g-error") - .focus(); - var message = "<ul id=\"g-action-status\" class=\"g-message-block\">"; - message += "<li class=\"g-error\">" + data.message + "</li>"; - message += "</ul>"; - $("#g-tag-admin").before(message); - } - } - }); - }; - ajaxify_editInPlaceForm(); -} diff --git a/modules/tag/views/admin_tags.html.php b/modules/tag/views/admin_tags.html.php index 10d7921a..b637a7f1 100644 --- a/modules/tag/views/admin_tags.html.php +++ b/modules/tag/views/admin_tags.html.php @@ -1,18 +1,15 @@ <?php defined("SYSPATH") or die("No direct script access.") ?> <script type="text/javascript"> - var TAG_RENAME_URL = <?= html::js_string(url::site("admin/tags/rename/__ID__")) ?>; $("document").ready(function() { // using JS for adding link titles to avoid running t() for each tag $("#g-tag-admin .g-tag-name").attr("title", <?= t("Click to edit this tag")->for_js() ?>); $("#g-tag-admin .g-delete-link").attr("title", $(".g-delete-link:first span").html()); // In-place editing for tag admin - $(".g-editable").bind("click", editInPlace); + $(".g-editable").gallery_in_place_edit({ + form_url: <?= html::js_string(url::site("admin/tags/form_rename/__ID__")) ?> + }); }); - // make some values available within tag.js - var csrf_token = "<?= $csrf ?>"; - var save_i18n = <?= html::js_string(t("save")->for_html_attr()) ?>; - var cancel_i18n = <?= html::js_string(t("cancel")->for_html_attr()) ?>; </script> <? $tags_per_column = $tags->count()/5 ?> |