Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20858: Fix Merging of Unselected Custom Fields #10831

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 99 additions & 6 deletions CRM/Dedupe/Merger.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,23 @@ public static function operationSql($mainId, $otherId, $tableName, $tableOperati
* @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 = NULL) {
$cidRefs = self::cidRefs();
$eidRefs = self::eidRefs();
$cpTables = self::cpTables();
$paymentTables = self::paymentTables();
// CRM-12695:
$membershipMerge = FALSE;

// getting all custom tables
$customTables = array();
if ($customTableToCopyFrom !== NULL) {
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
Expand Down Expand Up @@ -483,6 +491,11 @@ public static function moveContactBelongings($mainId, $otherId, $tables = FALSE,

$sqls = array();
foreach ($affected as $table) {
// skipping non selected custom table's value migration
if ($customTableToCopyFrom !== NULL && 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) {
Expand Down Expand Up @@ -510,10 +523,14 @@ public static function moveContactBelongings($mainId, $otherId, $tables = FALSE,
$preOperationSqls = self::operationSql($mainId, $otherId, $table, $tableOperations);
$sqls = array_merge($sqls, $preOperationSqls);

if ($customTableToCopyFrom !== NULL && in_array($table, $customTableToCopyFrom) && !self::customRecordExists($mainId, $table, $field)) {
$sqls[] = "INSERT INTO $table ($field) VALUES ($mainId)";
}
$sqls[] = "UPDATE IGNORE $table SET $field = $mainId WHERE $field = $otherId";
$sqls[] = "DELETE FROM $table WHERE $field = $otherId";
}
}

if (isset($eidRefs[$table])) {
foreach ($eidRefs[$table] as $entityTable => $entityId) {
$sqls[] = "UPDATE IGNORE $table SET $entityId = $mainId WHERE $entityId = $otherId AND $entityTable = 'civicrm_contact'";
Expand All @@ -538,6 +555,33 @@ public static function moveContactBelongings($mainId, $otherId, $tables = FALSE,
$transaction->commit();
}

/**
* Given a contact ID, will check if a record exists in given table.
*
* @param $contactID
* @param $table
* @param $idField
* Field where the contact's ID is stored in the table
*
* @return bool
* True if a record is found for the given contact ID, false otherwise
*/
private static function customRecordExists($contactID, $table, $idField) {
$sql = "
SELECT COUNT(*) AS count
FROM $table
WHERE $idField = $contactID
";
$dbResult = CRM_Core_DAO::executeQuery($sql);
$dbResult->fetch();

if ($dbResult->count > 0) {
return TRUE;
}

return FALSE;
}

/**
* Load all non-empty fields for the contacts
*
Expand Down Expand Up @@ -1463,7 +1507,8 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che

$qfZeroBug = 'e8cddb72-a257-11dc-b9cc-0016d3330ee9';
$relTables = CRM_Dedupe_Merger::relTables();
$moveTables = $locationMigrationInfo = $tableOperations = array();
$submittedCustomFields = $moveTables = $locationMigrationInfo = $tableOperations = array();

foreach ($migrationInfo as $key => $value) {
if ($value == $qfZeroBug) {
$value = '0';
Expand All @@ -1473,6 +1518,7 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che
$value != NULL
) {
$submitted[substr($key, 5)] = $value;
$submittedCustomFields[] = substr($key, 12);
}

// Set up initial information for handling migration of location blocks
Expand Down Expand Up @@ -1500,7 +1546,8 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che
}

// **** Do contact related migrations
CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId);
$customTablesToCopyValues = self::getAffectedCustomTables($submittedCustomFields);
CRM_Dedupe_Merger::moveContactBelongings($mainId, $otherId, FALSE, array(), $customTablesToCopyValues);

// FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact()
$names['gender'] = array('newName' => 'gender_id', 'groupName' => 'gender');
Expand Down Expand Up @@ -1567,19 +1614,19 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che
if (!empty($customfieldValues[$key])) {
$existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
if (is_array($existingValue) && !empty($existingValue)) {
$mergeValue = $submittedCustomValue = array();
$mergeValue = $submittedCustomFields = array();
if ($value == 'null') {
// CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
$submitted[$key] = $value;
}
else {
if ($value) {
$submittedCustomValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
$submittedCustomFields = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
}

// CRM-19653: overwrite or add the existing custom field value with dupicate contact's
// custom field value stored at $submittedCustomValue.
foreach ($submittedCustomValue as $k => $v) {
foreach ($submittedCustomFields as $k => $v) {
if ($v != '' && !in_array($v, $mergeValue)) {
$mergeValue[] = $v;
}
Expand Down Expand Up @@ -1746,6 +1793,52 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che
return TRUE;
}

/**
* Builds an Array of Custom tables for given custom field ID's.
*
* @param $customFieldIDs
*
* @return array
* Array of custom table names
*/
private static function getAffectedCustomTables($customFieldIDs) {
$customTableToCopyValues = array();

foreach ($customFieldIDs as $fieldID) {
if (!empty($fieldID)) {
$customField = NULL;
try {
$customField = civicrm_api3('custom_field', 'getsingle', array(
'id' => $fieldID,
'is_active' => TRUE,
));
}
catch (CiviCRM_API3_Exception $e) {
continue;
}
if (!civicrm_error($customField) && !empty($customField['custom_group_id'])) {
$customGroup = NULL;
try {
$customGroup = civicrm_api3('custom_group', 'getsingle', array(
'id' => $customField['custom_group_id'],
'is_active' => TRUE,
));
}
catch (CiviCRM_API3_Exception $e) {
// just ignore and continue
continue;
}

if (!civicrm_error($customGroup) && !empty($customGroup['table_name'])) {
$customTableToCopyValues[] = $customGroup['table_name'];
}
}
}
}

return $customTableToCopyValues;
}

/**
* Get fields in the contact table suitable for merging.
*
Expand Down
Loading