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