Preliminary cleanup - use CRM_Core_Exception, fix comments
[civicrm-core.git] / api / api.php
CommitLineData
6a488035
TO
1<?php
2
3/**
b081365f 4 * @file CiviCRM APIv3 API wrapper.
6a488035
TO
5 *
6 * @package CiviCRM_APIv3
6a488035
TO
7 */
8
11e09c59 9/**
9d32e6f7
EM
10 * CiviCRM API wrapper function.
11 *
6a488035
TO
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
8904518f 18 * @param null $extra
19 *
20 * @return array|int
6a488035
TO
21 */
22function civicrm_api($entity, $action, $params, $extra = NULL) {
7b810209 23 return \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params, $extra);
6a488035
TO
24}
25
6b359437 26/**
9d32e6f7
EM
27 * Version 3 wrapper for civicrm_api.
28 *
29 * Throws exception.
8904518f 30 *
cf470720
TO
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.
6b359437 37 *
8904518f 38 * @throws CiviCRM_API3_Exception
301906ab 39 *
6b359437 40 * @return array
6b359437 41 */
cf8f0fff 42function civicrm_api3($entity, $action, $params = []) {
6b359437 43 $params['version'] = 3;
7b810209 44 $result = \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params);
9b873358 45 if (is_array($result) && !empty($result['is_error'])) {
6b359437 46 throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result);
47 }
c4ac4df4 48 return $result;
6b359437 49}
6a488035 50
94359f7e 51/**
dc64d047
EM
52 * Call getfields from api wrapper.
53 *
54 * This function ensures that settings that
7cdbcb16
TO
55 * could alter getfields output (e.g. action for all api & profile_id for
56 * profile api ) are consistently passed in.
94359f7e 57 *
7cdbcb16
TO
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
94359f7e 61 *
62 * @todo other output modifiers include contact_type
63 *
64 * @param array $apiRequest
dc64d047 65 *
a6c01b45 66 * @return array
e94b9071 67 * getfields output
94359f7e 68 */
69function _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
a7488080 73 if (!empty($apiRequest['params']['api_action'])) {
35671d00
TO
74 // $apiRequest['params']['action'] = $apiRequest['params']['api_action'];
75 // unset($apiRequest['params']['api_action']);
94359f7e 76 }
cf8f0fff 77 return ['action' => ['api.aliases' => ['api_action']]];
94359f7e 78 }
cf8f0fff 79 $getFieldsParams = ['action' => $apiRequest['action']];
94359f7e 80 $entity = $apiRequest['entity'];
1644b908 81 if ($entity == 'Profile' && array_key_exists('profile_id', $apiRequest['params'])) {
94359f7e 82 $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id'];
83 }
84 $fields = civicrm_api3($entity, 'getfields', $getFieldsParams);
85 return $fields['values'];
86}
8904518f 87
6a488035
TO
88/**
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.
93 *
8904518f 94 * @param $result
95 *
5c766a0b 96 * @return bool
e94b9071 97 * true if error, false otherwise
6a488035
TO
98 */
99function civicrm_error($result) {
100 if (is_array($result)) {
101 return (array_key_exists('is_error', $result) &&
102 $result['is_error']
103 ) ? TRUE : FALSE;
104 }
105 return FALSE;
106}
107
aa1b1481 108/**
a828d7b8 109 * Get camel case version of entity name.
22242c87 110 *
4846df91 111 * @param string|null $entity
aa1b1481 112 *
4846df91 113 * @return string|null
aa1b1481 114 */
dc913073 115function _civicrm_api_get_camel_name($entity) {
4846df91 116 return is_string($entity) ? CRM_Utils_String::convertStringToCamel($entity) : NULL;
6a488035
TO
117}
118
11e09c59 119/**
22242c87
EM
120 * Swap out any $values vars.
121 *
122 * Ie. the value after $value is swapped for the parent $result
6a488035 123 * 'activity_type_id' => '$value.testfield',
e94b9071
CW
124 * 'tag_id' => '$value.api.tag.create.id',
125 * 'tag1_id' => '$value.api.entity.create.0.id'
d424ffde 126 *
e94b9071
CW
127 * @param array $params
128 * @param array $parentResult
129 * @param string $separator
6a488035 130 */
845d6d75 131function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '.') {
10befc1f 132 foreach ($params as $field => &$value) {
62780af8
JV
133 if (substr($field, 0, 4) == 'api.') {
134 // CRM-21246 - Leave nested calls alone.
135 continue;
136 }
6a488035 137 if (is_string($value) && substr($value, 0, 6) == '$value') {
10befc1f
CW
138 $value = _civicrm_api_replace_variable($value, $parentResult, $separator);
139 }
140 // Handle the operator syntax: array('OP' => $val)
141 elseif (is_array($value) && is_string(reset($value)) && substr(reset($value), 0, 6) == '$value') {
142 $key = key($value);
143 $value[$key] = _civicrm_api_replace_variable($value[$key], $parentResult, $separator);
640aec1d
CW
144 // A null value with an operator will cause an error, so remove it.
145 if ($value[$key] === NULL) {
146 $value = '';
147 }
10befc1f
CW
148 }
149 }
150}
6a488035 151
10befc1f
CW
152/**
153 * Swap out a $value.foo variable with the value from parent api results.
154 *
155 * Called by _civicrm_api_replace_variables to do the substitution.
156 *
157 * @param string $value
158 * @param array $parentResult
159 * @param string $separator
160 * @return mixed|null
161 */
162function _civicrm_api_replace_variable($value, $parentResult, $separator) {
163 $valueSubstitute = substr($value, 7);
6a488035 164
10befc1f
CW
165 if (!empty($parentResult[$valueSubstitute])) {
166 return $parentResult[$valueSubstitute];
167 }
168 else {
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.
172 $value = NULL;
6a488035 173
10befc1f 174 $fieldname = array_shift($stringParts);
6a488035 175
10befc1f
CW
176 //when our string is an array we will treat it as an array from that . onwards
177 $count = count($stringParts);
178 while ($count > 0) {
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);
6a488035 184 }
10befc1f 185 $value = $arrayLocation;
6a488035 186 }
10befc1f 187 $count = count($stringParts);
6a488035
TO
188 }
189 }
10befc1f 190 return $value;
6a488035
TO
191}
192
11e09c59 193/**
dc64d047 194 * Convert possibly camel name to underscore separated entity name.
6a488035 195 *
cf470720 196 * @param string $entity
7cdbcb16
TO
197 * Entity name in various formats e.g. Contribution, contribution,
198 * OptionValue, option_value, UFJoin, uf_join.
dc64d047 199 *
a6c01b45 200 * @return string
7cdbcb16 201 * Entity name in underscore separated format.
6a488035
TO
202 */
203function _civicrm_api_get_entity_name_from_camel($entity) {
1644b908 204 if (!$entity || $entity === strtolower($entity)) {
6a488035
TO
205 return $entity;
206 }
a861c4f5
SL
207 elseif ($entity == 'PCP') {
208 return 'pcp';
209 }
6a488035
TO
210 else {
211 $entity = ltrim(strtolower(str_replace('U_F',
212 'uf',
213 // That's CamelCase, beside an odd UFCamel that is expected as uf_camel
214 preg_replace('/(?=[A-Z])/', '_$0', $entity)
215 )), '_');
216 }
217 return $entity;
218}
11e09c59
TO
219
220/**
dc64d047
EM
221 * Having a DAO object find the entity name.
222 *
cf470720
TO
223 * @param object $bao
224 * DAO being passed in.
dc64d047 225 *
5d3523d9 226 * @return string
6a488035 227 */
9b873358 228function _civicrm_api_get_entity_name_from_dao($bao) {
6a488035 229 $daoName = str_replace("BAO", "DAO", get_class($bao));
4846df91 230 return CRM_Core_DAO_AllCoreTables::getBriefName($daoName);
6a488035 231}