From bd44e0df9eab07e1bb406cb320e970a35abd6a45 Mon Sep 17 00:00:00 2001 From: Allen Shaw Date: Thu, 2 May 2013 16:36:32 -0700 Subject: [PATCH] Refactored out of CRM_Core_PseudoConstant: ufGroup(), currencySymbols(). CRM-12464 ---------------------------------------- * CRM-12464: Search improvements in 4.4 http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/ACL/Form/ACL.php | 2 +- CRM/ACL/Page/ACL.php | 2 +- CRM/Contribute/BAO/Query.php | 4 +- CRM/Core/BAO/UFGroup.php | 6 +- CRM/Core/Config/Variables.php | 4 +- CRM/Core/Permission.php | 2 +- CRM/Core/PseudoConstant.php | 59 ++------- CRM/Profile/Form.php | 2 +- CRM/Report/Form/Grant/Statistics.php | 2 +- CRM/UF/Page/Group.php | 2 +- CRM/Utils/Money.php | 4 +- tests/phpunit/CRM/Core/PseudoConstantTest.php | 124 +++++++++++++++++- xml/schema/Contribute/Contribution.xml | 7 +- xml/schema/Contribute/ContributionPage.xml | 7 +- xml/schema/Contribute/ContributionRecur.xml | 5 + xml/schema/Contribute/ContributionSoft.xml | 5 + xml/schema/Contribute/Product.xml | 5 + xml/schema/Core/UFField.xml | 5 + xml/schema/Core/UFJoin.xml | 7 +- xml/schema/Event/Event.xml | 5 + xml/schema/Event/Participant.xml | 5 + xml/schema/Financial/FinancialItem.xml | 5 + xml/schema/Financial/FinancialTrxn.xml | 5 + xml/schema/Financial/OfficialReceipt.xml | 5 + xml/schema/Grant/Grant.xml | 5 + xml/schema/PCP/PCP.xml | 5 + xml/schema/Pledge/Pledge.xml | 5 + xml/schema/Pledge/PledgePayment.xml | 5 + 28 files changed, 225 insertions(+), 74 deletions(-) diff --git a/CRM/ACL/Form/ACL.php b/CRM/ACL/Form/ACL.php index 77bd2664bf..517a5c519f 100644 --- a/CRM/ACL/Form/ACL.php +++ b/CRM/ACL/Form/ACL.php @@ -170,7 +170,7 @@ class CRM_ACL_Form_ACL extends CRM_Admin_Form { $ufGroup = array('-1' => ts('- select -'), '0' => ts('All Profiles'), - ) + CRM_Core_PseudoConstant::ufGroup(); + ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); $event = array('-1' => ts('- select -'), '0' => ts('All Events'), diff --git a/CRM/ACL/Page/ACL.php b/CRM/ACL/Page/ACL.php index 7a0cd9cbc3..6eb0838281 100644 --- a/CRM/ACL/Page/ACL.php +++ b/CRM/ACL/Page/ACL.php @@ -180,7 +180,7 @@ ORDER BY entity_id ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id'); $ufGroup = array('-1' => ts('- select -'), '0' => ts('All Profiles'), - ) + CRM_Core_PseudoConstant::ufGroup(); + ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); $event = array('-1' => ts('- select -'), '0' => ts('All Events'), diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index d57cc09650..6c0791317a 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -509,7 +509,7 @@ class CRM_Contribute_BAO_Query { // Supporting search for currency type -- CRM-4711 case 'contribution_currency_type': - $currencySymbol = CRM_Core_PseudoConstant::currencySymbols('name'); + $currencySymbol = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')); $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_contribution.currency", $op, $currencySymbol[$value], "String" ); @@ -767,7 +767,7 @@ class CRM_Contribute_BAO_Query { ts('Currency Type'), array( '' => ts('- any -')) + - CRM_Core_PseudoConstant::currencySymbols('name') + CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')) ); $form->add('select', 'financial_type_id', diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 777444d25f..f7e93f1ea5 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -220,7 +220,7 @@ class CRM_Core_BAO_UFGroup extends CRM_Core_DAO_UFGroup { $fields = $subset; } else { - $ufGroups = CRM_Core_PseudoConstant::ufGroup(); + $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); $fields = array(); foreach ($ufGroups as $id => $title) { @@ -2446,7 +2446,7 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) */ static function getProfiles($types, $onlyPure = FALSE) { $profiles = array(); - $ufGroups = CRM_Core_PseudoConstant::ufgroup(); + $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $ufGroups); @@ -2479,7 +2479,7 @@ AND ( entity_id IS NULL OR entity_id <= 0 ) } $profiles = array(); - $ufGroups = CRM_Core_PseudoConstant::ufgroup(); + $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $ufGroups); diff --git a/CRM/Core/Config/Variables.php b/CRM/Core/Config/Variables.php index 66bf8e13d1..c06c81e55f 100644 --- a/CRM/Core/Config/Variables.php +++ b/CRM/Core/Config/Variables.php @@ -484,8 +484,8 @@ class CRM_Core_Config_Variables extends CRM_Core_Config_Defaults { static $cachedSymbol = NULL; if (!$cachedSymbol || $defaultCurrency) { if ($this->defaultCurrency || $defaultCurrency) { - $currencySymbolName = CRM_Core_PseudoConstant::currencySymbols('name'); - $currencySymbol = CRM_Core_PseudoConstant::currencySymbols(); + $currencySymbolName = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')); + $currencySymbol = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency'); $this->currencySymbols = array_combine($currencySymbolName, $currencySymbol); $currency = $defaultCurrency ? $defaultCurrency : $this->defaultCurrency; diff --git a/CRM/Core/Permission.php b/CRM/Core/Permission.php index d3c318cdbc..4b8aafbcd5 100644 --- a/CRM/Core/Permission.php +++ b/CRM/Core/Permission.php @@ -209,7 +209,7 @@ class CRM_Core_Permission { } public static function ufGroup($type = CRM_Core_Permission::VIEW) { - $ufGroups = CRM_Core_PseudoConstant::ufGroup(); + $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); $allGroups = array_keys($ufGroups); diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 15ecee40b0..656bed77e6 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -133,13 +133,6 @@ class CRM_Core_PseudoConstant { */ private static $staticGroup; - /** - * user framework groups - * @var array - * @static - */ - private static $ufGroup; - /** * currency codes * @var array @@ -147,13 +140,6 @@ class CRM_Core_PseudoConstant { */ private static $currencyCode; - /** - * currency Symbols - * @var array - * @static - */ - private static $currencySymbols; - /** * payment processor * @var array @@ -241,11 +227,13 @@ class CRM_Core_PseudoConstant { public static function get($daoName, $fieldName, $params = array()) { $dao = new $daoName; $fields = $dao->fields(); + $fieldKeys = $dao->fieldKeys(); + $fieldKey = $fieldKeys[$fieldName]; $dao->free(); - if (empty($fields[$fieldName])) { + if (empty($fields[$fieldKey])) { return FALSE; } - $fieldSpec = $fields[$fieldName]; + $fieldSpec = $fields[$fieldKey]; $flip = !empty($params['flip']); // If the field is an enum, explode the enum definition and return the array. @@ -299,7 +287,10 @@ class CRM_Core_PseudoConstant { // Get list of fields for the option table $daoName = CRM_Core_AllCoreTables::getClassForTable($pseudoconstant['table']); $dao = new $daoName; - $availableFields = $dao->fields(); + $availableFields = array_keys($dao->fieldKeys()); + if (in_array('is_active', $availableFields)) { + $wheres[] = 'is_active = 1'; + } $dao->free(); $select = "SELECT %1 AS id, %2 AS label"; @@ -882,21 +873,6 @@ WHERE id = %1"; return self::$staticGroup; } - /** - * Get all the user framework groups - * - * @access public - * - * @return array - array reference of all groups. - * @static - */ - public static function &ufGroup() { - if (!self::$ufGroup) { - self::populate(self::$ufGroup, 'CRM_Core_DAO_UFGroup', FALSE, 'title', 'is_active', NULL, 'title'); - } - return self::$ufGroup; - } - /** * Get all Relationship Types from database. * @@ -943,25 +919,6 @@ WHERE id = %1"; return self::$relationshipType[$valueColumnName]; } - /** - * Get all the Currency Symbols from Database - * - * @access public - * - * @return array - array reference of all Currency Symbols - * @static - * - * FIXME: this is not stored as an optionValue, and it's not tied to a single DB column; - * FIXME: It's used for a setting stored in option group 'currencies_enabled'. What to do? - */ - public static function ¤cySymbols($name = 'symbol', $key = 'id') { - $cacheKey = "{$name}_{$key}"; - if (!isset(self::$currencySymbols[$cacheKey])) { - self::populate(self::$currencySymbols[$cacheKey], 'CRM_Financial_DAO_Currency', TRUE, $name, NULL, NULL, 'name', $key); - } - return self::$currencySymbols[$cacheKey]; - } - /** * get all the ISO 4217 currency codes * diff --git a/CRM/Profile/Form.php b/CRM/Profile/Form.php index 5a5d792025..555651e01c 100644 --- a/CRM/Profile/Form.php +++ b/CRM/Profile/Form.php @@ -680,7 +680,7 @@ class CRM_Profile_Form extends CRM_Core_Form { CRM_Core_Permission::EDIT, NULL, 'civicrm_uf_group', - CRM_Core_PseudoConstant::ufGroup() + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id') ) ) ) { diff --git a/CRM/Report/Form/Grant/Statistics.php b/CRM/Report/Form/Grant/Statistics.php index 26d693aecf..b6b1ac4eb9 100644 --- a/CRM/Report/Form/Grant/Statistics.php +++ b/CRM/Report/Form/Grant/Statistics.php @@ -548,7 +548,7 @@ SELECT COUNT({$this->_aliases['civicrm_grant']}.id) as count , return; } - $currencies = CRM_Core_PseudoConstant::currencySymbols('symbol', 'name'); + $currencies = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'currency'); $currency = $currencies[$values['civicrm_grant_currency']]; if (!$customData) { diff --git a/CRM/UF/Page/Group.php b/CRM/UF/Page/Group.php index d3515ee31a..2c559e34ad 100644 --- a/CRM/UF/Page/Group.php +++ b/CRM/UF/Page/Group.php @@ -298,7 +298,7 @@ class CRM_UF_Page_Group extends CRM_Core_Page { return; } - $ufGroups = CRM_Core_PseudoConstant::ufGroup(); + $ufGroups = CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFGroup', 'uf_group_id'); CRM_Utils_Hook::aclGroup(CRM_Core_Permission::ADMIN, NULL, 'civicrm_uf_group', $ufGroups, $allUFGroups); foreach ($allUFGroups as $id => $value) { diff --git a/CRM/Utils/Money.php b/CRM/Utils/Money.php index 96f6476489..151c2f0627 100644 --- a/CRM/Utils/Money.php +++ b/CRM/Utils/Money.php @@ -78,8 +78,8 @@ class CRM_Utils_Money { } if (!self::$_currencySymbols) { - $currencySymbolName = CRM_Core_PseudoConstant::currencySymbols('name'); - $currencySymbol = CRM_Core_PseudoConstant::currencySymbols(); + $currencySymbolName = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency', array('labelColumn' => 'name')); + $currencySymbol = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution', 'currency'); self::$_currencySymbols = array_combine($currencySymbolName, $currencySymbol); } diff --git a/tests/phpunit/CRM/Core/PseudoConstantTest.php b/tests/phpunit/CRM/Core/PseudoConstantTest.php index 54d4f18b21..6e56febaff 100644 --- a/tests/phpunit/CRM/Core/PseudoConstantTest.php +++ b/tests/phpunit/CRM/Core/PseudoConstantTest.php @@ -48,13 +48,15 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { * DAO fields having a tag in the XML schema. */ function testOptionValues() { - $custom_group_name = 'Test custom group'; + + $custom_group_name = md5(microtime()); $api_params = array( - 'version' => 3, - 'title' => $custom_group_name, - 'extends' => 'Individual', + 'version' => 3, + 'title' => $custom_group_name, + 'extends' => 'Individual', + 'is_active' => TRUE, ); - civicrm_api('customGroup', 'create', $api_params); + $result = civicrm_api('customGroup', 'create', $api_params); /** * daoName/field combinations to test @@ -66,6 +68,118 @@ class CRM_Core_PseudoConstantTest extends CiviUnitTestCase { * - max: integer (default = 10) maximum number of option values expected. */ $fields = array( + 'CRM_Event_DAO_Participant' => array( + array( + 'fieldName' => 'fee_currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Core_DAO_UFField' => array( + array( + 'fieldName' => 'uf_group_id', + 'sample' => 'Name and Address', + 'max' => 15, + ), + ), + 'CRM_Core_DAO_UFJoin' => array( + array( + 'fieldName' => 'uf_group_id', + 'sample' => 'Name and Address', + 'max' => 15, + ), + ), + 'CRM_Contribute_DAO_ContributionSoft' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Contribute_DAO_Contribution' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Contribute_DAO_Product' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Contribute_DAO_ContributionPage' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Contribute_DAO_ContributionRecur' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Event_DAO_Event' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Financial_DAO_FinancialItem' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Financial_DAO_OfficialReceipt' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Financial_DAO_FinancialTrxn' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Grant_DAO_Grant' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Pledge_DAO_PledgePayment' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_Pledge_DAO_Pledge' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), + 'CRM_PCP_DAO_PCP' => array( + array( + 'fieldName' => 'currency', + 'sample' => '$', + 'max' => 200, + ), + ), 'CRM_Core_DAO_CustomField' => array( array( 'fieldName' => 'custom_group_id', diff --git a/xml/schema/Contribute/Contribution.xml b/xml/schema/Contribute/Contribution.xml index 60bd84cade..bbf4063f4b 100644 --- a/xml/schema/Contribute/Contribution.xml +++ b/xml/schema/Contribute/Contribution.xml @@ -195,7 +195,12 @@ /cur(rency)?/i /^[A-Z]{3}$/i 3 character string, value from config setting or input via user. - 1.3 + 1.3 + + civicrm_currency
+ name + symbol +
cancel_date diff --git a/xml/schema/Contribute/ContributionPage.xml b/xml/schema/Contribute/ContributionPage.xml index 3522119d18..d48d1886c8 100644 --- a/xml/schema/Contribute/ContributionPage.xml +++ b/xml/schema/Contribute/ContributionPage.xml @@ -382,7 +382,12 @@ /cur(rency)?/i /^[A-Z]{3}$/i 3 character string, value from config setting or input via user. - 3.3 + 3.3 + + civicrm_currency
+ name + symbol +
campaign_id diff --git a/xml/schema/Contribute/ContributionRecur.xml b/xml/schema/Contribute/ContributionRecur.xml index e2a2171a04..c6f6f8d47e 100644 --- a/xml/schema/Contribute/ContributionRecur.xml +++ b/xml/schema/Contribute/ContributionRecur.xml @@ -44,6 +44,11 @@ NULL 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
frequency_unit diff --git a/xml/schema/Contribute/ContributionSoft.xml b/xml/schema/Contribute/ContributionSoft.xml index dbdae71ad5..e298d9f806 100644 --- a/xml/schema/Contribute/ContributionSoft.xml +++ b/xml/schema/Contribute/ContributionSoft.xml @@ -70,6 +70,11 @@ NULL 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
pcp_id diff --git a/xml/schema/Contribute/Product.xml b/xml/schema/Contribute/Product.xml index 650ee4e8b6..8ac583a904 100644 --- a/xml/schema/Contribute/Product.xml +++ b/xml/schema/Contribute/Product.xml @@ -84,6 +84,11 @@ NULL 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
financial_type_id diff --git a/xml/schema/Core/UFField.xml b/xml/schema/Core/UFField.xml index 48ecbf7cda..47eafa0a65 100644 --- a/xml/schema/Core/UFField.xml +++ b/xml/schema/Core/UFField.xml @@ -24,6 +24,11 @@ true Which form does this field belong to. 1.1 + + civicrm_uf_group
+ id + title +
uf_group_id diff --git a/xml/schema/Core/UFJoin.xml b/xml/schema/Core/UFJoin.xml index 64c539329f..0af165a9d6 100644 --- a/xml/schema/Core/UFJoin.xml +++ b/xml/schema/Core/UFJoin.xml @@ -71,7 +71,12 @@ int unsigned true Which form does this field belong to. - 1.3 + 1.3 + + civicrm_uf_group
+ id + title +
uf_group_id diff --git a/xml/schema/Event/Event.xml b/xml/schema/Event/Event.xml index 4dffb82c41..2e3f4aa9a5 100644 --- a/xml/schema/Event/Event.xml +++ b/xml/schema/Event/Event.xml @@ -573,6 +573,11 @@ /^[A-Z]{3}$/i 3 character string, value from config setting or input via user. 3.3 + + civicrm_currency
+ name + symbol +
campaign_id diff --git a/xml/schema/Event/Participant.xml b/xml/schema/Event/Participant.xml index 604877a73c..1d5a9a3b6e 100644 --- a/xml/schema/Event/Participant.xml +++ b/xml/schema/Event/Participant.xml @@ -216,6 +216,11 @@ /^[A-Z]{3}$/i 3 character string, value derived from config setting. 3.0 + + civicrm_currency
+ name + symbol +
campaign_id diff --git a/xml/schema/Financial/FinancialItem.xml b/xml/schema/Financial/FinancialItem.xml index 540387acc7..586b1c2eda 100644 --- a/xml/schema/Financial/FinancialItem.xml +++ b/xml/schema/Financial/FinancialItem.xml @@ -74,6 +74,11 @@ 3 Currency for the amount 4.3 + + civicrm_currency
+ name + symbol +
financial_account_id diff --git a/xml/schema/Financial/FinancialTrxn.xml b/xml/schema/Financial/FinancialTrxn.xml index 511e8e9489..edea2e076a 100755 --- a/xml/schema/Financial/FinancialTrxn.xml +++ b/xml/schema/Financial/FinancialTrxn.xml @@ -112,6 +112,11 @@ /^[A-Z]{3}$/ 3 character string, value from config setting or input via user. 1.3 + + civicrm_currency
+ name + symbol +
payment_processor diff --git a/xml/schema/Financial/OfficialReceipt.xml b/xml/schema/Financial/OfficialReceipt.xml index f0250b075d..576768a5e8 100755 --- a/xml/schema/Financial/OfficialReceipt.xml +++ b/xml/schema/Financial/OfficialReceipt.xml @@ -125,6 +125,11 @@ /^[A-Z]{3}$/ 3 character string, value from config setting or input via user. 4.1 + + civicrm_currency
+ name + symbol +
msg_template_id diff --git a/xml/schema/Grant/Grant.xml b/xml/schema/Grant/Grant.xml index ef4b4b40f0..7560a4036b 100644 --- a/xml/schema/Grant/Grant.xml +++ b/xml/schema/Grant/Grant.xml @@ -128,6 +128,11 @@ true 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
currency diff --git a/xml/schema/PCP/PCP.xml b/xml/schema/PCP/PCP.xml index 37ebfbdeb3..5cde13f68b 100644 --- a/xml/schema/PCP/PCP.xml +++ b/xml/schema/PCP/PCP.xml @@ -131,6 +131,11 @@ NULL 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
referer diff --git a/xml/schema/Pledge/Pledge.xml b/xml/schema/Pledge/Pledge.xml index 4c95b343dd..50a0c19845 100644 --- a/xml/schema/Pledge/Pledge.xml +++ b/xml/schema/Pledge/Pledge.xml @@ -108,6 +108,11 @@ NULL 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
frequency_unit diff --git a/xml/schema/Pledge/PledgePayment.xml b/xml/schema/Pledge/PledgePayment.xml index dc2342d5e6..5d5adf2139 100644 --- a/xml/schema/Pledge/PledgePayment.xml +++ b/xml/schema/Pledge/PledgePayment.xml @@ -79,6 +79,11 @@ NULL 3 character string, value from config setting or input via user. 3.2 + + civicrm_currency
+ name + symbol +
scheduled_date -- 2.25.1