From 2761a9164b6d09f215c39d3300212b07c7f375ac Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 10 Jul 2019 13:42:39 +1200 Subject: [PATCH] [REF] partial cleanup on adding fields to returnProperties based on usage --- CRM/Export/BAO/Export.php | 26 +++-------- CRM/Export/BAO/ExportProcessor.php | 74 +++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 19684679a3..ad62043964 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -179,34 +179,22 @@ class CRM_Export_BAO_Export { $exportParams['postal_mailing_export']['postal_mailing_export'] == 1 ); - $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly); + $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress); if ($moreReturnProperties) { $processor->setAdditionalRequestedReturnProperties($moreReturnProperties); } $returnProperties = $processor->getReturnProperties(); $paymentTableId = $processor->getPaymentTableID(); - if ($mergeSameAddress) { + if ($processor->isMergeSameAddress()) { //make sure the addressee fields are selected //while using merge same address feature - $returnProperties['addressee'] = 1; - $returnProperties['postal_greeting'] = 1; - $returnProperties['email_greeting'] = 1; - $returnProperties['street_name'] = 1; - $returnProperties['household_name'] = 1; - $returnProperties['street_address'] = 1; - $returnProperties['city'] = 1; - $returnProperties['state_province'] = 1; - // some columns are required for assistance incase they are not already present - $exportParams['merge_same_address']['temp_columns'] = []; - $tempColumns = ['id', 'master_id', 'state_province_id', 'postal_greeting_id', 'addressee_id']; - foreach ($tempColumns as $column) { - if (!array_key_exists($column, $returnProperties)) { - $returnProperties[$column] = 1; - $column = $column == 'id' ? 'civicrm_primary_id' : $column; - $exportParams['merge_same_address']['temp_columns'][$column] = 1; - } + $exportParams['merge_same_address']['temp_columns'] = $processor->getAdditionalFieldsForSameAddressMerge(); + // This is silly - we should do this at the point when the array is used... + if (isset($exportParams['merge_same_address']['temp_columns']['id'])) { + unset($exportParams['merge_same_address']['temp_columns']['id']); + $exportParams['merge_same_address']['temp_columns']['civicrm_primary_id'] = 1; } } diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 909782e761..a587847921 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -79,6 +79,62 @@ class CRM_Export_BAO_ExportProcessor { */ protected $isMergeSameHousehold; + /** + * Should contacts with the same address be merged. + * + * @var bool + */ + protected $isMergeSameAddress = FALSE; + + /** + * Fields that need to be retrieved for address merge purposes but should not be in output. + * + * @var array + */ + protected $additionalFieldsForSameAddressMerge = []; + + /** + * Get additional non-visible fields for address merge purposes. + * + * @return array + */ + public function getAdditionalFieldsForSameAddressMerge(): array { + return $this->additionalFieldsForSameAddressMerge; + } + + /** + * Set additional non-visible fields for address merge purposes. + */ + public function setAdditionalFieldsForSameAddressMerge() { + if ($this->isMergeSameAddress) { + $fields = ['id', 'master_id', 'state_province_id', 'postal_greeting_id', 'addressee_id']; + foreach ($fields as $index => $field) { + if (!empty($this->getReturnProperties()[$field])) { + unset($fields[$index]); + } + } + $this->additionalFieldsForSameAddressMerge = array_fill_keys($fields, 1); + } + } + + /** + * Should contacts with the same address be merged. + * + * @return bool + */ + public function isMergeSameAddress(): bool { + return $this->isMergeSameAddress; + } + + /** + * Set same address is to be merged. + * + * @param bool $isMergeSameAddress + */ + public function setIsMergeSameAddress(bool $isMergeSameAddress) { + $this->isMergeSameAddress = $isMergeSameAddress; + } + /** * Only export contacts that can receive postal mail. * @@ -209,8 +265,9 @@ class CRM_Export_BAO_ExportProcessor { * @param string $queryOperator * @param bool $isMergeSameHousehold * @param bool $isPostalableOnly + * @param bool $isMergeSameAddress */ - public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE, $isPostalableOnly = FALSE) { + public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE, $isPostalableOnly = FALSE, $isMergeSameAddress = FALSE) { $this->setExportMode($exportMode); $this->setQueryMode(); $this->setQueryOperator($queryOperator); @@ -218,7 +275,9 @@ class CRM_Export_BAO_ExportProcessor { $this->setRelationshipTypes(); $this->setIsMergeSameHousehold($isMergeSameHousehold); $this->setIsPostalableOnly($isPostalableOnly); + $this->setIsMergeSameAddress($isMergeSameAddress); $this->setReturnProperties($this->determineReturnProperties()); + $this->setAdditionalFieldsForSameAddressMerge(); } /** @@ -253,7 +312,7 @@ class CRM_Export_BAO_ExportProcessor { * @return array */ public function getReturnProperties() { - return array_merge($this->returnProperties, $this->getAdditionalRequestedReturnProperties()); + return array_merge($this->returnProperties, $this->getAdditionalRequestedReturnProperties(), $this->getAdditionalFieldsForSameAddressMerge()); } /** @@ -1521,6 +1580,17 @@ class CRM_Export_BAO_ExportProcessor { else { $returnProperties = $this->getDefaultReturnProperties(); } + if ($this->isMergeSameAddress()) { + $returnProperties['addressee'] = 1; + $returnProperties['postal_greeting'] = 1; + $returnProperties['email_greeting'] = 1; + $returnProperties['street_name'] = 1; + $returnProperties['household_name'] = 1; + $returnProperties['street_address'] = 1; + $returnProperties['city'] = 1; + $returnProperties['state_province'] = 1; + + } return $returnProperties; } -- 2.25.1