Skip to content

Commit

Permalink
[REF] extract part of the function to build the location blocks
Browse files Browse the repository at this point in the history
Who hates the variable 'moniker'?
  • Loading branch information
eileenmcnaughton committed Oct 23, 2019
1 parent 90c7c74 commit a688241
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,52 +1130,14 @@ public static function getRowsElementsAndInfo($mainId, $otherId, $checkPermissio
// Set up useful information about the location blocks
$locationBlocks = self::getLocationBlockInfo();

$locations = [
'main' => [],
'other' => [],
];

// @todo This could probably be defined and used earlier
$mergeTargets = [
'main' => $mainId,
'other' => $otherId,
];
$locations = ['main' => [], 'other' => []];

foreach ($locationBlocks as $blockName => $blockInfo) {

// Collect existing fields from both 'main' and 'other' contacts first
// This allows us to match up location/types when building the table rows
foreach ($mergeTargets as $moniker => $cid) {
$searchParams = [
'contact_id' => $cid,
// CRM-17556 Order by field-specific criteria
'options' => [
'sort' => $blockInfo['sortString'],
],
];
$values = civicrm_api3($blockName, 'get', $searchParams);
if ($values['count']) {
$cnt = 0;
foreach ($values['values'] as $value) {
$locations[$moniker][$blockName][$cnt] = $value;
// Fix address display
if ($blockName == 'address') {
// For performance avoid geocoding while merging https://issues.civicrm.org/jira/browse/CRM-21786
// we can expect existing geocode values to be retained.
$value['skip_geocode'] = TRUE;
CRM_Core_BAO_Address::fixAddress($value);
unset($value['skip_geocode']);
$locations[$moniker][$blockName][$cnt]['display'] = CRM_Utils_Address::format($value);
}
// Fix email display
elseif ($blockName == 'email') {
$locations[$moniker][$blockName][$cnt]['display'] = CRM_Utils_Mail::format($value);
}

$cnt++;
}
}
}
$locations['main'] = self::buildLocationBlockForContact($mainId, $blockInfo, $blockName);
$locations['other'] = self::buildLocationBlockForContact($otherId, $blockInfo, $blockName);

// Now, build the table rows appropriately, based off the information on
// the 'other' contact
Expand Down Expand Up @@ -2565,4 +2527,49 @@ private static function getFieldValueAndLabel($field, $contact): array {
return [$label, $value];
}

/**
* Build up the location block for the contact in dedupe-screen display format.
*
* @param integer $cid
* @param array $blockInfo
* @param string $blockName
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/
private static function buildLocationBlockForContact($cid, $blockInfo, $blockName): array {
$searchParams = [
'contact_id' => $cid,
// CRM-17556 Order by field-specific criteria
'options' => [
'sort' => $blockInfo['sortString'],
],
];
$locationBlock = [];
$values = civicrm_api3($blockName, 'get', $searchParams);
if ($values['count']) {
$cnt = 0;
foreach ($values['values'] as $value) {
$locationBlock[$blockName][$cnt] = $value;
// Fix address display
if ($blockName == 'address') {
// For performance avoid geocoding while merging https://issues.civicrm.org/jira/browse/CRM-21786
// we can expect existing geocode values to be retained.
$value['skip_geocode'] = TRUE;
CRM_Core_BAO_Address::fixAddress($value);
unset($value['skip_geocode']);
$locationBlock[$blockName][$cnt]['display'] = CRM_Utils_Address::format($value);
}
// Fix email display
elseif ($blockName == 'email') {
$locationBlock[$blockName][$cnt]['display'] = CRM_Utils_Mail::format($value);
}

$cnt++;
}
}
return $locationBlock;
}

}

0 comments on commit a688241

Please sign in to comment.