From b0b40cd7636e8bcb51e38ce1337c077026f4e25d Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 17 Jul 2019 19:36:35 +1200 Subject: [PATCH] Move fetch Relationship details to processor --- CRM/Export/BAO/Export.php | 122 +---------------------------- CRM/Export/BAO/ExportProcessor.php | 120 ++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 120 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index cd1f3547f3..87aba60b69 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -417,124 +417,6 @@ LIMIT $offset, $limit } } - /** - * Get the values of linked household contact. - * - * @param CRM_Core_DAO $relDAO - * @param array $value - * @param string $field - * @param array $row - */ - private static function fetchRelationshipDetails($relDAO, $value, $field, &$row) { - $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); - $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); - $i18n = CRM_Core_I18n::singleton(); - $field = $field . '_'; - - foreach ($value as $relationField => $relationValue) { - if (is_object($relDAO) && property_exists($relDAO, $relationField)) { - $fieldValue = $relDAO->$relationField; - if ($relationField == 'phone_type_id') { - $fieldValue = $phoneTypes[$relationValue]; - } - elseif ($relationField == 'provider_id') { - $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders); - } - // CRM-13995 - elseif (is_object($relDAO) && in_array($relationField, [ - 'email_greeting', - 'postal_greeting', - 'addressee', - ])) { - //special case for greeting replacement - $fldValue = "{$relationField}_display"; - $fieldValue = $relDAO->$fldValue; - } - } - elseif (is_object($relDAO) && $relationField == 'state_province') { - $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id); - } - elseif (is_object($relDAO) && $relationField == 'country') { - $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id); - } - else { - $fieldValue = ''; - } - $relPrefix = $field . $relationField; - - if (is_object($relDAO) && $relationField == 'id') { - $row[$relPrefix] = $relDAO->contact_id; - } - elseif (is_array($relationValue) && $relationField == 'location') { - foreach ($relationValue as $ltype => $val) { - // If the location name has a space in it the we need to handle that. This - // is kinda hacky but specifically covered in the ExportTest so later efforts to - // improve it should be secure in the knowled it will be caught. - $ltype = str_replace(' ', '_', $ltype); - foreach (array_keys($val) as $fld) { - $type = explode('-', $fld); - $fldValue = "{$ltype}-" . $type[0]; - if (!empty($type[1])) { - $fldValue .= "-" . $type[1]; - } - // CRM-3157: localise country, region (both have ‘country’ context) - // and state_province (‘province’ context) - switch (TRUE) { - case (!is_object($relDAO)): - $row[$field . '_' . $fldValue] = ''; - break; - - case in_array('country', $type): - case in_array('world_region', $type): - $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue, - ['context' => 'country'] - ); - break; - - case in_array('state_province', $type): - $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue, - ['context' => 'province'] - ); - break; - - default: - $row[$field . '_' . $fldValue] = $relDAO->$fldValue; - break; - } - } - } - } - elseif (isset($fieldValue) && $fieldValue != '') { - //check for custom data - if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) { - $row[$relPrefix] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID); - } - else { - //normal relationship fields - // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context) - switch ($relationField) { - case 'country': - case 'world_region': - $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'country']); - break; - - case 'state_province': - $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'province']); - break; - - default: - $row[$relPrefix] = $fieldValue; - break; - } - } - } - else { - // if relation field is empty or null - $row[$relPrefix] = ''; - } - } - } - /** * Get the ids that we want to get related contact details for. * @@ -634,10 +516,10 @@ LIMIT $offset, $limit $relationQuery->convertToPseudoNames($allRelContactDAO); $row = []; // @todo pass processor to fetchRelationshipDetails and set fields directly within it. - self::fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row); + $processor->fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row); foreach (array_keys($relationReturnProperties) as $property) { if ($property === 'location') { - // @todo - simplify location in self::fetchRelationshipDetails - remove handling here. Or just call + // @todo - simplify location in fetchRelationshipDetails - remove handling here. Or just call // $processor->setRelationshipValue from fetchRelationshipDetails foreach ($relationReturnProperties['location'] as $locationName => $locationValues) { foreach (array_keys($locationValues) as $locationValue) { diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 674e404d5e..30bd8e1f7d 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -2204,4 +2204,124 @@ WHERE id IN ( $deleteIDString ) $this->setTemporaryTable($exportTempTable->getName()); } + /** + * Get the values of linked household contact. + * + * @param CRM_Core_DAO $relDAO + * @param array $value + * @param string $field + * @param array $row + * + * @throws \Exception + */ + public function fetchRelationshipDetails($relDAO, $value, $field, &$row) { + $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); + $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); + $i18n = CRM_Core_I18n::singleton(); + $field = $field . '_'; + + foreach ($value as $relationField => $relationValue) { + if (is_object($relDAO) && property_exists($relDAO, $relationField)) { + $fieldValue = $relDAO->$relationField; + if ($relationField == 'phone_type_id') { + $fieldValue = $phoneTypes[$relationValue]; + } + elseif ($relationField == 'provider_id') { + $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders); + } + // CRM-13995 + elseif (is_object($relDAO) && in_array($relationField, [ + 'email_greeting', + 'postal_greeting', + 'addressee', + ])) { + //special case for greeting replacement + $fldValue = "{$relationField}_display"; + $fieldValue = $relDAO->$fldValue; + } + } + elseif (is_object($relDAO) && $relationField == 'state_province') { + $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id); + } + elseif (is_object($relDAO) && $relationField == 'country') { + $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id); + } + else { + $fieldValue = ''; + } + $relPrefix = $field . $relationField; + + if (is_object($relDAO) && $relationField == 'id') { + $row[$relPrefix] = $relDAO->contact_id; + } + elseif (is_array($relationValue) && $relationField == 'location') { + foreach ($relationValue as $ltype => $val) { + // If the location name has a space in it the we need to handle that. This + // is kinda hacky but specifically covered in the ExportTest so later efforts to + // improve it should be secure in the knowled it will be caught. + $ltype = str_replace(' ', '_', $ltype); + foreach (array_keys($val) as $fld) { + $type = explode('-', $fld); + $fldValue = "{$ltype}-" . $type[0]; + if (!empty($type[1])) { + $fldValue .= "-" . $type[1]; + } + // CRM-3157: localise country, region (both have ‘country’ context) + // and state_province (‘province’ context) + switch (TRUE) { + case (!is_object($relDAO)): + $row[$field . '_' . $fldValue] = ''; + break; + + case in_array('country', $type): + case in_array('world_region', $type): + $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue, + ['context' => 'country'] + ); + break; + + case in_array('state_province', $type): + $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue, + ['context' => 'province'] + ); + break; + + default: + $row[$field . '_' . $fldValue] = $relDAO->$fldValue; + break; + } + } + } + } + elseif (isset($fieldValue) && $fieldValue != '') { + //check for custom data + if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) { + $row[$relPrefix] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID); + } + else { + //normal relationship fields + // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context) + switch ($relationField) { + case 'country': + case 'world_region': + $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'country']); + break; + + case 'state_province': + $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'province']); + break; + + default: + $row[$relPrefix] = $fieldValue; + break; + } + } + } + else { + // if relation field is empty or null + $row[$relPrefix] = ''; + } + } + } + } -- 2.25.1