From c0c9cd8276f94ca6167c74659c5efb09e1b33116 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 5 May 2013 21:36:03 -0700 Subject: [PATCH] Refactor language pseudoconstant CRM-12464 ---------------------------------------- * CRM-12464: Search improvements in 4.4 http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Contact/BAO/Contact.php | 3 +- .../Form/Edit/CommunicationPreferences.php | 5 ++- .../Form/Inline/CommunicationPreferences.php | 3 +- CRM/Contact/Form/Search/Criteria.php | 3 +- CRM/Core/BAO/UFGroup.php | 5 ++- CRM/Core/I18n.php | 2 +- CRM/Core/I18n/PseudoConstant.php | 13 ------- CRM/Core/OptionGroup.php | 35 ++++++++++--------- CRM/Core/PseudoConstant.php | 23 ++---------- CRM/Logging/Differ.php | 9 ++--- CRM/Profile/Selector/Listings.php | 9 ++--- CRM/Utils/PseudoConstant.php | 1 - tests/phpunit/CRM/Core/PseudoConstantTest.php | 18 ++++++++-- xml/schema/Contact/Contact.xml | 4 +++ 14 files changed, 57 insertions(+), 76 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index b6aedf1fc2..7d927fba25 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -2273,8 +2273,7 @@ AND civicrm_openid.is_primary = 1"; // get preferred languages if (!empty($contact->preferred_language)) { - $languages = CRM_Core_PseudoConstant::languages(); - $values['preferred_language'] = CRM_Utils_Array::value($contact->preferred_language, $languages); + $values['preferred_language'] = CRM_Core_PseudoConstant::getValue('CRM_Contact_DAO_Contact', 'preferred_language', $contact->preferred_language); } // Calculating Year difference diff --git a/CRM/Contact/Form/Edit/CommunicationPreferences.php b/CRM/Contact/Form/Edit/CommunicationPreferences.php index 259ea81bdd..8d6f4ffe47 100644 --- a/CRM/Contact/Form/Edit/CommunicationPreferences.php +++ b/CRM/Contact/Form/Edit/CommunicationPreferences.php @@ -83,7 +83,7 @@ class CRM_Contact_Form_Edit_CommunicationPreferences { ts('Preferred Language'), array( '' => ts('- select -')) + - CRM_Core_PseudoConstant::languages() + CRM_Contact_BAO_Contact::buildOptions('preferred_language') ); if (!empty($privacyOptions)) { @@ -162,8 +162,7 @@ class CRM_Contact_Form_Edit_CommunicationPreferences { static function setDefaultValues(&$form, &$defaults) { if (!empty($defaults['preferred_language'])) { - $languages = array_flip(CRM_Core_PseudoConstant::languages()); - $defaults['preferred_language'] = $languages[$defaults['preferred_language']]; + $defaults['preferred_language'] = CRM_Core_PseudoConstant::getKey('CRM_Contact_DAO_Contact', 'preferred_language', $defaults['preferred_language']); } // CRM-7119: set preferred_language to default if unset diff --git a/CRM/Contact/Form/Inline/CommunicationPreferences.php b/CRM/Contact/Form/Inline/CommunicationPreferences.php index 58bc83b341..9fc3ef70b4 100644 --- a/CRM/Contact/Form/Inline/CommunicationPreferences.php +++ b/CRM/Contact/Form/Inline/CommunicationPreferences.php @@ -60,8 +60,7 @@ class CRM_Contact_Form_Inline_CommunicationPreferences extends CRM_Contact_Form_ $defaults = parent::setDefaultValues(); if (!empty($defaults['preferred_language'])) { - $languages = array_flip(CRM_Core_PseudoConstant::languages()); - $defaults['preferred_language'] = $languages[$defaults['preferred_language']]; + $defaults['preferred_language'] = CRM_Core_PseudoConstant::getKey('CRM_Contact_DAO_Contact', 'preferred_language', $defaults['preferred_language']); } // CRM-7119: set preferred_language to default if unset diff --git a/CRM/Contact/Form/Search/Criteria.php b/CRM/Contact/Form/Search/Criteria.php index ac7f0e312a..8af5ba0026 100644 --- a/CRM/Contact/Form/Search/Criteria.php +++ b/CRM/Contact/Form/Search/Criteria.php @@ -254,8 +254,7 @@ class CRM_Contact_Form_Search_Criteria { $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Communication Method')); //CRM-6138 Preferred Language - $langPreff = CRM_Core_PseudoConstant::languages(); - $form->add('select', 'preferred_language', ts('Preferred Language'), array('' => ts('- any -')) + $langPreff); + $form->add('select', 'preferred_language', ts('Preferred Language'), array('' => ts('- any -')) + CRM_Contact_BAO_Contact::buildOptions('preferred_language')); // Phone search $form->addElement('text', 'phone_numeric', ts('Phone Number'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone')); diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index f7e93f1ea5..f998b898a2 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -981,9 +981,8 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { $values[$index] = implode(',', $compref); } elseif ($name === 'preferred_language') { - $languages = CRM_Core_PseudoConstant::languages(); $params[$index] = $details->$name; - $values[$index] = $languages[$details->$name]; + $values[$index] = CRM_Core_PseudoConstant::getValue('CRM_Contact_DAO_Contact', 'preferred_language', $details->$name); } elseif ($name == 'group') { $groups = CRM_Contact_BAO_GroupContact::getContactGroup($cid, 'Added', NULL, FALSE, TRUE); @@ -1929,7 +1928,7 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) $form->add('select', $name, $title, CRM_Core_SelectValues::pmf()); } elseif ($fieldName === 'preferred_language') { - $form->add('select', $name, $title, array('' => ts('- select -')) + CRM_Core_PseudoConstant::languages()); + $form->add('select', $name, $title, array('' => ts('- select -')) + CRM_Contact_BAO_Contact::buildOptions('preferred_language')); } elseif ($fieldName == 'external_identifier') { $form->add('text', $name, $title, $attributes, $required); diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 889d88ae45..d71c916bd7 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -106,7 +106,7 @@ class CRM_Core_I18n { static $enabled = NULL; if (!$all) { - $all = CRM_Core_I18n_PseudoConstant::languages(); + $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); // check which ones are available; add them to $all if not there already $config = CRM_Core_Config::singleton(); diff --git a/CRM/Core/I18n/PseudoConstant.php b/CRM/Core/I18n/PseudoConstant.php index 985b23021a..b96c2bc5ee 100644 --- a/CRM/Core/I18n/PseudoConstant.php +++ b/CRM/Core/I18n/PseudoConstant.php @@ -33,19 +33,6 @@ * */ class CRM_Core_I18n_PseudoConstant { - static function &languages() { - static $languages = NULL; - if ($languages === NULL) { - $rows = array(); - CRM_Core_OptionValue::getValues(array('name' => 'languages'), $rows, 'weight', TRUE); - - $languages = array(); - foreach ($rows as $row) { - $languages[$row['name']] = $row['label']; - } - } - return $languages; - } static function longForShort($short) { $longForShortMapping = self::longForShortMapping(); diff --git a/CRM/Core/OptionGroup.php b/CRM/Core/OptionGroup.php index 08bf0844db..6f4fd7faf9 100644 --- a/CRM/Core/OptionGroup.php +++ b/CRM/Core/OptionGroup.php @@ -100,22 +100,25 @@ class CRM_Core_OptionGroup { static function &values( $name, $flip = FALSE, $grouping = FALSE, $localize = FALSE, $condition = NULL, - $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE + $labelColumnName = 'label', $onlyActive = TRUE, $fresh = FALSE, $keyColumnName = 'value' ) { - $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive); + $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName); - if (array_key_exists($cacheKey, self::$_cache) && !$fresh) { - return self::$_cache[$cacheKey]; - } - - $cache = CRM_Utils_Cache::singleton(); - $var = $cache->get($cacheKey); - if ($var && !$fresh) { - return $var; + if (!$fresh) { + // Fetch from static var + if (array_key_exists($cacheKey, self::$_cache)) { + return self::$_cache[$cacheKey]; + } + // Fetch from main cache + $cache = CRM_Utils_Cache::singleton(); + $var = $cache->get($cacheKey); + if ($var) { + return $var; + } } $query = " -SELECT v.{$labelColumnName} as {$labelColumnName} ,v.value as value, v.grouping as grouping +SELECT v.{$labelColumnName} as {$labelColumnName} ,v.{$keyColumnName} as value, v.grouping as grouping FROM civicrm_option_value v, civicrm_option_group g WHERE v.option_group_id = g.id @@ -133,7 +136,7 @@ WHERE v.option_group_id = g.id $query .= $condition; } - $query .= " ORDER BY v.weight"; + $query .= " ORDER BY v.weight"; $p = array(1 => array($name, 'String')); $dao = CRM_Core_DAO::executeQuery($query, $p); @@ -160,15 +163,15 @@ WHERE v.option_group_id = g.id * @param $labelColumnName * @param $onlyActive */ - protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive) { - $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive); + protected static function flushValues($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName = 'value') { + $cacheKey = self::createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive, $keyColumnName); $cache = CRM_Utils_Cache::singleton(); $cache->delete($cacheKey); unset(self::$_cache[$cacheKey]); } - protected static function createCacheKey($name, $flip, $grouping, $localize, $condition, $labelColumnName, $onlyActive) { - $cacheKey = "CRM_OG_{$name}_{$flip}_{$grouping}_{$localize}_{$condition}_{$labelColumnName}_{$onlyActive}"; + protected static function createCacheKey() { + $cacheKey = "CRM_OG_" . serialize(func_get_args()); return $cacheKey; } diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 82e834fd72..e6c87c75bc 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -260,7 +260,8 @@ class CRM_Core_PseudoConstant { $params['condition'], $params['labelColumn'] ? $params['labelColumn'] : 'label', $params['onlyActive'], - $params['fresh'] + $params['fresh'], + $params['keyColumn'] ? $params['keyColumn'] : 'value' ); } @@ -1592,26 +1593,6 @@ WHERE id = %1 return self::$greetingDefaults; } - /** - * Get all the Languages from database. - * - * @access public - * @static - * - * @return array self::languages - array reference of all languages - * - */ - public static function &languages() { - return CRM_Core_I18n_PseudoConstant::languages(); - } - - /** - * Alias of above - */ - public static function &preferredLanguage() { - return CRM_Core_I18n_PseudoConstant::languages(); - } - /** * Get all extensions * diff --git a/CRM/Logging/Differ.php b/CRM/Logging/Differ.php index c98637ceea..81d73200fd 100644 --- a/CRM/Logging/Differ.php +++ b/CRM/Logging/Differ.php @@ -229,6 +229,7 @@ WHERE log_conn_id = %1 AND if (in_array($table, array_keys($daos))) { // FIXME: these should be populated with pseudo constants as they // were at the time of logging rather than their current values + // FIXME: Use *_BAO:buildOptions() method rather than pseudoconstants & fetch programmatically $values[$table] = array( 'contribution_page_id' => CRM_Contribute_PseudoConstant::contributionPage(), 'contribution_status_id' => CRM_Contribute_PseudoConstant::contributionStatus(), @@ -238,12 +239,12 @@ WHERE log_conn_id = %1 AND 'location_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), 'payment_instrument_id' => CRM_Contribute_PseudoConstant::paymentInstrument(), 'phone_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), - 'preferred_communication_method' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'), - 'preferred_language' => CRM_Core_PseudoConstant::languages(), - 'prefix_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'), + 'preferred_communication_method' => CRM_Contact_BAO_Contact::buildOptions('preferred_communication_method'), + 'preferred_language' => CRM_Contact_BAO_Contact::buildOptions('preferred_language'), + 'prefix_id' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'), 'provider_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), 'state_province_id' => CRM_Core_PseudoConstant::stateProvince(), - 'suffix_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'), + 'suffix_id' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'), 'website_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'), 'activity_type_id' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE), 'case_type_id' => CRM_Case_PseudoConstant::caseType('label', FALSE), diff --git a/CRM/Profile/Selector/Listings.php b/CRM/Profile/Selector/Listings.php index 4c3018f423..2d3aacbea1 100644 --- a/CRM/Profile/Selector/Listings.php +++ b/CRM/Profile/Selector/Listings.php @@ -568,9 +568,6 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR // we need to determine of overlay profile should be shown $showProfileOverlay = CRM_Core_BAO_UFGroup::showOverlayProfile(); - $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); - $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); - $languages = CRM_Core_PseudoConstant::languages(); while ($result->fetch()) { if (isset($result->country)) { // the query returns the untranslated country name @@ -606,7 +603,7 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR ) { $url = CRM_Utils_System::fixURL($result->$name); $typeId = substr($name, 0, -4) . "-website_type_id"; - $typeName = $websiteTypes[$result->$typeId]; + $typeName = CRM_Core_PseudoConstant::getValue('CRM_Core_DAO_Website', 'website_type_id', $result->$typeId); if ($typeName) { $row[] = "{$result->$name} (${typeName})"; } @@ -615,7 +612,7 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR } } elseif ($name == 'preferred_language') { - $row[] = $languages[$result->$name]; + $row[] = CRM_Core_PseudoConstant::getValue('CRM_Contact_DAO_Contact', 'preferred_language', $result->$name); } elseif ($multipleSelectFields && array_key_exists($name, $multipleSelectFields) @@ -642,7 +639,7 @@ class CRM_Profile_Selector_Listings extends CRM_Core_Selector_Base implements CR elseif (strpos($name, '-im')) { if (!empty($result->$name)) { $providerId = $name . "-provider_id"; - $providerName = $imProviders[$result->$providerId]; + $providerName = CRM_Core_PseudoConstant::getValue('CRM_Core_DAO_IM', 'provider_id', $result->$providerId); $row[] = $result->$name . " ({$providerName})"; } else { diff --git a/CRM/Utils/PseudoConstant.php b/CRM/Utils/PseudoConstant.php index d32454646b..92efe7e0d4 100644 --- a/CRM/Utils/PseudoConstant.php +++ b/CRM/Utils/PseudoConstant.php @@ -37,7 +37,6 @@ class CRM_Utils_PseudoConstant { 'CRM_Event_PseudoConstant', 'CRM_Contribute_PseudoConstant', 'CRM_Member_PseudoConstant', - 'CRM_Grant_PseudoConstant', ); /** diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index e0a035a61e..eab4917fa4 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -422,6 +422,11 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { 'sample' => 'Team', 'exclude' => 'Individual', ), + array( + 'fieldName' => 'preferred_language', + 'sample' => array('en_US' => 'English (United States)'), + 'max' => 250, + ), ), 'CRM_Batch_DAO_Batch' => array( array( @@ -497,7 +502,16 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { $this->assertNotEmpty($optionValues, $message); // Ensure sample value is contained in the returned optionValues. - $this->assertContains($field['sample'], $optionValues, $message); + if (!is_array($field['sample'])) { + $this->assertContains($field['sample'], $optionValues, $message); + } + // If sample is an array, we check keys and values + else { + foreach ($field['sample'] as $key => $value) { + $this->assertArrayHasKey($key, $optionValues, $message); + $this->assertEquals(CRM_Utils_Array::value($key, $optionValues), $value, $message); + } + } // Ensure exclude value is not contained in the optionValues if (!empty($field['exclude'])) { @@ -523,7 +537,7 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { 3 => 'Organization', ); // By default this should return an array keyed by name - $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type'); + $result = CRM_Contact_DAO_Contact::buildOptions('contact_type'); $this->assertEquals($byName, $result); // But we can also fetch by ID $result = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'contact_type', array('keyColumn' => 'id', 'labelColumn' => 'name')); diff --git a/xml/schema/Contact/Contact.xml b/xml/schema/Contact/Contact.xml index 4ab58b1cbd..6c90dc3b98 100644 --- a/xml/schema/Contact/Contact.xml +++ b/xml/schema/Contact/Contact.xml @@ -244,6 +244,10 @@ Which language is preferred for communication. FK to languages in civicrm_option_value. /^lang/i true + + languages + name + 3.2 -- 2.25.1