tp_servicedesk/app/extensions/IconsHelper.php
2025-05-10 15:26:21 +01:00

136 lines
4.5 KiB
PHP

<?php
class IconsHelper extends \Prefab {
static public $status_icons = [
'open' => ['fas fa-circle-dot has-text-success', "new"],
'in_progress' => ['fas fa-circle-play has-text-link', "reload"],
'on_hold' => ['fas fa-pause-circle has-text-warning',"pause"],
'completed' => ['fas fa-check has-text-danger', "check"]
];
static public $status_names = [
'open' => 'Open',
'in_progress' => 'In Progress',
'on_hold' => 'On Hold',
'completed' => 'Completed'
];
static public $priority_icons = [
'Low' => ['fas fa-circle-down',"green"],
'Medium' => ['fas fa-circle-dot', "yellow"],
'High' => ['fas fa-circle-up', "red"]
];
static public $priority_colors = [
'Low' => 'success',
'Medium' => 'warning',
'High' => 'danger',
'' => 'info'
];
static public function icons($node){
// debug_print($node);
$required = ['type', 'path'];
$check = self::checkAttributes($node, $required);
if(!is_null($check)){
return sprintf('<div class="notification is-danger is-light">%s</div>', $check);
}
$attr = $node['@attrib'];
$type = $attr['type'];
$path = $attr['path'];
$selected = $attr['selected'];
switch($attr['type']){
case 'status-selector':
// $selected = Base::instance()->get('GET.status') ?: null;
return '<?php echo \IconsHelper::instance()->renderStatusSelector(
Base::instance()->get("GET.status") ?: null, "'.$path.'"); ?>';
return self::renderStatusSelector($selected, $path);
default:
return '<div class="notification">unknown icon selector type</div>';
}
$tpl = Template::instance();
$f3 = Base::instance();
$context = $f3->hive();
$inner = $tpl->token($node[0], $context);
return '<?php echo IconsHelper::do_the_switch("' . $attr['type'] . '", ' . $inner . '); ?>';
}
private static function checkAttributes($node, array $required){
if(isset($node['@attrib'])){
$attr = $node['@attrib'];
$errors = [];
foreach($required as $key){
if(empty($attr[$key])){
$errors[] = "Error: '$key' is missing.";
}
}
return empty($errors) ? null : implode(" ", $errors);
}
return "Error: '@attrib' is missing";
}
public static function renderStatusSelector($current_status, $path)
{
$output = '<div class="block"><div class="field has-addons">';
foreach (self::$status_icons as $k => $icon) {
$active = ($current_status == $k);
$url = $path . ($active ? '' : '/?status=' . $k);
$class = 'button' . ($active ? ' is-inverted' : '');
$output .= '<p class="control">';
$output .= '<a href="' . $url . '" class="' . $class . '">';
$output .= '<span class="icon is-small"><i class="fas fa-' . $icon[0] . '"></i></span>';
$output .= '<span>' . self::$status_names[$k] . '</span>';
$output .= '</a>';
$output .= '</p>';
}
$output .= '</div></div>';
return $output;
}
static function do_the_switch($type, $value){
if($value !== null) {
$value = str_replace(' ', '_', strtolower($value));
}
$icon_class = '';
switch(strtolower($type)){
case 'status':
$icon_class = IconsHelper::$status_icons[$value] ?? ['fas fa-question-circle has-text-info', "🔲"];
break;
case 'priority':
$icon_class = IconsHelper::$priority_icons[$value] ?? ['fas fa-question-circle', "🔲"];
$icon_color = IconsHelper::$priority_colors[$value] ?? 'info';
break;
default:
$icon_class = 'fas fa-question-circle';
}
if($type == 'priority'){
// return '<p class="button"><span class="icon is-small">'
return '<span class="icon is-medium">
<i class="'.$icon_class[0].' fa-lg has-text-'.$icon_color.'"></i>
</span>';
} else {
return '<span class="icon is-medium"><i class="'.$icon_class[0].' fa-lg"></i></span>';
}
return '<span class="is-size-5">'.$icon_class[1].'</span>';
}
}
\Template::instance()->extend('icons', 'IconsHelper::icons');