Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /** | |
4 | * File for the CiviCRM APIv3 API wrapper | |
5 | * | |
6 | * @package CiviCRM_APIv3 | |
7 | * @subpackage API | |
8 | * | |
9 | * @copyright CiviCRM LLC (c) 2004-2013 | |
10 | * @version $Id: api.php 30486 2010-11-02 16:12:09Z shot $ | |
11 | */ | |
12 | ||
11e09c59 | 13 | /** |
6a488035 TO |
14 | * @param string $entity |
15 | * type of entities to deal with | |
16 | * @param string $action | |
17 | * create, get, delete or some special action name. | |
18 | * @param array $params | |
19 | * array to be passed to function | |
8904518f | 20 | * @param null $extra |
21 | * | |
22 | * @return array|int | |
6a488035 TO |
23 | */ |
24 | function civicrm_api($entity, $action, $params, $extra = NULL) { | |
5270c026 XD |
25 | $apiRequest = array(); |
26 | $apiRequest['entity'] = CRM_Utils_String::munge($entity); | |
27 | $apiRequest['action'] = CRM_Utils_String::munge($action); | |
28 | $apiRequest['version'] = civicrm_get_api_version($params); | |
29 | $apiRequest['params'] = $params; | |
30 | $apiRequest['extra'] = $extra; | |
31 | ||
a9396478 TO |
32 | $apiWrappers = array( |
33 | CRM_Utils_API_HTMLInputCoder::singleton(), | |
0e4d4398 | 34 | CRM_Utils_API_NullOutputCoder::singleton(), |
ac830223 | 35 | CRM_Utils_API_ReloadOption::singleton(), |
c8463688 | 36 | CRM_Utils_API_MatchOption::singleton(), |
a9396478 | 37 | ); |
5270c026 XD |
38 | CRM_Utils_Hook::apiWrappers($apiWrappers,$apiRequest); |
39 | ||
6a488035 TO |
40 | try { |
41 | require_once ('api/v3/utils.php'); | |
42 | require_once 'api/Exception.php'; | |
43 | if (!is_array($params)) { | |
44 | throw new API_Exception('Input variable `params` is not an array', 2000); | |
45 | } | |
46 | _civicrm_api3_initialize(); | |
47 | $errorScope = CRM_Core_TemporaryErrorScope::useException(); | |
6a488035 TO |
48 | // look up function, file, is_generic |
49 | $apiRequest += _civicrm_api_resolve($apiRequest); | |
db1791f7 | 50 | if (strtolower($action) == 'create' || strtolower($action) == 'delete' || strtolower($action) == 'submit') { |
6a488035 TO |
51 | $apiRequest['is_transactional'] = 1; |
52 | $transaction = new CRM_Core_Transaction(); | |
53 | } | |
54 | ||
91cd40ea ML |
55 | // support multi-lingual requests |
56 | if ($language = CRM_Utils_Array::value('option.language', $params)) { | |
57 | _civicrm_api_set_locale($language); | |
58 | } | |
59 | ||
6a488035 | 60 | _civicrm_api3_api_check_permission($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']); |
94359f7e | 61 | $fields = _civicrm_api3_api_getfields($apiRequest); |
6a488035 | 62 | // we do this before we |
94359f7e | 63 | _civicrm_api3_swap_out_aliases($apiRequest, $fields); |
6a488035 | 64 | if (strtolower($action) != 'getfields') { |
a7488080 | 65 | if (empty($apiRequest['params']['id'])) { |
94359f7e | 66 | $apiRequest['params'] = array_merge(_civicrm_api3_getdefaults($apiRequest, $fields), $apiRequest['params']); |
6a488035 TO |
67 | } |
68 | //if 'id' is set then only 'version' will be checked but should still be checked for consistency | |
94359f7e | 69 | civicrm_api3_verify_mandatory($apiRequest['params'], NULL, _civicrm_api3_getrequired($apiRequest, $fields)); |
6a488035 TO |
70 | } |
71 | ||
a9396478 | 72 | // For input filtering, process $apiWrappers in forward order |
6a488035 TO |
73 | foreach ($apiWrappers as $apiWrapper) { |
74 | $apiRequest = $apiWrapper->fromApiInput($apiRequest); | |
75 | } | |
76 | ||
77 | $function = $apiRequest['function']; | |
78 | if ($apiRequest['function'] && $apiRequest['is_generic']) { | |
79 | // Unlike normal API implementations, generic implementations require explicit | |
80 | // knowledge of the entity and action (as well as $params). Bundle up these bits | |
81 | // into a convenient data structure. | |
82 | $result = $function($apiRequest); | |
83 | } | |
84 | elseif ($apiRequest['function'] && !$apiRequest['is_generic']) { | |
94359f7e | 85 | _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $fields); |
6a488035 TO |
86 | |
87 | $result = isset($extra) ? $function($apiRequest['params'], $extra) : $function($apiRequest['params']); | |
88 | } | |
89 | else { | |
53949b36 | 90 | return civicrm_api3_create_error("API (" . $apiRequest['entity'] . ", " . $apiRequest['action'] . ") does not exist (join the API team and implement it!)"); |
6a488035 TO |
91 | } |
92 | ||
a9396478 TO |
93 | // For output filtering, process $apiWrappers in reverse order |
94 | foreach (array_reverse($apiWrappers) as $apiWrapper) { | |
6a488035 TO |
95 | $result = $apiWrapper->toApiOutput($apiRequest, $result); |
96 | } | |
97 | ||
98 | if (CRM_Utils_Array::value('format.is_success', $apiRequest['params']) == 1) { | |
99 | if ($result['is_error'] === 0) { | |
100 | return 1; | |
101 | } | |
102 | else { | |
103 | return 0; | |
104 | } | |
105 | } | |
a7488080 | 106 | if (!empty($apiRequest['params']['format.only_id']) && isset($result['id'])) { |
6a488035 TO |
107 | return $result['id']; |
108 | } | |
109 | if (CRM_Utils_Array::value('is_error', $result, 0) == 0) { | |
110 | _civicrm_api_call_nested_api($apiRequest['params'], $result, $apiRequest['action'], $apiRequest['entity'], $apiRequest['version']); | |
111 | } | |
112 | if (function_exists('xdebug_time_index') | |
113 | && CRM_Utils_Array::value('debug', $apiRequest['params']) | |
114 | // result would not be an array for getvalue | |
115 | && is_array($result) | |
116 | ) { | |
117 | $result['xdebug']['peakMemory'] = xdebug_peak_memory_usage(); | |
118 | $result['xdebug']['memory'] = xdebug_memory_usage(); | |
119 | $result['xdebug']['timeIndex'] = xdebug_time_index(); | |
120 | } | |
121 | ||
122 | return $result; | |
123 | } | |
124 | catch(PEAR_Exception $e) { | |
125 | if (CRM_Utils_Array::value('format.is_success', $apiRequest['params']) == 1) { | |
126 | return 0; | |
127 | } | |
c56bd790 XD |
128 | $error = $e->getCause(); |
129 | if ($error instanceof DB_Error) { | |
130 | $data["error_code"] = DB::errorMessage($error->getCode()); | |
131 | $data["sql"] = $error->getDebugInfo(); | |
132 | } | |
a7488080 | 133 | if (!empty($apiRequest['params']['debug'])) { |
9aeec998 | 134 | if(method_exists($e, 'getUserInfo')) { |
135 | $data['debug_info'] = $error->getUserInfo(); | |
136 | } | |
137 | if(method_exists($e, 'getExtraData')) { | |
138 | $data['debug_info'] = $data + $error->getExtraData(); | |
139 | } | |
6a488035 TO |
140 | $data['trace'] = $e->getTraceAsString(); |
141 | } | |
142 | else{ | |
143 | $data['tip'] = "add debug=1 to your API call to have more info about the error"; | |
144 | } | |
145 | $err = civicrm_api3_create_error($e->getMessage(), $data, $apiRequest); | |
a7488080 | 146 | if (!empty($apiRequest['is_transactional'])) { |
6a488035 TO |
147 | $transaction->rollback(); |
148 | } | |
149 | return $err; | |
150 | } | |
151 | catch (API_Exception $e){ | |
152 | if(!isset($apiRequest)){ | |
153 | $apiRequest = array(); | |
154 | } | |
155 | if (CRM_Utils_Array::value('format.is_success', CRM_Utils_Array::value('params',$apiRequest)) == 1) { | |
156 | return 0; | |
157 | } | |
158 | $data = $e->getExtraParams(); | |
159 | $data['entity'] = CRM_Utils_Array::value('entity', $apiRequest); | |
160 | $data['action'] = CRM_Utils_Array::value('action', $apiRequest); | |
161 | $err = civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode()); | |
162 | if (CRM_Utils_Array::value('debug', CRM_Utils_Array::value('params',$apiRequest)) | |
163 | && empty($data['trace']) // prevent recursion | |
164 | ) { | |
165 | $err['trace'] = $e->getTraceAsString(); | |
166 | } | |
a7488080 | 167 | if (!empty($apiRequest['is_transactional'])) { |
6a488035 TO |
168 | $transaction->rollback(); |
169 | } | |
170 | return $err; | |
171 | } | |
172 | catch(Exception $e) { | |
173 | if (CRM_Utils_Array::value('format.is_success', $apiRequest['params']) == 1) { | |
174 | return 0; | |
175 | } | |
176 | $data = array(); | |
177 | $err = civicrm_api3_create_error($e->getMessage(), $data, $apiRequest, $e->getCode()); | |
a7488080 | 178 | if (!empty($apiRequest['params']['debug'])) { |
6a488035 TO |
179 | $err['trace'] = $e->getTraceAsString(); |
180 | } | |
a7488080 | 181 | if (!empty($apiRequest['is_transactional'])) { |
6a488035 TO |
182 | $transaction->rollback(); |
183 | } | |
184 | return $err; | |
185 | } | |
186 | } | |
187 | ||
188 | /** | |
189 | * Look up the implementation for a given API request | |
190 | * | |
191 | * @param $apiRequest array with keys: | |
192 | * - entity: string, required | |
193 | * - action: string, required | |
194 | * - params: array | |
195 | * - version: scalar, required | |
196 | * | |
197 | * @return array with keys | |
198 | * - function: callback (mixed) | |
199 | * - is_generic: boolean | |
200 | */ | |
201 | function _civicrm_api_resolve($apiRequest) { | |
202 | static $cache; | |
203 | $cachekey = strtolower($apiRequest['entity']) . ':' . strtolower($apiRequest['action']) . ':' . $apiRequest['version']; | |
204 | if (isset($cache[$cachekey])) { | |
205 | return $cache[$cachekey]; | |
206 | } | |
207 | ||
208 | $camelName = _civicrm_api_get_camel_name($apiRequest['entity'], $apiRequest['version']); | |
209 | $actionCamelName = _civicrm_api_get_camel_name($apiRequest['action']); | |
210 | ||
211 | // Determine if there is an entity-specific implementation of the action | |
212 | $stdFunction = civicrm_api_get_function_name($apiRequest['entity'], $apiRequest['action'], $apiRequest['version']); | |
213 | if (function_exists($stdFunction)) { | |
214 | // someone already loaded the appropriate file | |
215 | // FIXME: This has the affect of masking bugs in load order; this is included to provide bug-compatibility | |
216 | $cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE); | |
217 | return $cache[$cachekey]; | |
218 | } | |
219 | ||
220 | $stdFiles = array( | |
221 | // By convention, the $camelName.php is more likely to contain the function, so test it first | |
222 | 'api/v' . $apiRequest['version'] . '/' . $camelName . '.php', | |
223 | 'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php', | |
224 | ); | |
225 | foreach ($stdFiles as $stdFile) { | |
6a488035 TO |
226 | if (CRM_Utils_File::isIncludable($stdFile)) { |
227 | require_once $stdFile; | |
228 | if (function_exists($stdFunction)) { | |
229 | $cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE); | |
230 | return $cache[$cachekey]; | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | // Determine if there is a generic implementation of the action | |
236 | require_once 'api/v3/Generic.php'; | |
237 | # $genericFunction = 'civicrm_api3_generic_' . $apiRequest['action']; | |
238 | $genericFunction = civicrm_api_get_function_name('generic', $apiRequest['action'], $apiRequest['version']); | |
239 | $genericFiles = array( | |
240 | // By convention, the Generic.php is more likely to contain the function, so test it first | |
241 | 'api/v' . $apiRequest['version'] . '/Generic.php', | |
242 | 'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php', | |
243 | ); | |
244 | foreach ($genericFiles as $genericFile) { | |
6a488035 TO |
245 | if (CRM_Utils_File::isIncludable($genericFile)) { |
246 | require_once $genericFile; | |
247 | if (function_exists($genericFunction)) { | |
248 | $cache[$cachekey] = array('function' => $genericFunction, 'is_generic' => TRUE); | |
249 | return $cache[$cachekey]; | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
254 | $cache[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE); | |
255 | return $cache[$cachekey]; | |
256 | } | |
8904518f | 257 | |
6b359437 | 258 | /** |
259 | * Version 3 wrapper for civicrm_api. Throws exception | |
8904518f | 260 | * |
6b359437 | 261 | * @param string $entity type of entities to deal with |
262 | * @param string $action create, get, delete or some special action name. | |
263 | * @param array $params array to be passed to function | |
264 | * | |
8904518f | 265 | * @throws CiviCRM_API3_Exception |
6b359437 | 266 | * @return array |
6b359437 | 267 | */ |
67744c4e | 268 | function civicrm_api3($entity, $action, $params = array()) { |
6b359437 | 269 | $params['version'] = 3; |
270 | $result = civicrm_api($entity, $action, $params); | |
d661281d | 271 | if(is_array($result) && !empty($result['is_error'])){ |
6b359437 | 272 | throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result); |
273 | } | |
c4ac4df4 | 274 | return $result; |
6b359437 | 275 | } |
6a488035 | 276 | |
94359f7e | 277 | /** |
278 | * Function to call getfields from api wrapper. This function ensures that settings that could alter | |
279 | * getfields output (e.g. action for all api & profile_id for profile api ) are consistently passed in. | |
280 | * | |
281 | * We check whether the api call is 'getfields' because if getfields is being called we return an empty array | |
282 | * as no alias swapping, validation or default filling is done on getfields & we want to avoid a loop | |
283 | * | |
284 | * @todo other output modifiers include contact_type | |
285 | * | |
286 | * @param array $apiRequest | |
287 | * @return getfields output | |
288 | */ | |
289 | function _civicrm_api3_api_getfields(&$apiRequest) { | |
290 | if (strtolower($apiRequest['action'] == 'getfields')) { | |
291 | // the main param getfields takes is 'action' - however this param is not compatible with REST | |
292 | // so we accept 'api_action' as an alias of action on getfields | |
a7488080 | 293 | if (!empty($apiRequest['params']['api_action'])) { |
94359f7e | 294 | // $apiRequest['params']['action'] = $apiRequest['params']['api_action']; |
295 | // unset($apiRequest['params']['api_action']); | |
296 | } | |
de0a1900 | 297 | return array('action' => array('api.aliases' => array('api_action'))); |
94359f7e | 298 | } |
299 | $getFieldsParams = array('action' => $apiRequest['action']); | |
300 | $entity = $apiRequest['entity']; | |
301 | if($entity == 'profile' && array_key_exists('profile_id', $apiRequest['params'])) { | |
302 | $getFieldsParams['profile_id'] = $apiRequest['params']['profile_id']; | |
303 | } | |
304 | $fields = civicrm_api3($entity, 'getfields', $getFieldsParams); | |
305 | return $fields['values']; | |
306 | } | |
8904518f | 307 | |
6a488035 TO |
308 | /** |
309 | * Load/require all files related to an entity. | |
310 | * | |
311 | * This should not normally be called because it's does a file-system scan; it's | |
312 | * only appropriate when introspection is really required (eg for "getActions"). | |
313 | * | |
314 | * @param string $entity | |
8904518f | 315 | * @param int $version |
316 | * | |
6a488035 TO |
317 | * @return void |
318 | */ | |
319 | function _civicrm_api_loadEntity($entity, $version = 3) { | |
320 | /* | |
321 | $apiRequest = array(); | |
322 | $apiRequest['entity'] = $entity; | |
323 | $apiRequest['action'] = 'pretty sure it will never exist. Trick to [try to] force resolve to scan everywhere'; | |
324 | $apiRequest['version'] = $version; | |
325 | // look up function, file, is_generic | |
326 | $apiRequest = _civicrm_api_resolve($apiRequest); | |
327 | */ | |
328 | ||
329 | $camelName = _civicrm_api_get_camel_name($entity, $version); | |
330 | ||
331 | // Check for master entity file; to match _civicrm_api_resolve(), only load the first one | |
6a488035 TO |
332 | $stdFile = 'api/v' . $version . '/' . $camelName . '.php'; |
333 | if (CRM_Utils_File::isIncludable($stdFile)) { | |
334 | require_once $stdFile; | |
335 | } | |
336 | ||
337 | // Check for standalone action files; to match _civicrm_api_resolve(), only load the first one | |
338 | $loaded_files = array(); // array($relativeFilePath => TRUE) | |
339 | $include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path())); | |
340 | foreach ($include_dirs as $include_dir) { | |
341 | $action_dir = implode(DIRECTORY_SEPARATOR, array($include_dir, 'api', "v${version}", $camelName)); | |
342 | if (! is_dir($action_dir)) { | |
343 | continue; | |
344 | } | |
345 | ||
346 | $iterator = new DirectoryIterator($action_dir); | |
347 | foreach ($iterator as $fileinfo) { | |
348 | $file = $fileinfo->getFilename(); | |
349 | if (array_key_exists($file, $loaded_files)) { | |
350 | continue; // action provided by an earlier item on include_path | |
351 | } | |
352 | ||
353 | $parts = explode(".", $file); | |
354 | if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file) ) { | |
355 | require_once $action_dir . DIRECTORY_SEPARATOR . $file; | |
356 | $loaded_files[$file] = TRUE; | |
357 | } | |
358 | } | |
359 | } | |
360 | } | |
361 | ||
362 | /** | |
363 | * | |
364 | * @deprecated | |
365 | */ | |
366 | function civicrm_api_get_function_name($entity, $action, $version = NULL) { | |
367 | ||
368 | if (empty($version)) { | |
369 | $version = civicrm_get_api_version(); | |
370 | } | |
371 | ||
372 | $entity = _civicrm_api_get_entity_name_from_camel($entity); | |
373 | return 'civicrm_api3' . '_' . $entity . '_' . $action; | |
374 | } | |
375 | ||
376 | /** | |
377 | * We must be sure that every request uses only one version of the API. | |
378 | * | |
379 | * @param $desired_version : array or integer | |
380 | * One chance to set the version number. | |
381 | * After that, this version number will be used for the remaining request. | |
382 | * This can either be a number, or an array(.., 'version' => $version, ..). | |
383 | * This allows to directly pass the $params array. | |
8904518f | 384 | * @return int |
6a488035 TO |
385 | */ |
386 | function civicrm_get_api_version($desired_version = NULL) { | |
387 | ||
388 | if (is_array($desired_version)) { | |
389 | // someone gave the full $params array. | |
390 | $params = $desired_version; | |
391 | $desired_version = empty($params['version']) ? NULL : (int) $params['version']; | |
392 | } | |
393 | if (isset($desired_version) && is_integer($desired_version)) { | |
394 | $_version = $desired_version; | |
395 | } | |
396 | else { | |
397 | // we will set the default to version 3 as soon as we find that it works. | |
398 | $_version = 3; | |
399 | } | |
400 | return $_version; | |
401 | } | |
402 | ||
403 | /** | |
404 | * Check if the result is an error. Note that this function has been retained from | |
405 | * api v2 for convenience but the result is more standardised in v3 and param | |
406 | * 'format.is_success' => 1 | |
407 | * will result in a boolean success /fail being returned if that is what you need. | |
408 | * | |
8904518f | 409 | * @param $result |
410 | * | |
411 | * @internal param array $params (reference ) input parameters | |
6a488035 TO |
412 | * |
413 | * @return boolean true if error, false otherwise | |
414 | * @static void | |
415 | * @access public | |
416 | */ | |
417 | function civicrm_error($result) { | |
418 | if (is_array($result)) { | |
419 | return (array_key_exists('is_error', $result) && | |
420 | $result['is_error'] | |
421 | ) ? TRUE : FALSE; | |
422 | } | |
423 | return FALSE; | |
424 | } | |
425 | ||
426 | function _civicrm_api_get_camel_name($entity, $version = NULL) { | |
6a488035 TO |
427 | if (empty($version)) { |
428 | $version = civicrm_get_api_version(); | |
429 | } | |
430 | ||
6a488035 TO |
431 | $fragments = explode('_', $entity); |
432 | foreach ($fragments as & $fragment) { | |
433 | $fragment = ucfirst($fragment); | |
434 | } | |
435 | // Special case: UFGroup, UFJoin, UFMatch, UFField | |
436 | if ($fragments[0] === 'Uf') { | |
437 | $fragments[0] = 'UF'; | |
438 | } | |
439 | return implode('', $fragments); | |
440 | } | |
441 | ||
11e09c59 | 442 | /** |
6a488035 TO |
443 | * Call any nested api calls |
444 | */ | |
445 | function _civicrm_api_call_nested_api(&$params, &$result, $action, $entity, $version) { | |
446 | $entity = _civicrm_api_get_entity_name_from_camel($entity); | |
6ebe79c1 BS |
447 | |
448 | //we don't need to worry about nested api in the getfields/getoptions actions, so just return immediately | |
449 | if (in_array(strtolower($action), array('getfields', 'getoptions'))) { | |
450 | return; | |
451 | } | |
452 | ||
6a488035 TO |
453 | if(strtolower($action) == 'getsingle'){ |
454 | // I don't understand the protocol here, but we don't want | |
455 | // $result to be a recursive array | |
456 | // $result['values'][0] = $result; | |
457 | $oldResult = $result; | |
458 | $result = array('values' => array(0 => $oldResult)); | |
459 | } | |
460 | foreach ($params as $field => $newparams) { | |
461 | if ((is_array($newparams) || $newparams === 1) && $field <> 'api.has_parent' && substr($field, 0, 3) == 'api') { | |
462 | ||
463 | // 'api.participant.delete' => 1 is a valid options - handle 1 instead of an array | |
464 | if ($newparams === 1) { | |
465 | $newparams = array('version' => $version); | |
466 | } | |
467 | // can be api_ or api. | |
468 | $separator = $field[3]; | |
469 | if (!($separator == '.' || $separator == '_')) { | |
470 | continue; | |
471 | } | |
472 | $subAPI = explode($separator, $field); | |
473 | ||
474 | $subaction = empty($subAPI[2]) ? $action : $subAPI[2]; | |
475 | $subParams = array( | |
476 | 'debug' => CRM_Utils_Array::value('debug', $params), | |
477 | ); | |
478 | $subEntity = $subAPI[1]; | |
479 | ||
480 | foreach ($result['values'] as $idIndex => $parentAPIValues) { | |
481 | ||
482 | if (strtolower($subEntity) != 'contact') { | |
483 | //contact spits the dummy at activity_id so what else won't it like? | |
484 | //set entity_id & entity table based on the parent's id & entity. e.g for something like | |
485 | //note if the parent call is contact 'entity_table' will be set to 'contact' & 'id' to the contact id from | |
486 | //the parent call. | |
487 | //in this case 'contact_id' will also be set to the parent's id | |
488 | $subParams["entity_id"] = $parentAPIValues['id']; | |
489 | $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity); | |
490 | $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id']; | |
491 | } | |
492 | if (strtolower($entity) != 'contact' && CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) { | |
493 | //e.g. if event_id is in the values returned & subentity is event then pass in event_id as 'id' | |
494 | //don't do this for contact as it does some wierd things like returning primary email & | |
495 | //thus limiting the ability to chain email | |
496 | //TODO - this might need the camel treatment | |
497 | $subParams['id'] = $parentAPIValues[$subEntity . "_id"]; | |
498 | } | |
499 | ||
500 | if (CRM_Utils_Array::value('entity_table', $result['values'][$idIndex]) == $subEntity) { | |
501 | $subParams['id'] = $result['values'][$idIndex]['entity_id']; | |
502 | } | |
503 | // if we are dealing with the same entity pass 'id' through (useful for get + delete for example) | |
504 | if (strtolower($entity) == strtolower($subEntity)) { | |
505 | $subParams['id'] = $result['values'][$idIndex]['id']; | |
506 | } | |
507 | ||
508 | ||
509 | $subParams['version'] = $version; | |
510 | if(!empty($params['check_permissions'])){ | |
511 | $subParams['check_permissions'] = $params['check_permissions']; | |
512 | } | |
513 | $subParams['sequential'] = 1; | |
514 | $subParams['api.has_parent'] = 1; | |
515 | if (array_key_exists(0, $newparams)) { | |
87840ea3 | 516 | $genericParams = $subParams; |
6a488035 | 517 | // it is a numerically indexed array - ie. multiple creates |
9465d059 | 518 | foreach ($newparams as $entityparams) { |
87840ea3 | 519 | $subParams = array_merge($genericParams, $entityparams); |
6a488035 TO |
520 | _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); |
521 | $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams); | |
522 | if ($result['is_error'] === 1) { | |
523 | throw new Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); | |
524 | } | |
525 | } | |
526 | } | |
527 | else { | |
528 | ||
529 | $subParams = array_merge($subParams, $newparams); | |
530 | _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); | |
531 | $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams); | |
532 | if (!empty($result['is_error'])) { | |
533 | throw new Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); | |
534 | } | |
535 | } | |
536 | } | |
537 | } | |
538 | } | |
539 | if(strtolower($action) == 'getsingle'){ | |
540 | $result = $result['values'][0]; | |
541 | } | |
542 | } | |
543 | ||
11e09c59 | 544 | /** |
6a488035 TO |
545 | * Swap out any $values vars - ie. the value after $value is swapped for the parent $result |
546 | * 'activity_type_id' => '$value.testfield', | |
547 | 'tag_id' => '$value.api.tag.create.id', | |
548 | 'tag1_id' => '$value.api.entity.create.0.id' | |
549 | */ | |
550 | function _civicrm_api_replace_variables($entity, $action, &$params, &$parentResult, $separator = '.') { | |
551 | ||
552 | ||
553 | foreach ($params as $field => $value) { | |
554 | ||
555 | if (is_string($value) && substr($value, 0, 6) == '$value') { | |
556 | $valuesubstitute = substr($value, 7); | |
557 | ||
558 | if (!empty($parentResult[$valuesubstitute])) { | |
559 | $params[$field] = $parentResult[$valuesubstitute]; | |
560 | } | |
561 | else { | |
562 | ||
563 | $stringParts = explode($separator, $value); | |
564 | unset($stringParts[0]); | |
565 | ||
566 | $fieldname = array_shift($stringParts); | |
567 | ||
568 | //when our string is an array we will treat it as an array from that . onwards | |
569 | $count = count($stringParts); | |
570 | while ($count > 0) { | |
571 | $fieldname .= "." . array_shift($stringParts); | |
572 | if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) { | |
573 | $arrayLocation = $parentResult[$fieldname]; | |
574 | foreach ($stringParts as $key => $value) { | |
575 | $arrayLocation = CRM_Utils_Array::value($value, $arrayLocation); | |
576 | } | |
577 | $params[$field] = $arrayLocation; | |
578 | } | |
579 | $count = count($stringParts); | |
580 | } | |
581 | } | |
582 | } | |
583 | } | |
584 | } | |
585 | ||
11e09c59 | 586 | /** |
6a488035 TO |
587 | * Convert possibly camel name to underscore separated entity name |
588 | * | |
589 | * @param string $entity entity name in various formats e.g. Contribution, contribution, OptionValue, option_value, UFJoin, uf_join | |
590 | * @return string $entity entity name in underscore separated format | |
47e6af81 CW |
591 | * |
592 | * FIXME: Why isn't this called first thing in civicrm_api wrapper? | |
6a488035 TO |
593 | */ |
594 | function _civicrm_api_get_entity_name_from_camel($entity) { | |
595 | if ($entity == strtolower($entity)) { | |
596 | return $entity; | |
597 | } | |
598 | else { | |
599 | $entity = ltrim(strtolower(str_replace('U_F', | |
600 | 'uf', | |
601 | // That's CamelCase, beside an odd UFCamel that is expected as uf_camel | |
602 | preg_replace('/(?=[A-Z])/', '_$0', $entity) | |
603 | )), '_'); | |
604 | } | |
605 | return $entity; | |
606 | } | |
11e09c59 TO |
607 | |
608 | /** | |
6a488035 TO |
609 | * Having a DAO object find the entity name |
610 | * @param object $bao DAO being passed in | |
5d3523d9 | 611 | * @return string |
6a488035 TO |
612 | */ |
613 | function _civicrm_api_get_entity_name_from_dao($bao){ | |
614 | $daoName = str_replace("BAO", "DAO", get_class($bao)); | |
edc091f1 | 615 | return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName)); |
6a488035 TO |
616 | } |
617 | ||
91cd40ea ML |
618 | /** |
619 | * Sets the tsLocale and dbLocale for multi-lingual sites. | |
620 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() | |
621 | * to avoid regressions from refactoring. | |
622 | */ | |
623 | function _civicrm_api_set_locale($lcMessagesRequest) { | |
624 | // We must validate whether the locale is valid, otherwise setting a bad | |
625 | // dbLocale could probably lead to sql-injection. | |
626 | $domain = new CRM_Core_DAO_Domain(); | |
627 | $domain->id = CRM_Core_Config::domainID(); | |
628 | $domain->find(TRUE); | |
629 | ||
630 | if ($domain->config_backend) { | |
631 | $defaults = unserialize($domain->config_backend); | |
632 | ||
633 | // are we in a multi-language setup? | |
634 | $multiLang = $domain->locales ? TRUE : FALSE; | |
635 | $lcMessages = NULL; | |
636 | ||
637 | // on multi-lang sites based on request and civicrm_uf_match | |
638 | if ($multiLang) { | |
639 | $languageLimit = array(); | |
640 | if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { | |
641 | $languageLimit = $defaults['languageLimit']; | |
642 | } | |
643 | ||
644 | if (in_array($lcMessagesRequest, array_keys($languageLimit))) { | |
645 | $lcMessages = $lcMessagesRequest; | |
646 | } | |
647 | else { | |
648 | throw new API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest))); | |
649 | } | |
650 | } | |
651 | ||
652 | global $dbLocale; | |
653 | ||
654 | // set suffix for table names - use views if more than one language | |
655 | if ($lcMessages) { | |
656 | $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; | |
657 | ||
658 | // FIXME: an ugly hack to fix CRM-4041 | |
659 | global $tsLocale; | |
660 | $tsLocale = $lcMessages; | |
661 | } | |
662 | } | |
663 | } |