CRM-13608 - API - Support 'sequential' param with getoptions
authorColeman Watts <coleman@civicrm.org>
Sat, 19 Oct 2013 15:39:25 +0000 (08:39 -0700)
committerColeman Watts <coleman@civicrm.org>
Sat, 19 Oct 2013 17:04:24 +0000 (10:04 -0700)
----------------------------------------
* CRM-13608: Api3 'getoptions' ordering doesn't work as JSON
  http://issues.civicrm.org/jira/browse/CRM-13608

api/v3/Generic.php
templates/CRM/Contact/Form/Search/Builder.js

index 392fd4cf722872bd583d9e1d093dde147f3260e4..f02af9ed9dd454b748efa19bdb2094af61f3c67b 100644 (file)
@@ -228,11 +228,18 @@ function civicrm_api3_generic_getoptions($apiRequest) {
   unset($apiRequest['params']['context'], $apiRequest['params']['field']);
 
   $baoName = _civicrm_api3_get_BAO($apiRequest['entity']);
-  $options = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
+  $options = $output = $baoName::buildOptions($fieldName, $context, $apiRequest['params']);
   if ($options === FALSE) {
     return civicrm_api3_create_error("The field '{$fieldName}' has no associated option list.");
   }
-  return civicrm_api3_create_success($options);
+  // Support 'sequential' output as a non-associative array
+  if (!empty($apiRequest['params']['sequential'])) {
+    $output = array();
+    foreach ($options as $key => $val) {
+      $output[] = array('key' => $key, 'value' => $val);
+    }
+  }
+  return civicrm_api3_create_success($output);
 }
 
 /**
index f1d0cdf1e452029533cdc58cbfab1b0afad1fd05..71b981f92ac6d469eb9349c32fadf622f69f131e 100644 (file)
    */
   function fetchOptions(row, field) {
     if (CRM.searchBuilder.fieldOptions[field] === 'yesno') {
-      CRM.searchBuilder.fieldOptions[field] = {1: ts('Yes'), 0: ts('No')};
+      CRM.searchBuilder.fieldOptions[field] = [{key: 1, value: ts('Yes')}, {key: 0, value: ts('No')}];
     }
     if (typeof(CRM.searchBuilder.fieldOptions[field]) == 'string') {
-      CRM.api(CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field}, {
+      CRM.api(CRM.searchBuilder.fieldOptions[field], 'getoptions', {field: field, sequential: 1}, {
         success: function(result, settings) {
           var field = settings.field;
           if (result.count) {
         options = [options[0]];
       }
     }
-    $.each(CRM.searchBuilder.fieldOptions[field], function(value, label) {
-      var selected = ($.inArray(value, options) > -1) ? 'selected="selected"' : '';
-      select.append('<option value="' + value + '"' + selected + '>' + label + '</option>');
+    $.each(CRM.searchBuilder.fieldOptions[field], function(key, option) {
+      var selected = ($.inArray(option.key, options) > -1) ? 'selected="selected"' : '';
+      select.append('<option value="' + option.key + '"' + selected + '>' + option.value + '</option>');
     });
     select.change();
   }