Merge pull request #3158 from eileenmcnaughton/tests
[civicrm-core.git] / api / v3 / Generic.php
index 3c4fc8a189947e6261c6931d4ecbd2533f9520ec..06804ad4b33dbab1b40083396ccc845c5b2234f0 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])
@@ -63,6 +59,8 @@ function civicrm_api3_generic_getfields($apiRequest) {
     case 'replace':
       $unique = FALSE;
     case 'get':
+    case 'getsingle':
+    case 'getcount':
       $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']);
       if (empty($metadata['id'])){
         // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias
@@ -84,7 +82,9 @@ function civicrm_api3_generic_getfields($apiRequest) {
 
     case 'delete':
       $metadata = array(
-        'id' => array('title' => 'Unique Identifier',
+        'id' => array(
+          'title' => $entity . ' ID',
+          'name' => 'id',
           'api.required' => 1,
           'api.aliases' => array($lcase_entity . '_id'),
           'type' => CRM_Utils_Type::T_INT,
@@ -94,11 +94,13 @@ function civicrm_api3_generic_getfields($apiRequest) {
     case 'getoptions':
       $metadata = array(
         'field' => array(
-          'title' => 'Field to retrieve options for',
+          'name' => 'field',
+          'title' => 'Field name',
           'api.required' => 1,
         ),
         'context' => array(
-          'title' => 'Context string',
+          'name' => 'context',
+          'title' => 'Context',
         ),
       );
         break;
@@ -109,8 +111,12 @@ function civicrm_api3_generic_getfields($apiRequest) {
 
   // find any supplemental information
   $hypApiRequest = array('entity' => $apiRequest['entity'], 'action' => $action, 'version' => $apiRequest['version']);
-  $hypApiRequest += _civicrm_api_resolve($hypApiRequest);
-  $helper = '_' . $hypApiRequest['function'] . '_spec';
+  try {
+    list ($apiProvider, $hypApiRequest) = \Civi\Core\Container::singleton()->get('civi_api_kernel')->resolve($hypApiRequest);
+    $helper = '_' . $hypApiRequest['function'] . '_spec';
+  } catch (\Civi\API\Exception\NotImplementedException $e) {
+    $helper = NULL;
+  }
   if (function_exists($helper)) {
     // alter
     $helper($metadata, $apiRequest);
@@ -119,7 +125,7 @@ function civicrm_api3_generic_getfields($apiRequest) {
   $fieldsToResolve = (array) CRM_Utils_Array::value('get_options', $apiOptions, array());
 
   foreach ($metadata as $fieldname => $fieldSpec) {
-    _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest['entity'], $fieldname, $fieldSpec, $fieldsToResolve);
+    _civicrm_api3_generic_get_metadata_options($metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve);
   }
 
   $results[$entity][$action][$sequential] = civicrm_api3_create_success($metadata, $apiRequest['params'], NULL, 'getfields');
@@ -239,7 +245,7 @@ function civicrm_api3_generic_getoptions($apiRequest) {
       $output[] = array('key' => $key, 'value' => $val);
     }
   }
-  return civicrm_api3_create_success($output);
+  return civicrm_api3_create_success($output, $apiRequest['params'], $apiRequest['entity'], 'getoptions');
 }
 
 /**
@@ -250,7 +256,6 @@ function civicrm_api3_generic_getoptions($apiRequest) {
  * - the reason for this is that checking / transformation is done on pseudoconstants but
  * - if the field is an FK then mysql will enforce the data quality (& we have handling on failure)
  * @todo - if may be we should define a 'resolve' key on the psuedoconstant for when these rules are not fine enough
- * 3) if there is an 'enum' then it is split up into the relevant options
  *
  * This function is only split out for the purpose of code clarity / comment block documentation
  * @param array $metadata the array of metadata that will form the result of the getfields function
@@ -258,8 +263,8 @@ function civicrm_api3_generic_getoptions($apiRequest) {
  * @param array $fieldSpec metadata for that field
  * @param array $fieldsToResolve anny field resolutions specifically requested
  */
-function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldname, $fieldSpec, $fieldsToResolve){
-  if(empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['enumValues'])) {
+function _civicrm_api3_generic_get_metadata_options(&$metadata, $apiRequest, $fieldname, $fieldSpec, $fieldsToResolve){
+  if (empty($fieldSpec['pseudoconstant']) && empty($fieldSpec['option_group_id'])) {
     return;
   }
 
@@ -267,7 +272,7 @@ function _civicrm_api3_generic_get_metadata_options(&$metadata, $entity, $fieldn
     return;
   }
 
-  $options = civicrm_api($entity, 'getoptions', array('version' => 3, 'field' => $fieldname));
+  $options = civicrm_api($apiRequest['entity'], 'getoptions', array('version' => 3, 'field' => $fieldname, 'sequential' => !empty($apiRequest['params']['sequential'])));
   if (is_array(CRM_Utils_Array::value('values', $options))) {
     $metadata[$fieldname]['options'] = $options['values'];
   }