summaryrefslogtreecommitdiff
path: root/modules/tag
diff options
context:
space:
mode:
authorAndy Staudacher <andy.st@gmail.com>2009-11-15 19:44:47 -0800
committerAndy Staudacher <andy.st@gmail.com>2009-11-15 19:44:47 -0800
commit0733dc37fda27a5ba35f9020edf3c66aa41a95a0 (patch)
tree6877946232f1b01b1c8709054c689f6658cef34f /modules/tag
parent218493c50be9362d4abed6900a816308fee5d978 (diff)
parent9379308f91a476f790fb8d444536719535c584e4 (diff)
Merge commit 'upstream/master'
Conflicts: modules/gallery/tests/xss_data.txt
Diffstat (limited to 'modules/tag')
-rw-r--r--modules/tag/controllers/admin_tags.php46
-rw-r--r--modules/tag/controllers/tags.php9
-rw-r--r--modules/tag/css/tag.css96
-rw-r--r--modules/tag/helpers/tag.php18
-rw-r--r--modules/tag/helpers/tag_block.php45
-rw-r--r--modules/tag/helpers/tag_theme.php28
-rw-r--r--modules/tag/js/tag.js81
-rw-r--r--modules/tag/models/tag.php14
-rw-r--r--modules/tag/views/admin_tags.html.php81
-rw-r--r--modules/tag/views/tag_block.html.php26
-rw-r--r--modules/tag/views/tag_cloud.html.php2
11 files changed, 249 insertions, 197 deletions
diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php
index 63f7957c..67587c2e 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)
+ ->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];
- }
+ $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 ($valid) {
+ if ($in_place_edit->validate()) {
$old_name = $tag->name;
- $tag->name = $new_name;
+ $tag->name = $in_place_edit->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" => $in_place_edit->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/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/css/tag.css b/modules/tag/css/tag.css
new file mode 100644
index 00000000..6d6438e3
--- /dev/null
+++ b/modules/tag/css/tag.css
@@ -0,0 +1,96 @@
+/* Tag cloud ~~~~~~~~~~~~~~~~~~~~~~~ */
+
+#g-tag-cloud ul {
+ font-size: 1.2em;
+ text-align: justify;
+}
+
+#g-tag-cloud ul li {
+ display: inline;
+ line-height: 1.5em;
+ text-align: justify;
+}
+
+#g-tag-cloud ul li a {
+ text-decoration: none;
+}
+
+#g-tag-cloud ul li span {
+ display: none;
+}
+
+#g-tag-cloud ul li.size1 a {
+ color: #9cf;
+ font-size: 80%;
+ font-weight: 100;
+}
+
+#g-tag-cloud ul li.size2 a {
+ color: #69f;
+ font-size: 90%;
+ font-weight: 300;
+}
+
+#g-tag-cloud ul li.size3 a {
+ color: #69c;
+ font-size: 100%;
+ font-weight: 500;
+}
+
+#g-tag-cloud ul li.size4 a {
+ color: #369;
+ font-size: 110%;
+ font-weight: 700;
+}
+
+#g-tag-cloud ul li.size5 a {
+ color: #0e2b52;
+ font-size: 120%;
+ font-weight: 900;
+}
+
+#g-tag-cloud ul li.size6 a {
+ color: #0e2b52;
+ font-size: 130%;
+ font-weight: 900;
+}
+
+#g-tag-cloud ul li.size7 a {
+ color: #0e2b52;
+ font-size: 140%;
+ font-weight: 900;
+}
+
+#g-tag-cloud ul li a:hover {
+ color: #f30;
+ text-decoration: underline;
+}
+
+/* Add tag form ~~~~~~~~~~~~~~~~~~~~ */
+
+#g-sidebar .g-short-form .textbox {
+ width: 11em;
+}
+
+/* Tag admin ~~~~~~~~~~~~~~~~~~~~~~~ */
+
+#g-tag-admin {
+ table-layout: fixed;
+}
+
+#g-tag-admin td {
+ border: 0;
+ vertical-align: top;
+}
+
+#g-tag-admin ul {
+ margin-bottom: 2em;
+}
+
+#g-tag-admin li {
+ padding: .1em 0 .2em 0;
+}
+
+#g-tag-admin form ul {
+ margin-bottom: 0;
+}
diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php
index be5461a4..feaf40c5 100644
--- a/modules/tag/helpers/tag.php
+++ b/modules/tag/helpers/tag.php
@@ -73,12 +73,15 @@ 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.
@@ -98,7 +101,7 @@ class tag_Core {
}
static function get_add_form($item) {
- $form = new Forge("tags", "", "post", array("id" => "gAddTagForm"));
+ $form = new Forge("tags", "", "post", array("id" => "g-add-tag-form", "class" => "g-short-form"));
$label = $item->is_album() ?
t("Add tag to album") :
($item->is_photo() ? t("Add tag to photo") : t("Add tag to movie"));
@@ -110,17 +113,8 @@ class tag_Core {
return $form;
}
- static function get_rename_form($tag) {
- $form = new Forge("admin/tags/rename/$tag->id", "", "post", array("id" => "gRenameTagForm"));
- $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" => "gDeleteTagForm"));
+ $form = new Forge("admin/tags/delete/$tag->id", "", "post", array("id" => "g-delete-tag-form"));
$group = $form->group("delete_tag")
->label(t("Really delete tag %tag_name?", array("tag_name" => $tag->name)));
$group->submit("")->value(t("Delete Tag"));
diff --git a/modules/tag/helpers/tag_block.php b/modules/tag/helpers/tag_block.php
new file mode 100644
index 00000000..20ef18fa
--- /dev/null
+++ b/modules/tag/helpers/tag_block.php
@@ -0,0 +1,45 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2009 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * 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 {
+ static function get_site_list() {
+ return array("tag" => t("Popular tags"));
+ }
+
+ static function get($block_id, $theme) {
+ $block = "";
+ switch ($block_id) {
+ case "tag":
+ $block = new Block();
+ $block->css_id = "g-tag";
+ $block->title = t("Popular tags");
+ $block->content = new View("tag_block.html");
+ $block->content->cloud = tag::cloud(30);
+
+ if ($theme->item() && $theme->page_type() != "tag" && access::can("edit", $theme->item())) {
+ $controller = new Tags_Controller();
+ $block->content->form = tag::get_add_form($theme->item());
+ } else {
+ $block->content->form = "";
+ }
+ break;
+ }
+ return $block;
+ }
+} \ No newline at end of file
diff --git a/modules/tag/helpers/tag_theme.php b/modules/tag/helpers/tag_theme.php
index 1bce9bd8..e966821a 100644
--- a/modules/tag/helpers/tag_theme.php
+++ b/modules/tag/helpers/tag_theme.php
@@ -21,33 +21,11 @@ class tag_theme_Core {
static function head($theme) {
$theme->css("jquery.autocomplete.css");
$theme->script("jquery.autocomplete.js");
- $theme->script("tag.js");
+ $theme->css("tag.css");
}
static function admin_head($theme) {
- $theme->script("tag.js");
- }
-
- static function sidebar_blocks($theme) {
- // @todo this needs to be data driven
-
- $block = new Block();
- $block->css_id = "gTag";
- $block->title = t("Popular Tags");
- $block->content = new View("tag_block.html");
- $block->content->cloud = tag::cloud(30);
-
- if ($theme->item() && $theme->page_type() != "tag" && access::can("edit", $theme->item())) {
- $controller = new Tags_Controller();
- $block->content->form = tag::get_add_form($theme->item());
- } else {
- $block->content->form = "";
- }
-
- return $block;
- }
-
- static function sort_by_name($tag1, $tag2) {
- return strcasecmp($tag1->name, $tag2->name);
+ $theme->css("tag.css");
+ $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
deleted file mode 100644
index 52c695c6..00000000
--- a/modules/tag/js/tag.js
+++ /dev/null
@@ -1,81 +0,0 @@
-$("document").ready(function() {
- ajaxify_tag_form();
-});
-
-function ajaxify_tag_form() {
- $("#gTag form").ajaxForm({
- dataType: "json",
- success: function(data) {
- if (data.result == "success") {
- $.get($("#gTagCloud").attr("title"), function(data, textStatus) {
- $("#gTagCloud").html(data);
- });
- }
- $("#gTag form").resetForm();
- }
- });
-}
-
-function closeEditInPlaceForms() {
- // closes currently open inplace edit forms
- if ($("#gRenameTagForm").length) {
- $("#gEditErrorMessage").remove();
- var li = $("#gRenameTagForm").parent();
- $("#gRenameTagForm").parent().html($("#gRenameTagForm").parent().data("revert"));
- li.height("");
- $(".gEditable", li).bind("click", editInPlace);
- $(".gDialogLink", 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('id').substr(5);
- var tag_name = $(this).html();
- var tag_width = $(this).width();
- $(this).parent().data("revert", $(this).parent().html());
- var form = '<form id="gRenameTagForm" method="post" class="ui-helper-clearfix" ';
- form += 'action="' + TAG_RENAME_URL.replace('__ID__', tag_id) + '">';
- form += '<input name="csrf" type="hidden" value="' + csrf_token + '" />';
- form += '<input id="name" name="name" type="text" class="textbox" value="' +
- str_replace('"', "&quot;", tag_name) + '" />';
- form += '<input type="submit" class="submit ui-state-default ui-corner-all" value="' + save_i18n + '" i/>';
- form += '<a href="#">' + cancel_i18n + '</a>';
- form += '</form>';
-
- // add edit form
- $(this).parent().html(form);
- $("#gRenameTagForm #name")
- .width(tag_width+30)
- .focus();
- //$("#gRenameTagForm").parent().height( $("#gRenameTagForm").height() );
- $("#gRenameTagForm a").bind("click", closeEditInPlaceForms);
-
- ajaxify_editInPlaceForm = function() {
- $("#gRenameTagForm").ajaxForm({
- dataType: "json",
- success: function(data) {
- if (data.result == "success") {
- closeEditInPlaceForms(); // close form
- $("#gTag-" + data.tag_id).text(data.new_tagname); // update tagname
- console.log(data);
- window.location.reload();
- } else if (data.result == "error") {
- $("#gRenameTagForm #name")
- .addClass("gError")
- .focus();
- $("#gTagAdmin").before("<p id=\"gEditErrorMessage\" class=\"gError\">" + data.message + "</p>");
- }
- }
- });
- };
- ajaxify_editInPlaceForm();
-}
-
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/admin_tags.html.php b/modules/tag/views/admin_tags.html.php
index 8f3693aa..b637a7f1 100644
--- a/modules/tag/views/admin_tags.html.php
+++ b/modules/tag/views/admin_tags.html.php
@@ -1,64 +1,59 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
-<script>
- var TAG_RENAME_URL = <?= html::js_string(url::site("admin/tags/rename/__ID__")) ?>;
+<script type="text/javascript">
$("document").ready(function() {
// using JS for adding link titles to avoid running t() for each tag
- $("#gTagAdmin .tag-name").attr("title", <?= t("Click to edit this tag")->for_js() ?>);
- $("#gTagAdmin .delete-link").attr("title", $(".delete-link:first span").html());
+ $("#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
- $(".gEditable").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>
-<div class="gBlock">
- <h2>
- <?= t("Tag Admin") ?>
- </h2>
- <? $tags_per_column = $tags->count()/5 ?>
- <? $column_tag_count = 0 ?>
+<? $tags_per_column = $tags->count()/5 ?>
+<? $column_tag_count = 0 ?>
- <table id="gTagAdmin" class="gBlockContent">
- <caption class="understate">
- <?= t2("There is one tag", "There are %count tags", $tags->count()) ?>
- </caption>
- <tr>
- <td>
+<div class="g-block">
+ <h1> <?= t("Tag Admin") ?> </h1>
+
+ <div class="g-block-content">
+ <table id="g-tag-admin">
+ <caption>
+ <?= t2("There is one tag", "There are %count tags", $tags->count()) ?>
+ </caption>
+ <tr>
+ <td>
<? foreach ($tags as $i => $tag): ?>
<? $current_letter = strtoupper(mb_substr($tag->name, 0, 1)) ?>
<? if ($i == 0): /* first letter */ ?>
- <strong><?= html::clean($current_letter) ?></strong>
- <ul>
+ <strong><?= html::clean($current_letter) ?></strong>
+ <ul>
<? elseif ($last_letter != $current_letter): /* new letter */ ?>
+ </ul>
<? if ($column_tag_count > $tags_per_column): /* new column */ ?>
- </td>
- <td>
<? $column_tag_count = 0 ?>
+ </td>
+ <td>
<? endif ?>
-
- </ul>
- <strong><?= html::clean($current_letter) ?></strong>
- <ul>
+ <strong><?= html::clean($current_letter) ?></strong>
+ <ul>
<? endif ?>
-
- <li>
- <span id="gTag-<?= $tag->id ?>" class="gEditable tag-name"><?= html::clean($tag->name) ?></span>
- <span class="understate">(<?= $tag->count ?>)</span>
- <a href="<?= url::site("admin/tags/form_delete/$tag->id") ?>"
- class="gDialogLink delete-link gButtonLink">
- <span class="ui-icon ui-icon-trash"><?= t("Delete this tag") ?></span></a>
- </li>
-
+ <li>
+ <span class="g-editable g-tag-name" rel="<?= $tag->id ?>"><?= html::clean($tag->name) ?></span>
+ <span class="g-understate">(<?= $tag->count ?>)</span>
+ <a href="<?= url::site("admin/tags/form_delete/$tag->id") ?>"
+ class="g-dialog-link g-delete-link g-button">
+ <span class="ui-icon ui-icon-trash"><?= t("Delete this tag") ?></span></a>
+ </li>
<? $column_tag_count++ ?>
<? $last_letter = $current_letter ?>
- <? endforeach /* $tags */ ?>
- </ul>
- </td>
- </tr>
- </table>
+ <? endforeach ?>
+ </ul>
+ </td>
+ </tr>
+ </table>
+ </div>
</div>
diff --git a/modules/tag/views/tag_block.html.php b/modules/tag/views/tag_block.html.php
index 59a4ef88..00b57360 100644
--- a/modules/tag/views/tag_block.html.php
+++ b/modules/tag/views/tag_block.html.php
@@ -1,17 +1,29 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
-<script>
- $("#gAddTagForm").ready(function() {
- var url = $("#gTagCloud").attr("title") + "/autocomplete";
- $("#gAddTagForm input:text").autocomplete(
+<script type="text/javascript">
+ $("#g-add-tag-form").ready(function() {
+ var url = $("#g-tag-cloud").attr("ref") + "/autocomplete";
+ $("#g-add-tag-form input:text").autocomplete(
url, {
max: 30,
multiple: true,
- multipleSeparator: ',',
- cacheLength: 1}
+ multipleSeparator: ',',
+ cacheLength: 1
+ }
);
+ $("#g-add-tag-form").ajaxForm({
+ dataType: "json",
+ success: function(data) {
+ if (data.result == "success") {
+ $.get($("#g-tag-cloud").attr("ref"), function(data, textStatus) {
+ $("#g-tag-cloud").html(data);
+ });
+ }
+ $("#g-add-tag-form").resetForm();
+ }
+ });
});
</script>
-<div id="gTagCloud" title="<?= url::site("tags") ?>">
+<div id="g-tag-cloud" ref="<?= url::site("tags") ?>">
<?= $cloud ?>
</div>
<?= $form ?> \ 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 @@
<? foreach ($tags as $tag): ?>
<li class="size<?=(int)(($tag->count / $max_count) * 7) ?>">
<span><?= $tag->count ?> photos are tagged with </span>
- <a href="<?= url::site("tags/$tag->id") ?>"><?= html::clean($tag->name) ?></a>
+ <a href="<?= $tag->url() ?>"><?= html::clean($tag->name) ?></a>
</li>
<? endforeach ?>
</ul>