Skip to content

Commit

Permalink
Merge pull request #14144 from eileenmcnaughton/dedupe4
Browse files Browse the repository at this point in the history
dev/core#723 [REF] extract chunk of code that definitely does something.
  • Loading branch information
eileenmcnaughton authored Apr 28, 2019
2 parents 07d7847 + 830ee2b commit ee22e17
Showing 1 changed file with 109 additions and 91 deletions.
200 changes: 109 additions & 91 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1603,98 +1603,9 @@ public static function moveAllBelongings($mainId, $otherId, $migrationInfo, $che
if (!isset($submitted)) {
$submitted = [];
}
$customFiles = [];
foreach ($submitted as $key => $value) {
if (substr($key, 0, 7) == 'custom_') {
$fid = (int) substr($key, 7);
if (empty($cFields[$fid])) {
continue;
}
$htmlType = $cFields[$fid]['attributes']['html_type'];
switch ($htmlType) {
case 'File':
$customFiles[] = $fid;
unset($submitted["custom_$fid"]);
break;

case 'Select Country':
case 'Select State/Province':
$submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
break;

case 'Select Date':
if ($cFields[$fid]['attributes']['is_view']) {
$submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
}
break;

case 'CheckBox':
case 'Multi-Select':
case 'Multi-Select Country':
case 'Multi-Select State/Province':
// Merge values from both contacts for multivalue fields, CRM-4385
// get the existing custom values from db.
$customParams = ['entityID' => $mainId, $key => TRUE];
$customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
if (!empty($customfieldValues[$key])) {
$existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
if (is_array($existingValue) && !empty($existingValue)) {
$mergeValue = $submittedCustomFields = [];
if ($value == 'null') {
// CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
$submitted[$key] = $value;
}
else {
if ($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 ($submittedCustomFields as $k => $v) {
if ($v != '' && !in_array($v, $mergeValue)) {
$mergeValue[] = $v;
}
}

//keep state and country as array format.
//for checkbox and m-select format w/ VALUE_SEPARATOR
if (in_array($htmlType, [
'CheckBox',
'Multi-Select',
])) {
$submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
$mergeValue
) . CRM_Core_DAO::VALUE_SEPARATOR;
}
else {
$submitted[$key] = $mergeValue;
}
}
}
}
elseif (in_array($htmlType, [
'Multi-Select Country',
'Multi-Select State/Province',
])) {
//we require submitted values should be in array format
if ($value) {
$mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
//hack to remove null values from array.
$mergeValue = [];
foreach ($mergeValueArray as $k => $v) {
if ($v != '') {
$mergeValue[] = $v;
}
}
$submitted[$key] = $mergeValue;
}
}
break;

default:
break;
}
}
list($cFields, $customFiles, $submitted) = self::processCustomFields($mainId, $key, $cFields, $customFiles, $submitted, $value);
}

// **** Do file custom fields related migrations
Expand Down Expand Up @@ -2317,6 +2228,113 @@ protected static function swapOutFieldsAffectedByQFZeroBug(&$migrationInfo) {
}
}

/**
* Honestly - what DOES this do - hopefully some refactoring will reveal it's purpose.
*
* @param $mainId
* @param $key
* @param $cFields
* @param $customFiles
* @param $submitted
* @param $value
*
* @return array
*/
protected static function processCustomFields($mainId, $key, $cFields, $customFiles, $submitted, $value) {
if (substr($key, 0, 7) == 'custom_') {
$fid = (int) substr($key, 7);
if (empty($cFields[$fid])) {
return [$cFields, $customFiles, $submitted];
}
$htmlType = $cFields[$fid]['attributes']['html_type'];
switch ($htmlType) {
case 'File':
$customFiles[] = $fid;
unset($submitted["custom_$fid"]);
break;

case 'Select Country':
case 'Select State/Province':
$submitted[$key] = CRM_Core_BAO_CustomField::displayValue($value, $fid);
break;

case 'Select Date':
if ($cFields[$fid]['attributes']['is_view']) {
$submitted[$key] = date('YmdHis', strtotime($submitted[$key]));
}
break;

case 'CheckBox':
case 'Multi-Select':
case 'Multi-Select Country':
case 'Multi-Select State/Province':
// Merge values from both contacts for multivalue fields, CRM-4385
// get the existing custom values from db.
$customParams = ['entityID' => $mainId, $key => TRUE];
$customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams);
if (!empty($customfieldValues[$key])) {
$existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]);
if (is_array($existingValue) && !empty($existingValue)) {
$mergeValue = $submittedCustomFields = [];
if ($value == 'null') {
// CRM-19074 if someone has deliberately chosen to overwrite with 'null', respect it.
$submitted[$key] = $value;
}
else {
if ($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 ($submittedCustomFields as $k => $v) {
if ($v != '' && !in_array($v, $mergeValue)) {
$mergeValue[] = $v;
}
}

//keep state and country as array format.
//for checkbox and m-select format w/ VALUE_SEPARATOR
if (in_array($htmlType, [
'CheckBox',
'Multi-Select',
])) {
$submitted[$key] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
$mergeValue
) . CRM_Core_DAO::VALUE_SEPARATOR;
}
else {
$submitted[$key] = $mergeValue;
}
}
}
}
elseif (in_array($htmlType, [
'Multi-Select Country',
'Multi-Select State/Province',
])) {
//we require submitted values should be in array format
if ($value) {
$mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value);
//hack to remove null values from array.
$mergeValue = [];
foreach ($mergeValueArray as $k => $v) {
if ($v != '') {
$mergeValue[] = $v;
}
}
$submitted[$key] = $mergeValue;
}
}
break;

default:
break;
}
}
return [$cFields, $customFiles, $submitted];
}

/**
* Get metadata for the custom fields for the merge.
*
Expand Down

0 comments on commit ee22e17

Please sign in to comment.