Merge pull request #15818 from colemanw/fields
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.help.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 CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19
20 /**
21 * Adds inline help
22 *
23 * @param array $params
24 * The function params.
25 * @param CRM_Core_Smarty $smarty
26 * Reference to the smarty object.
27 *
28 * @return string
29 * the help html to be inserted
30 */
31 function smarty_function_help($params, &$smarty) {
32 if (!isset($params['id']) || !isset($smarty->_tpl_vars['config'])) {
33 return NULL;
34 }
35
36 if (empty($params['file']) && isset($smarty->_tpl_vars['tplFile'])) {
37 $params['file'] = $smarty->_tpl_vars['tplFile'];
38 }
39 elseif (empty($params['file'])) {
40 return NULL;
41 }
42
43 $params['file'] = str_replace(['.tpl', '.hlp'], '', $params['file']);
44
45 if (empty($params['title'])) {
46 $vars = $smarty->get_template_vars();
47 $smarty->assign('id', $params['id'] . '-title');
48 $name = trim($smarty->fetch($params['file'] . '.hlp'));
49 $additionalTPLFile = $params['file'] . '.extra.hlp';
50 if ($smarty->template_exists($additionalTPLFile)) {
51 $name .= trim($smarty->fetch($additionalTPLFile));
52 }
53 // Ensure we didn't change any existing vars CRM-11900
54 foreach ($vars as $key => $value) {
55 if ($smarty->get_template_vars($key) !== $value) {
56 $smarty->assign($key, $value);
57 }
58 }
59 }
60 else {
61 $name = trim(strip_tags($params['title']));
62 }
63
64 $class = "helpicon";
65 if (!empty($params['class'])) {
66 $class .= " {$params['class']}";
67 }
68
69 // Escape for html
70 $title = htmlspecialchars(ts('%1 Help', [1 => $name]));
71 // Escape for html and js
72 $name = htmlspecialchars(json_encode($name), ENT_QUOTES);
73
74 // Format params to survive being passed through json & the url
75 unset($params['text'], $params['title']);
76 foreach ($params as &$param) {
77 $param = is_bool($param) || is_numeric($param) ? (int) $param : (string) $param;
78 }
79 return '<a class="' . $class . '" title="' . $title . '" aria-label="' . $title . '" href="#" onclick=\'CRM.help(' . $name . ', ' . json_encode($params) . '); return false;\'>&nbsp;</a>';
80 }