Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-02-05-17-16-30
[civicrm-core.git] / api / api.php
index 39bad896677f109b168a5e205e75a46d253d0aaa..3dca094dbccb1ff2ac68c42a42288a9ed44ffad2 100644 (file)
@@ -62,7 +62,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
     // we do this before we
     _civicrm_api3_swap_out_aliases($apiRequest, $fields);
     if (strtolower($action) != 'getfields') {
-      if (!CRM_Utils_Array::value('id', $apiRequest['params'])) {
+      if (empty($apiRequest['params']['id'])) {
         $apiRequest['params'] = array_merge(_civicrm_api3_getdefaults($apiRequest, $fields), $apiRequest['params']);
       }
       //if 'id' is set then only 'version' will be checked but should still be checked for consistency
@@ -87,7 +87,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
       $result = isset($extra) ? $function($apiRequest['params'], $extra) : $function($apiRequest['params']);
     }
     else {
-      return civicrm_api3_create_error("API (" . $apiRequest['entity'] . "," . $apiRequest['action'] . ") does not exist (join the API team and implement it!)");
+      return civicrm_api3_create_error("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)");
     }
 
     // For output filtering, process $apiWrappers in reverse order
@@ -103,7 +103,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
         return 0;
       }
     }
-    if (CRM_Utils_Array::value('format.only_id', $apiRequest['params']) && isset($result['id'])) {
+    if (!empty($apiRequest['params']['format.only_id']) && isset($result['id'])) {
       return $result['id'];
     }
     if (CRM_Utils_Array::value('is_error', $result, 0) == 0) {
@@ -130,7 +130,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
       $data["error_code"] = DB::errorMessage($error->getCode());
       $data["sql"] = $error->getDebugInfo();
     }
-    if (CRM_Utils_Array::value('debug', $apiRequest['params'])) {
+    if (!empty($apiRequest['params']['debug'])) {
       if(method_exists($e, 'getUserInfo')) {
         $data['debug_info'] = $error->getUserInfo();
       }
@@ -143,7 +143,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
       $data['tip'] = "add debug=1 to your API call to have more info about the error";
     }
     $err = civicrm_api3_create_error($e->getMessage(), $data, $apiRequest);
-    if (CRM_Utils_Array::value('is_transactional', $apiRequest)) {
+    if (!empty($apiRequest['is_transactional'])) {
       $transaction->rollback();
     }
     return $err;
@@ -164,7 +164,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
     ) {
       $err['trace'] = $e->getTraceAsString();
     }
-    if (CRM_Utils_Array::value('is_transactional', $apiRequest)) {
+    if (!empty($apiRequest['is_transactional'])) {
       $transaction->rollback();
     }
     return $err;
@@ -175,10 +175,10 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
     }
     $data = array();
     $err = civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode());
-    if (CRM_Utils_Array::value('debug', $apiRequest['params'])) {
+    if (!empty($apiRequest['params']['debug'])) {
       $err['trace'] = $e->getTraceAsString();
     }
-    if (CRM_Utils_Array::value('is_transactional', $apiRequest)) {
+    if (!empty($apiRequest['is_transactional'])) {
       $transaction->rollback();
     }
     return $err;
@@ -265,7 +265,7 @@ function _civicrm_api_resolve($apiRequest) {
  * @throws CiviCRM_API3_Exception
  * @return array
  */
-function civicrm_api3($entity, $action, $params) {
+function civicrm_api3($entity, $action, $params = array()) {
   $params['version'] = 3;
   $result = civicrm_api($entity, $action, $params);
   if(is_array($result) && !empty($result['is_error'])){
@@ -290,11 +290,11 @@ function _civicrm_api3_api_getfields(&$apiRequest) {
   if (strtolower($apiRequest['action'] == 'getfields')) {
     // the main param getfields takes is 'action' - however this param is not compatible with REST
     // so we accept 'api_action' as an alias of action on getfields
-    if (CRM_Utils_Array::value('api_action', $apiRequest['params'])) {
+    if (!empty($apiRequest['params']['api_action'])) {
     //  $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
      // unset($apiRequest['params']['api_action']);
     }
-    return array('action' => array('api_aliases' => array('action')));
+    return array('action' => array('api.aliases' => array('api_action')));
   }
   $getFieldsParams = array('action' => $apiRequest['action']);
   $entity = $apiRequest['entity'];
@@ -444,6 +444,12 @@ function _civicrm_api_get_camel_name($entity, $version = NULL) {
  */
 function _civicrm_api_call_nested_api(&$params, &$result, $action, $entity, $version) {
   $entity = _civicrm_api_get_entity_name_from_camel($entity);
+
+  //we don't need to worry about nested api in the getfields/getoptions actions, so just return immediately
+  if (in_array(strtolower($action), array('getfields', 'getoptions'))) {
+    return;
+  }
+
   if(strtolower($action) == 'getsingle'){
     // I don't understand the protocol here, but we don't want
     // $result to be a recursive array
@@ -509,7 +515,7 @@ function _civicrm_api_call_nested_api(&$params, &$result, $action, $entity, $ver
         if (array_key_exists(0, $newparams)) {
           $genericParams = $subParams;
           // it is a numerically indexed array - ie. multiple creates
-          foreach ($newparams as $entity => $entityparams) {
+          foreach ($newparams as $entityparams) {
             $subParams = array_merge($genericParams, $entityparams);
             _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator);
             $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams);