Merge pull request #2514 from colemanw/ac2
[civicrm-core.git] / api / v3 / Generic.php
index 30a0ea0247bc80cdceaacbb8fe24334ac9e86e5f..920ff3f4cba9301e1f780b1e741babe7facda7c9 100644 (file)
@@ -40,13 +40,9 @@ function civicrm_api3_generic_getfields($apiRequest) {
   $action       = strtolower(CRM_Utils_Array::value('action', $apiRequest['params']));
   $sequential = empty($apiRequest['params']) ? 0 : 1;
   $apiOptions = CRM_Utils_Array::value('options', $apiRequest['params'], array());
-  if ($action == 'getvalue' || $action == 'getvalue' || $action == 'getcount') {
+  if (!$action || $action == 'getvalue' || $action == 'getcount') {
     $action = 'get';
   }
-
-  if (empty($action)) {
-    $action='get';
-  }
   // determines whether to use unique field names - seem comment block above
   $unique = TRUE;
   if (empty($apiOptions) && isset($results[$entity . $subentity]) && isset($action, $results[$entity . $subentity])
@@ -87,6 +83,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 +109,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 +135,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'];
 }
 
@@ -183,7 +183,7 @@ function civicrm_api3_generic_getvalue($apiRequest) {
   }
 
   // we only take "return=" as valid options
-  if (CRM_Utils_Array::value('return', $apiRequest['params'])) {
+  if (!empty($apiRequest['params']['return'])) {
     if (!isset($result['values'][0][$apiRequest['params']['return']])) {
       return civicrm_api3_create_error("field " . $apiRequest['params']['return'] . " unset or not existing", array('invalid_field' => $apiRequest['params']['return']));
     }
@@ -221,13 +221,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, $apiRequest['params'], $apiRequest['entity'], 'getoptions');
 }
 
 /**