summaryrefslogtreecommitdiff
path: root/modules/tag/helpers
diff options
context:
space:
mode:
authorRomain LE DISEZ <romain.git@ledisez.net>2009-07-20 18:46:41 +0200
committerRomain LE DISEZ <romain.git@ledisez.net>2009-07-20 18:46:41 +0200
commit00b3ca82de3a4858745988943a46d07b1c0fd006 (patch)
tree158896d430014e1f896df0b19acc57aeb602c9ab /modules/tag/helpers
parent8cbf43ed2abe9772045766e598bb9fe3d4def983 (diff)
parentfb7d99740d9868938ec21449d57b9b3fe991f513 (diff)
Merge commit 'upstream/master'
Diffstat (limited to 'modules/tag/helpers')
-rw-r--r--modules/tag/helpers/tag.php20
-rw-r--r--modules/tag/helpers/tag_event.php24
2 files changed, 38 insertions, 6 deletions
diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php
index ab5ee303..1fb2e940 100644
--- a/modules/tag/helpers/tag.php
+++ b/modules/tag/helpers/tag.php
@@ -127,4 +127,24 @@ class tag_Core {
$group->submit("")->value(t("Delete Tag"));
return $form;
}
+
+ /**
+ * Delete all tags associated with an item
+ */
+ static function clear_all($item) {
+ $db = Database::instance();
+ $db->query("UPDATE {tags} SET `count` = `count` - 1 WHERE `count` > 0 " .
+ "AND `id` IN (SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)");
+ $db->delete("items_tags", array("item_id" => "$item->id"));
+ }
+
+ /**
+ * Get rid of any tags that have no associated items.
+ */
+ static function compact() {
+ // @todo There's a potential race condition here which we can solve by adding a lock around
+ // this and all the cases where we create/update tags. I'm loathe to do that since it's an
+ // extremely rare case.
+ Database::instance() ->delete("tags", array("count" => 0));
+ }
} \ No newline at end of file
diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php
index 0164f556..d13d1340 100644
--- a/modules/tag/helpers/tag_event.php
+++ b/modules/tag/helpers/tag_event.php
@@ -60,11 +60,23 @@ class tag_event_Core {
}
static function item_deleted($item) {
- $db = Database::instance();
- $db->query("UPDATE {tags} SET `count` = `count` - 1 WHERE `count` > 0 " .
- "AND `id` IN (SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)");
- $db->query("DELETE FROM {tags} WHERE `count` = 0 AND `id` IN (" .
- "SELECT `tag_id` from {items_tags} WHERE `item_id` = $item->id)");
- $db->delete("items_tags", array("item_id" => "$item->id"));
+ tag::clear_all($item);
+ tag::compact();
+ }
+
+ static function item_edit_form($item, $form) {
+ $tag_value = implode("; ", tag::item_tags($item));
+ $form->edit_item->input("tags")->label(t("Tags (separate by , or ;)"))
+ ->value($tag_value);
+ }
+
+ static function item_edit_form_completed($item, $form) {
+ tag::clear_all($item);
+ foreach (preg_split("/[,;]/", $form->edit_item->tags->value) as $tag_name) {
+ if ($tag_name) {
+ tag::add($item, $tag_name);
+ }
+ }
+ tag::compact();
}
}