Merge pull request #10560 from civicrm/4.7.21-rc
[civicrm-core.git] / api / api.php
index 32b0c2297fe22bcec60c0d76c1658f75f570f894..5c432630a8e5ab8ab5d4f6dfc49c6517f8df2405 100644 (file)
@@ -20,7 +20,7 @@
  * @return array|int
  */
 function civicrm_api($entity, $action, $params, $extra = NULL) {
-  return \Civi::service('civi_api_kernel')->run($entity, $action, $params, $extra);
+  return \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params, $extra);
 }
 
 /**
@@ -40,7 +40,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
  */
 function civicrm_api3($entity, $action, $params = array()) {
   $params['version'] = 3;
-  $result = civicrm_api($entity, $action, $params);
+  $result = \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params);
   if (is_array($result) && !empty($result['is_error'])) {
     throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
   }
@@ -128,42 +128,61 @@ function _civicrm_api_get_camel_name($entity) {
  * @param string $separator
  */
 function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '.') {
-
-  foreach ($params as $field => $value) {
-
+  foreach ($params as $field => &$value) {
     if (is_string($value) && substr($value, 0, 6) == '$value') {
-      $valueSubstitute = substr($value, 7);
-
-      if (!empty($parentResult[$valueSubstitute])) {
-        $params[$field] = $parentResult[$valueSubstitute];
+      $value = _civicrm_api_replace_variable($value, $parentResult, $separator);
+    }
+    // Handle the operator syntax: array('OP' => $val)
+    elseif (is_array($value) && is_string(reset($value)) && substr(reset($value), 0, 6) == '$value') {
+      $key = key($value);
+      $value[$key] = _civicrm_api_replace_variable($value[$key], $parentResult, $separator);
+      // A null value with an operator will cause an error, so remove it.
+      if ($value[$key] === NULL) {
+        $value = '';
       }
-      else {
+    }
+  }
+}
 
-        $stringParts = explode($separator, $value);
-        unset($stringParts[0]);
+/**
+ * Swap out a $value.foo variable with the value from parent api results.
+ *
+ * Called by _civicrm_api_replace_variables to do the substitution.
+ *
+ * @param string $value
+ * @param array $parentResult
+ * @param string $separator
+ * @return mixed|null
+ */
+function _civicrm_api_replace_variable($value, $parentResult, $separator) {
+  $valueSubstitute = substr($value, 7);
 
-        $fieldname = array_shift($stringParts);
+  if (!empty($parentResult[$valueSubstitute])) {
+    return $parentResult[$valueSubstitute];
+  }
+  else {
+    $stringParts = explode($separator, $value);
+    unset($stringParts[0]);
+    // CRM-16168 If we have failed to swap it out we should unset it rather than leave the placeholder.
+    $value = NULL;
 
-        //when our string is an array we will treat it as an array from that . onwards
-        $count = count($stringParts);
-        while ($count > 0) {
-          $fieldname .= "." . array_shift($stringParts);
-          if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) {
-            $arrayLocation = $parentResult[$fieldname];
-            foreach ($stringParts as $key => $innerValue) {
-              $arrayLocation = CRM_Utils_Array::value($innerValue, $arrayLocation);
-            }
-            $params[$field] = $arrayLocation;
-          }
-          $count = count($stringParts);
+    $fieldname = array_shift($stringParts);
+
+    //when our string is an array we will treat it as an array from that . onwards
+    $count = count($stringParts);
+    while ($count > 0) {
+      $fieldname .= "." . array_shift($stringParts);
+      if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) {
+        $arrayLocation = $parentResult[$fieldname];
+        foreach ($stringParts as $key => $innerValue) {
+          $arrayLocation = CRM_Utils_Array::value($innerValue, $arrayLocation);
         }
+        $value = $arrayLocation;
       }
-      // CRM-16168 If we have failed to swap it out we should unset it rather than leave the placeholder.
-      if (substr($params[$field], 0, 6) == '$value') {
-        $params[$field] = NULL;
-      }
+      $count = count($stringParts);
     }
   }
+  return $value;
 }
 
 /**