1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
<?php
$formulaire = new Formulaire();
$formulaire->set_class("add");
$champs = array(
array(
"name" => "titre",
"type" => "text",
"id" => "titre",
"value" => $p['titre'],
"maxlength" => 255,
"required" => true
),
array(
"name" => "text",
"type" => "textarea",
"id" => "text",
"value" => $p['text'],
"required" => true
),
array(
"name" => "__submit",
"type" => "submit",
"id" => "submit",
"value" => "Envoyer"
)
);
$formulaire->creer_formulaire($champs,"tpl"));
class Formulaire {
// Formulaire
private $formulaire = "";
private $action;
private $id_formulaire = "formulaire";
private $method = "post";
public $class_formulaire;
// Champs
private $nameInput;
private $typeInput;
private $valueDefaultInput;
private $optionsInput = array();
private $idInput;
private $requiredInput = false;
private $maxlenghtInput;
private $minlenghtInput;
// Divers
private $array_inputs_formulaire = array();
private $list_inputs_required = array();
/*
* Ecriture un champ selon tableau de paramètres
*/
private function creer_champ($arrayValues){
$return_input = "";
$this->nameInput = $arrayValues[name];
$this->typeInput = ( $arrayValues[type] == "" ) ? 'text' : $arrayValues[type];
$this->valueInput = $arrayValues[value];
$this->optionsInput = $arrayValues[options];
$this->idInput = ($arrayValues[id] == "") ? $this->nameInput : $arrayValues[id];
$this->maxlengthInput = $arrayValues[maxlength];
$this->minlengthInput = $arrayValues[minlenght];
if($this->typeInput != ""){
switch($this->typeInput){
// Champ text
case 'text' :
$return_input = '<input type="'.$this->typeInput.'" name="'.$this->nameInput.'" id="'.$this->idInput.'" value="'.$this->valueInput.'" maxlength="'.$this->maxlengthInput.'"/>' . chr(13);
break;
// Champ textarea
case 'textarea' :
$return_input = '<textarea name="'.$this->nameInput.'" id="'.$this->idInput.'">'.$this->valueInput.'</textarea>' . chr(13);
break;
// Champ hidden
case 'hidden' :
$return_input = '<input type="'.$this->typeInput.'" name="'.$this->nameInput.'" id="'.$this->idInput.'" value="'.$this->valueInput.'" />' . chr(13);
break;
// Champ select
case 'select' :
$return_input = '<select name="'.$this->nameInput.'" id="'.$this->idInput.'">' . chr(13);
foreach($this->optionsInput as $k=>$v){
$etatSelect = ($_POST[$this->nameInput] == $v) ? 'selected="selected"' : '';
$return_input .= '<option value="'.$v.'" '.$etatSelect.'>'.$k.'</option>' . chr(13);
}
$return_input .= '</select>' . chr(13);
break;
// Champ checkbox
case 'checkbox' :
if(count($this->optionsInput)>1){
$arrayValeursCheckbox = '[]';
}
foreach($this->optionsInput as $k=>$v){
$etatCheckbox = (is_array($_POST[$this->nameInput]) && in_array($v,$_POST[$this->nameInput])) ? 'checked="checked' : '';
$return_input .= $k .'<input type="'.$this->typeInput.'" name="'.$this->nameInput.$arrayValeursCheckbox.'" id="'.$this->idInput.'" value="'.$v.'" '.$etatCheckbox.'>' . chr(13);
}
break;
// Champ submit
case 'submit' :
$return_input = '<input type="'.$this->typeInput.'" name="'.$this->nameInput.'" id="'.$this->idInput.'" value="'.$this->valueInput.'" />' . chr(13);
break;
}
}else{
echo "Impossible de créer le champ : type incorrect";
}
return $return_input;
}
/*
* Ecriture du formulaire
* Traitement du tableau $this->array_inputs_formulaire pour afficher chaque champ du formulaire
*/
public function creer_formulaire($champs, $type_return_formulaire=null){
// Création du formulaire avec les balises <form> et </form>
$this->formulaire = '<form id="'.$this->id_formulaire.'" method="'.$this->method.'" action="'.$this->action.'" '.$this->class_formulaire.'>' . chr(13);
if(is_array($champs) && count($champs)>0){
// Création des champs
foreach($champs as $input){
// on ajoute au tableau des champs obligatoires les champs concernés
$this->set_liste_inputs_required($input[name],$input[required]);
$this->formulaire .= '<p>';
$this->formulaire .= $this->creer_champ($input);
$this->formulaire .= '</p>';
}
}
$this->formulaire .= '</form>' . chr(13);
// Type de retour du formulaire : vers un template (tpl) ou affichage avec un echo
if($type_return_formulaire == "tpl"){
return $this->formulaire;
}else{
echo $this->formulaire;
}
}
/*
* Traitement du formulaire
*/
public function traitement_formulaire(){
$checkForm = false;
if(count($this->list_inputs_required)==0){
$checkForm = true;
}
return $checkForm;
}
/*
* Afficher erreur du formulaire
*/
public function return_error_formulaire(){
if(count($this->get_liste_inputs_required())>0){
foreach($this->get_liste_inputs_required() as $inputError){
if($inputError == ""){
echo "Le champ " . $inputError . " n'est pas rempli correctement" . '<br/>';
}
}
}
}
/*
* Associer une class css au formulaire
*/
public function set_class($class){
if($class != ""){
$this->class_formulaire = ' class="'.$class.'" ';
}
}
/*
* Ajoute un champ obligatoire à au tableau list_inputs_required
*/
private function set_liste_inputs_required($name,$required){
if($required==1){
$this->list_inputs_required[] = $name;
}
}
/* Retourne la liste des champs obligatoires
*
*/
public function get_liste_inputs_required(){
return $this->list_inputs_required;
}
}
?> |
Partager