From 6cc0aa1c2c9852ca86e8be8498f8331ef8b9baa8 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 19 Jun 2024 15:31:07 -0400 Subject: [PATCH] Find state when not in default country --- CRM/Import/Parser.php | 25 ++++++++++++++++++- .../CRM/Contact/Import/Parser/ContactTest.php | 15 ++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index dd410fd6d63..da87cfbc49c 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1580,7 +1580,30 @@ protected function getTransformedFieldValue(string $fieldName, $importedValue) { } $comparisonValue = $this->getComparisonValue($importedValue); - return $options[$comparisonValue] ?? 'invalid_import_value'; + $resolvedValue = $options[$comparisonValue] ?? 'invalid_import_value'; + if (in_array($fieldName, ['state_province_id', 'county_id'], TRUE) && $resolvedValue === 'invalid_import_value') { + if ($fieldName === 'state_province_id') { + $stateID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_state_province WHERE name = %1', [1 => [$comparisonValue, 'String']]); + if (!$stateID) { + $stateID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_state_province WHERE abbreviation = %1', [1 => [$comparisonValue, 'String']]); + } + if ($stateID) { + $this->importableFieldsMetadata['state_province_id']['options'][$comparisonValue] = $stateID; + return $stateID; + } + } + if ($fieldName === 'county_id') { + $countyID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_county WHERE name = %1', [1 => [$comparisonValue, 'String']]); + if (!$countyID) { + $countyID = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_county WHERE abbreviation = %1', [1 => [$comparisonValue, 'String']]); + } + if ($countyID) { + $this->importableFieldsMetadata['county_id']['options'][$comparisonValue] = $countyID; + return $countyID; + } + } + } + return $resolvedValue; } // @todo - make this generic - for fields where getOptions doesn't fetch // getOptions does not retrieve these fields with high potential results diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 2984ae3e98a..7f9b06d0e9f 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -330,14 +330,21 @@ public function testImportMasterAddress(): void { */ public function testImportNonDefaultCountryState(): void { \Civi::settings()->set('defaultContactCountry', 1228); - $this->validateCSV('individual_country_state.csv', [ + $csv = 'individual_country_state.csv'; + $mapper = [ ['first_name'], ['last_name'], ['state_province', 'Primary'], ['country', 'Primary'], - ]); - $dataSource = $this->getDataSource(); - $row = $dataSource->getRow(); + ]; + $this->validateCSV($csv, $mapper); + $this->importCSV($csv, $mapper); + $address = Address::get(FALSE) + ->addWhere('country_id.name', '=', 'Canada') + ->addWhere('state_province_id.name', '=', 'Alberta') + ->addSelect('contact_id.display_name') + ->execute()->single(); + $this->assertEquals('bob', $address['contact_id.display_name']); } /**