From 3dfaea8ddfd93d057e4a5ad3271ca5c9604d3746 Mon Sep 17 00:00:00 2001 From: francescbassas Date: Wed, 27 Jul 2016 00:28:43 +0200 Subject: [PATCH] CRM-19133 Unassigning contact subtype for a custom group causes data loss --- CRM/Contact/BAO/ContactType.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/ContactType.php b/CRM/Contact/BAO/ContactType.php index efc0ca1f2c..7b0dece4ba 100644 --- a/CRM/Contact/BAO/ContactType.php +++ b/CRM/Contact/BAO/ContactType.php @@ -893,7 +893,7 @@ WHERE extends = %1 AND " . implode(" OR ", $subTypeClause); * * @return bool */ - public static function deleteCustomRowsOfSubtype($gID, $subtypes = array()) { + public static function deleteCustomRowsOfSubtype($gID, $subtypes = array(), $subtypesToPreserve = array() ) { if (!$gID or empty($subtypes)) { return FALSE; } @@ -902,11 +902,19 @@ WHERE extends = %1 AND " . implode(" OR ", $subTypeClause); // drop triggers CRM-13587 CRM_Core_DAO::dropTriggers($tableName); + + foreach ($subtypesToPreserve as $subtypeToPreserve) { + $subtypeToPreserve = CRM_Utils_Type::escape($subtypeToPreserve, 'String'); + $subtypesToPreserveClause[] = "( civicrm_contact.contact_sub_type NOT LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtypeToPreserve . CRM_Core_DAO::VALUE_SEPARATOR . "%')"; + } + $subtypesToPreserveClause = implode(' AND ', $subtypesToPreserveClause); + $subtypeClause = array(); foreach ($subtypes as $subtype) { $subtype = CRM_Utils_Type::escape($subtype, 'String'); - $subtypeClause[] = "civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR . "%'"; + $subtypeClause[] = "( civicrm_contact.contact_sub_type LIKE '%" . CRM_Core_DAO::VALUE_SEPARATOR . $subtype . CRM_Core_DAO::VALUE_SEPARATOR . "%'" + . " AND " . $subtypesToPreserveClause . ")"; } $subtypeClause = implode(' OR ', $subtypeClause); -- 2.25.1