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 +++++++++++++++------------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'modules/tag/controllers') 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"); + } + } + } -- cgit v1.2.3 From beb63a83804e57050a23b3a90ebed93be41b6769 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 6 Nov 2009 23:05:20 -0800 Subject: Clean up the In place edit api: 1) Only allow 1 in place edit to be active at a time (gets around the issue of using an id to identify the form 2) remove the add_ prefix from some of the api methods 3) clean up inconsistent naming --- lib/gallery.in_place_edit.js | 7 +++++++ modules/gallery/libraries/InPlaceEdit.php | 8 ++++---- modules/tag/controllers/admin_tags.php | 18 +++++++++--------- modules/tag/css/tag.css | 4 ++-- 4 files changed, 22 insertions(+), 15 deletions(-) (limited to 'modules/tag/controllers') diff --git a/lib/gallery.in_place_edit.js b/lib/gallery.in_place_edit.js index 38c4efb1..4a90581b 100644 --- a/lib/gallery.in_place_edit.js +++ b/lib/gallery.in_place_edit.js @@ -15,6 +15,13 @@ var tag_width = $(target).width(); $(self).data("tag_width", tag_width); + var form = $("#g-inplace-edit-form"); + if (form.length > 0) { + var parent = form.parent(); + form.remove(); + parent.children().show(); + } + $.get(self.options.form_url.replace("__ID__", $(target).attr('rel')), function(data) { var parent = $(target).parent(); parent.children().hide(); diff --git a/modules/gallery/libraries/InPlaceEdit.php b/modules/gallery/libraries/InPlaceEdit.php index 057874e3..67ab3805 100644 --- a/modules/gallery/libraries/InPlaceEdit.php +++ b/modules/gallery/libraries/InPlaceEdit.php @@ -35,22 +35,22 @@ class InPlaceEdit_Core { return $instance; } - public function add_action($action) { + public function action($action) { $this->action = $action; return $this; } - public function add_rules($rules) { + public function rules($rules) { $this->rules += $rules; return $this; } - public function add_messages($messages) { + public function messages($messages) { $this->messages += $messages; return $this; } - public function add_callback($callback) { + public function callback($callback) { $this->callback = $callback; return $this; } diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php index d79f697c..67587c2e 100644 --- a/modules/tag/controllers/admin_tags.php +++ b/modules/tag/controllers/admin_tags.php @@ -70,7 +70,7 @@ class Admin_Tags_Controller extends Admin_Controller { $tag = ORM::factory("tag", $id); if ($tag->loaded) { print InPlaceEdit::factory($tag->name) - ->add_action("admin/tags/rename/$id") + ->action("admin/tags/rename/$id") ->render(); } } @@ -83,15 +83,15 @@ class Admin_Tags_Controller extends Admin_Controller { kohana::show_404(); } - $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")); + $in_place_edit = InPlaceEdit::factory($tag->name) + ->action("admin/tags/rename/$tag->id") + ->rules(array("required", "length[1,64]")) + ->messages(array("in_use" => t("There is already a tag with that name"))) + ->callback(array($this, "check_for_duplicate")); - if ($inplaceedit->validate()) { + if ($in_place_edit->validate()) { $old_name = $tag->name; - $tag->name = $inplaceedit->value(); + $tag->name = $in_place_edit->value(); $tag->save(); $message = t("Renamed tag %old_name to %new_name", @@ -101,7 +101,7 @@ class Admin_Tags_Controller extends Admin_Controller { print json_encode(array("result" => "success")); } else { - print json_encode(array("result" => "error", "form" => $inplaceedit->render())); + print json_encode(array("result" => "error", "form" => $in_place_edit->render())); } } diff --git a/modules/tag/css/tag.css b/modules/tag/css/tag.css index 03ed444b..6d6438e3 100644 --- a/modules/tag/css/tag.css +++ b/modules/tag/css/tag.css @@ -91,6 +91,6 @@ padding: .1em 0 .2em 0; } -#g-rename-tag-form ul { +#g-tag-admin form ul { margin-bottom: 0; -} \ No newline at end of file +} -- cgit v1.2.3 From 846f365db9d3c1b61ed4bd68316bd5e7b80e56ec Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 14 Nov 2009 16:17:19 -0800 Subject: Normalize tags a bit. - Create Tag_Model::url() to mimic Item_Model::url() - Use the same pagination logic as we do for viewing items --- modules/tag/controllers/tags.php | 9 +++++++-- modules/tag/models/tag.php | 14 ++++++++++++++ modules/tag/views/tag_cloud.html.php | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) (limited to 'modules/tag/controllers') diff --git a/modules/tag/controllers/tags.php b/modules/tag/controllers/tags.php index 1bd6b3cc..c3b14fcc 100644 --- a/modules/tag/controllers/tags.php +++ b/modules/tag/controllers/tags.php @@ -25,13 +25,18 @@ class Tags_Controller extends REST_Controller { $page = (int) $this->input->get("page", "1"); $children_count = $tag->items_count(); $offset = ($page-1) * $page_size; + $max_pages = max(ceil($children_count / $page_size), 1); // Make sure that the page references a valid offset - if ($page < 1 || ($children_count && $page > ceil($children_count / $page_size))) { - Kohana::show_404(); + if ($page < 1) { + url::redirect($album->abs_url()); + } else if ($page > $max_pages) { + url::redirect($album->abs_url("page=$max_pages")); } $template = new Theme_View("page.html", "tag"); + $template->set_global("page", $page); + $template->set_global("max_pages", $max_pages); $template->set_global("page_size", $page_size); $template->set_global("tag", $tag); $template->set_global("children", $tag->items($page_size, $offset)); diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index d9488e1c..49512daa 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -102,4 +102,18 @@ class Tag_Model extends ORM { } return $result; } + + /** + * Return the server-relative url to this item, eg: + * /gallery3/index.php/tags/35 + * + * @param string $query the query string (eg "page=3") + */ + public function url($query=null) { + $url = url::site("tags/$this->id"); + if ($query) { + $url .= "?$query"; + } + return $url; + } } \ No newline at end of file diff --git a/modules/tag/views/tag_cloud.html.php b/modules/tag/views/tag_cloud.html.php index d6a0b5f8..de12bb49 100644 --- a/modules/tag/views/tag_cloud.html.php +++ b/modules/tag/views/tag_cloud.html.php @@ -3,7 +3,7 @@
  • count ?> photos are tagged with - id") ?>">name) ?> + name) ?>
  • -- cgit v1.2.3