Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-11-03-17-29-16
[civicrm-core.git] / api / v3 / Generic.php
index 30a0ea0247bc80cdceaacbb8fe24334ac9e86e5f..f02af9ed9dd454b748efa19bdb2094af61f3c67b 100644 (file)
@@ -87,6 +87,7 @@ function civicrm_api3_generic_getfields($apiRequest) {
         'id' => array('title' => 'Unique Identifier',
           'api.required' => 1,
           'api.aliases' => array($lcase_entity . '_id'),
+          'type' => CRM_Utils_Type::T_INT,
         ));
       break;
 
@@ -112,7 +113,7 @@ function civicrm_api3_generic_getfields($apiRequest) {
   $helper = '_' . $hypApiRequest['function'] . '_spec';
   if (function_exists($helper)) {
     // alter
-    $helper($metadata);
+    $helper($metadata, $apiRequest);
   }
 
   $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array());
@@ -138,6 +139,9 @@ function civicrm_api3_generic_getcount($apiRequest) {
   if(is_numeric (CRM_Utils_Array::value('values', $result))) {
     return (int) $result['values'];
   }
+  if(!isset($result['count'])) {
+    throw new API_Exception(ts('Unexpected result from getcount') . print_r($result, TRUE));
+  }
   return $result['count'];
 }
 
@@ -221,13 +225,21 @@ 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 = $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);
 }
 
 /**