diff --git a/src/Form.php b/src/Form.php index 58124ff..2026673 100644 --- a/src/Form.php +++ b/src/Form.php @@ -172,7 +172,7 @@ class Form /** * Set the Form Templating to use. - * Can be : Bootstrap3, Bootstrap4 (later Bulma) + * Can be : Bootstrap3, Bootstrap4 Bulma1 * @param string $formTemplate The template to use */ public function formTemplate($formTemplate): self @@ -180,7 +180,7 @@ class Form if ( ! in_array( $formTemplate, - ["Bootstrap3", "Bootstrap4"], + ["Bootstrap3", "Bootstrap4", "Bulma1"], true ) ) { @@ -354,7 +354,10 @@ class Form if ($this->formName != "") { $res .= " id='$this->formName'"; } - $res .= " class='" . $this->formClass . "'>\n"; + if ($this->formTemplate !== "Bulma1") { + $res .= " class='" . $this->formClass . "'"; + } + $res .= ">\n"; $group = ""; if (isset($_SESSION["domframework"]["form"][$this->formName]["values"])) { $values = $_SESSION["domframework"]["form"][$this->formName]["values"]; @@ -364,6 +367,7 @@ class Form } foreach ($this->fields as $field) { $field->formName = $this->formName; + $field->formClass = $this->formClass; if ( isset($field->group) && $field->group !== $group && $group !== "" || !isset($field->group) && $group !== "" diff --git a/src/Formfield.php b/src/Formfield.php index 4a478f9..6ded760 100644 --- a/src/Formfield.php +++ b/src/Formfield.php @@ -18,6 +18,10 @@ class Formfield * The form name */ public $formName; + /** + * The form class (horizontal / vertical) + */ + public $formClass; /** * The name of the field */ @@ -74,7 +78,7 @@ class Formfield /** * The statut of error of the field */ - public $error; + public $errors; /** * Number of rows */ @@ -1875,4 +1879,993 @@ class Formfield $res .= " \n"; // End form-group return $res; } + + ////////////////////// + //// BULMA1 //// + ////////////////////// + /** + * Return the checkbox defined + */ + private function fieldBulma1checkbox(): string + { + // No $this->multiple, $this->rows $this->cols $this->placeholder, + // $this->maxlength + if (! is_array($this->titles) || count($this->titles) === 0) { + $titles = [""]; + } else { + $titles = $this->titles; + } + $res = ""; + return $res; + } + + /** + * Return the hidden field defined + */ + private function fieldBulma1hidden(): string + { + $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 fieldBulma1password(): string + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") { + $res .= "
\n"; + $res .= " titlewidth > 0) { + $res .= " class='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 .= "
\n"; + $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='input"; + if (isset($this->errors)) { + $res .= " is-danger"; + } + $res .= "'"; + if (isset($this->hidden) && $this->hidden !== false) { + $res .= " style='display:none'"; + } + 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 .= "/>"; + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + return $res; + } + + /** + * Return the radio field defined + */ + private function fieldBulma1radio(): string + { + $res = ""; + // No $this->multiple, $this->rows $this->cols $this->placeholder + // $this->maxlength + $res .= "
\n"; + if ($this->label !== "") { + $res .= "
\n"; + $res .= " titlewidth > 0) { + $res .= " class='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 .= "
\n"; + $res .= "
\n"; + $res .= "
\n"; + foreach ($this->titles as $key => $val) { + $res .= " \n"; // End label radio + if (isset($this->errors) && $key === count($this->titles) - 1) { + $res .= "
"; + $res .= htmlspecialchars($this->errors[1]); + $res .= "
\n"; + } + } + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + return $res; + } + + /** + * Return the checkbox defined + */ + private function fieldBulma1select(): string + { + // No $this->placeholder $this->maxlength + $res = ""; + // $values->$this, $this->cols + $res .= "
\n"; + if ($this->label !== "") { + $res .= "
\n"; + $res .= " titlewidth > 0) { + $res .= " class='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 .= "
\n"; + $res .= "
\n"; + $res .= "
\n"; + $res .= "
\n"; + 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='"; + if (isset($this->errors)) { + $res .= "input is-danger"; + } + $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"; + $res .= "
\n"; // DIV SELECT + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + return $res; + $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 fieldBulma1submit(): string + { + $res = ""; + // No $this->label, $this->multiple, $this->error, $this->rows, + // $this->cols $this->placeholder $this->maxlength + $res .= "
\n"; + $res .= "
"; + // Leave empty for spacing + $res .= "
\n"; + $res .= "
\n"; + $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='button is-primary"; + if (isset($this->errors)) { + $res .= " is-danger"; + } + $res .= "'"; + 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 .= "/>"; + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + return $res; + } + + /** + * Return the textarea defined + */ + private function fieldBulma1textarea(): string + { + $res = ""; + // No $this->multiple, $this->titles + $res .= "
\n"; + if ($this->label !== "") { + $res .= "
\n"; + $res .= " titlewidth > 0) { + $res .= " class='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 .= "
\n"; + $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='textarea"; + if (isset($this->errors)) { + $res .= " is-danger"; + } + $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"; + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + return $res; + } + + /** + * Return the text defined + */ + private function fieldBulma1text(): string + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") { + $res .= "
\n"; + $res .= " titlewidth > 0) { + $res .= " class='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 .= "
\n"; + $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='input"; + if (isset($this->errors)) { + $res .= " is-danger"; + } + $res .= "'"; + if (isset($this->hidden) && $this->hidden !== false) { + $res .= " style='display:none'"; + } + 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 .= "/>"; + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + return $res; + } + + /** + * Return the file defined + */ + private function fieldBulma1file(): string + { + $res = ""; + // No $this->multiple, $this->titles, $this->rows, $this->cols + $res .= "
\n"; + if ($this->label !== "") { + $res .= "
\n"; + $res .= " titlewidth > 0) { + $res .= " class='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 .= "
\n"; + $res .= "
\n"; + $res .= "
\n"; + $res .= "
errors)) { + $res .= " is-danger"; + } + $res .= "'>\n"; + $res .= " \n"; + $res .= "
\n"; + $res .= "
\n"; // Control + 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 field + $res .= "
\n"; // End field-body + $res .= "
\n"; // End form-class + + // Manage the name of the file in the box + $res .= "\n"; + + return $res; + $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; + } }