Merge pull request #14928 from lcdservices/dev-core-1158
[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 * $Id$
17 *
18 */
19
20 /**
21 * @param $params
22 * @param $smarty
23 * @return string|void
24 */
25 function smarty_function_crmAPI($params, &$smarty) {
26 if (!array_key_exists('entity', $params)) {
27 $smarty->trigger_error("assign: missing 'entity' parameter");
28 return "crmAPI: missing 'entity' parameter";
29 }
30 $errorScope = CRM_Core_TemporaryErrorScope::create(['CRM_Utils_REST', 'fatal']);
31 $entity = $params['entity'];
32 $action = CRM_Utils_Array::value('action', $params, 'get');
33 $params['sequential'] = CRM_Utils_Array::value('sequential', $params, 1);
34 $var = CRM_Utils_Array::value('var', $params);
35 CRM_Utils_Array::remove($params, 'entity', 'action', 'var');
36 $params['version'] = 3;
37 require_once 'api/api.php';
38 $result = civicrm_api($entity, $action, $params);
39 unset($errorScope);
40 if ($result === FALSE) {
41 $smarty->trigger_error("Unknown error");
42 }
43
44 if (!empty($result['is_error'])) {
45 $smarty->trigger_error("{crmAPI} " . $result["error_message"]);
46 }
47
48 if (!$var) {
49 return json_encode($result);
50 }
51 if (!empty($params['json'])) {
52 $smarty->assign($var, json_encode($result));
53 }
54 else {
55 $smarty->assign($var, $result);
56 }
57 }