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

[REF] minor extraction with code to build dedupe arrays #15519

Merged
merged 1 commit into from
Oct 16, 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
89 changes: 52 additions & 37 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,43 +1092,7 @@ public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissio
continue;
}
foreach (['main' => $main, 'other' => $other] as $moniker => $contact) {
$value = $label = CRM_Utils_Array::value($field, $contact);
$fieldSpec = $fields[$field];
if (!empty($fieldSpec['serialize']) && is_array($value)) {
// In practice this only applies to preferred_communication_method as the sub types are skipped above
// and no others are serialized.
$labels = [];
foreach ($value as $individualValue) {
$labels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $field, $individualValue);
}
$label = implode(', ', $labels);
// We serialize this due to historic handling but it's likely that if we just left it as an
// array all would be well & we would have less code.
$value = CRM_Core_DAO::serializeField($value, $fieldSpec['serialize']);
}
elseif (!empty($fieldSpec['type']) && $fieldSpec['type'] == CRM_Utils_Type::T_DATE) {
if ($value) {
$value = str_replace('-', '', $value);
$label = CRM_Utils_Date::customFormat($label);
}
else {
$value = "null";
}
}
elseif (!empty($fields[$field]['type']) && $fields[$field]['type'] == CRM_Utils_Type::T_BOOLEAN) {
if ($label === '0') {
$label = ts('[ ]');
}
if ($label === '1') {
$label = ts('[x]');
}
}
elseif (!empty($fieldSpec['pseudoconstant'])) {
$label = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $field, $value);
}
elseif ($field == 'current_employer_id' && !empty($value)) {
$label = "$value (" . CRM_Contact_BAO_Contact::displayName($value) . ")";
}
list($label, $value) = self::getFieldValueAndLabel($field, $contact);
$rows["move_$field"][$moniker] = $label;
if ($moniker == 'other') {
//CRM-14334
Expand Down Expand Up @@ -2550,4 +2514,55 @@ protected static function ignoredFields(): array {
return $keysToIgnore;
}

/**
* Get the field value & label for the given field.
*
* @param $field
* @param $contact
*
* @return array
* @throws \Exception
*/
private static function getFieldValueAndLabel($field, $contact): array {
$fields = self::getMergeFieldsMetadata();
$value = $label = CRM_Utils_Array::value($field, $contact);
$fieldSpec = $fields[$field];
if (!empty($fieldSpec['serialize']) && is_array($value)) {
// In practice this only applies to preferred_communication_method as the sub types are skipped above
// and no others are serialized.
$labels = [];
foreach ($value as $individualValue) {
$labels[] = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $field, $individualValue);
}
$label = implode(', ', $labels);
// We serialize this due to historic handling but it's likely that if we just left it as an
// array all would be well & we would have less code.
$value = CRM_Core_DAO::serializeField($value, $fieldSpec['serialize']);
}
elseif (!empty($fieldSpec['type']) && $fieldSpec['type'] == CRM_Utils_Type::T_DATE) {
if ($value) {
$value = str_replace('-', '', $value);
$label = CRM_Utils_Date::customFormat($label);
}
else {
$value = "null";
}
}
elseif (!empty($fields[$field]['type']) && $fields[$field]['type'] == CRM_Utils_Type::T_BOOLEAN) {
if ($label === '0') {
$label = ts('[ ]');
}
if ($label === '1') {
$label = ts('[x]');
}
}
elseif (!empty($fieldSpec['pseudoconstant'])) {
$label = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $field, $value);
}
elseif ($field == 'current_employer_id' && !empty($value)) {
$label = "$value (" . CRM_Contact_BAO_Contact::displayName($value) . ")";
}
return [$label, $value];
}

}