From cc6096780b8598509ab04b00a58ccdb83f6ba4e9 Mon Sep 17 00:00:00 2001 From: DBinaghi <39097246+DBinaghi@users.noreply.github.com> Date: Mon, 13 Jan 2020 13:52:46 +0100 Subject: [PATCH] Added removal of tag to deleteTags Function would delete taggings, but not the tag, even when there were no more references to that tag in whole database. Now, when removing a tag from item, if there are no more references to that tag it will be deleted. --- application/models/Mixin/Tag.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/application/models/Mixin/Tag.php b/application/models/Mixin/Tag.php index 4c736fd681..6fab7e66fd 100644 --- a/application/models/Mixin/Tag.php +++ b/application/models/Mixin/Tag.php @@ -132,6 +132,7 @@ public function deleteTags($tags, $delimiter = null) $findWith['tag'] = $tags; $findWith['record'] = $this->_record; + $db = $this->_record->getDb(); $taggings = $this->_joinTable->findBy($findWith); $removed = array(); @@ -139,6 +140,14 @@ public function deleteTags($tags, $delimiter = null) $removed[] = $this->_tagTable->find($tagging->tag_id); $tagging->delete(); } + + foreach ($tags as $tag) { + $count = $this->_joinTable->count(array('tag' => $tag)); + if ($count == 0) { + $db->delete($db->Tags, array('name = ?' => $tag)); + } + } + $nameForHook = strtolower($this->_type); fire_plugin_hook("remove_{$nameForHook}_tag", array('record' => $this->_record, 'removed' => $removed));