From 72a8ce696e37a74ed8bbea31364b39f9e0776bbc Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 5 Nov 2009 12:59:37 -0800 Subject: 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. --- modules/tag/controllers/admin_tags.php | 46 ++++++++++------------ modules/tag/helpers/tag.php | 15 +++---- modules/tag/helpers/tag_theme.php | 8 +--- modules/tag/js/tag.js | 71 ---------------------------------- modules/tag/views/admin_tags.html.php | 9 ++--- 5 files changed, 30 insertions(+), 119 deletions(-) (limited to 'modules') 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 += ''; - 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 = ""; - $("#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 @@ count()/5 ?> -- cgit v1.2.3