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
Branches
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
<?php <?php
/** /**
* html gui elements * html gui elements
* for bootstrap 3 * for bootstrap 3..5
*
* CI SERVER GUI * CI SERVER GUI
* *
* $oHtml=new htmlguielements(); * $oHtml=new htmlguielements();
...@@ -9,52 +10,59 @@ ...@@ -9,52 +10,59 @@
* echo $oHtml->getBox('error', 'errormessage'); * echo $oHtml->getBox('error', 'errormessage');
* echo $oHtml->getIcon('fa-pencil'); * echo $oHtml->getIcon('fa-pencil');
* *
* echo $oHtml->getLink(array( * echo $oHtml->getLink([
* 'href'=>'https://www.axel-hahn.de', * 'href'=>'https://www.axel-hahn.de',
* 'class'=>'btn btn-primary', * 'class'=>'btn btn-primary',
* 'icon'=>'fa-close', * 'icon'=>'fa-close',
* 'label'=>'linked text', * 'label'=>'linked text',
* )); * ]);
* *
* echo $oHtml->getTabs( * echo $oHtml->getTabs(
* array( * [
* 'tab 1'=>'Inhalt #1', * 'tab 1'=>'Inhalt #1',
* 'tab 2'=>'Inhalt #2', * 'tab 2'=>'Inhalt #2',
* ) * ]
* ); * );
* *
* *
* @author hahn * @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 // bootstrap defaults
'primary'=>array('class'=>'btn-primary', 'icon'=>''), 'primary' => ['class' => 'btn-primary', 'icon' => ''],
'success'=>array('class'=>'btn-success', 'icon'=>''), 'success' => ['class' => 'btn-success', 'icon' => ''],
'info'=>array('class'=>'btn-info', 'icon'=>''), 'info' => ['class' => 'btn-info', 'icon' => ''],
'warning'=>array('class'=>'btn-warning', 'icon'=>''), 'warning' => ['class' => 'btn-warning', 'icon' => ''],
'danger'=>array('class'=>'btn-danger', 'icon'=>''), 'danger' => ['class' => 'btn-danger', 'icon' => ''],
// custom buttons // custom buttons
'close'=>array('class'=>'btn-danger', 'icon'=>'fa-solid fa-times'), 'close' => ['class' => 'btn-danger', 'icon' => 'fa-solid fa-times'],
'error'=>array('class'=>'btn-danger', 'icon'=>'fa-solid fa-bolt'), 'error' => ['class' => 'btn-danger', 'icon' => 'fa-solid fa-bolt'],
'ok'=>array('class'=>'btn-primary', 'icon'=>'fa-solid fa-check'), 'ok' => ['class' => 'btn-primary', 'icon' => 'fa-solid fa-check'],
// deploy actions and buttons // deploy actions and buttons
'accept'=>array('class'=>''), 'accept' => ['class' => ''],
'build'=>array('class'=>''), 'build' => ['class' => ''],
'cleanup'=>array('class'=>''), 'cleanup' => ['class' => ''],
'deploy'=>array('class'=>'', 'icon'=>'fa-solid fa-forward'), 'deploy' => ['class' => '', 'icon' => 'fa-solid fa-forward'],
'new'=>array('class'=>'', 'icon'=>'fa-regular fa-star'), 'new' => ['class' => '', 'icon' => 'fa-regular fa-star'],
'overview'=>array('class'=>''), 'overview' => ['class' => ''],
'phase'=>array('class'=>'', 'icon'=>'fa-solid fa-chevron-right'), 'phase' => ['class' => '', 'icon' => 'fa-solid fa-chevron-right'],
'rollback'=>array('class'=>'', 'icon'=>'fa-solid fa-forward'), 'rollback' => ['class' => '', 'icon' => 'fa-solid fa-forward'],
'setup'=>array('class'=>''), 'setup' => ['class' => ''],
), ],
'icons'=>array( 'icons' => [
'menu' => 'fa-solid fa-chevron-right', 'menu' => 'fa-solid fa-chevron-right',
'valuestore' => 'fa-solid fa-tags', 'valuestore' => 'fa-solid fa-tags',
...@@ -81,7 +89,7 @@ class htmlguielements{ ...@@ -81,7 +89,7 @@ class htmlguielements{
'help' => 'fa-solid fa-life-ring', 'help' => 'fa-solid fa-life-ring',
'login' => 'fa-solid fa-right-to-bracket', 'login' => 'fa-solid fa-right-to-bracket',
'new' => 'fa-regular fa-star', 'new' => 'fa-regular fa-star',
'phase'=>'fa-solid fa-chevron-right', // 'phase' => 'fa-solid fa-chevron-right',
'poweroff' => 'fa-solid fa-power-off', 'poweroff' => 'fa-solid fa-power-off',
'refresh' => 'fa-solid fa-sync', 'refresh' => 'fa-solid fa-sync',
'rollback' => 'fa-solid fa-forward', 'rollback' => 'fa-solid fa-forward',
...@@ -130,11 +138,15 @@ class htmlguielements{ ...@@ -130,11 +138,15 @@ class htmlguielements{
'sign-error' => 'fa-solid fa-bolt', 'sign-error' => 'fa-solid fa-bolt',
'sign-ok' => 'fa-solid fa-check', 'sign-ok' => 'fa-solid fa-check',
'sign-success' => '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{ ...@@ -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 $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 * @param string $sDefault use default if key does not exists
* @return string * @return string
*/ */
public function addAttributeFromKey($sAttribute, $aData, $sDefault=''){ public function addAttributeFromKey(string $sAttribute, array $aData, string $sDefault = ''): string
{
return (isset($aData[$sAttribute]) return (isset($aData[$sAttribute])
? $this->addAttribute($sAttribute, $aData[$sAttribute]) ? $this->addAttribute($sAttribute, $aData[$sAttribute])
: $this->addAttribute($sAttribute, $sDefault) : $this->addAttribute($sAttribute, $sDefault)
...@@ -156,22 +169,24 @@ class htmlguielements{ ...@@ -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 $sAttribute html attribute to add
* @param string $sValue value of attribute * @param string $sValue value of attribute
* @return string * @return string
*/ */
public function addAttribute($sAttribute, $sValue){ public function addAttribute(string $sAttribute, string $sValue): string
{
return ($sValue ? ' ' . $sAttribute . '="' . $sValue . '"' : ''); 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 * @param array $aItem
* @return string * @return string
*/ */
public function addAllAttributes($aItem){ public function addAllAttributes(array $aItem): string
{
$sReturn = ''; $sReturn = '';
foreach (array_keys($aItem) as $sKey) { foreach (array_keys($aItem) as $sKey) {
$sReturn .= $this->addAttributeFromKey($sKey, $aItem); $sReturn .= $this->addAttributeFromKey($sKey, $aItem);
...@@ -184,45 +199,47 @@ class htmlguielements{ ...@@ -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 * @param string $sLabel label of icon
* @return string * @return string
*/ */
public function getIcon($sLabel){ public function getIcon(string $sLabel): string
{
if (!$sLabel) { if (!$sLabel) {
return ''; return '';
} }
$sPrefix=( strpos($sLabel, 'fa-')===0 ? 'fa' : '');
// if(!$sPrefix){
if (isset($this->aCfg['icons'][$sLabel])) { if (isset($this->aCfg['icons'][$sLabel])) {
return $this->getIconByType($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 * @param string $sLabel label of icon
* @return string * @return string
*/ */
public function getIconClass($sLabel){ public function getIconClass(string $sLabel): string
{
if (!$sLabel) { if (!$sLabel) {
return ''; return '';
} }
if (isset($this->aCfg['icons'][$sLabel])) { if (isset($this->aCfg['icons'][$sLabel])) {
return $this->aCfg['icons'][$sLabel]; return $this->aCfg['icons'][$sLabel];
} }
$sPrefix=( strpos($sLabel, 'fa-')===0 ? 'fa' : ''); return $sLabel;
return ($sPrefix ? $sPrefix . ' ' : '').$sLabel;
} }
/** /**
* get a default icon from config * Get a default icon from config
*
* @param string $sType icon type * @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]) return (isset($this->aCfg['icons'][$sType])
? $this->getIcon($this->aCfg['icons'][$sType]) ? $this->getIcon($this->aCfg['icons'][$sType])
: '' : ''
...@@ -230,18 +247,19 @@ class htmlguielements{ ...@@ -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" * @param array $aItem array with link attributes; href for target; "label" and "icon"
* @return string * @return string
*/ */
public function getLink($aItem){ public function getLink(array $aItem): string
{
$sHref = $this->addAttributeFromKey('href', $aItem, '#'); $sHref = $this->addAttributeFromKey('href', $aItem, '#');
$sLabel = (isset($aItem['icon']) ? $this->getIcon($aItem['icon']) : '') $sLabel = (isset($aItem['icon']) ? $this->getIcon($aItem['icon']) : '')
. (isset($aItem['label']) ? $aItem['label'] : ''); . (isset($aItem['label']) ? $aItem['label'] : '');
foreach(array('href', 'icon', 'label') as $sKey){ foreach (['href', 'icon', 'label'] as $sKey) {
if (isset($aItem[$sKey])) { if (isset($aItem[$sKey])) {
unset($aItem[$sKey]); unset($aItem[$sKey]);
} }
...@@ -256,13 +274,14 @@ class htmlguielements{ ...@@ -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 * local default settings in $this->aCfg
* *
* @param array $aItem * @param array $aItem
* @return array * @return array
*/ */
protected function _getButtonattributesByType($aItem){ protected function _getButtonattributesByType(array $aItem): array
{
$aReturn = $aItem; $aReturn = $aItem;
if (isset($this->aCfg['buttons'][$aItem['type']])) { if (isset($this->aCfg['buttons'][$aItem['type']])) {
$sClass = $this->aCfg['buttons'][$aItem['type']]['class']; $sClass = $this->aCfg['buttons'][$aItem['type']]['class'];
...@@ -285,13 +304,14 @@ class htmlguielements{ ...@@ -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" * @param array $aItem array with link attributes; href for target; "label" and "icon"
* @return string * @return string
*/ */
public function getLinkButton($aItem){ public function getLinkButton($aItem)
foreach(array('class', 'icon') as $sKey){ {
foreach (['class', 'icon'] as $sKey) {
if (!isset($aItem[$sKey])) { if (!isset($aItem[$sKey])) {
$aItem[$sKey] = ''; $aItem[$sKey] = '';
} }
...@@ -317,18 +337,20 @@ class htmlguielements{ ...@@ -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 $sWarnlevel one of error|success|info|warning to get a colored box
* @param string $sMessage message text * @param string $sMessage message text
* @return string * @return string
*/ */
public function getBox($sWarnlevel, $sMessage) { public function getBox(string $sWarnlevel, string $sMessage): string
$aCfg = array( {
"error" => array("class" => "alert alert-danger", "prefix" => t("error")), $aCfg = [
"success" => array("class" => "alert alert-success", "prefix" => t("success")), "error" => ["class" => "alert alert-danger", "prefix" => t("error")],
"info" => array("class" => "alert alert-info", "prefix" => t("info")), "success" => ["class" => "alert alert-success", "prefix" => t("success")],
"warning" => array("class" => "alert alert-warning", "prefix" => t("warning")), "info" => ["class" => "alert alert-info", "prefix" => t("info")],
); "warning" => ["class" => "alert alert-warning", "prefix" => t("warning")],
];
$sClass = ""; $sClass = "";
if (isset($aCfg[$sWarnlevel])) { if (isset($aCfg[$sWarnlevel])) {
$sClass = $aCfg[$sWarnlevel]["class"]; $sClass = $aCfg[$sWarnlevel]["class"];
...@@ -337,60 +359,14 @@ class htmlguielements{ ...@@ -337,60 +359,14 @@ class htmlguielements{
return '<div' . $this->addAttribute('class', $sClass) . '>' . $sMessage . '</div>'; 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 * get html code for a table
* *
* @param array $aTabledata array with subkeys "header" and "body" * @param array $aTabledata array with subkeys "header" and "body"
* @return string * @return string
*/ */
public function getTable($aTabledata) { public function getTable(array $aTabledata): string
{
$sTHead = ''; $sTHead = '';
$sTBody = ''; $sTBody = '';
if (isset($aTabledata['body'])) { 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