Merge pull request #23702 from colemanw/loadingPlaceholders
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.crmAPI.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright TTTP
16 *
17 */
18
19 /**
20 * @param $params
21 * @param $smarty
22 * @return string|void
23 */
24 function smarty_function_crmAPI($params, &$smarty) {
25 if (!array_key_exists('entity', $params)) {
26 $smarty->trigger_error("assign: missing 'entity' parameter");
27 return "crmAPI: missing 'entity' parameter";
28 }
29 $errorScope = CRM_Core_TemporaryErrorScope::create(['CRM_Utils_REST', 'fatal']);
30 $entity = $params['entity'];
31 $action = CRM_Utils_Array::value('action', $params, 'get');
32 $params['sequential'] = CRM_Utils_Array::value('sequential', $params, 1);
33 $var = $params['var'] ?? NULL;
34 CRM_Utils_Array::remove($params, 'entity', 'action', 'var');
35 $params['version'] = 3;
36 require_once 'api/api.php';
37 $result = civicrm_api($entity, $action, $params);
38 unset($errorScope);
39 if ($result === FALSE) {
40 $smarty->trigger_error("Unknown error");
41 }
42
43 if (!empty($result['is_error'])) {
44 $smarty->trigger_error("{crmAPI} " . $result["error_message"]);
45 }
46
47 if (!$var) {
48 return json_encode($result);
49 }
50 if (!empty($params['json'])) {
51 $smarty->assign($var, json_encode($result));
52 }
53 else {
54 $smarty->assign($var, $result);
55 }
56 }