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