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