From 0fbb860e5d0e829ca15737a2027e94be55dc5f61 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 2 Jun 2022 19:13:13 +1200 Subject: [PATCH] Do not rely in init to set whether address parsing is on --- CRM/Contact/Import/Parser/Contact.php | 21 ++++++++++++++++----- CRM/Import/Parser.php | 1 - 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 00b74465f87a..378a14a41686 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -30,7 +30,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { protected $_mapperKeys = []; protected $_allExternalIdentifiers = []; - protected $_parseStreetAddress; /** * Array of successfully imported contact id's @@ -88,6 +87,13 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { */ public $_dedupeRuleGroupID = NULL; + /** + * Addresses that failed to parse. + * + * @var array + */ + private $_unparsedStreetAddressContacts = []; + /** * The initializer code, called before processing. */ @@ -96,8 +102,13 @@ public function init() { foreach ($this->getImportableFieldsMetadata() as $name => $field) { $this->addField($name, $field['title'], CRM_Utils_Array::value('type', $field), CRM_Utils_Array::value('headerPattern', $field), CRM_Utils_Array::value('dataPattern', $field), CRM_Utils_Array::value('hasLocationType', $field)); } + } - $this->_parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE); + /** + * Is street address parsing enabled for the site. + */ + protected function isParseStreetAddress() : bool { + return (bool) (CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options')['street_address_parsing'] ?? FALSE); } /** @@ -529,7 +540,7 @@ private function formatCommonData($params, &$formatted) { } // parse street address, CRM-5450 - if ($this->_parseStreetAddress) { + if ($this->isParseStreetAddress()) { if (array_key_exists('address', $formatted) && is_array($formatted['address'])) { foreach ($formatted['address'] as $instance => & $address) { $streetAddress = $address['street_address'] ?? NULL; @@ -776,7 +787,7 @@ public function createContact(&$formatted, &$contactFields, $onDuplicate, $conta $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); //get the id of the contact whose street address is not parsable, CRM-5886 - if ($this->_parseStreetAddress && property_exists($newContact, 'address') && $newContact->address) { + if ($this->isParseStreetAddress() && property_exists($newContact, 'address') && $newContact->address) { foreach ($newContact->address as $address) { if (!empty($address['street_address']) && (empty($address['street_number']) || empty($address['street_name']))) { $this->_unparsedStreetAddressContacts[] = [ @@ -2171,7 +2182,7 @@ private function checkStatesForCountry(int $countryID, array $possibleStates) { */ protected function getStatus($outcome): string { if ($outcome === CRM_Import_Parser::VALID) { - return empty($this->_parseStreetAddress) ? 'IMPORTED' : 'warning_unparsed_address'; + return empty($this->_unparsedStreetAddressContacts) ? 'IMPORTED' : 'warning_unparsed_address'; } return [ CRM_Import_Parser::DUPLICATE => 'DUPLICATE', diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index f2d6ecbf967a..d25e5e1a55b1 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1542,7 +1542,6 @@ protected function getFieldMappings(): array { return $mappedFields; } - /** * Check if an error in custom data. *