Merge pull request #15649 from JMAConsulting/core-1346
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.crmSetting.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 * Retrieve CiviCRM settings from the api for use in templates.
22 *
23 * @param $params
24 * @param $smarty
25 *
26 * @return int|string|null
27 */
28 function smarty_function_crmSetting($params, &$smarty) {
29
30 $errorScope = CRM_Core_TemporaryErrorScope::create(['CRM_Utils_REST', 'fatal']);
31 unset($params['method']);
32 unset($params['assign']);
33 $params['version'] = 3;
34
35 require_once 'api/api.php';
36 $result = civicrm_api('setting', 'getvalue', $params);
37 unset($errorScope);
38 // Core-688 FALSE is returned by Boolean settings, thus giving false errors.
39 if ($result === NULL) {
40 $smarty->trigger_error("Unknown error");
41 return NULL;
42 }
43
44 if (empty($params['var'])) {
45 return is_numeric($result) ? $result : json_encode($result);
46 }
47 if (!empty($params['json'])) {
48 $smarty->assign($params["var"], json_encode($result));
49 }
50 else {
51 $smarty->assign($params["var"], $result);
52 }
53 }