From c101151616033d53587d1435881dae0fa45aeefa Mon Sep 17 00:00:00 2001 From: Andy Lindeman Date: Sat, 23 Apr 2011 12:04:12 -0400 Subject: Allow tags to be merged by renaming * Fixes #1628 --- modules/tag/models/tag.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'modules/tag/models') diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index bd665667..bb79e707 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -69,13 +69,23 @@ class Tag_Model_Core extends ORM { * to this tag. */ public function save() { - $related_item_ids = array(); - foreach (db::build() - ->select("item_id") - ->from("items_tags") - ->where("tag_id", "=", $this->id) - ->execute() as $row) { - $related_item_ids[$row->item_id] = 1; + // Check to see if another tag exists with the same name + $duplicate_tag = ORM::factory("tag") + ->where("name", "=", $this->name) + ->where("id", "!=", $this->id) + ->find(); + if ($duplicate_tag->loaded()) { + // If so, tag its items with this tag so as to merge it + $duplicate_tag_items = ORM::factory("item") + ->join("items_tags", "items.id", "items_tags.item_id") + ->where("items_tags.tag_id", "=", $duplicate_tag->id) + ->find_all(); + foreach ($duplicate_tag_items as $item) { + $this->add($item); + } + + // ... and remove the duplicate tag + $duplicate_tag->delete(); } if (isset($this->object_relations["items"])) { -- cgit v1.2.3