Skip to content

Instantly share code, notes, and snippets.

@lcdservices
Created April 22, 2019 22:53
fix merge custom file fields
--- C:/Users/Brian/Documents/CLIENT/sites/all/modules/civicrm/CRM/Dedupe/Merger.php Mon Apr 22 17:11:24 2019
+++ C:/Users/Brian/Documents/CLIENT/sites/all/civicustom/php/CRM/Dedupe/Merger.php Mon Apr 22 17:36:01 2019
@@ -494,19 +494,18 @@
$mainId = (int) $mainId;
$otherId = (int) $otherId;
$multi_value_tables = array_keys(CRM_Dedupe_Merger::getMultiValueCustomSets('cidRefs'));
$sqls = array();
foreach ($affected as $table) {
+ //LCD skip ALL single-value custom tables
// skipping non selected single-value custom table's value migration
- if (!in_array($table, $multi_value_tables)) {
- if ($customTableToCopyFrom !== NULL && in_array($table, $customTables) && !in_array($table, $customTableToCopyFrom)) {
- continue;
- }
+ if (!in_array($table, $multi_value_tables) && in_array($table, $customTables)) {
+ continue;
}
// Call custom processing function for objects that require it
if (isset($cpTables[$table])) {
foreach ($cpTables[$table] as $className => $fnName) {
$className::$fnName($mainId, $otherId, $sqls, $tables, $tableOperations);
}
@@ -1718,15 +1717,16 @@
// get the contact_id -> file_id mapping
$fileIds = array();
$sql = "SELECT entity_id, {$columnName} AS file_id FROM {$tableName} WHERE entity_id IN ({$mainId}, {$otherId})";
$dao = CRM_Core_DAO::executeQuery($sql);
while ($dao->fetch()) {
$fileIds[$dao->entity_id] = $dao->file_id;
- if ($dao->entity_id == $mainId) {
+ //LCD only delete reference if file exists
+ if ($dao->entity_id == $mainId && !empty($fileIds[$mainId])) {
CRM_Core_BAO_File::deleteFileReferences($fileIds[$mainId], $mainId, $customId);
}
}
$dao->free();
// move the other contact's file to main contact
//NYSS need to INSERT or UPDATE depending on whether main contact has an existing record
@@ -1750,14 +1750,19 @@
}
else {
$sql = "
INSERT INTO civicrm_entity_file ( entity_table, entity_id, file_id )
VALUES ( '{$tableName}', {$mainId}, {$fileIds[$otherId]} )";
}
CRM_Core_DAO::executeQuery($sql);
+
+ //LCD clear value from Other record
+ $sql = "UPDATE {$tableName} SET {$columnName} = NULL WHERE entity_id = {$otherId}";
+ CRM_Core_DAO::executeQuery($sql);
+
}
// move view only custom fields CRM-5362
$viewOnlyCustomFields = array();
foreach ($submitted as $key => $value) {
$fid = CRM_Core_BAO_CustomField::getKeyID($key);
if ($fid && array_key_exists($fid, $cFields) && !empty($cFields[$fid]['attributes']['is_view'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment