* @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 = [""];
} else {
$titles = $this->titles;
}
$res = "";
$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"; // 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"; // End form-group
return $res;
}
/**
* Return the text defined
*/
private function fieldBootstrap3text()
{
$res = "";
// No $this->multiple, $this->titles, $this->rows, $this->cols
$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"; // 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"; // End form-group
return $res;
}
/**
* Return the text defined
*/
private function fieldBootstrap4text()
{
$res = "";
// No $this->multiple, $this->titles, $this->rows, $this->cols
$res .= "