form : update presentation and add folding

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4976 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2019-02-06 12:43:14 +00:00
parent cefe122b54
commit a7923b731d

198
form.php
View File

@@ -1,55 +1,71 @@
<?php <?php
/** DomFramework /** DomFramework
@package domframework * @package domframework
@author Dominique Fournier <dominique@fournier38.fr> */ * @author Dominique Fournier <dominique@fournier38.fr>
*/
require_once ("domframework/csrf.php"); require_once ("domframework/csrf.php");
/** This class permit to create easily some forms to HTML (or text mode in /** This class permit to create easily some forms to HTML (or text mode in
* future). * future).
* Each field can be checked in AJAX or HTML. */ * Each field can be checked in AJAX or HTML.
*/
class form class form
{ {
/** All the fields */ /** All the fields
*/
private $fields = NULL; private $fields = NULL;
/** The name of the form */ /** The name of the form
*/
private $formName; private $formName;
/** Allow to debug the PHP */ /** Allow to debug the PHP
*/
public $debug=0; public $debug=0;
/** CSRF protection /** CSRF protection
* By default, the CSRF protection is active if a SESSION is active too. * By default, the CSRF protection is active if a SESSION is active too.
* It can be disabled if needed. An Exception is raised if the form is send * It can be disabled if needed. An Exception is raised if the form is send
* back without the token */ * back without the token
*/
public $csrf=TRUE; public $csrf=TRUE;
/** Name of the CSRF hidden field in HTML page */ /** Name of the CSRF hidden field in HTML page
*/
public $csrfField = "CSRF_TOKEN"; public $csrfField = "CSRF_TOKEN";
/** The CSRF token value */ /** The CSRF token value
*/
private $csrfToken = ""; private $csrfToken = "";
/** The method used to send the values */ /** The method used to send the values
*/
private $method = "post"; private $method = "post";
/** The Bootstrap width of the column of titles */ /** The Bootstrap width of the column of titles
*/
public $titlewidth = 2; public $titlewidth = 2;
/** The Bootstrap width of the column of fields */ /** The Bootstrap width of the column of fields
*/
public $fieldwidth = 10; public $fieldwidth = 10;
/** Define a class for form object */ /** Define a class for form object
*/
public $formClass = "form-horizontal"; public $formClass = "form-horizontal";
/** The logging callable method */ /** The logging callable method
*/
private $loggingCallable = null; private $loggingCallable = null;
/** The logging basemsg */ /** The logging basemsg
*/
private $loggingBasemsg = ""; private $loggingBasemsg = "";
/** Create a form /** Create a form
* @param string|null $formName The form name * @param string|null $formName The form name
*/ */
public function __construct ($formName = "form") public function __construct ($formName = "form")
// {{{
{ {
$this->formName = $formName; $this->formName = $formName;
} }
// }}}
// The setters of the properties // The setters of the properties
// {{{ // {{{
@@ -125,6 +141,7 @@ class form
* @param string $msg The message to store * @param string $msg The message to store
*/ */
private function loggingCallable ($prio, $msg) private function loggingCallable ($prio, $msg)
// {{{
{ {
if (! is_callable ($this->loggingCallable)) if (! is_callable ($this->loggingCallable))
return; return;
@@ -133,6 +150,7 @@ class form
$base = $this->loggingBasemsg. " "; $base = $this->loggingBasemsg. " ";
call_user_func ($this->loggingCallable, $prio, $base.$msg); call_user_func ($this->loggingCallable, $prio, $base.$msg);
} }
// }}}
/** Save the array of fields into the structure. /** Save the array of fields into the structure.
* Available : * Available :
@@ -163,23 +181,29 @@ class form
* @param array $fields The fields to be displayed * @param array $fields The fields to be displayed
*/ */
public function fields ($fields) public function fields ($fields)
// {{{
{ {
$this->fields = $fields; $this->fields = $fields;
} }
// }}}
/** Add a field to the form. For the details of a field, see the description /** Add a field to the form. For the details of a field, see the description
* in fields method * in fields method
* @param object $field The field to add * @param object $field The field to add
*/ */
public function addfield ($field) public function addfield ($field)
// {{{
{ {
$this->fields[] = $field; $this->fields[] = $field;
} }
// }}}
/** Return the values provided by the user. Test the CSRF before continue /** Return the values provided by the user. Test the CSRF before continue
* NEVER read the values from $_POST in your codes or CSRF will not be * NEVER read the values from $_POST in your codes or CSRF will not be
* checked */ * checked
*/
public function values () public function values ()
// {{{
{ {
$values = array (); $values = array ();
if ($this->method === "post") if ($this->method === "post")
@@ -235,6 +259,7 @@ class form
return $values; return $values;
} }
// }}}
/** Return the fields in HTML code. If $values is provided, use it in place /** Return the fields in HTML code. If $values is provided, use it in place
* of default values. In case of select boxes, $values are the selected * of default values. In case of select boxes, $values are the selected
@@ -244,9 +269,11 @@ class form
* GET) * GET)
* @param array|null $values The default values of the fields * @param array|null $values The default values of the fields
* @param array|null $errors The fields to put in error with the associated * @param array|null $errors The fields to put in error with the associated
* message */ * message
*/
public function printHTML ($method = 'post', $values = NULL, public function printHTML ($method = 'post', $values = NULL,
$errors = array()) $errors = array())
// {{{
{ {
if (count ($this->fields) === 0) if (count ($this->fields) === 0)
{ {
@@ -367,23 +394,30 @@ class form
$res .= "</form>\n"; $res .= "</form>\n";
return $res; return $res;
} }
// }}}
/** Check the token from the user /** Check the token from the user
* @param string $tokenFromUser The value form the user's token */ * @param string $tokenFromUser The value form the user's token
*/
public function checkToken ($tokenFromUser) public function checkToken ($tokenFromUser)
// {{{
{ {
$csrf = new csrf (); $csrf = new csrf ();
$csrf->field = $this->csrfField; $csrf->field = $this->csrfField;
$csrf->checkToken ($tokenFromUser); $csrf->checkToken ($tokenFromUser);
} }
// }}}
/** Return the token generated in form */ /** Return the token generated in form
*/
public function getToken () public function getToken ()
// {{{
{ {
if ($this->csrfToken === "") if ($this->csrfToken === "")
$this->createToken (); $this->createToken ();
return $this->csrfToken; return $this->csrfToken;
} }
// }}}
/** Check if the parameters are correct with the defined fields /** Check if the parameters are correct with the defined fields
* Need the session ! * Need the session !
@@ -393,6 +427,7 @@ class form
* @return array containing the errors * @return array containing the errors
*/ */
public function verify ($values, $fields=array ()) public function verify ($values, $fields=array ())
// {{{
{ {
if (count ($fields) === 0) if (count ($fields) === 0)
{ {
@@ -410,6 +445,7 @@ class form
} }
return $errors; return $errors;
} }
// }}}
/** If there is at least one error reported in $errors, save the old values /** If there is at least one error reported in $errors, save the old values
* and the errors in the session, and redirect to the provided url. * and the errors in the session, and redirect to the provided url.
@@ -431,6 +467,7 @@ class form
$route->redirect ("/admin/space/"); $route->redirect ("/admin/space/");
*/ */
public function redirectIfError ($values, $errors, $route, $url = "") public function redirectIfError ($values, $errors, $route, $url = "")
// {{{
{ {
$this->saveValuesErrors ($values, $errors); $this->saveValuesErrors ($values, $errors);
if ($url === "") if ($url === "")
@@ -438,6 +475,7 @@ class form
if (count ($errors)) $route->redirect ($url); if (count ($errors)) $route->redirect ($url);
$this->saveValuesErrorsReset (); $this->saveValuesErrorsReset ();
} }
// }}}
/** Save the values and errors to be displayed in the next page if the session /** Save the values and errors to be displayed in the next page if the session
* is available * is available
@@ -446,6 +484,7 @@ class form
* @param array|null $errors The errors detected by a verify * @param array|null $errors The errors detected by a verify
*/ */
public function saveValuesErrors ($values, $errors=array ()) public function saveValuesErrors ($values, $errors=array ())
// {{{
{ {
if (isset ($_SESSION)) if (isset ($_SESSION))
{ {
@@ -453,15 +492,18 @@ class form
$_SESSION["domframework"]["form"][$this->formName]["errors"] = $errors; $_SESSION["domframework"]["form"][$this->formName]["errors"] = $errors;
} }
} }
// }}}
/** Reset the saved values to provide a clean form next page /** Reset the saved values to provide a clean form next page
* Need the session to work * Need the session to work
*/ */
public function saveValuesErrorsReset () public function saveValuesErrorsReset ()
// {{{
{ {
unset ($_SESSION["domframework"]["form"][$this->formName]["values"]); unset ($_SESSION["domframework"]["form"][$this->formName]["values"]);
unset ($_SESSION["domframework"]["form"][$this->formName]["errors"]); unset ($_SESSION["domframework"]["form"][$this->formName]["errors"]);
} }
// }}}
/** Get the stored values if there is one. If there is no stored values, /** Get the stored values if there is one. If there is no stored values,
* return the values provided as parameter * return the values provided as parameter
@@ -469,6 +511,7 @@ class form
* @return array The values to use * @return array The values to use
*/ */
public function getOldValues ($values) public function getOldValues ($values)
// {{{
{ {
if (isset ($_SESSION["domframework"]["form"][$this->formName]["values"])) if (isset ($_SESSION["domframework"]["form"][$this->formName]["values"]))
{ {
@@ -477,6 +520,7 @@ class form
} }
return $values; return $values;
} }
// }}}
/** Get the stored errors if there is one. If there is no sorted errors, /** Get the stored errors if there is one. If there is no sorted errors,
* return the errors provided as parameter * return the errors provided as parameter
@@ -484,6 +528,7 @@ class form
* @return array The errors to use * @return array The errors to use
*/ */
public function getOldErrors ($errors) public function getOldErrors ($errors)
// {{{
{ {
if (isset ($_SESSION["domframework"]["form"][$this->formName]["errors"])) if (isset ($_SESSION["domframework"]["form"][$this->formName]["errors"]))
{ {
@@ -492,6 +537,7 @@ class form
} }
return $errors; return $errors;
} }
// }}}
/** Convert Date received in one format to another. /** Convert Date received in one format to another.
* If the provided string is not corresponding to the format, don't change * If the provided string is not corresponding to the format, don't change
@@ -503,6 +549,7 @@ class form
* @return string * @return string
*/ */
public function convertDate ($inputDate, $inputFormat, $outputFormat) public function convertDate ($inputDate, $inputFormat, $outputFormat)
// {{{
{ {
$date = DateTime::CreateFromFormat ($inputFormat, $inputDate); $date = DateTime::CreateFromFormat ($inputFormat, $inputDate);
if ($date === false) if ($date === false)
@@ -512,64 +559,90 @@ class form
return $inputDate; return $inputDate;
return $date->format ($outputFormat); return $date->format ($outputFormat);
} }
// }}}
} }
/** The definition of a formfield */ /** The definition of a formfield
*/
class formfield class formfield
{ {
/** The form name */ /** The form name
*/
public $formName; public $formName;
/** The name of the field */ /** The name of the field
*/
public $name; public $name;
/** The label of the field */ /** The label of the field
*/
public $label; public $label;
/** The titles of the field */ /** The titles of the field
*/
public $titles; public $titles;
/** The defaults values of the field */ /** The defaults values of the field
*/
public $defaults; public $defaults;
/** The type of the field (text, password, checkbox, select)*/ /** The type of the field (text, password, checkbox, select)
*/
public $type="text"; public $type="text";
/** The state of the field : hidden or show */ /** The state of the field : hidden or show
*/
public $hidden = false; public $hidden = false;
/** Allow a help message to be displayed below the field. In case of error, /** Allow a help message to be displayed below the field. In case of error,
* it is overrided by the error message */ * it is overrided by the error message
*/
public $help; public $help;
/** Display the placeholder if needed */ /** Display the placeholder if needed
*/
public $placeholder = false; public $placeholder = false;
/** The multiplicity of selection of the field (available in select only)*/ /** The multiplicity of selection of the field (available in select only)
*/
public $multiple; public $multiple;
/** The name of group for the fields */ /** The name of group for the fields
*/
public $group; public $group;
/** The read-only feature of the field */ /** The read-only feature of the field
*/
public $readonly; public $readonly;
/** The field is mandatory */ /** The field is mandatory
*/
public $mandatory; public $mandatory;
/** The statut of error of the field */ /** The statut of error of the field
*/
public $error; public $error;
/** Number of rows */ /** Number of rows
*/
public $rows; public $rows;
/** Number of columns */ /** Number of columns
*/
public $cols; public $cols;
/** The Bootstrap width of the column of titles */ /** The Bootstrap width of the column of titles
*/
public $titlewidth = 2; public $titlewidth = 2;
/** The Bootstrap width of the column of fields */ /** The Bootstrap width of the column of fields
*/
public $fieldwidth = 10; public $fieldwidth = 10;
/** When adding a field, the name and the label are the minimum mandatory /** When adding a field, the name and the label are the minimum mandatory
* @param string $name Name of the field * @param string $name Name of the field
* @param string|null $label Label of the field */ * @param string|null $label Label of the field
*/
public function __construct ($name, $label = "") public function __construct ($name, $label = "")
// {{{
{ {
$this->name = $name; $this->name = $name;
$this->label = $label; $this->label = $label;
} }
// }}}
/** Display really the form */ /** Display really the form
*/
public function display () public function display ()
// {{{
{ {
$func = "field".$this->type; $func = "field".$this->type;
return $this->$func (); return $this->$func ();
} }
// }}}
// Setters for all the properties of the class // Setters for all the properties of the class
// {{{ // {{{
@@ -664,8 +737,10 @@ class formfield
} }
// }}} // }}}
/** Return the checkbox defined */ /** Return the checkbox defined
*/
public function fieldcheckbox () public function fieldcheckbox ()
// {{{
{ {
// No $this->multiple, $this->rows $this->cols $this->placeholder, // No $this->multiple, $this->rows $this->cols $this->placeholder,
// $this->maxlength // $this->maxlength
@@ -786,9 +861,12 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
/** Return the hidden field defined */ /** Return the hidden field defined
*/
public function fieldhidden () public function fieldhidden ()
// {{{
{ {
$res = ""; $res = "";
// No $this->label, $this->multiple, $this->readonly, $this->hidden, // No $this->label, $this->multiple, $this->readonly, $this->hidden,
@@ -805,9 +883,12 @@ class formfield
$res .= "/>\n"; $res .= "/>\n";
return $res; return $res;
} }
// }}}
/** Return the password field defined */ /** Return the password field defined
*/
public function fieldpassword () public function fieldpassword ()
// {{{
{ {
$res = ""; $res = "";
// No $this->multiple, $this->rows $this->cols // No $this->multiple, $this->rows $this->cols
@@ -876,9 +957,12 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
/** Return the radio field defined */ /** Return the radio field defined
*/
public function fieldradio () public function fieldradio ()
// {{{
{ {
$res = ""; $res = "";
// No $this->multiple, $this->rows $this->cols $this->placeholder // No $this->multiple, $this->rows $this->cols $this->placeholder
@@ -957,9 +1041,12 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
/** Return the checkbox defined */ /** Return the checkbox defined
*/
public function fieldselect () public function fieldselect ()
// {{{
{ {
// No $this->placeholder $this->maxlength // No $this->placeholder $this->maxlength
$res = ""; $res = "";
@@ -1083,9 +1170,12 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
/** Return the submit defined */ /** Return the submit defined
*/
public function fieldsubmit () public function fieldsubmit ()
// {{{
{ {
$res = ""; $res = "";
// No $this->label, $this->multiple, $this->error, $this->rows, // No $this->label, $this->multiple, $this->error, $this->rows,
@@ -1123,9 +1213,12 @@ class formfield
$res .= " </div>\n"; $res .= " </div>\n";
return $res; return $res;
} }
// }}}
/** Return the textarea defined */ /** Return the textarea defined
*/
public function fieldtextarea () public function fieldtextarea ()
// {{{
{ {
$res = ""; $res = "";
// No $this->multiple, $this->titles // No $this->multiple, $this->titles
@@ -1197,9 +1290,12 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
/** Return the text defined */ /** Return the text defined
*/
public function fieldtext () public function fieldtext ()
// {{{
{ {
$res = ""; $res = "";
// No $this->multiple, $this->titles, $this->rows, $this->cols // No $this->multiple, $this->titles, $this->rows, $this->cols
@@ -1268,9 +1364,12 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
/** Return the file defined */ /** Return the file defined
*/
public function fieldfile () public function fieldfile ()
// {{{
{ {
$res = ""; $res = "";
// No $this->multiple, $this->titles, $this->rows, $this->cols // No $this->multiple, $this->titles, $this->rows, $this->cols
@@ -1351,4 +1450,5 @@ class formfield
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
} }
// }}}
} }