From e48f541c6148497411c9791dbd18ccb1124384bb Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 23 Oct 2019 12:26:10 +0000 Subject: [PATCH] form : Add Bootstrap4 support (change formThemplate to select). Bootstrap 3 always the default one git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5614 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- form.php | 831 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 819 insertions(+), 12 deletions(-) diff --git a/form.php b/form.php index ba6d0b4..c55d1c8 100644 --- a/form.php +++ b/form.php @@ -57,6 +57,10 @@ class form */ private $loggingBasemsg = ""; + /** Form template (Bootstrap3 by default) + */ + private $formTemplate = "Bootstrap3"; + /** Create a form * @param string|null $formName The form name */ @@ -143,6 +147,18 @@ class form $this->loggingCallable = $loggingCallable; $this->loggingBasemsg = $loggingBasemsg; } + + /** Set the Form Templating to use. + * Can be : Bootstrap3 (and later Bootstrap4, Bulma) + */ + public function formTemplate ($formTemplate) + { + if (! in_array ($formTemplate, + array ("Bootstrap3", "Bootstrap4"))) + throw new \Exception ("Unknown formTemplate provided", 500); + $this->formTemplate = $formTemplate; + return $this; + } // }}} /** The private method to log if the $this->loggingCallable is defined @@ -352,6 +368,7 @@ class form } $field->titlewidth = $this->titlewidth; $field->fieldwidth = $this->fieldwidth; + $field->formTemplate = $this->formTemplate; $res .= $field->display (); } @@ -654,7 +671,7 @@ class formfield public function display () // {{{ { - $func = "field".$this->type; + $func = "field".$this->formTemplate.$this->type; return $this->$func (); } // }}} @@ -752,9 +769,13 @@ class formfield } // }}} + ////////////////////////// + //// BOOTSTRAP3 //// + ////////////////////////// + // {{{ /** Return the checkbox defined */ - public function fieldcheckbox () + private function fieldBootstrap3checkbox () // {{{ { // No $this->multiple, $this->rows $this->cols $this->placeholder, @@ -769,7 +790,7 @@ class formfield $res .= " \n"; } $res .= "
\n"; - if (count ($this->titles) === 0) + if (! is_array ($this->titles) || count ($this->titles) === 0) $this->titles = array (""); foreach ($this->titles as $key=>$val) { @@ -880,7 +901,7 @@ class formfield /** Return the hidden field defined */ - public function fieldhidden () + private function fieldBootstrap3hidden () // {{{ { $res = ""; @@ -902,7 +923,7 @@ class formfield /** Return the password field defined */ - public function fieldpassword () + private function fieldBootstrap3password () // {{{ { $res = ""; @@ -976,7 +997,7 @@ class formfield /** Return the radio field defined */ - public function fieldradio () + private function fieldBootstrap3radio () // {{{ { $res = ""; @@ -1060,7 +1081,7 @@ class formfield /** Return the checkbox defined */ - public function fieldselect () + private function fieldBootstrap3select () // {{{ { // No $this->placeholder $this->maxlength @@ -1186,7 +1207,7 @@ class formfield /** Return the submit defined */ - public function fieldsubmit () + private function fieldBootstrap3submit () // {{{ { $res = ""; @@ -1229,7 +1250,7 @@ class formfield /** Return the textarea defined */ - public function fieldtextarea () + private function fieldBootstrap3textarea () // {{{ { $res = ""; @@ -1306,7 +1327,7 @@ class formfield /** Return the text defined */ - public function fieldtext () + private function fieldBootstrap3text () // {{{ { $res = ""; @@ -1380,7 +1401,7 @@ class formfield /** Return the file defined */ - public function fieldfile () + private function fieldBootstrap3file () // {{{ { $res = ""; @@ -1464,4 +1485,790 @@ class formfield return $res; } // }}} + // }}} + + ////////////////////////// + //// BOOTSTRAP4 //// + ////////////////////////// + // {{{ + /** Return the checkbox defined + */ + private function fieldBootstrap4checkbox () + // {{{ + { + // No $this->multiple, $this->rows $this->cols $this->placeholder, + // $this->maxlength + $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 (is_array ($this->titles) && count ($this->titles) > 1) + $res .= "_0"; + $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_array ($this->titles) || count ($this->titles) === 0) + $this->titles = array (""); + 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'"; + $res .= " for='". $this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_0'"; + 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; + } + // }}} + + // }}} }