diff options
Diffstat (limited to 'modules/tag')
| -rw-r--r-- | modules/tag/helpers/tag.php | 20 | ||||
| -rw-r--r-- | modules/tag/helpers/tag_event.php | 24 |
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(); } } |
