From b35800bdc374238da1a99dd255b22b7389973b7b Mon Sep 17 00:00:00 2001 From: Eaiman Shoshi Date: Fri, 14 Jul 2017 03:06:34 +0600 Subject: [PATCH] Fix for jira issue CRM-20858 This PR is for jira issue https://issues.civicrm.org/jira/browse/CRM-20858 --- CRM/Dedupe/Merger.php | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index 400d3364c6..bcd51ea87b 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -437,15 +437,21 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m * @param int $otherId * @param bool $tables * @param array $tableOperations + * @param array $customTableToCopyFrom */ - public static function moveContactBelongings($mainId, $otherId, $tables = FALSE, $tableOperations = array()) { + public static function moveContactBelongings($mainId, $otherId, $tables = FALSE, $tableOperations = array(), $customTableToCopyFrom = array()) { $cidRefs = self::cidRefs(); $eidRefs = self::eidRefs(); $cpTables = self::cpTables(); $paymentTables = self::paymentTables(); // CRM-12695: $membershipMerge = FALSE; - + + // getting all custom tables + $customTables = array(); + self::addCustomTablesExtendingContactsToCidRefs($customTables); + $customTables = array_keys($customTables); + $affected = array_merge(array_keys($cidRefs), array_keys($eidRefs)); if ($tables !== FALSE) { // if there are specific tables, sanitize the list @@ -483,6 +489,11 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m $sqls = array(); foreach ($affected as $table) { + // skipping non selected custom table's value migration + if(in_array($table, $customTables) && !in_array($table, $customTableToCopyFrom)){ + continue; + } + // Call custom processing function for objects that require it if (isset($cpTables[$table])) { foreach ($cpTables[$table] as $className => $fnName) { @@ -1464,6 +1475,8 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m $qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9'; $relTables = CRM_Dedupe_Merger::relTables(); $moveTables = $locationMigrationInfo = $tableOperations = array(); + // variable for capturing id of civicrm_custom_field id + $submittedCustomValue = array(); foreach ($migrationInfo as $key => $value) { if ($value == $qfZeroBug) { $value = '0'; @@ -1473,6 +1486,8 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m $value != NULL ) { $submitted[substr($key, 5)] = $value; + // capturing id + array_push($submittedCustomValue, substr($key, 12)); } // Set up initial information for handling migration of location blocks @@ -1499,8 +1514,25 @@ INNER JOIN civicrm_membership membership2 ON membership1.membership_type_id = m unset($moveTables, $tableOperations); } + // capturing the custom table names. The table's value we have to merge + // from duplicate contact to original contact + $customTableToCopyValues = array(); + foreach ($submittedCustomValue as $key=>$value){ + $result = civicrm_api3('custom_field', 'get', array('id' => $value, 'is_active' => TRUE)); + if(!$result['is_error']){ + foreach ($result['values'] as $k=>$v){ + $result_ = civicrm_api3('custom_group', 'get', array('id' => $result['values'][$k]['custom_group_id'], 'is_active' => TRUE)); + if(!$result_['is_error']) { + foreach ($result_['values'] as $kk => $vv) { + array_push($customTableToCopyValues, $result_['values'][$kk]['table_name']); + } + } + } + } + } + // **** Do contact related migrations - CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId); + CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId, FALSE, array(), $customTableToCopyValues); // FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact() $names['gender'] = array('newName' => 'gender_id', 'groupName' => 'gender'); -- 2.25.1