Merge pull request #6424 from seamuslee001/4.6-CRM-13644
[civicrm-core.git] / api / api.php
index 9425c81057f3784d785ba656636f93b572045aca..8b55d97347e26335c6a7e5a35d93db3ac4556a8d 100644 (file)
@@ -1,16 +1,14 @@
 <?php
 
 /**
- * File for the CiviCRM APIv3 API wrapper
+ * @file CiviCRM APIv3 API wrapper.
  *
  * @package CiviCRM_APIv3
- * @subpackage API
- *
- * @copyright CiviCRM LLC (c) 2004-2014
- * @version $Id: api.php 30486 2010-11-02 16:12:09Z shot $
  */
 
 /**
+ * CiviCRM API wrapper function.
+ *
  * @param string $entity
  *   type of entities to deal with
  * @param string $action
@@ -26,7 +24,9 @@ function civicrm_api($entity, $action, $params, $extra = NULL) {
 }
 
 /**
- * Version 3 wrapper for civicrm_api. Throws exception
+ * Version 3 wrapper for civicrm_api.
+ *
+ * Throws exception.
  *
  * @param string $entity
  *   Type of entities to deal with.
@@ -48,7 +48,9 @@ function civicrm_api3($entity, $action, $params = array()) {
 }
 
 /**
- * Call getfields from api wrapper. This function ensures that settings that
+ * Call getfields from api wrapper.
+ *
+ * This function ensures that settings that
  * could alter getfields output (e.g. action for all api & profile_id for
  * profile api ) are consistently passed in.
  *
@@ -59,6 +61,7 @@ function civicrm_api3($entity, $action, $params = array()) {
  * @todo other output modifiers include contact_type
  *
  * @param array $apiRequest
+ *
  * @return array
  *   getfields output
  */
@@ -74,7 +77,7 @@ function _civicrm_api3_api_getfields(&$apiRequest) {
   }
   $getFieldsParams = array('action' => $apiRequest['action']);
   $entity = $apiRequest['entity'];
-  if ($entity == 'profile' && array_key_exists('profile_id', $apiRequest['params'])) {
+  if ($entity == 'Profile' && array_key_exists('profile_id', $apiRequest['params'])) {
     $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id'];
   }
   $fields = civicrm_api3($entity, 'getfields', $getFieldsParams);
@@ -102,16 +105,20 @@ function civicrm_error($result) {
 }
 
 /**
- * @param $entity
+ * Get camel case version of entity name.
  *
- * @return string
+ * @param string|null $entity
+ *
+ * @return string|null
  */
 function _civicrm_api_get_camel_name($entity) {
-  return CRM_Utils_String::convertStringToCamel($entity);
+  return is_string($entity) ? CRM_Utils_String::convertStringToCamel($entity) : NULL;
 }
 
 /**
- * Swap out any $values vars - ie. the value after $value is swapped for the parent $result
+ * Swap out any $values vars.
+ *
+ * Ie. the value after $value is swapped for the parent $result
  * 'activity_type_id' => '$value.testfield',
  * 'tag_id'  => '$value.api.tag.create.id',
  * 'tag1_id' => '$value.api.entity.create.0.id'
@@ -151,23 +158,26 @@ function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '
           $count = count($stringParts);
         }
       }
+      // 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;
+      }
     }
   }
 }
 
 /**
- * Convert possibly camel name to underscore separated entity name
+ * Convert possibly camel name to underscore separated entity name.
  *
  * @param string $entity
  *   Entity name in various formats e.g. Contribution, contribution,
  *   OptionValue, option_value, UFJoin, uf_join.
+ *
  * @return string
  *   Entity name in underscore separated format.
- *
- * @fixme Why isn't this called first thing in civicrm_api wrapper?
  */
 function _civicrm_api_get_entity_name_from_camel($entity) {
-  if ($entity == strtolower($entity)) {
+  if (!$entity || $entity === strtolower($entity)) {
     return $entity;
   }
   else {
@@ -181,12 +191,14 @@ function _civicrm_api_get_entity_name_from_camel($entity) {
 }
 
 /**
- * Having a DAO object find the entity name
+ * Having a DAO object find the entity name.
+ *
  * @param object $bao
  *   DAO being passed in.
+ *
  * @return string
  */
 function _civicrm_api_get_entity_name_from_dao($bao) {
   $daoName = str_replace("BAO", "DAO", get_class($bao));
-  return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName));
+  return CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
 }