From a3d8b3903cc3098ccdf60003e046524a24b842d0 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 27 Jul 2013 14:06:49 -0700 Subject: [PATCH] Support additional option lists - state, country, boolean CRM-12464 ---------------------------------------- * CRM-12464: Add PseudoConstants to Schema Metadata http://issues.civicrm.org/jira/browse/CRM-12464 --- CRM/Core/PseudoConstant.php | 62 +++++++++++++++++++++++++-------- api/v3/Generic.php | 3 +- xml/schema/Core/OptionValue.xml | 5 +++ 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/CRM/Core/PseudoConstant.php b/CRM/Core/PseudoConstant.php index 32a177bac8..cf7df6d713 100644 --- a/CRM/Core/PseudoConstant.php +++ b/CRM/Core/PseudoConstant.php @@ -239,20 +239,38 @@ class CRM_Core_PseudoConstant { // Custom fields are not in the schema if (strpos($fieldName, 'custom_') === 0 && is_numeric($fieldName[7])) { - $customFieldID = (int) substr($fieldName, 7); - $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldID, 'option_group_id'); - - $options = CRM_Core_OptionGroup::valuesByID($optionGroupID, - $flip, - $params['grouping'], - $params['localize'], - // Note: for custom fields the 'name' column is NULL - CRM_Utils_Array::value('labelColumn', $params, 'label'), - $params['onlyActive'], - $params['fresh'] - ); + $customField = new CRM_Core_DAO_CustomField(); + $customField->id = (int) substr($fieldName, 7); + $customField->find(TRUE); + $options = FALSE; - CRM_Utils_Hook::customFieldOptions($customFieldID, $options, FALSE); + if (!empty($customField->option_group_id)) { + $options = CRM_Core_OptionGroup::valuesByID($customField->option_group_id, + $flip, + $params['grouping'], + $params['localize'], + // Note: for custom fields the 'name' column is NULL + CRM_Utils_Array::value('labelColumn', $params, 'label'), + $params['onlyActive'], + $params['fresh'] + ); + } + else { + if ($customField->data_type === 'StateProvince') { + $options = self::stateProvince(); + } + elseif ($customField->data_type === 'Country') { + $options = $context == 'validate' ? self::countryIsoCode() : self::country(); + } + elseif ($customField->data_type === 'Boolean') { + $options = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No')); + } + $options = $options && $flip ? array_flip($options) : $options; + } + if ($options !== FALSE) { + CRM_Utils_Hook::customFieldOptions($customField->id, $options, FALSE); + } + $customField->free(); return $options; } @@ -390,9 +408,17 @@ class CRM_Core_PseudoConstant { $output[$dao->id] = $dao->label; } $dao->free(); - if (!empty($params['localize'])) { + // Localize results + if (!empty($params['localize']) || $pseudoconstant['table'] == 'civicrm_country' || $pseudoconstant['table'] == 'civicrm_state_province') { + $I18nParams = array(); + if ($pseudoconstant['table'] == 'civicrm_country') { + $I18nParams['context'] = 'country'; + } + if ($pseudoconstant['table'] == 'civicrm_state_province') { + $I18nParams['context'] = 'province'; + } $i18n = CRM_Core_I18n::singleton(); - $i18n->localizeArray($output); + $i18n->localizeArray($output, $I18nParams); // Maintain sort by label if ($order == "ORDER BY %2") { CRM_Utils_Array::asort($output); @@ -403,6 +429,12 @@ class CRM_Core_PseudoConstant { return $flip ? array_flip($output) : $output; } } + + // Return "Yes" and "No" for boolean fields + elseif (CRM_Utils_Array::value('type', $fieldSpec) === CRM_Utils_Type::T_BOOLEAN) { + $output = $context == 'validate' ? array(0, 1) : array(1 => ts('Yes'), 0 => ts('No')); + return $flip ? array_flip($output) : $output; + } // If we're still here, it's an error. Return FALSE. return FALSE; } diff --git a/api/v3/Generic.php b/api/v3/Generic.php index d2619f0e1c..9b3d4e2182 100644 --- a/api/v3/Generic.php +++ b/api/v3/Generic.php @@ -222,9 +222,10 @@ function civicrm_api3_generic_getoptions($apiRequest) { // Validate 'context' from params $context = CRM_Utils_Array::value('context', $apiRequest['params']); CRM_Core_DAO::buildOptionsContext($context); + unset($apiRequest['params']['context'], $apiRequest['params']['field']); $baoName = _civicrm_api3_get_BAO($apiRequest['entity']); - $options = $baoName::buildOptions($fieldName, $context); + $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']); if ($options === FALSE) { return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list."); } diff --git a/xml/schema/Core/OptionValue.xml b/xml/schema/Core/OptionValue.xml index 783810d6c7..8bee6a2eb9 100644 --- a/xml/schema/Core/OptionValue.xml +++ b/xml/schema/Core/OptionValue.xml @@ -23,6 +23,11 @@ true Group which this option belongs to. 1.5 + + civicrm_option_group
+ id + name +
option_group_id -- 2.25.1