4 * @file CiviCRM APIv3 API wrapper.
6 * @package CiviCRM_APIv3
10 * CiviCRM API wrapper function.
12 * @param string $entity
13 * type of entities to deal with
14 * @param string $action
15 * create, get, delete or some special action name.
16 * @param array $params
17 * array to be passed to function
22 function civicrm_api($entity, $action, $params, $extra = NULL) {
23 return \Civi
::service('civi_api_kernel')->runSafe($entity, $action, $params, $extra);
27 * Version 3 wrapper for civicrm_api.
31 * @param string $entity
32 * Type of entities to deal with.
33 * @param string $action
34 * Create, get, delete or some special action name.
35 * @param array $params
36 * Array to be passed to function.
38 * @throws CiviCRM_API3_Exception
42 function civicrm_api3($entity, $action, $params = []) {
43 $params['version'] = 3;
44 $result = \Civi
::service('civi_api_kernel')->runSafe($entity, $action, $params);
45 if (is_array($result) && !empty($result['is_error'])) {
46 throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array
::value('error_code', $result, 'undefined'), $result);
52 * Call getfields from api wrapper.
54 * This function ensures that settings that
55 * could alter getfields output (e.g. action for all api & profile_id for
56 * profile api ) are consistently passed in.
58 * We check whether the api call is 'getfields' because if getfields is
59 * being called we return an empty array as no alias swapping, validation or
60 * default filling is done on getfields & we want to avoid a loop
62 * @todo other output modifiers include contact_type
64 * @param array $apiRequest
69 function _civicrm_api3_api_getfields(&$apiRequest) {
70 if (strtolower($apiRequest['action'] == 'getfields')) {
71 // the main param getfields takes is 'action' - however this param is not compatible with REST
72 // so we accept 'api_action' as an alias of action on getfields
73 if (!empty($apiRequest['params']['api_action'])) {
74 // $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
75 // unset($apiRequest['params']['api_action']);
77 return ['action' => ['api.aliases' => ['api_action']]];
79 $getFieldsParams = ['action' => $apiRequest['action']];
80 $entity = $apiRequest['entity'];
81 if ($entity == 'Profile' && array_key_exists('profile_id', $apiRequest['params'])) {
82 $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id'];
84 $fields = civicrm_api3($entity, 'getfields', $getFieldsParams);
85 return $fields['values'];
89 * Check if the result is an error. Note that this function has been retained from
90 * api v2 for convenience but the result is more standardised in v3 and param
91 * 'format.is_success' => 1
92 * will result in a boolean success /fail being returned if that is what you need.
97 * true if error, false otherwise
99 function civicrm_error($result) {
100 if (is_array($result)) {
101 return (array_key_exists('is_error', $result) &&
109 * Get camel case version of entity name.
111 * @param string|null $entity
113 * @return string|null
115 function _civicrm_api_get_camel_name($entity) {
116 return is_string($entity) ? CRM_Utils_String
::convertStringToCamel($entity) : NULL;
120 * Swap out any $values vars.
122 * Ie. the value after $value is swapped for the parent $result
123 * 'activity_type_id' => '$value.testfield',
124 * 'tag_id' => '$value.api.tag.create.id',
125 * 'tag1_id' => '$value.api.entity.create.0.id'
127 * @param array $params
128 * @param array $parentResult
129 * @param string $separator
131 function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '.') {
132 foreach ($params as $field => &$value) {
133 if (substr($field, 0, 4) == 'api.') {
134 // CRM-21246 - Leave nested calls alone.
137 if (is_string($value) && substr($value, 0, 6) == '$value') {
138 $value = _civicrm_api_replace_variable($value, $parentResult, $separator);
140 // Handle the operator syntax: array('OP' => $val)
141 elseif (is_array($value) && is_string(reset($value)) && substr(reset($value), 0, 6) == '$value') {
143 $value[$key] = _civicrm_api_replace_variable($value[$key], $parentResult, $separator);
144 // A null value with an operator will cause an error, so remove it.
145 if ($value[$key] === NULL) {
153 * Swap out a $value.foo variable with the value from parent api results.
155 * Called by _civicrm_api_replace_variables to do the substitution.
157 * @param string $value
158 * @param array $parentResult
159 * @param string $separator
162 function _civicrm_api_replace_variable($value, $parentResult, $separator) {
163 $valueSubstitute = substr($value, 7);
165 if (!empty($parentResult[$valueSubstitute])) {
166 return $parentResult[$valueSubstitute];
169 $stringParts = explode($separator, $value);
170 unset($stringParts[0]);
171 // CRM-16168 If we have failed to swap it out we should unset it rather than leave the placeholder.
174 $fieldname = array_shift($stringParts);
176 //when our string is an array we will treat it as an array from that . onwards
177 $count = count($stringParts);
179 $fieldname .= "." . array_shift($stringParts);
180 if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) {
181 $arrayLocation = $parentResult[$fieldname];
182 foreach ($stringParts as $key => $innerValue) {
183 $arrayLocation = CRM_Utils_Array
::value($innerValue, $arrayLocation);
185 $value = $arrayLocation;
187 $count = count($stringParts);
194 * Convert possibly camel name to underscore separated entity name.
196 * @param string $entity
197 * Entity name in various formats e.g. Contribution, contribution,
198 * OptionValue, option_value, UFJoin, uf_join.
201 * Entity name in underscore separated format.
203 function _civicrm_api_get_entity_name_from_camel($entity) {
204 if (!$entity ||
$entity === strtolower($entity)) {
207 elseif ($entity == 'PCP') {
211 $entity = ltrim(strtolower(str_replace('U_F',
213 // That's CamelCase, beside an odd UFCamel that is expected as uf_camel
214 preg_replace('/(?=[A-Z])/', '_$0', $entity)
221 * Having a DAO object find the entity name.
224 * DAO being passed in.
228 function _civicrm_api_get_entity_name_from_dao($bao) {
229 $daoName = str_replace("BAO", "DAO", get_class($bao));
230 return CRM_Core_DAO_AllCoreTables
::getBriefName($daoName);