Merge pull request #972 from deepak-srivastava/hr
[civicrm-core.git] / api / v3 / Generic.php
index ac9c7cb0246f273b29cd656d6f6f8166369d9774..e86bd43d63c53babfca87b3b1f6b6bbc7a5a0191 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-// $Id$
 
 /**
  * Get information about fields for a given api request. Getfields information
@@ -56,23 +55,28 @@ function civicrm_api3_generic_getfields($apiRequest) {
   switch ($action) {
     case 'getfields':
       $values = _civicrm_api_get_fields($entity, false, $apiRequest['params']);
-      $results[$entity][$action] = civicrm_api3_create_success($values,
-        $apiRequest['params'], $entity, 'getfields'
-      );
-      return $results[$entity][$action];
-
-                case 'getfields':
-                  return civicrm_api3_create_success(_civicrm_api_get_fields($apiRequest['entity']));
+      return civicrm_api3_create_success($values, $apiRequest['params'], $entity, 'getfields');
     case 'create':
     case 'update':
     case 'replace':
       $unique = FALSE;
     case 'get':
       $metadata = _civicrm_api_get_fields($apiRequest['entity'], $unique, $apiRequest['params']);
-      if (empty($metadata['id']) && !empty($metadata[strtolower($apiRequest['entity']) . '_id'])) {
-        $metadata['id'] = $metadata[$lcase_entity . '_id'];
+      if (empty($metadata['id'])){
+        // if id is not set we will set it eg. 'id' from 'case_id', case_id will be an alias
+        if(!empty($metadata[strtolower($apiRequest['entity']) . '_id'])) {
+          $metadata['id'] = $metadata[$lcase_entity . '_id'];
+          unset($metadata[$lcase_entity . '_id']);
+          $metadata['id']['api.aliases'] = array($lcase_entity . '_id');
+        }
+      }
+      else{
+        // really the preference would be to set the unique name in the xml
+        // question is which is a less risky fix this close to a release - setting in xml for the known failure
+        // (note) or setting for all api where fields is returning 'id' & we want to accept 'note_id' @ the api layer
+        // nb we don't officially accept note_id anyway - rationale here is more about centralising a now-tested
+        // inconsistency
         $metadata['id']['api.aliases'] = array($lcase_entity . '_id');
-        unset($metadata[$lcase_entity . '_id']);
       }
       break;
 
@@ -193,43 +197,17 @@ function civicrm_api3_generic_replace($apiRequest) {
 /**
  * API wrapper for getoptions function
  *
- * @param array $apiRequest api request as an array. Keys are
+ * @param array $apiRequest api request as an array.
  *
  * @return array of results
  */
 function civicrm_api3_generic_getoptions($apiRequest) {
-  $field = $apiRequest['params']['field'];
-  $getFieldsArray = array(
-    'version' => 3,
-    'action' => 'create',
-    'options' => array('get_options' => $field),
-  );
-  // First try to retrieve the options from getfields
-  $result = civicrm_api($apiRequest['entity'], 'getfields', $getFieldsArray);
-  if (!isset($result['values'][$field]) && isset($result['values'][$field . '_id'])) {
-    $field = $field . '_id';
-  }
-  if (!empty($result['values'][$field]['options'])) {
-    return civicrm_api3_create_success($result['values'][$field]['options']);
-  }
-  // If that didn't work, try the constant api
-  if (substr($field, -3) == '_id') {
-    // Convert foo_id to just plain foo
-    $field = substr($field, 0, -3);
-  }
-  $params = array('name' => _civicrm_api_get_camel_name($field));
-  $entity = strtolower($apiRequest['entity']);
-  if ($entity == 'contribution') {
-    $params['class'] = 'CRM_Contribute_PseudoConstant';
-  }
-  elseif ($entity == 'event' || $entity == 'participant') {
-    $params['class'] = 'CRM_Event_PseudoConstant';
-  }
-  elseif (strpos($entity, 'membership') === 0) {
-    $params['class'] = 'CRM_Member_PseudoConstant';
+  $daoName = _civicrm_api3_get_DAO($apiRequest['entity']);
+  $options = $daoName::buildOptions($apiRequest['params']['field']);
+  if ($options === FALSE) {
+    return civicrm_api3_create_error("The field '{$apiRequest['params']['field']}' either doesn't exist or has no associated option list.");
   }
-  require_once 'api/v3/Constant.php';
-  return civicrm_api3_constant_get($params);
+  return civicrm_api3_create_success($options);
 }
 
 /**