| 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 | |
| 13 | /** |
| 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 | * @param null $extra |
| 21 | * |
| 22 | * @return array|int |
| 23 | */ |
| 24 | function civicrm_api($entity, $action, $params, $extra = NULL) { |
| 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 | |
| 32 | $apiWrappers = array( |
| 33 | CRM_Utils_API_HTMLInputCoder::singleton(), |
| 34 | CRM_Utils_API_NullOutputCoder::singleton(), |
| 35 | CRM_Utils_API_ReloadOption::singleton(), |
| 36 | CRM_Utils_API_MatchOption::singleton(), |
| 37 | ); |
| 38 | CRM_Utils_Hook::apiWrappers($apiWrappers,$apiRequest); |
| 39 | |
| 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(); |
| 48 | // look up function, file, is_generic |
| 49 | $apiRequest += _civicrm_api_resolve($apiRequest); |
| 50 | if (strtolower($action) == 'create' || strtolower($action) == 'delete' || strtolower($action) == 'submit') { |
| 51 | $apiRequest['is_transactional'] = 1; |
| 52 | $transaction = new CRM_Core_Transaction(); |
| 53 | } |
| 54 | |
| 55 | // support multi-lingual requests |
| 56 | if ($language = CRM_Utils_Array::value('option.language', $params)) { |
| 57 | _civicrm_api_set_locale($language); |
| 58 | } |
| 59 | |
| 60 | _civicrm_api3_api_check_permission($apiRequest['entity'], $apiRequest['action'], $apiRequest['params']); |
| 61 | $fields = _civicrm_api3_api_getfields($apiRequest); |
| 62 | // we do this before we |
| 63 | _civicrm_api3_swap_out_aliases($apiRequest, $fields); |
| 64 | if (strtolower($action) != 'getfields') { |
| 65 | if (!CRM_Utils_Array::value('id', $apiRequest['params'])) { |
| 66 | $apiRequest['params'] = array_merge(_civicrm_api3_getdefaults($apiRequest, $fields), $apiRequest['params']); |
| 67 | } |
| 68 | //if 'id' is set then only 'version' will be checked but should still be checked for consistency |
| 69 | civicrm_api3_verify_mandatory($apiRequest['params'], NULL, _civicrm_api3_getrequired($apiRequest, $fields)); |
| 70 | } |
| 71 | |
| 72 | // For input filtering, process $apiWrappers in forward order |
| 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']) { |
| 85 | _civicrm_api3_validate_fields($apiRequest['entity'], $apiRequest['action'], $apiRequest['params'], $fields); |
| 86 | |
| 87 | $result = isset($extra) ? $function($apiRequest['params'], $extra) : $function($apiRequest['params']); |
| 88 | } |
| 89 | else { |
| 90 | return civicrm_api3_create_error("API (" . $apiRequest['entity'] . "," . $apiRequest['action'] . ") does not exist (join the API team and implement it!)"); |
| 91 | } |
| 92 | |
| 93 | // For output filtering, process $apiWrappers in reverse order |
| 94 | foreach (array_reverse($apiWrappers) as $apiWrapper) { |
| 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 | } |
| 106 | if (CRM_Utils_Array::value('format.only_id', $apiRequest['params']) && isset($result['id'])) { |
| 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 | } |
| 128 | $error = $e->getCause(); |
| 129 | if ($error instanceof DB_Error) { |
| 130 | $data["error_code"] = DB::errorMessage($error->getCode()); |
| 131 | $data["sql"] = $error->getDebugInfo(); |
| 132 | } |
| 133 | if (CRM_Utils_Array::value('debug', $apiRequest['params'])) { |
| 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 | } |
| 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); |
| 146 | if (CRM_Utils_Array::value('is_transactional', $apiRequest)) { |
| 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 | } |
| 167 | if (CRM_Utils_Array::value('is_transactional', $apiRequest)) { |
| 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()); |
| 178 | if (CRM_Utils_Array::value('debug', $apiRequest['params'])) { |
| 179 | $err['trace'] = $e->getTraceAsString(); |
| 180 | } |
| 181 | if (CRM_Utils_Array::value('is_transactional', $apiRequest)) { |
| 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) { |
| 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) { |
| 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 | } |
| 257 | |
| 258 | /** |
| 259 | * Version 3 wrapper for civicrm_api. Throws exception |
| 260 | * |
| 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 | * |
| 265 | * @throws CiviCRM_API3_Exception |
| 266 | * @return array |
| 267 | */ |
| 268 | function civicrm_api3($entity, $action, $params = array()) { |
| 269 | $params['version'] = 3; |
| 270 | $result = civicrm_api($entity, $action, $params); |
| 271 | if(is_array($result) && !empty($result['is_error'])){ |
| 272 | throw new CiviCRM_API3_Exception($result['error_message'], CRM_Utils_Array::value('error_code', $result, 'undefined'), $result); |
| 273 | } |
| 274 | return $result; |
| 275 | } |
| 276 | |
| 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 |
| 293 | if (CRM_Utils_Array::value('api_action', $apiRequest['params'])) { |
| 294 | // $apiRequest['params']['action'] = $apiRequest['params']['api_action']; |
| 295 | // unset($apiRequest['params']['api_action']); |
| 296 | } |
| 297 | return array('action' => array('api.aliases' => array('api_action'))); |
| 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 | } |
| 307 | |
| 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 |
| 315 | * @param int $version |
| 316 | * |
| 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 |
| 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. |
| 384 | * @return int |
| 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 | * |
| 409 | * @param $result |
| 410 | * |
| 411 | * @internal param array $params (reference ) input parameters |
| 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) { |
| 427 | if (empty($version)) { |
| 428 | $version = civicrm_get_api_version(); |
| 429 | } |
| 430 | |
| 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 | |
| 442 | /** |
| 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); |
| 447 | if(strtolower($action) == 'getsingle'){ |
| 448 | // I don't understand the protocol here, but we don't want |
| 449 | // $result to be a recursive array |
| 450 | // $result['values'][0] = $result; |
| 451 | $oldResult = $result; |
| 452 | $result = array('values' => array(0 => $oldResult)); |
| 453 | } |
| 454 | foreach ($params as $field => $newparams) { |
| 455 | if ((is_array($newparams) || $newparams === 1) && $field <> 'api.has_parent' && substr($field, 0, 3) == 'api') { |
| 456 | |
| 457 | // 'api.participant.delete' => 1 is a valid options - handle 1 instead of an array |
| 458 | if ($newparams === 1) { |
| 459 | $newparams = array('version' => $version); |
| 460 | } |
| 461 | // can be api_ or api. |
| 462 | $separator = $field[3]; |
| 463 | if (!($separator == '.' || $separator == '_')) { |
| 464 | continue; |
| 465 | } |
| 466 | $subAPI = explode($separator, $field); |
| 467 | |
| 468 | $subaction = empty($subAPI[2]) ? $action : $subAPI[2]; |
| 469 | $subParams = array( |
| 470 | 'debug' => CRM_Utils_Array::value('debug', $params), |
| 471 | ); |
| 472 | $subEntity = $subAPI[1]; |
| 473 | |
| 474 | foreach ($result['values'] as $idIndex => $parentAPIValues) { |
| 475 | |
| 476 | if (strtolower($subEntity) != 'contact') { |
| 477 | //contact spits the dummy at activity_id so what else won't it like? |
| 478 | //set entity_id & entity table based on the parent's id & entity. e.g for something like |
| 479 | //note if the parent call is contact 'entity_table' will be set to 'contact' & 'id' to the contact id from |
| 480 | //the parent call. |
| 481 | //in this case 'contact_id' will also be set to the parent's id |
| 482 | $subParams["entity_id"] = $parentAPIValues['id']; |
| 483 | $subParams['entity_table'] = 'civicrm_' . _civicrm_api_get_entity_name_from_camel($entity); |
| 484 | $subParams[strtolower($entity) . "_id"] = $parentAPIValues['id']; |
| 485 | } |
| 486 | if (strtolower($entity) != 'contact' && CRM_Utils_Array::value(strtolower($subEntity . "_id"), $parentAPIValues)) { |
| 487 | //e.g. if event_id is in the values returned & subentity is event then pass in event_id as 'id' |
| 488 | //don't do this for contact as it does some wierd things like returning primary email & |
| 489 | //thus limiting the ability to chain email |
| 490 | //TODO - this might need the camel treatment |
| 491 | $subParams['id'] = $parentAPIValues[$subEntity . "_id"]; |
| 492 | } |
| 493 | |
| 494 | if (CRM_Utils_Array::value('entity_table', $result['values'][$idIndex]) == $subEntity) { |
| 495 | $subParams['id'] = $result['values'][$idIndex]['entity_id']; |
| 496 | } |
| 497 | // if we are dealing with the same entity pass 'id' through (useful for get + delete for example) |
| 498 | if (strtolower($entity) == strtolower($subEntity)) { |
| 499 | $subParams['id'] = $result['values'][$idIndex]['id']; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | $subParams['version'] = $version; |
| 504 | if(!empty($params['check_permissions'])){ |
| 505 | $subParams['check_permissions'] = $params['check_permissions']; |
| 506 | } |
| 507 | $subParams['sequential'] = 1; |
| 508 | $subParams['api.has_parent'] = 1; |
| 509 | if (array_key_exists(0, $newparams)) { |
| 510 | $genericParams = $subParams; |
| 511 | // it is a numerically indexed array - ie. multiple creates |
| 512 | foreach ($newparams as $entityparams) { |
| 513 | $subParams = array_merge($genericParams, $entityparams); |
| 514 | _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); |
| 515 | $result['values'][$result['id']][$field][] = civicrm_api($subEntity, $subaction, $subParams); |
| 516 | if ($result['is_error'] === 1) { |
| 517 | throw new Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | else { |
| 522 | |
| 523 | $subParams = array_merge($subParams, $newparams); |
| 524 | _civicrm_api_replace_variables($subAPI[1], $subaction, $subParams, $result['values'][$idIndex], $separator); |
| 525 | $result['values'][$idIndex][$field] = civicrm_api($subEntity, $subaction, $subParams); |
| 526 | if (!empty($result['is_error'])) { |
| 527 | throw new Exception($subEntity . ' ' . $subaction . 'call failed with' . $result['error_message']); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | if(strtolower($action) == 'getsingle'){ |
| 534 | $result = $result['values'][0]; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Swap out any $values vars - ie. the value after $value is swapped for the parent $result |
| 540 | * 'activity_type_id' => '$value.testfield', |
| 541 | 'tag_id' => '$value.api.tag.create.id', |
| 542 | 'tag1_id' => '$value.api.entity.create.0.id' |
| 543 | */ |
| 544 | function _civicrm_api_replace_variables($entity, $action, &$params, &$parentResult, $separator = '.') { |
| 545 | |
| 546 | |
| 547 | foreach ($params as $field => $value) { |
| 548 | |
| 549 | if (is_string($value) && substr($value, 0, 6) == '$value') { |
| 550 | $valuesubstitute = substr($value, 7); |
| 551 | |
| 552 | if (!empty($parentResult[$valuesubstitute])) { |
| 553 | $params[$field] = $parentResult[$valuesubstitute]; |
| 554 | } |
| 555 | else { |
| 556 | |
| 557 | $stringParts = explode($separator, $value); |
| 558 | unset($stringParts[0]); |
| 559 | |
| 560 | $fieldname = array_shift($stringParts); |
| 561 | |
| 562 | //when our string is an array we will treat it as an array from that . onwards |
| 563 | $count = count($stringParts); |
| 564 | while ($count > 0) { |
| 565 | $fieldname .= "." . array_shift($stringParts); |
| 566 | if (array_key_exists($fieldname, $parentResult) && is_array($parentResult[$fieldname])) { |
| 567 | $arrayLocation = $parentResult[$fieldname]; |
| 568 | foreach ($stringParts as $key => $value) { |
| 569 | $arrayLocation = CRM_Utils_Array::value($value, $arrayLocation); |
| 570 | } |
| 571 | $params[$field] = $arrayLocation; |
| 572 | } |
| 573 | $count = count($stringParts); |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Convert possibly camel name to underscore separated entity name |
| 582 | * |
| 583 | * @param string $entity entity name in various formats e.g. Contribution, contribution, OptionValue, option_value, UFJoin, uf_join |
| 584 | * @return string $entity entity name in underscore separated format |
| 585 | * |
| 586 | * FIXME: Why isn't this called first thing in civicrm_api wrapper? |
| 587 | */ |
| 588 | function _civicrm_api_get_entity_name_from_camel($entity) { |
| 589 | if ($entity == strtolower($entity)) { |
| 590 | return $entity; |
| 591 | } |
| 592 | else { |
| 593 | $entity = ltrim(strtolower(str_replace('U_F', |
| 594 | 'uf', |
| 595 | // That's CamelCase, beside an odd UFCamel that is expected as uf_camel |
| 596 | preg_replace('/(?=[A-Z])/', '_$0', $entity) |
| 597 | )), '_'); |
| 598 | } |
| 599 | return $entity; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * Having a DAO object find the entity name |
| 604 | * @param object $bao DAO being passed in |
| 605 | * @return string |
| 606 | */ |
| 607 | function _civicrm_api_get_entity_name_from_dao($bao){ |
| 608 | $daoName = str_replace("BAO", "DAO", get_class($bao)); |
| 609 | return _civicrm_api_get_entity_name_from_camel(CRM_Core_DAO_AllCoreTables::getBriefName($daoName)); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Sets the tsLocale and dbLocale for multi-lingual sites. |
| 614 | * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() |
| 615 | * to avoid regressions from refactoring. |
| 616 | */ |
| 617 | function _civicrm_api_set_locale($lcMessagesRequest) { |
| 618 | // We must validate whether the locale is valid, otherwise setting a bad |
| 619 | // dbLocale could probably lead to sql-injection. |
| 620 | $domain = new CRM_Core_DAO_Domain(); |
| 621 | $domain->id = CRM_Core_Config::domainID(); |
| 622 | $domain->find(TRUE); |
| 623 | |
| 624 | if ($domain->config_backend) { |
| 625 | $defaults = unserialize($domain->config_backend); |
| 626 | |
| 627 | // are we in a multi-language setup? |
| 628 | $multiLang = $domain->locales ? TRUE : FALSE; |
| 629 | $lcMessages = NULL; |
| 630 | |
| 631 | // on multi-lang sites based on request and civicrm_uf_match |
| 632 | if ($multiLang) { |
| 633 | $languageLimit = array(); |
| 634 | if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) { |
| 635 | $languageLimit = $defaults['languageLimit']; |
| 636 | } |
| 637 | |
| 638 | if (in_array($lcMessagesRequest, array_keys($languageLimit))) { |
| 639 | $lcMessages = $lcMessagesRequest; |
| 640 | } |
| 641 | else { |
| 642 | throw new API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest))); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | global $dbLocale; |
| 647 | |
| 648 | // set suffix for table names - use views if more than one language |
| 649 | if ($lcMessages) { |
| 650 | $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : ''; |
| 651 | |
| 652 | // FIXME: an ugly hack to fix CRM-4041 |
| 653 | global $tsLocale; |
| 654 | $tsLocale = $lcMessages; |
| 655 | } |
| 656 | } |
| 657 | } |