diff options
| author | Andy Lindeman <andy@highgroove.com> | 2011-04-23 12:04:12 -0400 |
|---|---|---|
| committer | Andy Lindeman <andy@highgroove.com> | 2011-04-23 12:04:43 -0400 |
| commit | c101151616033d53587d1435881dae0fa45aeefa (patch) | |
| tree | c848494ad67ed0c10cfdf6b7f7f5d00b92717d40 /modules/tag/models | |
| parent | 563a25f559a0247b2e3f4e96b191e7138c8e8ea5 (diff) | |
Allow tags to be merged by renaming
* Fixes #1628
Diffstat (limited to 'modules/tag/models')
| -rw-r--r-- | modules/tag/models/tag.php | 24 |
1 files changed, 17 insertions, 7 deletions
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"])) { |
