)
* @param array
* @param Smarty
* @return string now formatted to use popover natively
*
* params still relevant:
*
* text Required: the text/html to display in the popup window
* trigger 'onClick' and native bootstrap params: 'click', 'hover', 'focus', 'manual' ('hover' default)
* sticky false/true - this is currently an alias for trigger['click'] which is wrong.
* Sticky should define whether the popup should stay until clicked, not how it is triggered.
* width in pixels?
* fullhtml
* delay number of miliseconds to delay showing or hiding of popover. If just one number, then it will apply to both
* show and hide, or use "500|1000" to have a 500 ms show delay and a 1000 ms hide delay
*/
function smarty_function_popup($params, $smarty)
{
// Defaults
$options = [
'data-bs-toggle' => 'popover',
'data-bs-container' => 'body',
'data-bs-trigger' => 'hover focus',
'data-bs-content' => '',
];
foreach ($params as $key => $value) {
switch ($key) {
case 'text':
$options['data-bs-content'] = $value;
break;
case 'trigger':
switch ($value) {
// is this legacy? should not be used anywhere
case 'onclick':
case 'onClick':
$options['data-bs-trigger'] = 'click';
break;
// support native bootstrap params - could be moved to default but not sure whether it breaks something
case 'hover focus':
case 'focus hover':
case 'click':
case 'hover':
case 'focus':
case 'manual':
$options['data-bs-trigger'] = $value;
break;
default:
break;
}
break;
case 'caption':
$options['title'] = $value;
break;
case 'width':
case 'height':
$options[$key] = $value;
break;
case 'sticky':
$options['data-bs-trigger'] = 'click';
break;
case 'fullhtml':
$options['data-bs-html'] = true;
break;
case 'background':
if (! empty($params['width'])) {
if (! isset($params["height"])) {
$params["height"] = 300;
}
$options['data-bs-content'] = "" . $options['data-bs-content'] . "
";
} else {
$options['data-bs-content'] = "" . $options['data-bs-content'] . "
";
}
$options['data-bs-html'] = true;
break;
}
}
if (empty($options['title']) && empty($options['data-bs-content'])) {
trigger_error("popover: attribute 'text' or 'caption' required");
return false;
}
$options['data-bs-content'] = preg_replace(['/\\\\r\n/','/\\\\n/','/\\\\r/', '/\\t/'], '', $options['data-bs-content']);
$retval = '';
foreach ($options as $k => $v) {
$retval .= $k . '="' . (new Laminas\Escaper\Escaper())->escapeHtmlAttr($v) . '" ';
}
//handle delay param here since slashes added by the above break the code
if (! empty($params['delay'])) {
$explode = explode('|', $params['delay']);
if (count($explode) == 1) {
$delay = (int) $explode[0];
} else {
$delay = '{"show":' . (int) $explode[0] . ', "hide":' . (int) $explode[1] . '}';
}
$retval .= ' data-bs-delay=\'' . $delay . '\'';
} else {
// add a short default open and close delay so they don't appear by accident and you can hover over the popover
$retval .= ' data-bs-delay=\'{"show":500,"hide":250}\'';
}
return $retval;
}