Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-21769: Show 'unsupported locale for parsing' warning only when enabling address parsing #11672

Merged
merged 6 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions CRM/Admin/Form/Preferences/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,29 @@ public function postProcess() {
}

$this->_params = $this->controller->exportValues($this->_name);
$addressOptions = CRM_Core_OptionGroup::values('address_options', TRUE);

// check if county option has been set
$options = CRM_Core_OptionGroup::values('address_options', FALSE, FALSE, TRUE);
foreach ($options as $key => $title) {
if ($title == ts('County')) {
// check if the $key is present in $this->_params
if (isset($this->_params['address_options']) &&
!empty($this->_params['address_options'][$key])
) {
// print a status message to the user if county table seems small
$countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
if ($countyCount < 10) {
CRM_Core_Session::setStatus(ts('You have enabled the County option. Please ensure you populate the county table in your CiviCRM Database. You can find extensions to populate counties in the <a %1>CiviCRM Extensions Directory</a>.', array(1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', array('reset' => 1), TRUE, 'extensions-addnew') . '"')),
ts('Populate counties'),
"info"
);
}
}
if (CRM_Utils_Array::value($addressOptions['County'], $this->_params['address_options'])) {
$countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county");
if ($countyCount < 10) {
CRM_Core_Session::setStatus(ts('You have enabled the County option. Please ensure you populate the county table in your CiviCRM Database. You can find extensions to populate counties in the <a %1>CiviCRM Extensions Directory</a>.', array(1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', array('reset' => 1), TRUE, 'extensions-addnew') . '"')),
ts('Populate counties'),
"info"
);
}
}

// check that locale supports address parsing
if (
CRM_Utils_Array::value($addressOptions['Street Address Parsing'], $this->_params['address_options']) &&
!CRM_Core_BAO_Address::isSupportedParsingLocale()
) {
$config = CRM_Core_Config::singleton();
$locale = $config->lcMessages;
CRM_Core_Session::setStatus(ts('Default locale (%1) does not support street parsing. en_US locale will be used instead.', [1 => $locale]), ts('Unsupported Locale'), 'alert');
}

$this->postProcessCommon();
}

Expand Down
52 changes: 35 additions & 17 deletions CRM/Core/BAO/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,25 +714,11 @@ public static function addressSequence() {
* parsed fields values.
*/
public static function parseStreetAddress($streetAddress, $locale = NULL) {
$config = CRM_Core_Config::singleton();

/* locales supported include:
* en_US - http://pe.usps.com/cpim/ftp/pubs/pub28/pub28.pdf
* en_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp
* fr_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-f.asp
* NB: common use of comma after street number also supported
* default is en_US
*/

$supportedLocalesForParsing = array('en_US', 'en_CA', 'fr_CA');
if (!$locale) {
$locale = $config->lcMessages;
}
// as different locale explicitly requested but is not available, display warning message and set $locale = 'en_US'
if (!in_array($locale, $supportedLocalesForParsing)) {
CRM_Core_Session::setStatus(ts('Unsupported locale specified to parseStreetAddress: %1. Proceeding with en_US locale.', array(1 => $locale)), ts('Unsupported Locale'), 'alert');
// use 'en_US' for address parsing if the requested locale is not supported.
if (!self::isSupportedParsingLocale($locale)) {
$locale = 'en_US';
}

$emptyParseFields = $parseFields = array(
'street_name' => '',
'street_unit' => '',
Expand Down Expand Up @@ -876,6 +862,38 @@ public static function parseStreetAddress($streetAddress, $locale = NULL) {
return $parseFields;
}

/**
* Determines if the specified locale is
* supported by address parsing.
* If no locale is specified then it
* will check the default configured locale.
*
* locales supported include:
* en_US - http://pe.usps.com/cpim/ftp/pubs/pub28/pub28.pdf
* en_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp
* fr_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-f.asp
* NB: common use of comma after street number also supported
*
* @param string $locale
* The locale to be checked
*
* @return bool
*/
public static function isSupportedParsingLocale($locale = NULL) {
if (!$locale) {
$config = CRM_Core_Config::singleton();
$locale = $config->lcMessages;
}

$parsingSupportedLocales = array('en_US', 'en_CA', 'fr_CA');

if (in_array($locale, $parsingSupportedLocales)) {
return TRUE;
}

return FALSE;
}

/**
* Validate the address fields based on the address options enabled.
* in the Address Settings
Expand Down
67 changes: 67 additions & 0 deletions CRM/Utils/Check/Component/AddressParsing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Utils_Check_Component_AddressParsing extends CRM_Utils_Check_Component {

public static function checkLocaleSupportsAddressParsing() {

$addressOptions = CRM_Core_BAO_Setting::valueOptions(
CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'address_options'
);

$messages = [];

if ($addressOptions['street_address_parsing']) {
if (!CRM_Core_BAO_Address::isSupportedParsingLocale()) {
$config = CRM_Core_Config::singleton();
$messages[] = new CRM_Utils_Check_Message(
__FUNCTION__,
ts(
'<a href=' .
CRM_Utils_System::url('civicrm/admin/setting/preferences/address', 'reset=1') .
'">Street address parsing</a> is enabled but not supported by <a href="' .
CRM_Utils_System::url('civicrm/admin/setting/localization', 'reset=1') .
'">your locale</a> (%1).',
[1 => $config->lcMessages]
),
ts('Street address parsing'),
\Psr\Log\LogLevel::WARNING,
'fa-address-card'
);
}
}

return $messages;
}

}
51 changes: 51 additions & 0 deletions tests/phpunit/CRM/Core/BAO/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,57 @@ public function testParseStreetAddress() {
$this->assertNotContains('street_number_suffix', $parsedStreetAddress);
}

/**
* @dataProvider supportedAddressParsingLocales
*/
public function testIsSupportedByAddressParsingReturnTrueForSupportedLocales($locale) {
$isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale);
$this->assertTrue($isSupported);
}

/**
* @dataProvider supportedAddressParsingLocales
*/
public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLocales($locale) {
CRM_Core_Config::singleton()->lcMessages = $locale;
$isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale();
$this->assertTrue($isSupported);

}

public function supportedAddressParsingLocales() {
return array(
array('en_US'),
array('en_CA'),
array('fr_CA'),
);
}

/**
* @dataProvider sampleOFUnsupportedAddressParsingLocales
*/
public function testIsSupportedByAddressParsingReturnFalseForUnSupportedLocales($locale) {
$isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale);
$this->assertFalse($isNotSupported);
}

/**
* @dataProvider sampleOFUnsupportedAddressParsingLocales
*/
public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultLocales($locale) {
CRM_Core_Config::singleton()->lcMessages = $locale;
$isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale();
$this->assertFalse($isNotSupported);
}

public function sampleOFUnsupportedAddressParsingLocales() {
return array(
array('en_GB'),
array('af_ZA'),
array('da_DK'),
);
}

/**
* CRM-21214 - Ensure all child addresses are updated correctly - 1.
* 1. First, create three contacts: A, B, and C
Expand Down