Commit | Line | Data |
---|---|---|
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 | * |
be76e704 | 19 | * @return array|int|Civi\Api4\Generic\Result |
6a488035 | 20 | */ |
f27ab61d CW |
21 | function civicrm_api(string $entity, string $action, array $params) { |
22 | return \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params); | |
6a488035 TO |
23 | } |
24 | ||
f0b90b06 | 25 | /** |
0493ec47 | 26 | * CiviCRM API version 4. |
f0b90b06 | 27 | * |
9cea3619 CW |
28 | * This API (Application Programming Interface) is used to access and manage data in CiviCRM. |
29 | * | |
30 | * APIv4 is the latest stable version. | |
31 | * | |
0493ec47 | 32 | * @see https://docs.civicrm.org/dev/en/latest/api/v4/usage/ |
9cea3619 CW |
33 | * |
34 | * @param string $entity Name of the CiviCRM entity to access. | |
136ca5bb CW |
35 | * All entity names are capitalized CamelCase, e.g. `ContributionPage`. |
36 | * Most entities correspond to a database table (e.g. `Contact` is the table `civicrm_contact`). | |
37 | * For a complete list of available entities, call `civicrm_api4('Entity', 'get');` | |
9cea3619 CW |
38 | * |
39 | * @param string $action The "verb" of the api call. | |
136ca5bb | 40 | * For a complete list of actions for a given entity (e.g. `Contact`), call `civicrm_api4('Contact', 'getActions');` |
9cea3619 CW |
41 | * |
42 | * @param array $params An array of API input keyed by parameter name. | |
43 | * The easiest way to discover all available parameters is to visit the API Explorer on your CiviCRM site. | |
44 | * The API Explorer is listed in the CiviCRM menu under Support -> Developer. | |
45 | * | |
46 | * @param string|int|array $index Controls the Result array format. | |
47 | * By default the api Result contains a non-associative array of data. Passing an $index tells the api to | |
48 | * automatically reformat the array, depending on the variable type passed: | |
136ca5bb CW |
49 | * - **Integer:** return a single result array; |
50 | * e.g. `$index = 0` will return the first result, 1 will return the second, and -1 will return the last. | |
51 | * - **String:** index the results by a field value; | |
52 | * e.g. `$index = "name"` will return an associative array with the field 'name' as keys. | |
53 | * - **Non-associative array:** return a single value from each result; | |
54 | * e.g. `$index = ['title']` will return a non-associative array of strings - the 'title' field from each result. | |
55 | * - **Associative array:** a combination of the previous two modes; | |
56 | * e.g. `$index = ['name' => 'title']` will return an array of strings - the 'title' field keyed by the 'name' field. | |
f0b90b06 C |
57 | * |
58 | * @return \Civi\Api4\Generic\Result | |
59 | * @throws \API_Exception | |
60 | * @throws \Civi\API\Exception\NotImplementedException | |
61 | */ | |
0b0b329b | 62 | function civicrm_api4(string $entity, string $action, array $params = [], $index = NULL) { |
56d00639 CW |
63 | $indexField = $index && is_string($index) && !CRM_Utils_Rule::integer($index) ? $index : NULL; |
64 | $removeIndexField = FALSE; | |
65 | ||
66e016df | 66 | // If index field is not part of the select query, we add it here and remove it below (except for oddball "Setting" api) |
67fea9b4 | 67 | if ($indexField && !empty($params['select']) && is_array($params['select']) && !($entity === 'Setting' && $action === 'get') && !\Civi\Api4\Utils\SelectUtil::isFieldSelected($indexField, $params['select'])) { |
56d00639 CW |
68 | $params['select'][] = $indexField; |
69 | $removeIndexField = TRUE; | |
70 | } | |
3a8dc228 | 71 | $apiCall = \Civi\API\Request::create($entity, $action, ['version' => 4] + $params); |
4a7dc83a CW |
72 | |
73 | if ($index && is_array($index)) { | |
74 | $indexCol = reset($index); | |
75 | $indexField = key($index); | |
66e016df CW |
76 | // Index array indicates only 1 or 2 fields need to be selected (except for oddball "Setting" api) |
77 | if ($entity !== 'Setting' && property_exists($apiCall, 'select')) { | |
4a7dc83a CW |
78 | $apiCall->setSelect([$indexCol]); |
79 | if ($indexField && $indexField != $indexCol) { | |
80 | $apiCall->addSelect($indexField); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
f0b90b06 C |
85 | $result = $apiCall->execute(); |
86 | ||
87 | // Index results by key | |
56d00639 CW |
88 | if ($indexField) { |
89 | $result->indexBy($indexField); | |
90 | if ($removeIndexField) { | |
91 | foreach ($result as $key => $value) { | |
92 | unset($result[$key][$indexField]); | |
93 | } | |
94 | } | |
f0b90b06 C |
95 | } |
96 | // Return result at index | |
56d00639 | 97 | elseif (CRM_Utils_Rule::integer($index)) { |
f0b90b06 C |
98 | $item = $result->itemAt($index); |
99 | if (is_null($item)) { | |
100 | throw new \API_Exception("Index $index not found in api results"); | |
101 | } | |
102 | // Attempt to return a Result object if item is array, otherwise just return the item | |
103 | if (!is_array($item)) { | |
104 | return $item; | |
105 | } | |
106 | $result->exchangeArray($item); | |
f0b90b06 | 107 | } |
4a7dc83a CW |
108 | if (!empty($indexCol)) { |
109 | $result->exchangeArray($result->column($indexCol)); | |
110 | } | |
f0b90b06 C |
111 | return $result; |
112 | } | |
113 | ||
6b359437 | 114 | /** |
9d32e6f7 EM |
115 | * Version 3 wrapper for civicrm_api. |
116 | * | |
117 | * Throws exception. | |
8904518f | 118 | * |
cf470720 TO |
119 | * @param string $entity |
120 | * Type of entities to deal with. | |
121 | * @param string $action | |
122 | * Create, get, delete or some special action name. | |
123 | * @param array $params | |
124 | * Array to be passed to function. | |
6b359437 | 125 | * |
8904518f | 126 | * @throws CiviCRM_API3_Exception |
301906ab | 127 | * |
e0f5b841 BT |
128 | * @return array|int |
129 | * Dependant on the $action | |
6b359437 | 130 | */ |
0b0b329b | 131 | function civicrm_api3(string $entity, string $action, array $params = []) { |
6b359437 | 132 | $params['version'] = 3; |
7b810209 | 133 | $result = \Civi::service('civi_api_kernel')->runSafe($entity, $action, $params); |
9b873358 | 134 | if (is_array($result) && !empty($result['is_error'])) { |
6b359437 | 135 | throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result); |
136 | } | |
c4ac4df4 | 137 | return $result; |
6b359437 | 138 | } |
6a488035 | 139 | |
94359f7e | 140 | /** |
dc64d047 EM |
141 | * Call getfields from api wrapper. |
142 | * | |
143 | * This function ensures that settings that | |
7cdbcb16 TO |
144 | * could alter getfields output (e.g. action for all api & profile_id for |
145 | * profile api ) are consistently passed in. | |
94359f7e | 146 | * |
7cdbcb16 TO |
147 | * We check whether the api call is 'getfields' because if getfields is |
148 | * being called we return an empty array as no alias swapping, validation or | |
149 | * default filling is done on getfields & we want to avoid a loop | |
94359f7e | 150 | * |
151 | * @todo other output modifiers include contact_type | |
152 | * | |
153 | * @param array $apiRequest | |
dc64d047 | 154 | * |
a6c01b45 | 155 | * @return array |
e94b9071 | 156 | * getfields output |
94359f7e | 157 | */ |
158 | function _civicrm_api3_api_getfields(&$apiRequest) { | |
159 | if (strtolower($apiRequest['action'] == 'getfields')) { | |
160 | // the main param getfields takes is 'action' - however this param is not compatible with REST | |
161 | // so we accept 'api_action' as an alias of action on getfields | |
a7488080 | 162 | if (!empty($apiRequest['params']['api_action'])) { |
35671d00 TO |
163 | // $apiRequest['params']['action'] = $apiRequest['params']['api_action']; |
164 | // unset($apiRequest['params']['api_action']); | |
94359f7e | 165 | } |
cf8f0fff | 166 | return ['action' => ['api.aliases' => ['api_action']]]; |
94359f7e | 167 | } |
cf8f0fff | 168 | $getFieldsParams = ['action' => $apiRequest['action']]; |
94359f7e | 169 | $entity = $apiRequest['entity']; |
1644b908 | 170 | if ($entity == 'Profile' && array_key_exists('profile_id', $apiRequest['params'])) { |
94359f7e | 171 | $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id']; |
172 | } | |
173 | $fields = civicrm_api3($entity, 'getfields', $getFieldsParams); | |
174 | return $fields['values']; | |
175 | } | |
8904518f | 176 | |
6a488035 TO |
177 | /** |
178 | * Check if the result is an error. Note that this function has been retained from | |
179 | * api v2 for convenience but the result is more standardised in v3 and param | |
180 | * 'format.is_success' => 1 | |
181 | * will result in a boolean success /fail being returned if that is what you need. | |
182 | * | |
5e21e0f3 | 183 | * @param mixed $result |
8904518f | 184 | * |
5c766a0b | 185 | * @return bool |
e94b9071 | 186 | * true if error, false otherwise |
6a488035 TO |
187 | */ |
188 | function civicrm_error($result) { | |
1699214f | 189 | return is_array($result) && !empty($result['is_error']); |
6a488035 TO |
190 | } |
191 | ||
aa1b1481 | 192 | /** |
a828d7b8 | 193 | * Get camel case version of entity name. |
22242c87 | 194 | * |
4846df91 | 195 | * @param string|null $entity |
aa1b1481 | 196 | * |
4846df91 | 197 | * @return string|null |
aa1b1481 | 198 | */ |
dc913073 | 199 | function _civicrm_api_get_camel_name($entity) { |
a0f864fd | 200 | return is_string($entity) ? \Civi\API\Request::normalizeEntityName($entity) : NULL; |
6a488035 TO |
201 | } |
202 | ||
11e09c59 | 203 | /** |
22242c87 EM |
204 | * Swap out any $values vars. |
205 | * | |
206 | * Ie. the value after $value is swapped for the parent $result | |
6a488035 | 207 | * 'activity_type_id' => '$value.testfield', |
e94b9071 CW |
208 | * 'tag_id' => '$value.api.tag.create.id', |
209 | * 'tag1_id' => '$value.api.entity.create.0.id' | |
d424ffde | 210 | * |
e94b9071 CW |
211 | * @param array $params |
212 | * @param array $parentResult | |
213 | * @param string $separator | |
6a488035 | 214 | */ |
845d6d75 | 215 | function _civicrm_api_replace_variables(&$params, &$parentResult, $separator = '.') { |
10befc1f | 216 | foreach ($params as $field => &$value) { |
62780af8 JV |
217 | if (substr($field, 0, 4) == 'api.') { |
218 | // CRM-21246 - Leave nested calls alone. | |
219 | continue; | |
220 | } | |
6a488035 | 221 | if (is_string($value) && substr($value, 0, 6) == '$value') { |
10befc1f CW |
222 | $value = _civicrm_api_replace_variable($value, $parentResult, $separator); |
223 | } | |
224 | // Handle the operator syntax: array('OP' => $val) | |
225 | elseif (is_array($value) && is_string(reset($value)) && substr(reset($value), 0, 6) == '$value') { | |
226 | $key = key($value); | |
227 | $value[$key] = _civicrm_api_replace_variable($value[$key], $parentResult, $separator); | |
640aec1d CW |
228 | // A null value with an operator will cause an error, so remove it. |
229 | if ($value[$key] === NULL) { | |
230 | $value = ''; | |
231 | } | |
10befc1f CW |
232 | } |
233 | } | |
234 | } | |
6a488035 | 235 | |
10befc1f CW |
236 | /** |
237 | * Swap out a $value.foo variable with the value from parent api results. | |
238 | * | |
239 | * Called by _civicrm_api_replace_variables to do the substitution. | |
240 | * | |
241 | * @param string $value | |
242 | * @param array $parentResult | |
243 | * @param string $separator | |
244 | * @return mixed|null | |
245 | */ | |
246 | function _civicrm_api_replace_variable($value, $parentResult, $separator) { | |
247 | $valueSubstitute = substr($value, 7); | |
6a488035 | 248 | |
10befc1f CW |
249 | if (!empty($parentResult[$valueSubstitute])) { |
250 | return $parentResult[$valueSubstitute]; | |
251 | } | |
252 | else { | |
253 | $stringParts = explode($separator, $value); | |
254 | unset($stringParts[0]); | |
255 | // CRM-16168 If we have failed to swap it out we should unset it rather than leave the placeholder. | |
256 | $value = NULL; | |
6a488035 | 257 | |
10befc1f | 258 | $fieldname = array_shift($stringParts); |
6a488035 | 259 | |
10befc1f CW |
260 | //when our string is an array we will treat it as an array from that . onwards |
261 | $count = count($stringParts); | |
262 | while ($count > 0) { | |
263 | $fieldname .= "." . array_shift($stringParts); | |
264 | if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) { | |
265 | $arrayLocation = $parentResult[$fieldname]; | |
266 | foreach ($stringParts as $key => $innerValue) { | |
f748c073 | 267 | $arrayLocation = $arrayLocation[$innerValue] ?? NULL; |
6a488035 | 268 | } |
10befc1f | 269 | $value = $arrayLocation; |
6a488035 | 270 | } |
10befc1f | 271 | $count = count($stringParts); |
6a488035 TO |
272 | } |
273 | } | |
10befc1f | 274 | return $value; |
6a488035 TO |
275 | } |
276 | ||
11e09c59 | 277 | /** |
dc64d047 | 278 | * Convert possibly camel name to underscore separated entity name. |
6a488035 | 279 | * |
cf470720 | 280 | * @param string $entity |
7cdbcb16 TO |
281 | * Entity name in various formats e.g. Contribution, contribution, |
282 | * OptionValue, option_value, UFJoin, uf_join. | |
dc64d047 | 283 | * |
a6c01b45 | 284 | * @return string |
7cdbcb16 | 285 | * Entity name in underscore separated format. |
74c303ca | 286 | * |
287 | * @deprecated | |
6a488035 TO |
288 | */ |
289 | function _civicrm_api_get_entity_name_from_camel($entity) { | |
74c303ca | 290 | if (!$entity) { |
291 | // @todo - this should not be called when empty. | |
292 | return ''; | |
6a488035 | 293 | } |
74c303ca | 294 | return CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($entity); |
6a488035 | 295 | } |
11e09c59 TO |
296 | |
297 | /** | |
dc64d047 EM |
298 | * Having a DAO object find the entity name. |
299 | * | |
3d182a04 | 300 | * @param CRM_Core_DAO $bao |
cf470720 | 301 | * DAO being passed in. |
dc64d047 | 302 | * |
5d3523d9 | 303 | * @return string |
6a488035 | 304 | */ |
9b873358 | 305 | function _civicrm_api_get_entity_name_from_dao($bao) { |
3d182a04 | 306 | return CRM_Core_DAO_AllCoreTables::getBriefName(get_class($bao)); |
6a488035 | 307 | } |