From 8ff43cf2314dfbaccd4cb3b5d2ba5db9ab50e902 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 24 Jul 2017 15:40:26 +1200 Subject: [PATCH] Towards CRM-19888 extract code that resolves defaults --- CRM/Contact/BAO/Contact.php | 82 +++++++++++++++++++++++-------------- CRM/Utils/Array.php | 4 +- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 56599265fd..93275ced6e 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -535,6 +535,48 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); } + /** + * Resolve a state province string (UT or Utah) to an ID. + * + * If country has been passed in we should select a state belonging to that country. + * + * Alternatively we should choose from enabled countries, prioritising the default country. + * + * @param array $values + * @param int|NULL $countryID + * + * @return int|null + */ + protected static function resolveStateProvinceID($values, $countryID) { + + if ($countryID) { + $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($countryID); + } + else { + $stateProvinceList = CRM_Core_PseudoConstant::stateProvince(); + } + if (CRM_Utils_Array::lookupValue($values, + 'state_province', + $stateProvinceList, + TRUE + )) { + return $values['state_province_id']; + } + + if (!empty($countryID)) { + $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($countryID, 'abbreviation'); + } + else { + $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceAbbreviation(); + } + CRM_Utils_Array::lookupValue($values, + 'state_province', + $stateProvinceList, + TRUE + ); + return CRM_Utils_Array::value('state_province_id', $values); + } + /** * Create last viewed link to recently updated contact. * @@ -579,10 +621,15 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); /** * Get the values for pseudoconstants for name->value and reverse. * + * @deprecated + * + * This is called specifically from the contact import parser & should be moved there + * as it is not truly a generic function. + * * @param array $defaults * (reference) the default values, some of which need to be resolved. * @param bool $reverse - * True if we want to resolve the values in the reverse direction (value -> name). + * Always true as this function is only called from one place.. */ public static function resolveDefaults(&$defaults, $reverse = FALSE) { // Hack for birth_date. @@ -638,36 +685,9 @@ WHERE civicrm_contact.id = " . CRM_Utils_Type::escape($id, 'Integer'); $reverse ); } - - // CRM-7597 - // if we find a country id above, we need to restrict it to that country - // rather than the list of all countries - - if (!empty($values['country_id'])) { - $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id']); - } - else { - $stateProvinceList = CRM_Core_PseudoConstant::stateProvince(); - } - if (!CRM_Utils_Array::lookupValue($values, - 'state_province', - $stateProvinceList, - $reverse - ) && - $reverse - ) { - - if (!empty($values['country_id'])) { - $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceForCountry($values['country_id'], 'abbreviation'); - } - else { - $stateProvinceList = CRM_Core_PseudoConstant::stateProvinceAbbreviation(); - } - CRM_Utils_Array::lookupValue($values, - 'state_province', - $stateProvinceList, - $reverse - ); + $stateProvinceID = self::resolveStateProvinceID($values, CRM_Utils_Array::value('country_id', $values), $reverse); + if ($stateProvinceID) { + $values['state_province_id'] = $stateProvinceID; } if (!empty($values['state_province_id'])) { diff --git a/CRM/Utils/Array.php b/CRM/Utils/Array.php index 7f17ae14cc..4adc3598d3 100644 --- a/CRM/Utils/Array.php +++ b/CRM/Utils/Array.php @@ -392,9 +392,7 @@ class CRM_Utils_Array { /** * Convert associative array names to values and vice-versa. * - * This function is used by both the web form layer and the api. Note that - * the api needs the name => value conversion, also the view layer typically - * requires value => name conversion + * This function is used by by import functions and some webforms. * * @param array $defaults * @param string $property -- 2.25.1