From 6fd41a6c0fb7479ef83071c3be8b5ca5f5f8903e Mon Sep 17 00:00:00 2001 From: Dominique FOURNIER Date: Thu, 21 Jul 2022 14:36:53 +0200 Subject: [PATCH] Formfield added --- src/Formfield.php | 1715 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1715 insertions(+) create mode 100644 src/Formfield.php diff --git a/src/Formfield.php b/src/Formfield.php new file mode 100644 index 0000000..dcfcd1d --- /dev/null +++ b/src/Formfield.php @@ -0,0 +1,1715 @@ + + * @license BSD + */ + +namespace Domframework; + +/** The definition of a formfield + */ +class Formfield +{ + /** The form name + */ + public $formName; + /** The name of the field + */ + public $name; + /** The label of the field + */ + public $label; + /** The titles of the field + */ + public $titles; + /** The defaults values of the field + */ + public $defaults; + /** The type of the field (text, password, checkbox, select) + */ + public $type="text"; + /** The state of the field : hidden or show + */ + public $hidden = false; + /** Allow a help message to be displayed below the field. In case of error, + * it is overrided by the error message + */ + public $help; + /** Display the placeholder if needed + */ + public $placeholder = false; + /** The multiplicity of selection of the field (available in select only) + */ + public $multiple; + /** The name of group for the fields + */ + public $group; + /** The read-only feature of the field + */ + public $readonly; + /** The field is mandatory + */ + public $mandatory; + /** The statut of error of the field + */ + public $error; + /** Number of rows + */ + public $rows; + /** Number of columns + */ + public $cols; + /** The Bootstrap width of the column of titles + */ + public $titlewidth = 2; + /** The Bootstrap width of the column of fields + */ + public $fieldwidth = 10; + + /** When adding a field, the name and the label are the minimum mandatory + * @param string $name Name of the field + * @param string|null $label Label of the field + */ + public function __construct ($name, $label = "") + // {{{ + { + $this->name = $name; + $this->label = $label; + } + // }}} + + /** Display really the form + */ + public function display () + // {{{ + { + $func = "field".$this->formTemplate.$this->type; + return $this->$func (); + } + // }}} + + // Setters for all the properties of the class + // {{{ + /** Set the type of the field + * @param string $val The value of the type of the field + */ + public function type ($val) + { + $this->type = $val; + return $this; + } + + /** Set the hidden of the field + * @param string $val The value of the hidden of the field + */ + public function hidden ($val) + { + $this->hidden = !! $val; + return $this; + } + + /** Set the help of the field + * @param string $val The value of the help of the field + */ + public function help ($val) + { + $this->help = $val; + return $this; + } + + /** Set the placeholder + * @param string $val The value of the placeholder + */ + public function placeholder ($val) + { + $this->placeholder = $val; + return $this; + } + + /** Set the multiple + * @param string $val The value of the multiple + */ + public function multiple ($val) + { + $this->multiple = $val; + return $this; + } + + /** Set the group + * @param string $val The value of the group + */ + public function group ($val) + { + $this->group = $val; + return $this; + } + + /** Set the readonly + * @param string $val The value of the readonly + */ + public function readonly ($val) + { + $this->readonly = !! $val; + return $this; + } + + /** Set the mandatory + * @param string $val The value of the mandatory + */ + public function mandatory ($val) + { + $this->mandatory = !! $val; + return $this; + } + + /** Set the rows + * @param string $val The value of the rows + */ + public function rows ($val) + { + $this->rows = $val; + return $this; + } + + /** Set the cols + * @param string $val The value of the cols + */ + public function cols ($val) + { + $this->cols = $val; + return $this; + } + // }}} + + ////////////////////////// + //// BOOTSTRAP3 //// + ////////////////////////// + // {{{ + /** Return the checkbox defined + */ + private function fieldBootstrap3checkbox () + // {{{ + { + // No $this->multiple, $this->rows $this->cols $this->placeholder, + // $this->maxlength + if (! is_array ($this->titles) || count ($this->titles) === 0) + $titles = array (""); + else + $titles = $this->titles; + $res = ""; + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + foreach ($titles as $key=>$val) + { + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]"; + if (count ($titles) > 1) + $res .= "[$key]"; + $res .= "' value='unset'"; + $res .= "/>"; + $res .= "\n"; + $res .= "
\n"; + } + + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the hidden field defined + */ + private function fieldBootstrap3hidden () + // {{{ + { + $res = ""; + // No $this->label, $this->multiple, $this->readonly, $this->hidden, + // $this->rows $this->cols $this->placeholder $this->maxlength + $res .= "name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->values)) + $res .= " value='".htmlspecialchars ($this->values)."'"; + else + $res .= " value='".htmlspecialchars ($this->defaults)."'"; + $res .= "/>\n"; + return $res; + } + // }}} + + /** Return the password field defined + */ + private function fieldBootstrap3password () + // {{{ + { + $res = ""; + // No $this->multiple, $this->rows $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->values)) + $res .= " value='".htmlspecialchars ($this->values, + ENT_QUOTES)."'"; + else + $res .= " value='".htmlspecialchars ($this->defaults, ENT_QUOTES). + "'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + $res .= " class='form-control'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + if (isset ($this->cols)) + $res .= " size='".$this->cols."'"; + if (isset ($this->maxlength)) + $res .= " maxlength='".$this->maxlength."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + if (isset ($this->placeholder) && $this->placeholder !== FALSE) + $res .= " placeholder='".htmlentities ($this->placeholder, ENT_QUOTES). + "'"; + $res .= "/>\n"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the radio field defined + */ + private function fieldBootstrap3radio () + // {{{ + { + $res = ""; + // No $this->multiple, $this->rows $this->cols $this->placeholder + // $this->maxlength + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + if (is_string ($this->defaults)) + $this->defaults = array ($this->defaults); + $res .= " name, ENT_QUOTES)."]'"; + $res .= " value='unset'"; + $res .= "/>\n"; + foreach ($this->titles as $key=>$val) + { + $res .= "
"; + $res .= " \n"; // End label radio + $res .= "
"; + } + + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the checkbox defined + */ + private function fieldBootstrap3select () + // {{{ + { + // No $this->placeholder $this->maxlength + $res = ""; + // $values->$this, $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + if (isset ($this->defaults) && is_array ($this->defaults)) + { + if (isset ($this->readonly) && $this->readonly !== FALSE) + { + foreach ($this->defaults as $key=>$val) + { + $res .= " multiple) && $this->multiple !== FALSE) + { + $res .= " name='$this->formName"."["; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."][". + htmlspecialchars ($key, ENT_QUOTES)."]'"; + } + else + { + $res .= " name='$this->formName"."["; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."]'"; + } + $res .= " value='"; + $res .= htmlspecialchars ($key, ENT_QUOTES)."'"; + $res .= "/>\n"; + } + } + + $res .= " name, ENT_QUOTES)."]"; + if (isset ($this->multiple) && $this->multiple !== FALSE) + $res .= "[]"; + $res .= "'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->multiple) && $this->multiple !== FALSE) + $res .= " multiple='multiple'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " disabled='disabled'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= " class='form-control'"; + if (isset ($this->rows)) + $res .= " size='".$this->rows."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + $res .= ">\n"; + foreach ($this->defaults as $key=>$val) + { + if (! is_string ($val)) + { + throw new \Exception ("Value as defaut for $this->name::$key is not ". + "a string (".gettype ($val).")"); + } + $res .= " \n"; + } + + $res .= " \n"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + } + else + { + $res .= dgettext ("domframework", "No value provided"); + } + + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the submit defined + */ + private function fieldBootstrap3submit () + // {{{ + { + $res = ""; + // No $this->label, $this->multiple, $this->error, $this->rows, + // $this->cols $this->placeholder $this->maxlength + $res .= "
\n"; + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " disabled='disabled'"; + if (isset ($this->defaults)) + $res .= " value='".htmlspecialchars ($this->defaults, ENT_QUOTES). + "'"; + elseif (isset ($this->label)) + $res .= " value='".htmlspecialchars ($this->label, ENT_QUOTES)."'"; + $res .= " class='form-control btn-primary'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + // Block the submit button 10s. The user can not double click on it and + // submit two times the POST to the server + // Re-enable after 15s, if there is a problem with the server + // This code is needed by Chrome and Edge which allow multiple submission of + // a form + $res .= " onclick='submit=this ; "; + $res .= " setTimeout(function() {"; + $res .= " submit.setAttribute(\"disabled\", \"disabled\");"; + $res .= " }, 1);"; + $res .= "'"; + $res .= "/>\n"; + $res .= "
\n"; + $res .= "
\n"; + return $res; + } + // }}} + + /** Return the textarea defined + */ + private function fieldBootstrap3textarea () + // {{{ + { + $res = ""; + // No $this->multiple, $this->titles + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + $res .= " class='form-control'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + if (!isset ($this->cols)) + $this->cols = 20; + $res .= " cols='".$this->cols."'"; + if (!isset ($this->rows)) + $this->rows = 4; + $res .= " rows='".$this->rows."'"; + if (isset ($this->maxlength)) + $res .= " maxlength='".$this->maxlength."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + if (isset ($this->placeholder) && $this->placeholder !== FALSE) + $res .= " placeholder='".htmlentities ($this->placeholder, ENT_QUOTES). + "'"; + $res .= ">"; + if (isset ($this->values)) + $res .= htmlspecialchars ($this->values, ENT_QUOTES); + else + $res .= htmlspecialchars ($this->defaults, ENT_QUOTES); + $res .= "\n"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the text defined + */ + private function fieldBootstrap3text () + // {{{ + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->values)) + $res .= " value='".htmlspecialchars ($this->values, + ENT_QUOTES)."'"; + else + $res .= " value='".htmlspecialchars ($this->defaults, ENT_QUOTES). + "'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + $res .= " class='form-control'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + if (isset ($this->cols)) + $res .= " size='".$this->cols."'"; + if (isset ($this->maxlength)) + $res .= " maxlength='".$this->maxlength."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + if (isset ($this->placeholder) && $this->placeholder !== FALSE) + $res .= " placeholder='".htmlentities ($this->placeholder, ENT_QUOTES). + "'"; + $res .= "/>\n"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the file defined + */ + private function fieldBootstrap3file () + // {{{ + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " \n"; + } + $res .= "
\n"; + if (isset ($this->defaults)) + { + $res .= " \n"; // End labels + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " "; + if (isset ($this->help)) + $res .= "".$this->help.""; + if (isset ($this->help) && isset ($this->errors)) + $res .= "
"; + if (isset ($this->errors)) + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + // }}} + + ////////////////////////// + //// BOOTSTRAP4 //// + ////////////////////////// + // {{{ + /** Return the checkbox defined + */ + private function fieldBootstrap4checkbox () + // {{{ + { + // No $this->multiple, $this->rows $this->cols $this->placeholder, + // $this->maxlength + if (! is_array ($this->titles) || count ($this->titles) === 0) + $this->titles = array (""); + $res = ""; + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + if (is_array ($this->titles) && count ($this->titles) == 1 && + reset ($this->titles) === "") + { + // If more than one title is set, the main label is unusable, so + // do not link it to the checkbox + // If there is no label on the checkbox, this label must be set + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES); + $res .= "'"; + } + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + foreach ($this->titles as $key=>$val) + { + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]"; + if (count ($this->titles) > 1) + $res .= "[$key]"; + $res .= "' value='unset'"; + $res .= "/>\n"; + $res .= " name, ENT_QUOTES)."]"; + if (count ($this->titles) > 1) + $res .= "[$key]"; + $res .= "' id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES); + if (count ($this->titles) > 1) + $res .= "_$key"; + $res .= "'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " disabled='disabled'"; + // Do not check by default ! + // Check is enable if not null and not false and not "unset" and not "" + if (count ($this->titles) === 1) + { + if (isset ($this->values) && + $this->values !== null && + $this->values !== false && + $this->values !== "unset" && + $this->values !== "") + $res .= " checked='checked'"; + elseif (isset ($this->defaults) && + $this->defaults !== null && + $this->defaults !== false && + $this->defaults !== "unset" && + $this->defaults !== "") + $res .= " checked='checked'"; + } + else + { + if (isset ($this->values[$key])) + { + if ($this->values[$key] !== null && + $this->values[$key] !== false && + $this->values[$key] !== "unset" && + $this->values[$key] !== "") + $res .= " checked='checked'"; + } + elseif (isset ($this->defaults[$key])) + { + if ($this->defaults[$key] !== null && + $this->defaults[$key] !== false && + $this->defaults[$key] !== "unset" && + $this->defaults[$key] !== "") + $res .= " checked='checked'"; + } + } + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= " class='form-check-input"; + if (isset ($this->errors)) + { + $res .= " is-invalid"; + } + $res .= "'"; + if (isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + $res .= "/>\n"; + $res .= "
\n"; + } + + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the hidden field defined + */ + private function fieldBootstrap4hidden () + // {{{ + { + $res = ""; + // No $this->label, $this->multiple, $this->readonly, $this->hidden, + // $this->rows $this->cols $this->placeholder $this->maxlength + $res .= "name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->values)) + $res .= " value='".htmlspecialchars ($this->values)."'"; + else + $res .= " value='".htmlspecialchars ($this->defaults)."'"; + $res .= "/>\n"; + return $res; + } + // }}} + + /** Return the password field defined + */ + private function fieldBootstrap4password () + // {{{ + { + $res = ""; + // No $this->multiple, $this->rows $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->values)) + $res .= " value='".htmlspecialchars ($this->values, + ENT_QUOTES)."'"; + else + $res .= " value='".htmlspecialchars ($this->defaults, ENT_QUOTES). + "'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + $res .= " class='form-control"; + if (isset ($this->errors)) + $res .= " is-invalid"; + $res .= "'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + if (isset ($this->cols)) + $res .= " size='".$this->cols."'"; + if (isset ($this->maxlength)) + $res .= " maxlength='".$this->maxlength."'"; + if (isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + if (isset ($this->placeholder) && $this->placeholder !== FALSE) + $res .= " placeholder='".htmlentities ($this->placeholder, ENT_QUOTES). + "'"; + $res .= "/>\n"; + if (isset ($this->errors)) + { + $res .= "
"; + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the radio field defined + */ + private function fieldBootstrap4radio () + // {{{ + { + $res = ""; + // No $this->multiple, $this->rows $this->cols $this->placeholder + // $this->maxlength + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + if (is_array ($this->titles) && count ($this->titles) == 1 && + reset ($this->titles) === "") + { + // If more than one title is set, the main label is unusable, so + // do not link it to the radio + // If there is no label on the checkbox, this label must be set + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES); + $res .= "'"; + } + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + if (is_string ($this->defaults)) + $this->defaults = array ($this->defaults); + $res .= " name, ENT_QUOTES)."]'"; + $res .= " value='unset'"; + $res .= "/>\n"; + foreach ($this->titles as $key=>$val) + { + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_$key'"; + $res .= " value='".htmlspecialchars ($val, ENT_QUOTES)."'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + if (isset ($this->values) && + $this->values === $val) + $res .= " checked='checked'"; + elseif (isset ($this->defaults[0]) && + $this->defaults[0] === $val) + $res .= " checked='checked'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= " class='form-check-input"; + if (isset ($this->errors)) + $res .= " is-invalid"; + $res .= "'"; + if (isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + $res .= "/>\n"; + $res .= " \n"; // End label radio + if (isset ($this->errors) && $key === count ($this->titles) - 1) + { + $res .= "
"; + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + $res .= "
\n"; + } + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the checkbox defined + */ + private function fieldBootstrap4select () + // {{{ + { + // No $this->placeholder $this->maxlength + $res = ""; + // $values->$this, $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + if (isset ($this->defaults) && is_array ($this->defaults)) + { + if (isset ($this->readonly) && $this->readonly !== FALSE) + { + foreach ($this->defaults as $key=>$val) + { + $res .= " multiple) && $this->multiple !== FALSE) + { + $res .= " name='$this->formName"."["; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."][". + htmlspecialchars ($key, ENT_QUOTES)."]'"; + } + else + { + $res .= " name='$this->formName"."["; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."]'"; + } + $res .= " value='"; + $res .= htmlspecialchars ($key, ENT_QUOTES)."'"; + $res .= "/>\n"; + } + } + + $res .= " name, ENT_QUOTES)."]"; + if (isset ($this->multiple) && $this->multiple !== FALSE) + $res .= "[]"; + $res .= "'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->multiple) && $this->multiple !== FALSE) + $res .= " multiple='multiple'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " disabled='disabled'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= " class='form-control"; + if (isset ($this->errors)) + $res .= " is-invalid"; + $res .= "'"; + if (isset ($this->rows)) + $res .= " size='".$this->rows."'"; + if (isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + $res .= ">\n"; + foreach ($this->defaults as $key=>$val) + { + if (! is_string ($val)) + { + throw new \Exception ("Value as defaut for $this->name::$key is not ". + "a string (".gettype ($val).")"); + } + $res .= " \n"; + } + + $res .= " \n"; + if (isset ($this->errors)) + { + $res .= "
"; + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + } + else + { + $res .= dgettext ("domframework", "No value provided"); + } + + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the submit defined + */ + private function fieldBootstrap4submit () + // {{{ + { + $res = ""; + // No $this->label, $this->multiple, $this->error, $this->rows, + // $this->cols $this->placeholder $this->maxlength + $res .= "
\n"; + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " disabled='disabled'"; + if (isset ($this->defaults)) + $res .= " value='".htmlspecialchars ($this->defaults, ENT_QUOTES). + "'"; + elseif (isset ($this->label)) + $res .= " value='".htmlspecialchars ($this->label, ENT_QUOTES)."'"; + $res .= " class='form-control btn-primary'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + // Block the submit button 10s. The user can not double click on it and + // submit two times the POST to the server + // Re-enable after 15s, if there is a problem with the server + // This code is needed by Chrome and Edge which allow multiple submission of + // a form + $res .= " onclick='submit=this ; "; + $res .= " setTimeout(function() {"; + $res .= " submit.setAttribute(\"disabled\", \"disabled\");"; + $res .= " }, 1);"; + $res .= "'"; + $res .= "/>\n"; + $res .= "
\n"; + $res .= "
\n"; + return $res; + } + // }}} + + /** Return the textarea defined + */ + private function fieldBootstrap4textarea () + // {{{ + { + $res = ""; + // No $this->multiple, $this->titles + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + $res .= " class='form-control"; + if (isset ($this->errors)) + $res .= " is-invalid"; + $res .= "'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + if (!isset ($this->cols)) + $this->cols = 20; + $res .= " cols='".$this->cols."'"; + if (!isset ($this->rows)) + $this->rows = 4; + $res .= " rows='".$this->rows."'"; + if (isset ($this->maxlength)) + $res .= " maxlength='".$this->maxlength."'"; + if (isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + if (isset ($this->placeholder) && $this->placeholder !== FALSE) + $res .= " placeholder='".htmlentities ($this->placeholder, ENT_QUOTES). + "'"; + $res .= ">"; + if (isset ($this->values)) + $res .= htmlspecialchars ($this->values, ENT_QUOTES); + else + $res .= htmlspecialchars ($this->defaults, ENT_QUOTES); + $res .= "\n"; + if (isset ($this->errors)) + { + $res .= "
"; + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the text defined + */ + private function fieldBootstrap4text () + // {{{ + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + $res .= " name, ENT_QUOTES)."]'"; + $res .= " id='$this->formName"."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->values)) + $res .= " value='".htmlspecialchars ($this->values, + ENT_QUOTES)."'"; + else + $res .= " value='".htmlspecialchars ($this->defaults, ENT_QUOTES). + "'"; + if (isset ($this->readonly) && $this->readonly !== FALSE) + $res .= " readonly='readonly'"; + $res .= " class='form-control"; + if (isset ($this->errors)) + $res .= " is-invalid"; + $res .= "'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + if (isset ($this->cols)) + $res .= " size='".$this->cols."'"; + if (isset ($this->maxlength)) + $res .= " maxlength='".$this->maxlength."'"; + if (isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } + if (isset ($this->placeholder) && $this->placeholder !== FALSE) + $res .= " placeholder='".htmlentities ($this->placeholder, ENT_QUOTES). + "'"; + $res .= "/>\n"; + if (isset ($this->errors)) + { + $res .= "
"; + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + /** Return the file defined + */ + private function fieldBootstrap4file () + // {{{ + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") + { + $res .= " titlewidth > 0) + $res .= " class='col-sm-$this->titlewidth col-form-label'"; + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."'"; + if (isset ($this->hidden) && $this->hidden !== FALSE) + $res .= " style='display:none'"; + $res .= ">"; + $res .= htmlspecialchars ($this->label); + if (isset ($this->mandatory) && $this->mandatory !== FALSE) + $res .= " *"; + else + $res .= " "; + $res .= "\n"; + } + $res .= "
\n"; + if (isset ($this->defaults)) + { + $res .= " \n"; // End labels + if (isset ($this->errors)) + { + $res .= "
"; + $res .= htmlspecialchars ($this->errors[1]); + $res .= "
\n"; + } + if (isset ($this->help)) + { + $res .= " name, ENT_QUOTES)."_help'>"; + $res .= $this->help; + $res .= "\n"; + } + $res .= "
\n"; // End controls + $res .= "
\n"; // End form-group + return $res; + } + // }}} + + // }}} +}