engelsystem/includes/view/UserHintsRenderer.php

63 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2016-11-15 16:28:20 +01:00
<?php
namespace Engelsystem;
2017-01-02 03:57:23 +01:00
class UserHintsRenderer
{
2017-01-03 03:22:48 +01:00
/** @var string[] */
2017-01-02 03:57:23 +01:00
private $hints = [];
2016-11-15 16:28:20 +01:00
2017-01-02 03:57:23 +01:00
private $important = false;
2016-11-15 16:28:20 +01:00
2017-01-02 15:43:36 +01:00
/**
* Add a hint to the list, if its not null and a not empty string.
*
2017-01-03 03:22:48 +01:00
* @param string $hint The hint
* @param boolean $important Is the hint important?
2017-01-02 15:43:36 +01:00
*/
public function addHint($hint, $important = false)
{
2018-01-14 17:47:26 +01:00
if (!empty($hint)) {
2017-01-02 15:43:36 +01:00
if ($important) {
$this->important = true;
$this->hints[] = error($hint, true);
} else {
$this->hints[] = info($hint, true);
}
}
}
/**
2023-01-22 19:16:33 +01:00
* Render the added hints to a popover for the toolbar.
2017-01-03 03:22:48 +01:00
*
2023-01-22 19:16:33 +01:00
* @return string
2017-01-02 15:43:36 +01:00
*/
2023-01-22 19:16:33 +01:00
public function render()
2017-01-02 15:43:36 +01:00
{
2023-01-22 19:16:33 +01:00
if (count($this->hints) > 0) {
$class_hint = $this->important ? 'danger' : 'info';
$icon = $this->important ? 'exclamation-triangle' : 'info-circle';
$data_bs_attributes = [
'toggle' => 'popover',
'container' => 'body',
'placement' => 'bottom',
'custom-class' => 'popover--userhints',
'html' => 'true',
'content' => htmlspecialchars(join('', $this->hints)),
];
$attr = '';
foreach ($data_bs_attributes as $attr_key => $attr_value) {
$attr .= ' data-bs-' . $attr_key . '="' . $attr_value . '"';
}
2017-01-02 15:43:36 +01:00
2023-01-22 19:16:33 +01:00
return '<li class="nav-item nav-item--userhints d-flex align-items-center bg-' . $class_hint . '">'
. '<a class="nav-link dropdown-toggle text-light" href="#" role="button"' . $attr . '>'
. icon($icon)
. '</a>'
. '</li>';
}
return '';
2017-01-02 15:43:36 +01:00
}
2016-11-15 16:28:20 +01:00
}