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

formgen: php8 only; added variable types; short array syntax

parent 521674a6
Branches
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
......@@ -9,52 +9,57 @@
feature complete.
---------------------------------------------------------------------
2013-11-08 Axel <axel.hahn@iml.unibe.ch>
Axel <axel.hahn@iml.unibe.ch>
2013-11-08 Axel
2024-08-23 v1.1 Axel php8 only; added variable types; short array syntax
###################################################################### */
class formgen {
class formgen
{
var $aForm = array();
var $aForm = [];
var $sRequired = ' <span title="Eingabe ist erforderlich"><span style="color:#c00;">*</span></span>';
/**
* constructor
* Constructor
* @param array $aNewFormData
* @return boolean
*/
public function __construct($aNewFormData = array()) {
public function __construct($aNewFormData = [])
{
if (is_array($aNewFormData) && count($aNewFormData)) {
return $this->setFormarray($aNewFormData);
$this->setFormarray($aNewFormData);
}
return true;
}
/**
* set a new array
* Set a new array for a new form
* @param array $aNewFormData
* @return boolean
*/
public function setFormarray($aNewFormData = array()) {
public function setFormarray($aNewFormData = [])
{
if (!is_array($aNewFormData) || !count($aNewFormData)) {
return false;
}
return $this->aForm = $aNewFormData;
$this->aForm = $aNewFormData;
return true;
}
/**
* get html code for a completely rendered form
* Get html code for a completely rendered form
* @param string $sFormId
* @return string html output
*/
public function renderHtml($sFormId) {
public function renderHtml(string $sFormId): string
{
$sReturn = false;
if (!isset($this->aForm[$sFormId])) {
die("ERROR: " . __CLASS__ . ":" . __FUNCTION__ . " - form id " . $sFormId . " does not exist.");
throw new Exception("ERROR: " . __CLASS__ . ":" . __FUNCTION__ . " - form id " . $sFormId . " does not exist.");
}
// FORM tag
$sReturn .= '<form ';
if (isset($this->aForm[$sFormId]["meta"])) {
foreach (array("method", "action", "target", "accept-charset", "class", "id", "name") as $sAttr) {
foreach (["method", "action", "target", "accept-charset", "class", "id", "name"] as $sAttr) {
if (isset($this->aForm[$sFormId]["meta"][$sAttr])) {
$sReturn .= $sAttr . '="' . $this->aForm[$sFormId]["meta"][$sAttr] . '" ';
}
......@@ -72,12 +77,13 @@ class formgen {
}
/**
* add html attributes if they exist
* Add html attributes if they exist
* @param array $aAttributes list of attributes to search for
* @param array $elementData array of form element
* @return string
*/
private function _addHtmlAtrributes($aAttributes, $elementData) {
private function _addHtmlAtrributes(array $aAttributes, array $elementData): string
{
$sReturn = false;
foreach ($aAttributes as $sAtrr) {
if (isset($elementData[$sAtrr]) && $elementData[$sAtrr]) {
......@@ -89,36 +95,50 @@ class formgen {
return $sReturn;
}
private function _addLabel($sLabel, $sFor, $sClass = false) {
$sReturn = false;
$sReturn = '<label for="' . $sFor . '"';
if ($sClass)
$sReturn.=' class="' . $sClass . '"';
$sReturn.='>' . $sLabel . '</label>';
$sReturn.="\n";
return $sReturn;
/**
* Add a label next to a form element
* @param string $sLabel Labeltext to show
* @param string $sFor for attribute to ad (points to the id of the form element)
* @param string $sClass css class
* @return string
*/
private function _addLabel(string $sLabel, string $sFor, string $sClass = ''): string
{
return "<label for=\"$sFor\""
. ($sClass ? " class=\"$sClass\"" : '')
. ">$sLabel</label>"
. "\n"
;
}
private function _checkReqiredKeys($aArray, $aRequiredKeys, $sLabel = false) {
/**
* Ensure that all required keys are set
* @param array $aArray given array
* @param array $aRequiredKeys set of required keys
* @param string $sLabel form label - will be shown in error message
* @throws \Exception
* @return bool
*/
private function _checkReqiredKeys(array $aArray, array $aRequiredKeys, string $sLabel = ''): bool
{
$bReturn = true;
foreach ($aRequiredKeys as $sKey) {
if (!isset($aArray[$sKey])) {
die("ERROR: $sLabel<br>Missing key \"$sKey\" in the array of a form element:<pre>" . print_r($aArray, true) . "</pre>");
$bReturn = false;
throw new Exception("ERROR: $sLabel<br>Missing key \"$sKey\" in the array of a form element:<pre>" . print_r($aArray, true) . "</pre>");
}
}
return $bReturn;
}
/**
* render a single form element
* Render a single form element
* @param string $sId id of a form element
* @param array $elementData array of form element
* @return string html output
*/
public function renderHtmlElement($sId, $elementData) {
public function renderHtmlElement(string $sId, array $elementData): string
{
$sReturn = false;
$aAllowedHtmlAttributes = array();
$sDefaultAttributes = ""
. "class,"
......@@ -132,7 +152,7 @@ class formgen {
if (!isset($elementData["type"])) {
print_r($elementData);
die("ERROR: " . __CLASS__ . ":" . __FUNCTION__ . " - key &quot;type&quot; does not exist.");
throw new Exception("ERROR: " . __CLASS__ . ":" . __FUNCTION__ . " - key &quot;type&quot; does not exist.");
}
$sFormElement = false;
......@@ -149,7 +169,7 @@ class formgen {
switch ($elementData["type"]) {
case "button":
$this->_checkReqiredKeys($elementData, array("value"));
$this->_checkReqiredKeys($elementData, ["value"]);
$elementData["class"] = $elementData["class"] ? $elementData["class"] : "btn btn-default";
$sFormElement .= ' <button id="' . $sId . '" ';
$sFormElement .= $this->_addHtmlAtrributes(explode(",", "$sDefaultAttributes,checked,name"), $elementData);
......@@ -160,7 +180,7 @@ class formgen {
break;
case "checkbox":
$this->_checkReqiredKeys($elementData, array("name"));
$this->_checkReqiredKeys($elementData, ["name"]);
foreach ($elementData["options"] as $idOption => $aOptionData) {
$sFormElement .= "\n" . '<div class="checkbox">';
$s = preg_replace('/\W/iu', '', $sId . $idOption);
......@@ -181,7 +201,7 @@ class formgen {
break;
case "hidden":
$this->_checkReqiredKeys($elementData, array("value"));
$this->_checkReqiredKeys($elementData, ["value"]);
$sFormElement .= ' <input type="hidden" id="' . $sId . '" ';
$sFormElement .= $this->_addHtmlAtrributes(explode(",", "name,value"), $elementData);
$sFormElement .= " />";
......@@ -196,7 +216,7 @@ class formgen {
break;
case "radio":
$this->_checkReqiredKeys($elementData, array("name"));
$this->_checkReqiredKeys($elementData, ["name"]);
foreach ($elementData["options"] as $idOption => $aOptionData) {
$sFormElement .= "\n" . '<div class="radio">';
$s = preg_replace('/\W/iu', '', $sId . $idOption);
......@@ -224,7 +244,7 @@ class formgen {
case "select":
// HINWEIS optgroups werden nicht unterstuezt - nur einfache Listen
$this->_checkReqiredKeys($elementData, array("name"));
$this->_checkReqiredKeys($elementData, ["name"]);
$sDivClass = (isset($elementData["inline"]) && $elementData["inline"]) ? "form-group" : "col-sm-10";
$elementData['class'] .= " form-control";
$sFormElement .= '<div class="' . $sDivClass . '">' . "\n" . '<select id="' . $sId . '" ';
......@@ -252,7 +272,7 @@ class formgen {
break;
case "submit":
$this->_checkReqiredKeys($elementData, array("value"));
$this->_checkReqiredKeys($elementData, ["value"]);
$sClass = "btn btn-primary ";
if (isset($elementData["class"])) {
$sClass .= $elementData["class"];
......@@ -268,11 +288,11 @@ class formgen {
case "text":
case "password":
$this->_checkReqiredKeys($elementData, array("name"));
$this->_checkReqiredKeys($elementData, ["name"]);
$sFormElement .= ' <input type="' . $elementData["type"] . '" id="' . $sId . '" class="form-control col-sm-10" ';
$aAllowedHtmlAttributes["text"] = explode(",", "");
$sFormElement .= $this->_addHtmlAtrributes(explode(",", "$sDefaultAttributes,name,autocomplete,autofocus,list,disabled,onchange,pattern,placeholder,required,size,value"), $elementData);
// $sFormElement.=$this->_addHtmlAtrributes(array("name", "value", "size", "placeholder", "required"), $elementData);
// $sFormElement.=$this->_addHtmlAtrributes(["name", "value", "size", "placeholder", "required"], $elementData);
// IE: Return abfangen lassen
// $sFormElement.=' onkeypress="return checkKey(event);"';
$sFormElement .= ' />';
......@@ -293,11 +313,11 @@ class formgen {
break;
case "textarea":
$this->_checkReqiredKeys($elementData, array("name"));
$this->_checkReqiredKeys($elementData, ["name"]);
$sFormElement .= ' <textarea id="' . $sId . '" class="form-control col-sm-10" ';
$aAllowedHtmlAttributes["text"] = explode(",", "");
$sFormElement .= $this->_addHtmlAtrributes(explode(",", "$sDefaultAttributes,name,onchange,placeholder,required,cols,rows"), $elementData);
// $sFormElement.=$this->_addHtmlAtrributes(array("name", "value", "size", "placeholder", "required"), $elementData);
// $sFormElement.=$this->_addHtmlAtrributes(["name", "value", "size", "placeholder", "required"], $elementData);
$sFormElement .= '>' . $elementData['value'] . '</textarea>';
$sFormElement .= "\n";
......@@ -318,7 +338,8 @@ class formgen {
if (isset($elementData["mode"]) && $elementData["mode"] == 'table' && $sHtmlTable) {
$sHtmlDefault = $sHtmlTable;
} else {
if ($elementData["type"] != "button"
if (
$elementData["type"] != "button"
&& $elementData["type"] != "fieldset"
&& $elementData["type"] != "markup"
&& $elementData["type"] != "hidden"
......@@ -337,5 +358,3 @@ class formgen {
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment