Skip to content

Commit

Permalink
Find state when not in default country
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jun 19, 2024
1 parent ffaecc4 commit 6cc0aa1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
25 changes: 24 additions & 1 deletion CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
Expand Down

0 comments on commit 6cc0aa1

Please sign in to comment.