From c0a431b4c163a484cf468fc5e444f9e4599f9241 Mon Sep 17 00:00:00 2001
From: satish <satish.dvnk@vtiger.com>
Date: Mon, 30 Oct 2017 18:03:56 +0530
Subject: [PATCH] Tag fields are pointed to cf table for the modules Assets,
 Services etc..

---
 modules/Migration/schema/701_to_710.php | 24 +++++++++++++++++++++++-
 vtlib/Vtiger/PackageImport.php          | 13 +++----------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/modules/Migration/schema/701_to_710.php b/modules/Migration/schema/701_to_710.php
index 8a3e282ad..cb2cbaf98 100644
--- a/modules/Migration/schema/701_to_710.php
+++ b/modules/Migration/schema/701_to_710.php
@@ -49,7 +49,7 @@ if (defined('VTIGER_UPGRADE')) {
 		$db->pquery('ALTER TABLE vtiger_tab ADD COLUMN sync_action_for_duplicates INT(1) DEFAULT 1');
 	}
 
-	//Start - Enable prevention for Accounts module
+	//START - Enable prevention for Accounts module
 	$accounts = 'Accounts';
 	$db->pquery('UPDATE vtiger_field SET isunique=? WHERE fieldname=? AND tabid=(SELECT tabid FROM vtiger_tab WHERE name=?)', array(1, 'accountname', $accounts));
 	$db->pquery('UPDATE vtiger_tab SET allowduplicates=? WHERE name=?', array(0, $accounts));
@@ -79,6 +79,28 @@ if (defined('VTIGER_UPGRADE')) {
 	echo '<br>Succecssfully added Webforms attachements<br>';
 	//END::Webform Attachements
 
+	//START::Tag fields are pointed to cf table for the modules Assets, Services etc..
+	$fieldName = 'tags';
+	$moduleModels = Vtiger_Module_Model::getAll();
+	foreach ($moduleModels as $moduleModel) {
+		$baseTableId = $moduleModel->basetableid;
+		if ($baseTableId) {
+			$baseTableName = $moduleModel->basetable;
+			$customTableName = $baseTableName.'cf';
+			$customTableColumns = $db->getColumnNames($customTableName);
+			if (in_array($fieldName, $customTableColumns)) {
+				$fieldModel = Vtiger_Field_Model::getInstance($fieldName, $moduleModel);
+				$db->pquery("UPDATE vtiger_field SET tablename=? WHERE fieldid=?", array($baseTableName, $fieldModel->id));
+				$db->pquery("ALTER TABLE $baseTableName ADD COLUMN $fieldName VARCHAR(1)", array());
+
+				$db->pquery("UPDATE $baseTableName, $customTableName SET $baseTableName.tags=$customTableName.tags WHERE $baseTableName.$baseTableId=$customTableName.$baseTableId", array());
+				$db->pquery("ALTER TABLE $customTableName DROP COLUMN $fieldName", array());
+			}
+		}
+	}
+	echo '<br>Succecssfully generalized tag fields<br>';
+	//START::Tag fields are pointed to cf table for the modules Assets, Services etc..
+
 	//START::Follow & unfollow features
 	$em = new VTEventsManager($db);
 	$em->registerHandler('vtiger.entity.aftersave', 'modules/Vtiger/handlers/FollowRecordHandler.php', 'FollowRecordHandler');
diff --git a/vtlib/Vtiger/PackageImport.php b/vtlib/Vtiger/PackageImport.php
index 2618652c6..973544d20 100644
--- a/vtlib/Vtiger/PackageImport.php
+++ b/vtlib/Vtiger/PackageImport.php
@@ -923,18 +923,11 @@ class Vtiger_PackageImport extends Vtiger_PackageExport {
 		}
 
 		if (Vtiger_Field::getInstance('tags', $moduleInstance) === false && $moduleModel->isTagsEnabled()) {
-			$module = (string) $moduleInstance->name;
-			$focus = CRMEntity::getInstance($module);
-			if (isset($focus->customFieldTable)) {
-				$tableName = $focus->customFieldTable[0];
-			} else {
-				$tableName = 'vtiger_'.strtolower($module).'cf';
-			}
 			//Adding tag field
 			$field = new Vtiger_Field();
-			$field->name = "tags";
-			$field->label = "tags";
-			$field->table = $tableName;
+			$field->name = 'tags';
+			$field->label = 'tags';
+			$field->table = $moduleInstance->basetable;
 			$field->presence = 2;
 			$field->displaytype = 6;
 			$field->readonly = 1;
-- 
GitLab