From da86a8e1c832aced1b7c44e9f8e6d9afec44b34e Mon Sep 17 00:00:00 2001 From: DBinaghi <39097246+DBinaghi@users.noreply.github.com> Date: Sun, 5 Jan 2020 13:49:37 +0100 Subject: [PATCH 1/2] Added function to merge tags Added function to be referenced to from public function renameAjaxAction() in order to merge tags in case the new tag name is already found in the table --- application/models/Table/Tag.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/application/models/Table/Tag.php b/application/models/Table/Tag.php index 71b95e7bba..144cc43b82 100644 --- a/application/models/Table/Tag.php +++ b/application/models/Table/Tag.php @@ -191,4 +191,15 @@ public function findTagNamesLike($partialName, $limit = 10) $tags = $db->fetchCol($sql, array($partialName . '%')); return $tags; } + + public function mergeTags($oldTagId, $newTagId) + { + $db = $this->getDb(); + $sql = "UPDATE $db->RecordsTag SET tag_id = $newTagId, time = CURRENT_TIMESTAMP WHERE tag_id = $oldTagId AND record_id NOT IN (SELECT record_id FROM (SELECT DISTINCT record_id FROM $db->RecordsTag WHERE tag_id = $newTagId) AS tmptable)"; + $db->query($sql); + $sql = "DELETE FROM $db->RecordsTag WHERE tag_id = $oldTagId"; + $db->query($sql); + $sql = "DELETE FROM $db->Tag WHERE id = $oldTagId"; + $db->query($sql); + } } From 58413e64abb0d04f9f132e5b3dde2e654fff7f42 Mon Sep 17 00:00:00 2001 From: DBinaghi <39097246+DBinaghi@users.noreply.github.com> Date: Mon, 4 May 2020 18:59:24 +0200 Subject: [PATCH 2/2] Added optional $recordType to mergeTags function, and counting results --- application/models/Table/Tag.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/application/models/Table/Tag.php b/application/models/Table/Tag.php index 144cc43b82..450c180bca 100644 --- a/application/models/Table/Tag.php +++ b/application/models/Table/Tag.php @@ -192,14 +192,20 @@ public function findTagNamesLike($partialName, $limit = 10) return $tags; } - public function mergeTags($oldTagId, $newTagId) + public function mergeTags($oldTagId, $newTagId, $recordType = '') { - $db = $this->getDb(); - $sql = "UPDATE $db->RecordsTag SET tag_id = $newTagId, time = CURRENT_TIMESTAMP WHERE tag_id = $oldTagId AND record_id NOT IN (SELECT record_id FROM (SELECT DISTINCT record_id FROM $db->RecordsTag WHERE tag_id = $newTagId) AS tmptable)"; - $db->query($sql); - $sql = "DELETE FROM $db->RecordsTag WHERE tag_id = $oldTagId"; - $db->query($sql); - $sql = "DELETE FROM $db->Tag WHERE id = $oldTagId"; - $db->query($sql); + if ($recordType != '') $recordType = " AND record_type = '" . $recordType . "'"; + + $db = $this->getDb(); + $sql = "UPDATE $db->RecordsTag SET tag_id = $newTagId, time = CURRENT_TIMESTAMP WHERE tag_id = $oldTagId" . $recordType . " AND record_id NOT IN (SELECT record_id FROM (SELECT DISTINCT record_id FROM $db->RecordsTag WHERE tag_id = $newTagId) AS tmptable)"; + $db->query($sql); + $sql = "DELETE FROM $db->RecordsTag WHERE tag_id = $oldTagId" . $recordType; + $db->query($sql); + $sql = "DELETE FROM $db->Tag WHERE id = $oldTagId"; + $db->query($sql); + + $sql = "SELECT COUNT(id) AS tagCount FROM $db->RecordsTag WHERE tag_id = $newTagId" . $recordType; + $tagCount = $db->fetchOne($sql); + return $tagCount; } }