Support additional option lists - state, country, boolean CRM-12464
authorColeman Watts <coleman@civicrm.org>
Sat, 27 Jul 2013 21:06:49 +0000 (14:06 -0700)
committerColeman Watts <coleman@civicrm.org>
Sat, 27 Jul 2013 21:06:49 +0000 (14:06 -0700)
----------------------------------------
* CRM-12464: Add PseudoConstants to Schema Metadata
  http://issues.civicrm.org/jira/browse/CRM-12464

CRM/Core/PseudoConstant.php
api/v3/Generic.php
xml/schema/Core/OptionValue.xml

index 32a177bac88b87aaaf4bba046a39bb78ab897060..cf7df6d713030b3e4981673ca02211aef2e1751b 100644 (file)
@@ -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;
   }
index d2619f0e1c442e1e293ca727e89408dede327fd5..9b3d4e21822ae11672e7f88392a98386dad05165 100644 (file)
@@ -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.");
   }
index 783810d6c734fb24ad4289050eda92fa741c1459..8bee6a2eb953e4e0cbd8178726038edcd6926355 100644 (file)
       <required>true</required>
       <comment>Group which this option belongs to.</comment>
        <add>1.5</add>
+      <pseudoconstant>
+        <table>civicrm_option_group</table>
+        <keyColumn>id</keyColumn>
+        <labelColumn>name</labelColumn>
+      </pseudoconstant>
   </field>
   <foreignKey>
        <name>option_group_id</name>