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

dev/core#723 [REF] extract chunk of code that definitely does something. #14144

Merged
merged 1 commit into from
Apr 28, 2019
Merged
Changes from all 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
200 changes: 109 additions & 91 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1684,98 +1684,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 @@ -2398,6 +2309,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