Skip to content
Snippets Groups Projects
Commit cf32d6ec authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

htmlguielements: php8 only; added variable types; short array syntax; remove unneeded methods

parent c5e98210
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
This commit is part of merge request !66. Comments created here will be created in the context of that merge request.
<?php
/**
* html gui elements
* for bootstrap 3
* for bootstrap 3..5
*
* CI SERVER GUI
*
* $oHtml=new htmlguielements();
......@@ -9,52 +10,59 @@
* echo $oHtml->getBox('error', 'errormessage');
* echo $oHtml->getIcon('fa-pencil');
*
* echo $oHtml->getLink(array(
* echo $oHtml->getLink([
* 'href'=>'https://www.axel-hahn.de',
* 'class'=>'btn btn-primary',
* 'icon'=>'fa-close',
* 'label'=>'linked text',
* ));
* ]);
*
* echo $oHtml->getTabs(
* array(
* [
* 'tab 1'=>'Inhalt #1',
* 'tab 2'=>'Inhalt #2',
* )
* ]
* );
*
*
* @author hahn
*
* 2024-08-23 v1.1 Axel Hahn php8 only; added variable types; short array syntax; remove unneeded methods
*/
class htmlguielements{
class htmlguielements
{
var $aCfg=array(
'buttons'=>array(
/**
* Configuration array with icons
* @var array
*/
var $aCfg = [
'buttons' => [
// bootstrap defaults
'primary'=>array('class'=>'btn-primary', 'icon'=>''),
'success'=>array('class'=>'btn-success', 'icon'=>''),
'info'=>array('class'=>'btn-info', 'icon'=>''),
'warning'=>array('class'=>'btn-warning', 'icon'=>''),
'danger'=>array('class'=>'btn-danger', 'icon'=>''),
'primary' => ['class' => 'btn-primary', 'icon' => ''],
'success' => ['class' => 'btn-success', 'icon' => ''],
'info' => ['class' => 'btn-info', 'icon' => ''],
'warning' => ['class' => 'btn-warning', 'icon' => ''],
'danger' => ['class' => 'btn-danger', 'icon' => ''],
// custom buttons
'close'=>array('class'=>'btn-danger', 'icon'=>'fa-solid fa-times'),
'error'=>array('class'=>'btn-danger', 'icon'=>'fa-solid fa-bolt'),
'ok'=>array('class'=>'btn-primary', 'icon'=>'fa-solid fa-check'),
'close' => ['class' => 'btn-danger', 'icon' => 'fa-solid fa-times'],
'error' => ['class' => 'btn-danger', 'icon' => 'fa-solid fa-bolt'],
'ok' => ['class' => 'btn-primary', 'icon' => 'fa-solid fa-check'],
// deploy actions and buttons
'accept'=>array('class'=>''),
'build'=>array('class'=>''),
'cleanup'=>array('class'=>''),
'deploy'=>array('class'=>'', 'icon'=>'fa-solid fa-forward'),
'new'=>array('class'=>'', 'icon'=>'fa-regular fa-star'),
'overview'=>array('class'=>''),
'phase'=>array('class'=>'', 'icon'=>'fa-solid fa-chevron-right'),
'rollback'=>array('class'=>'', 'icon'=>'fa-solid fa-forward'),
'setup'=>array('class'=>''),
),
'icons'=>array(
'accept' => ['class' => ''],
'build' => ['class' => ''],
'cleanup' => ['class' => ''],
'deploy' => ['class' => '', 'icon' => 'fa-solid fa-forward'],
'new' => ['class' => '', 'icon' => 'fa-regular fa-star'],
'overview' => ['class' => ''],
'phase' => ['class' => '', 'icon' => 'fa-solid fa-chevron-right'],
'rollback' => ['class' => '', 'icon' => 'fa-solid fa-forward'],
'setup' => ['class' => ''],
],
'icons' => [
'menu' => 'fa-solid fa-chevron-right',
'valuestore' => 'fa-solid fa-tags',
......@@ -81,7 +89,7 @@ class htmlguielements{
'help' => 'fa-solid fa-life-ring',
'login' => 'fa-solid fa-right-to-bracket',
'new' => 'fa-regular fa-star',
'phase'=>'fa-solid fa-chevron-right',
// 'phase' => 'fa-solid fa-chevron-right',
'poweroff' => 'fa-solid fa-power-off',
'refresh' => 'fa-solid fa-sync',
'rollback' => 'fa-solid fa-forward',
......@@ -130,11 +138,15 @@ class htmlguielements{
'sign-error' => 'fa-solid fa-bolt',
'sign-ok' => 'fa-solid fa-check',
'sign-success' => 'fa-solid fa-check',
),
);
],
];
public function __construct() {
return true;
/**
* Constructor
*/
public function __construct()
{
// nothing here
}
// ----------------------------------------------------------------------
......@@ -142,13 +154,14 @@ class htmlguielements{
// ----------------------------------------------------------------------
/**
* add an html attribute if the attribute exists as a key
* Add an html attribute if the attribute exists as a key
* @param string $sAttribute html attribute to add
* @param string $aData item array
* @param array $aData item array
* @param string $sDefault use default if key does not exists
* @return string
*/
public function addAttributeFromKey($sAttribute, $aData, $sDefault=''){
public function addAttributeFromKey(string $sAttribute, array $aData, string $sDefault = ''): string
{
return (isset($aData[$sAttribute])
? $this->addAttribute($sAttribute, $aData[$sAttribute])
: $this->addAttribute($sAttribute, $sDefault)
......@@ -156,22 +169,24 @@ class htmlguielements{
}
/**
* add an html attribute if value is not empty
* Add an html attribute if value is not empty
* @param string $sAttribute html attribute to add
* @param string $sValue value of attribute
* @return string
*/
public function addAttribute($sAttribute, $sValue){
public function addAttribute(string $sAttribute, string $sValue): string
{
return ($sValue ? ' ' . $sAttribute . '="' . $sValue . '"' : '');
}
/**
* get html attributes as string from all keys of given hash
* Get html attributes as string from all keys of given hash
*
* @param array $aItem
* @return string
*/
public function addAllAttributes($aItem){
public function addAllAttributes(array $aItem): string
{
$sReturn = '';
foreach (array_keys($aItem) as $sKey) {
$sReturn .= $this->addAttributeFromKey($sKey, $aItem);
......@@ -184,45 +199,47 @@ class htmlguielements{
// ----------------------------------------------------------------------
/**
* get html code for icon; glypphicons and font-awesome is supported
* Get html code for icon; glypphicons and font-awesome is supported
*
* @param string $sLabel label of icon
* @return string
*/
public function getIcon($sLabel){
public function getIcon(string $sLabel): string
{
if (!$sLabel) {
return '';
}
$sPrefix=( strpos($sLabel, 'fa-')===0 ? 'fa' : '');
// if(!$sPrefix){
if (isset($this->aCfg['icons'][$sLabel])) {
return $this->getIconByType($sLabel);
}
return '<i'.$this->addAttribute('class', ($sPrefix ? $sPrefix . ' ' : '').$sLabel).'></i> ';
return '<i' . $this->addAttribute('class', $sLabel) . '></i> ';
}
/**
* get html code for icon; glypphicons and font-awesome is supported
* Get html code for icon
*
* @param string $sLabel label of icon
* @return string
*/
public function getIconClass($sLabel){
public function getIconClass(string $sLabel): string
{
if (!$sLabel) {
return '';
}
if (isset($this->aCfg['icons'][$sLabel])) {
return $this->aCfg['icons'][$sLabel];
}
$sPrefix=( strpos($sLabel, 'fa-')===0 ? 'fa' : '');
return ($sPrefix ? $sPrefix . ' ' : '').$sLabel;
return $sLabel;
}
/**
* get a default icon from config
* Get a default icon from config
*
* @param string $sType icon type
* @return array
* @return string
*/
public function getIconByType($sType){
public function getIconByType(string $sType): string
{
return (isset($this->aCfg['icons'][$sType])
? $this->getIcon($this->aCfg['icons'][$sType])
: ''
......@@ -230,18 +247,19 @@ class htmlguielements{
}
/**
* get html code for icon; glypphicons and font-awesome is supported
* Get html code for icon; glypphicons and font-awesome is supported
*
* @param array $aItem array with link attributes; href for target; "label" and "icon"
* @return string
*/
public function getLink($aItem){
public function getLink(array $aItem): string
{
$sHref = $this->addAttributeFromKey('href', $aItem, '#');
$sLabel = (isset($aItem['icon']) ? $this->getIcon($aItem['icon']) : '')
. (isset($aItem['label']) ? $aItem['label'] : '');
foreach(array('href', 'icon', 'label') as $sKey){
foreach (['href', 'icon', 'label'] as $sKey) {
if (isset($aItem[$sKey])) {
unset($aItem[$sKey]);
}
......@@ -256,13 +274,14 @@ class htmlguielements{
}
/**
* add default css classes and colors based on $aItem['type'] and the
* Add default css classes and colors based on $aItem['type'] and the
* local default settings in $this->aCfg
*
* @param array $aItem
* @return array
*/
protected function _getButtonattributesByType($aItem){
protected function _getButtonattributesByType(array $aItem): array
{
$aReturn = $aItem;
if (isset($this->aCfg['buttons'][$aItem['type']])) {
$sClass = $this->aCfg['buttons'][$aItem['type']]['class'];
......@@ -285,13 +304,14 @@ class htmlguielements{
/**
* get html code for icon; glypphicons and font-awesome is supported
* Get html code for a button like link
*
* @param array $aItem array with link attributes; href for target; "label" and "icon"
* @return string
*/
public function getLinkButton($aItem){
foreach(array('class', 'icon') as $sKey){
public function getLinkButton($aItem)
{
foreach (['class', 'icon'] as $sKey) {
if (!isset($aItem[$sKey])) {
$aItem[$sKey] = '';
}
......@@ -317,18 +337,20 @@ class htmlguielements{
// ----------------------------------------------------------------------
/**
* get html code of a div around a message
* Get html code of a div around a message
*
* @param string $sWarnlevel one of error|success|info|warning to get a colored box
* @param string $sMessage message text
* @return string
*/
public function getBox($sWarnlevel, $sMessage) {
$aCfg = array(
"error" => array("class" => "alert alert-danger", "prefix" => t("error")),
"success" => array("class" => "alert alert-success", "prefix" => t("success")),
"info" => array("class" => "alert alert-info", "prefix" => t("info")),
"warning" => array("class" => "alert alert-warning", "prefix" => t("warning")),
);
public function getBox(string $sWarnlevel, string $sMessage): string
{
$aCfg = [
"error" => ["class" => "alert alert-danger", "prefix" => t("error")],
"success" => ["class" => "alert alert-success", "prefix" => t("success")],
"info" => ["class" => "alert alert-info", "prefix" => t("info")],
"warning" => ["class" => "alert alert-warning", "prefix" => t("warning")],
];
$sClass = "";
if (isset($aCfg[$sWarnlevel])) {
$sClass = $aCfg[$sWarnlevel]["class"];
......@@ -337,60 +359,14 @@ class htmlguielements{
return '<div' . $this->addAttribute('class', $sClass) . '>' . $sMessage . '</div>';
}
/**
* get html code for tabs with content
*
* @staticvar int $iCounter internal counter for tabs ans content
* @param array $aTabData tab data; key is the tab label; value the content of its tab
* @return string
*/
public function getNav__UNUSED($aTabData){
$sTabs='';
$sContent='';
static $iCounter=0;
$iTab=0;
if (!is_array($aTabData) || !count($aTabData)){
return false;
}
$sNavType=$aTabData['options']['type']; // "tabs" or "pills"
$sNavCss='nav nav-'.$sNavType;
if (isset($aTabData['options']['stacked']) && $aTabData['options']['stacked']){
$sNavCss.=' nav-stacked';
}
if (isset($aTabData['options']['justified']) && $aTabData['options']['justified']){
$sNavCss.=' nav-justified';
}
$sNavType=$aTabData['options']['justified'];
foreach ($aTabData['tabs'] as $sTabLabel=>$sTabContent){
$iCounter++;
$iTab++;
$sId="tab-generated-$iCounter";
$sTabs.= ($iTab==1 ? '<li class="active"' : '<li')
. ' role="presentation">'
. '<a href="#'.$sId.'" data-toggle="tab">' . $sTabLabel . '</a></li>'
;
$sContent.='<div class="tab-pane'
.($iTab==1 ? ' active' : '')
.'" id="'.$sId.'">'
.$sTabContent
.'</div>'
;
}
return '<div class="tabbable">'
. '<ul class="'.$sNavCss.'">'. $sTabs.'</ul>'
. '<div class="tab-content">'.$sContent.'</div>'
. '</div>';
}
/**
* get html code for a table
*
* @param array $aTabledata array with subkeys "header" and "body"
* @return string
*/
public function getTable($aTabledata) {
public function getTable(array $aTabledata): string
{
$sTHead = '';
$sTBody = '';
if (isset($aTabledata['body'])) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment