izForm
[ class tree: izForm ] [ index: izForm ] [ all elements ]

Source for file izform.class.php

Documentation is available at izform.class.php

  1. <?php
  2.  
  3.  
  4. /**
  5. * Classe de génération et de contrôle de formulaires
  6. *
  7. * @package izForm
  8. * @author Loic Mathaud <loic@mathaud.net>
  9. *
  10. */
  11. # ******* BEGIN LICENSE BLOCK *******
  12. #
  13. # Copyright (c) 2003, Loic Mathaud
  14. # All rights reserved.
  15. #
  16. # Redistribution and use in source and binary forms, with or without
  17. # modification, are permitted provided that the following conditions are met:
  18. #
  19. # * Redistributions of source code must retain the above copyright notice,
  20. # this list of conditions and the following disclaimer.
  21. # * Redistributions in binary form must reproduce the above copyright notice,
  22. # this list of conditions and the following disclaimer in the documentation
  23. # and/or other materials provided with the distribution.
  24. # * Neither the name of izForm nor the names of its contributors
  25. # may be used to endorse or promote products derived from this software
  26. # without specific prior written permission.
  27. #
  28. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  30. # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  31. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  32. # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  33. # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  38. # THE POSSIBILITY OF SUCH DAMAGE
  39. #
  40. # ******* END LICENSE BLOCK *******
  41.  
  42.  
  43.  
  44. /**
  45. * définie la balise par défaut qui encadre les éléments de formulaire
  46. */
  47. define( 'IZF_DEFAULT_WRAPPER', 'P' );
  48.  
  49.  
  50.  
  51. /**
  52. * classe IzForm
  53. *
  54. * @package izForm
  55. */
  56. class IzForm {
  57. /**
  58. * Chaîne de caractère contenant la sortie html du formulaire
  59. *
  60. * @var string
  61. * @access private
  62. */
  63. var $elementsHtml;
  64. /**
  65. * Tableau contenant les éléments du formulaire
  66. *
  67. * @var array
  68. * @access private
  69. */
  70. var $elements = array();
  71. /**
  72. * Tableau contenant les éléments à charger dans une liste déroulante
  73. *
  74. * @var array
  75. * @access private
  76. */
  77. var $selectElements = array();
  78. /**
  79. * Tableau de 2 valeurs contenant les indices qui permettront de positionner un élément fieldset
  80. *
  81. * @var array
  82. * @access private
  83. */
  84. var $fieldSetElements = array();
  85. /**
  86. * Tableau de 2 valeurs contenant les indices qui permettront de positionner un élément optgroup dans une liste déroulante
  87. *
  88. * @var array
  89. * @access private
  90. */
  91. var $optGroupElements = array();
  92. /**
  93. * Tableau qui est la copie du tableau superglobal correspond à la méthode employée pour soumettre le formulaire
  94. *
  95. * @var array
  96. * @access public
  97. */
  98. var $submittedData = array();
  99. /**
  100. * Tableau des valeurs à contrôler une fois le formulaire soumis
  101. *
  102. * @var array
  103. * @access private
  104. */
  105. var $verifData = array();
  106. /**
  107. * Défini la manière dont est affiché le label
  108. *
  109. * Par défaut, vaut 'wrapp' : le label englobe l'élément de formulaire
  110. * Autre valeur possible : 'nowrapp' : le label est refermé avant l'élément de formulaire
  111. *
  112. * @var string
  113. */
  114. var $labelMode = 'wrapp';
  115. /**
  116. * Constructeur de la classe
  117. *
  118. * Défini le script cible du formulaire, la méthode par laquelle il est soumis
  119. * et quel est le type d'encryption de celui-ci
  120. * Le constructeur enregistre la balise d'ouverture du formulaire,
  121. * copie dans la variable de classe {@link $submittedData} le bon tableau superglobal en fonction
  122. * de la méthode employée pour envoyer le formulaire
  123. *
  124. * @param string $method methode par laquelle est soumis le formulaire. Peut prendre les valeurs 'post' ou 'get' ('post' par defaut)
  125. * @param string $action script cible du formulaire. Si $action n'est pas spécifiée elle prend pour valeur le script même qui contient le formulaire
  126. * @param string $id identifiant du formulaire. Prend 'form1' par défaut
  127. * @param int $enctype type d'encryption du formulaire (0: multipart/form-data, 1: application/x-www-form-urlencoded)
  128. * @access public
  129. */
  130. function IzForm( $method = 'post', $action = '', $id = 'form1', $enctype = 1 ) {
  131. $this->method = strtoupper( $method );
  132. if ( $this->method == 'POST' ) {
  133. $this->submittedData = $_POST;
  134. } elseif ( $this->method == 'GET' ) {
  135. $this->submittedData == $_GET;
  136. }
  137. switch ($enctype) {
  138. case 0:
  139. $enctype = "multipart/form-data";
  140. break;
  141. case 1:
  142. $enctype = "application/x-www-form-urlencoded";
  143. break;
  144. }
  145. $attributes['action'] = ( $action == '' ) ? $_SERVER['PHP_SELF'] : $action;
  146. $attributes['id'] = $id;
  147. $attributes['method'] = strtolower( $method );
  148. $attributes['enctype'] = $enctype;
  149. $res = array( 'type' => 'open_form', 'attributes' => $attributes );
  150. $this->setElement($res);
  151. }
  152. /**
  153. * Ajoute un element de type input type="text" au formulaire
  154. *
  155. * @param string $name nom du champs texte
  156. * @param string $label label du champs texte: texte placé devant le champ pour l'identifier
  157. * @param string $value valeur du champs texte
  158. * @param int $size taille du champs texte
  159. * @param int $maxlength taille maximale du champs texte
  160. * @param string $id identifiant du champs texte. Si n'est pas affecté, prend la valeur de $name
  161. * @param string $before élement ouvrant encadrant le champs texte. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  162. * @param string $after élement fermant encadrant le champs texte. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  163. * @param string $lbl_attributes attributs pouvant être ajoutés au label du champs texte pour les css (class, id, style inline) ou du javascript
  164. * @param string $elem_attributes attributs pouvant être ajoutés au champs texte pour les css (class, id, style inline) ou du javascript ou autre
  165. * @access public
  166. */
  167. function addText( $name, $label = '', $value = '' , $size = '', $maxlength = '', $id = '', $before = '', $after = '', $elem_attributes = '', $lbl_attributes = '' ) {
  168. $attributes['name'] = $name;
  169. $attributes['label'] = $label;
  170. $attributes['value'] = $value;
  171. $attributes['size'] = $size;
  172. $attributes['maxlength'] = $maxlength;
  173. $attributes['id'] = ( empty($id) ? $name : $id );
  174. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  175. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  176. $attributes['before'] = $before;
  177. $attributes['after'] = $after;
  178. $attributes['lbl_attributes'] = '';
  179. $attributes['elem_attributes'] = '';
  180. if ( !empty($lbl_attributes) ) {
  181. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  182. }
  183. if ( !empty($elem_attributes) ) {
  184. $attributes['elem_attributes'] = ' '. $elem_attributes;
  185. }
  186. $res = array( 'type' => 'text', 'attributes' => $attributes);
  187. $wrappOpenAttributes['description'] = 'open';
  188. $wrappCloseAttributes['description'] = 'close';
  189. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  190. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  191. $this->setElement($wrappOpen);
  192. $this->setElement($res);
  193. $this->setElement($wrappClose);
  194. }
  195. /**
  196. * Ajoute un element de type input type="password" au formulaire
  197. *
  198. * @param string $name nom du champs password
  199. * @param string $label label du champs password: texte place devant le champ pour l'identifier
  200. * @param string $value valeur du champs password
  201. * @param int $size taille du champs password
  202. * @param int $maxlength taille maximale du champs password
  203. * @param string $id identifiant du champs password. Si n'est pas affecté, prend la valeur de $name
  204. * @param string $before élement ouvrant encadrant le champs password. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  205. * @param string $after élement fermant encadrant le champs password. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  206. * @param string $lbl_attributes attributs pouvant être ajoutés au label du champs password pour les css (class, id, style inline) ou du javascript
  207. * @param string $elem_attributes attributs pouvant être ajoutés au champs password pour les css (class, id, style inline) ou du javascript ou autre
  208. * @access public
  209. */
  210. function addPassword( $name, $label = '', $value = '', $size = '', $maxlength = '', $id = '', $before = '', $after = '', $elem_attributes = '', $lbl_attributes = '' ) {
  211. $attributes['name'] = $name;
  212. $attributes['label'] = $label;
  213. $attributes['value'] = $value;
  214. $attributes['size'] = $size;
  215. $attributes['maxlength'] = $maxlength;
  216. $attributes['id'] = ( empty($id) ? $name : $id );
  217. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  218. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  219. $attributes['before'] = $before;
  220. $attributes['after'] = $after;
  221. $attributes['lbl_attributes'] = '';
  222. $attributes['elem_attributes'] = '';
  223. if ( !empty($lbl_attributes) ) {
  224. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  225. }
  226. if ( !empty($elem_attributes) ) {
  227. $attributes['elem_attributes'] = ' '. $elem_attributes;
  228. }
  229. $res = array( 'type' => 'password', 'attributes' => $attributes);
  230. $wrappOpenAttributes['description'] = 'open';
  231. $wrappCloseAttributes['description'] = 'close';
  232. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  233. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  234. $this->setElement($wrappOpen);
  235. $this->setElement($res);
  236. $this->setElement($wrappClose);
  237. }
  238. /**
  239. * Ajoute un element de type input type="checkbox" au formulaire
  240. *
  241. * @param string $name nom de l'élément checkbox
  242. * @param string $label label de l'élément checkbox : texte place après l'élement pour l'identifier
  243. * @param string $value valeur de l'élément checkbox
  244. * @param boolen $check valeur par défaut de l'état de la case à cocher (0 = non cochée, 1 = cochée)
  245. * @param string $id identifiant de l'élément checkbox. Si n'est pas affecté, prend la valeur de $name
  246. * @param string $before élement ouvrant encadrant l'élément checkbox. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  247. * @param string $after élement fermant encadrant l'élément checkbox. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  248. * @param string $lbl_attributes attributs pouvant être ajoutés au label de l'élément checkbox pour les css (class, id, style inline) ou du javascript
  249. * @param string $elem_attributes attributs pouvant être ajoutés à l'élément checkbox pour les css (class, id, style inline) ou du javascript ou autre
  250. * @access public
  251. */
  252. function addCheckBox( $name, $label, $value, $check = 0, $id = '', $before = '', $after = '', $elem_attributes = '', $lbl_attributes = '' ) {
  253. $attributes['name'] = $name;
  254. $attributes['label'] = $label;
  255. $attributes['value'] = $value;
  256. $attributes['check'] = $check;
  257. $attributes['id'] = ( empty($id) ? $name . '-' . $value : $id );
  258. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  259. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  260. $attributes['before'] = $before;
  261. $attributes['after'] = $after;
  262. $attributes['lbl_attributes'] = '';
  263. $attributes['elem_attributes'] = '';
  264. if ( !empty($lbl_attributes) ) {
  265. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  266. }
  267. if ( !empty($elem_attributes) ) {
  268. $attributes['elem_attributes'] = ' '. $elem_attributes;
  269. }
  270. $res = array( 'type' => 'checkbox', 'attributes' => $attributes);
  271. $wrappOpenAttributes['description'] = 'open';
  272. $wrappCloseAttributes['description'] = 'close';
  273. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  274. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  275. $this->setElement($wrappOpen);
  276. $this->setElement($res);
  277. $this->setElement($wrappClose);
  278. }
  279. /**
  280. * Ajoute un element de type input type="radio" au formulaire
  281. *
  282. * @param string $name nom de l'élément radio
  283. * @param string $label label de l'élément radio : texte place après l'élement pour l'identifier
  284. * @param string $value valeur de l'élément radio
  285. * @param boolen $check valeur par défaut de l'état du bouton radio (0 = non coché, 1 = coché)
  286. * @param string $id identifiant de l'élément radio. Si n'est pas affecté, prend la valeur de $name
  287. * @param string $before élement ouvrant encadrant l'élément radio. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  288. * @param string $after élement fermant encadrant l'élément radio. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  289. * @param string $lbl_attributes attributs pouvant être ajoutés au label de l'élément radio pour les css (class, id, style inline) ou du javascript
  290. * @param string $elem_attributes attributs pouvant être ajoutés à l'élément radio pour les css (class, id, style inline) ou du javascript ou autre
  291. * @access public
  292. */
  293. function addRadio( $name, $label, $value, $check = 0, $id = '', $before = '', $after = '', $elem_attributes = '', $lbl_attributes = '' ) {
  294. $attributes['name'] = $name;
  295. $attributes['label'] = $label;
  296. $attributes['value'] = $value;
  297. $attributes['check'] = $check;
  298. $attributes['id'] = ( empty($id) ? $name . '-' . $value : $id );
  299. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  300. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  301. $attributes['before'] = $before;
  302. $attributes['after'] = $after;
  303. $attributes['lbl_attributes'] = '';
  304. $attributes['elem_attributes'] = '';
  305. if ( !empty($lbl_attributes) ) {
  306. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  307. }
  308. if ( !empty($elem_attributes) ) {
  309. $attributes['elem_attributes'] = ' '. $elem_attributes;
  310. }
  311. $res = array( 'type' => 'radio', 'attributes' => $attributes);
  312. $wrappOpenAttributes['description'] = 'open';
  313. $wrappCloseAttributes['description'] = 'close';
  314. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  315. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  316. $this->setElement($wrappOpen);
  317. $this->setElement($res);
  318. $this->setElement($wrappClose);
  319. }
  320. /**
  321. * Ajoute un element de type input type="submit" au formulaire
  322. *
  323. * @param string $value valeur du bouton submit (texte affiché sur le bouton)
  324. * @param string $name nom du bouton submit
  325. * @param string $label label du bouton submit : texte place devant le bouton pour l'identifier
  326. * @param string $id identifiant du bouton submit. Si n'est pas affecté, prend la valeur de $name
  327. * @param string $before élement ouvrant encadrant le bouton submit. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  328. * @param string $after élement fermant encadrant le bouton submit. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  329. * @param string $lbl_attributes attributs pouvant être ajoutés au label du bouton submit pour les css (class, id, style inline) ou du javascript
  330. * @param string $elem_attributes attributs pouvant être ajoutés au bouton submit pour les css (class, id, style inline) ou du javascript ou autre
  331. * @access public
  332. */
  333. function addSubmit( $value, $name = '', $label = '', $id = '', $before = '', $after = '', $lbl_attributes = '', $elem_attributes = '' ) {
  334. $attributes['value'] = $value;
  335. $attributes['name'] = $name;
  336. $attributes['label'] = $label;
  337. $attributes['id'] = ( empty($id) ? $name : $id );
  338. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  339. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  340. $attributes['before'] = $before;
  341. $attributes['after'] = $after;
  342. $attributes['lbl_attributes'] = '';
  343. $attributes['elem_attributes'] = '';
  344. if ( !empty($lbl_attributes) ) {
  345. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  346. }
  347. if ( !empty($elem_attributes) ) {
  348. $attributes['elem_attributes'] = ' '. $elem_attributes;
  349. }
  350. $res = array( 'type' => 'submit', 'attributes' => $attributes);
  351. $wrappOpenAttributes['description'] = 'open';
  352. $wrappCloseAttributes['description'] = 'close';
  353. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  354. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  355. $this->setElement($wrappOpen);
  356. $this->setElement($res);
  357. $this->setElement($wrappClose);
  358. }
  359. /**
  360. * Ajoute un element de type input type="reset" au formulaire
  361. *
  362. * @param string $value valeur du bouton reset (texte affiché sur le bouton)
  363. * @param string $name nom du bouton reset
  364. * @param string $label label du bouton reset : texte place devant le bouton pour l'identifier
  365. * @param string $id identifiant du bouton reset. Si n'est pas affecté, prend la valeur de $name
  366. * @param string $before élement ouvrant encadrant le bouton reset. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  367. * @param string $after élement fermant encadrant le bouton reset. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  368. * @param string $lbl_attributes attributs pouvant être ajoutés au label du bouton reset pour les css (class, id, style inline) ou du javascript
  369. * @param string $elem_attributes attributs pouvant être ajoutés au bouton reset pour les css (class, id, style inline) ou du javascript ou autre
  370. * @access public
  371. */
  372. function addReset( $value, $name = '', $label = '', $id = '', $before = '', $after = '', $lbl_attributes = '', $elem_attributes = '' ) {
  373. $attributes['value'] = $value;
  374. $attributes['name'] = $name;
  375. $attributes['label'] = $label;
  376. $attributes['id'] = ( empty($id) ? $name : $id );
  377. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  378. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  379. $attributes['before'] = $before;
  380. $attributes['after'] = $after;
  381. $attributes['lbl_attributes'] = '';
  382. $attributes['elem_attributes'] = '';
  383. if ( !empty($lbl_attributes) ) {
  384. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  385. }
  386. if ( !empty($elem_attributes) ) {
  387. $attributes['elem_attributes'] = ' '. $elem_attributes;
  388. }
  389. $res = array( 'type' => 'reset', 'attributes' => $attributes);
  390. $wrappOpenAttributes['description'] = 'open';
  391. $wrappCloseAttributes['description'] = 'close';
  392. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  393. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  394. $this->setElement($wrappOpen);
  395. $this->setElement($res);
  396. $this->setElement($wrappClose);
  397. }
  398. /**
  399. * Ajoute un element de type input type="file" au formulaire
  400. *
  401. * @param string $name nom du bouton parcourir pour joindre un fichier
  402. * @param string $label label du bouton parcourir : texte place devant le bouton pour l'identifier
  403. * @param string $id identifiant du bouton parcourir. Si n'est pas affecté, prend la valeur de $name
  404. * @param string $before élement ouvrant encadrant le bouton parcourir. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  405. * @param string $after élement fermant encadrant le bouton parcourir. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  406. * @param string $lbl_attributes attributs pouvant être ajoutés au label du bouton parcourir pour les css (class, id, style inline) ou du javascript
  407. * @param string $elem_attributes attributs pouvant être ajoutés au bouton parcourir pour les css (class, id, style inline) ou du javascript ou autre
  408. * @access public
  409. */
  410. function addFile( $name , $label = '', $id = '', $before = '', $after = '', $lbl_attributes = '', $elem_attributes = '' ) {
  411. $attributes['name'] = $name;
  412. $attributes['label'] = $label;
  413. $attributes['id'] = ( empty($id) ? $name : $id );
  414. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  415. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  416. $attributes['before'] = $before;
  417. $attributes['after'] = $after;
  418. $attributes['lbl_attributes'] = '';
  419. $attributes['elem_attributes'] = '';
  420. if ( !empty($lbl_attributes) ) {
  421. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  422. }
  423. if ( !empty($elem_attributes) ) {
  424. $attributes['elem_attributes'] = ' '. $elem_attributes;
  425. }
  426. $res = array( 'type' => 'file', 'attributes' => $attributes);
  427. $wrappOpenAttributes['description'] = 'open';
  428. $wrappCloseAttributes['description'] = 'close';
  429. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  430. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  431. $this->setElement($wrappOpen);
  432. $this->setElement($res);
  433. $this->setElement($wrappClose);
  434. }
  435. /**
  436. * Ajoute un element de type input type="hidden" au formulaire
  437. *
  438. * @param string $name nom du champ caché
  439. * @param string $value valeur du champ caché
  440. * @access public
  441. */
  442. function addHidden( $name, $value) {
  443. $attributes['name'] = $name;
  444. $attributes['value'] = $value;
  445. $res = array( 'type' => 'hidden', 'attributes' => $attributes);
  446. $this->setElement($res);
  447. }
  448. /**
  449. * Ajoute un element de type textarea au formulaire
  450. *
  451. * @param string $name nom de la zone de texte
  452. * @param int $rows nombre de lignes de la zone de texte
  453. * @param int $cols nombre de colonnes de la zone de texte
  454. * @param string $label label de la zone de texte : texte place devant cette zone pour l'identifier
  455. * @param string $value valeur de la zone de texte
  456. * @param string $id identifiant unique de la zone de texte. Si n'est pas affecté, prend la valeur de $name
  457. * @param string $before élement ouvrant encadrant la zone de texte. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  458. * @param string $after élement fermant encadrant la zone de texte. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  459. * @param string $lbl_attributes attributs pouvant être ajoutés au label de la zone de texte pour les css (class, id, style inline) ou du javascript
  460. * @param string $elem_attributes attributs pouvant être ajoutés à la zone de texte pour les css (class, id, style inline) ou du javascript ou autre
  461. * @access public
  462. */
  463. function addTextArea( $name, $rows, $cols, $label = '', $value = '', $id = '', $before = '', $after = '', $lbl_attributes = '', $elem_attributes = '' ) {
  464. $attributes['name'] = $name;
  465. $attributes['rows'] = $rows;
  466. $attributes['cols'] = $cols;
  467. $attributes['label'] = $label;
  468. $attributes['value'] = $value;
  469. $attributes['id'] = ( empty($id) ? $name : $id );
  470. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  471. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  472. $attributes['before'] = $before;
  473. $attributes['after'] = $after;
  474. $attributes['lbl_attributes'] = '';
  475. $attributes['elem_attributes'] = '';
  476. if ( !empty($lbl_attributes) ) {
  477. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  478. }
  479. if ( !empty($elem_attributes) ) {
  480. $attributes['elem_attributes'] = ' '. $elem_attributes;
  481. }
  482. $res = array( 'type' => 'textarea', 'attributes' => $attributes);
  483. $wrappOpenAttributes['description'] = 'open';
  484. $wrappCloseAttributes['description'] = 'close';
  485. $wrappOpen = array( 'type' => 'wrapp_'. IZF_DEFAULT_WRAPPER, 'attributes' => $wrappOpenAttributes );
  486. $wrappClose = array( 'type' => 'wrapp_'. IZF_DEFAULT_WRAPPER, 'attributes' => $wrappCloseAttributes );
  487. $this->setElement($wrappOpen);
  488. $this->setElement($res);
  489. $this->setElement($wrappClose);
  490. }
  491. /**
  492. * Ajoute un paragraphe de texte dans le formulaire
  493. *
  494. * @param string $value texte du paragraphe
  495. * @param string $elem_attributes attributs pouvant être ajoutés au paragraphe pour les css (class, id, style inline)
  496. * @access public
  497. */
  498. function addParagraph( $value, $elem_attributes = '' ) {
  499. $attributes['value'] = $value;
  500. $attributes['elem_attributes'] = '';
  501. if ( !empty($elem_attributes) ) {
  502. $attributes['elem_attributes'] = ' '. $elem_attributes;
  503. }
  504. $res = array( 'type' => 'paragraph', 'attributes' => $attributes);
  505. $this->setElement($res);
  506. }
  507. /**
  508. * Ajoute un élément '<option></option> dans une liste déroulante
  509. * Charge la paire valeur/label dans la liste deroulante
  510. *
  511. * addDataInSelect précharge en fait les données qui seront dans la liste déroulante
  512. * Il faut donc précharger toutes les valaurs que l'on souhaite pour la liste avant d'ajouter celle-ci
  513. * avec la méthode {@link addSelect()}
  514. *
  515. * @param string $value valeur de l'attribut 'value' de la balise 'option'
  516. * @param string $label texte visible de l'element option de la liste deroulante
  517. * @access public
  518. */
  519. function addDataInSelect( $value, $label ) {
  520. $attributes['value'] = $value;
  521. $attributes['label'] = $label;
  522. $res = array( 'type' => 'option', 'attributes' => $attributes);
  523. $this->setSelectElement($res);
  524. }
  525. /**
  526. * Ajoute un élément dans le tableau selectElement[], tableau de préchargement des éléments pour une lsite déroulante
  527. *
  528. * @param array $res tableau associatif contenu la valeur et le label de la balise '<option>' à rajouter à la liste déroulante
  529. * @access private
  530. */
  531. function setSelectElement( $res ) {
  532. $this->selectElements[] = $res;
  533. }
  534. /**
  535. * Défini les limites pour mettre des éléments '<option>' d'une liste déroulante dans une balsie '<optgroup>'
  536. *
  537. * Cette méthode défini les bornes parmi les éléments déjà préchargés par la méthode {@link addDataInSelect()}
  538. *
  539. * @param string $start élement de début. Correspond à l'attribut $value du premier élément qui sera dans l'optgroup
  540. * @param string $end élement de fin. Correspond à l'attribut $value du dernier élément qui sera dans l'optgroup
  541. * @access public
  542. */
  543. function setOptGroupLimiters( $start, $end ) {
  544. foreach ( $this->selectElements as $id => $elem ) {
  545. foreach ( $elem as $k => $attribut ) {
  546. if ( $k == 'attributes' ) {
  547. if ( isset($attribut['value']) && $attribut['value'] == $start ) {
  548. $this->optGroupElements[0] = $id;
  549. break 2;
  550. }
  551. }
  552. }
  553. }
  554. $elements = array_reverse($this->selectElements, TRUE);
  555. foreach ( $elements as $id => $elem ) {
  556. foreach ( $elem as $k => $attribut ) {
  557. if ( $k == 'attributes' ) {
  558. if ( isset($attribut['value']) && $attribut['value'] == $end ) {
  559. $this->optGroupElements[1] = $id;
  560. break 2;
  561. }
  562. }
  563. }
  564. }
  565. }
  566. /**
  567. * Ajoute la balise '<optgroup>' après avoir défini ses bornes
  568. *
  569. * @param string $label label de la balise <optgroup>
  570. * @access public
  571. */
  572. function addOptGroup($label) {
  573. foreach ( $this->selectElements as $k => $elem ) {
  574. if ( $k == $this->optGroupElements[0] ) {
  575. $attributes['label'] = $label;
  576. $res = array( 'type' => 'optgroup_open', 'attributes' => $attributes);
  577. $elemCopy[] = $res;
  578. $elemCopy[] = $elem;
  579. } elseif ( $k == $this->optGroupElements[1] ) {
  580. $res = array( 'type' => 'optgroup_close');
  581. $elemCopy[] = $elem;
  582. $elemCopy[] = $res;
  583. } else {
  584. $elemCopy[] = $elem;
  585. }
  586. }
  587. $this->selectElements = $elemCopy;
  588. }
  589. /**
  590. * Ajoute une liste deroulante au formulaire
  591. *
  592. * Vous devez tout d'abord charger les elements constituants la liste avec les méthodes
  593. * {@link addDataInSelect()}, {@link setOptGroupLimiters()} et {@link addOptGroup()}
  594. * Une fois la liste ajoutée, le tableau des éléments qui ont été préchargés est vidé
  595. *
  596. * @param string $name nom de la liste déroulante
  597. * @param string $label label de la liste deroulante: texte placé devant pour l'identifier
  598. * @param string $selected indique l'élément de la liste déroulante séléctionné par defaut. Correspond à l'attribut 'value' de la balsie '<option>'
  599. * @param string $id identifiant unique de la liste déroulante
  600. * @param int $size nombre de lignes visibles de la liste déroulante. Vaut 1 par defaut
  601. * @param string $before élement ouvrant encadrant laliste déroulante. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  602. * @param string $after élement fermant encadrant la liste déroulante. Peut prendre 3 valeurs : P (paragraphe), D (div) ou B (espace). Vaut P par défaut
  603. * @param string $lbl_attributes attributs pouvant être ajoutés au label de la liste déroulante pour les css (class, id ou style inline) ou du javascript
  604. * @param string $elem_attributes attributs pouvant être ajoutés à la liste déroulante pour les css (class, id ou style inline) ou du javascript
  605. * @access public
  606. */
  607. function addSelect( $name, $label = '', $selected = '', $id = '', $size = '1', $multiple = '0', $before = '', $after = '', $lbl_attributes = '', $elem_attributes = '' ) {
  608. $attributes['name'] = $name;
  609. $attributes['label'] = $label;
  610. $attributes['selected'] = $selected;
  611. $attributes['size'] = $size;
  612. $attributes['multiple'] = $multiple;
  613. $attributes['id'] = ( empty($id) ? $name : $id );
  614. $before = ( empty($before) ? IZF_DEFAULT_WRAPPER : $before );
  615. $after = ( empty($after) ? IZF_DEFAULT_WRAPPER : $after );
  616. $attributes['before'] = $before;
  617. $attributes['after'] = $after;
  618. $attributes['lbl_attributes'] = '';
  619. $attributes['elem_attributes'] = '';
  620. if ( !empty($lbl_attributes) ) {
  621. $attributes['lbl_attributes'] = ' '. $lbl_attributes;
  622. }
  623. if ( !empty($elem_attributes) ) {
  624. $attributes['elem_attributes'] = ' '. $elem_attributes;
  625. }
  626. $attributes['data'] = $this->selectElements;
  627. $res = array( 'type' => 'select', 'attributes' => $attributes);
  628. $wrappOpenAttributes['description'] = 'open';
  629. $wrappCloseAttributes['description'] = 'close';
  630. $wrappOpen = array( 'type' => 'wrapp_'. $before, 'attributes' => $wrappOpenAttributes );
  631. $wrappClose = array( 'type' => 'wrapp_'. $after, 'attributes' => $wrappCloseAttributes );
  632. $this->setElement($wrappOpen);
  633. $this->setElement($res);
  634. $this->setElement($wrappClose);
  635. $this->resetDataInSelect();
  636. }
  637. /**
  638. * Vide la liste des éléments à charger dans une liste déroulante
  639. *
  640. * @access private
  641. */
  642. function resetDataInSelect() {
  643. $this->selectElements = array();
  644. }
  645. /**
  646. * Défini les limites pour mettre un élément fieldset dans le formulaire
  647. *
  648. * Cette méthode défini les bornes parmi les éléments du formulaire
  649. *
  650. * @param string $start élement de début. Correspond à l'attribut $name du premier élément qui sera dans l'optgroup
  651. * @param string $end élement de fin. Correspond à l'attribut $name du dernier élément qui sera dans l'optgroup
  652. * @access public
  653. */
  654. function setFieldSetLimiters( $start, $end ) {
  655. foreach ( $this->elements as $id => $elem ) {
  656. foreach ( $elem as $k => $attribut ) {
  657. if ( $k == 'attributes' ) {
  658. if ( isset($attribut['name']) && $attribut['name'] == $start ) {
  659. $this->fieldSetElements[0] = $id - 1;
  660. break 2;
  661. }
  662. }
  663. }
  664. }
  665. $elements = array_reverse($this->elements, TRUE);
  666. foreach ( $elements as $id => $elem ) {
  667. foreach ( $elem as $k => $attribut ) {
  668. if ( $k == 'attributes' ) {
  669. if ( isset($attribut['name']) && $attribut['name'] == $end ) {
  670. $this->fieldSetElements[1] = $id + 1;
  671. break 2;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. /**
  678. * Ajoute un fieldset après avoir défini ses bornes avec la méthode {@link setFieldSetLimiters()}
  679. *
  680. * @param string $legend légende associée au fieldset
  681. * @param string $elem_attributes attributs pouvant être ajoutés au fieldset pour les css (class, id ou style inline) ou du javascript
  682. * @access public
  683. */
  684. function addFieldSet( $legend = '', $elem_attributes = '' ) {
  685. foreach ( $this->elements as $k => $elem ) {
  686. if ( $k == $this->fieldSetElements[0] ) {
  687. $attributes['legend'] = $legend;
  688. $attributes['elem_attributes'] = '';
  689. if ( !empty($elem_attributes) ) {
  690. $attributes['elem_attributes'] = ' '. $elem_attributes;
  691. }
  692. $res = array( 'type' => 'fieldset_open', 'attributes' => $attributes);
  693. $elemCopy[] = $res;
  694. $elemCopy[] = $elem;
  695. } elseif ( $k == $this->fieldSetElements[1] ) {
  696. $res = array( 'type' => 'fieldset_close');
  697. $elemCopy[] = $elem;
  698. $elemCopy[] = $res;
  699. } else {
  700. $elemCopy[] = $elem;
  701. }
  702. }
  703. $this->elements = $elemCopy;
  704. }
  705. /**
  706. * Ajoute un élément de formulaire dans le tableau elements[] qui les contient tous
  707. *
  708. * @param array tableau contenant les paramètres d'un élément de formulaire à ajouter
  709. * @access private
  710. */
  711. function setElement( $res ) {
  712. $this->elements[] = $res;
  713. }
  714. /**
  715. * Affiche le formulaire
  716. *
  717. * Cette méthode ajoute le dernier élément au formulaire (balise de fermeture de celui-ci) puis
  718. * va faire générer la sortie html pour chaque élément avant de l'afficher
  719. * @access public
  720. */
  721. function displayForm() {
  722. $res['type'] = 'close_form';
  723. $this->setElement($res);
  724. foreach ( $this->elements as $elem ) {
  725. $this->setElementHtml( $elem );
  726. }
  727. echo $this->elementsHtml;
  728. }
  729. /**
  730. * Génère la sortie html pour les éléments de formulaire
  731. *
  732. * Stock la sortie html dans la chaine {@link elementsHtml}
  733. *
  734. * @param array $res tableau contenant les paramètres d'un élement de formulaire
  735. * @access private
  736. */
  737. function setElementHtml( $res ) {
  738. if ( ( is_array( $res ) && count( $res ) >= 1 ) ) {
  739. switch ( $res['type'] ) {
  740. case 'wrapp_P':
  741. if ( $res['attributes']['description'] == 'open' ) {
  742. $this->elementsHtml .= '<p>' . "\n";
  743. } elseif ( $res['attributes']['description'] == 'close' ) {
  744. $this->elementsHtml .= '</p>' . "\n";
  745. }else {
  746. $this->elementsHtml .= '' . "\n";
  747. }
  748. break;
  749. case 'wrapp_D':
  750. if ( $res['attributes']['description'] == 'open' ) {
  751. $this->elementsHtml .= '<div>' . "\n";
  752. } elseif ( $res['attributes']['description'] == 'close' ) {
  753. $this->elementsHtml .= '</div>' . "\n";
  754. }else {
  755. $this->elementsHtml .= '' . "\n";
  756. }
  757. break;
  758. case 'wrapp_B':
  759. if ( $res['attributes']['description'] == 'open' ) {
  760. $this->elementsHtml .= '' . "\n";
  761. } elseif ( $res['attributes']['description'] == 'close' ) {
  762. $this->elementsHtml .= ' ' . "\n";
  763. }else {
  764. $this->elementsHtml .= ' ' . "\n";
  765. }
  766. break;
  767. case 'fieldset_open':
  768. $this->elementsHtml .= '<fieldset'. $res['attributes']['elem_attributes'] .'>' . "\n";
  769. if ( !empty($res['attributes']['legend']) ) {
  770. $this->elementsHtml .= '<legend>'. $res['attributes']['legend'] .'</legend>' . "\n";
  771. }
  772. break;
  773. case 'fieldset_close':
  774. $this->elementsHtml .= '</fieldset>' . "\n";
  775. break;
  776. case 'optgroup_open':
  777. $this->elementsHtml .= "\n<optgroup label=\"". $res['attributes']['label'] ."\">";
  778. break;
  779. case 'optgroup_close':
  780. $this->elementsHtml .= "</optgroup>\n";
  781. break;
  782. case 'open_form':
  783. $this->elementsHtml .= "<form action=\"". $res['attributes']['action'] . "\" ".
  784. "method=\"". $res['attributes']['method'] . "\" ".
  785. "id=\"". $res['attributes']['id'] ."\" ".
  786. "enctype=\"". $res['attributes']['enctype'] . "\">\n";
  787. break;
  788. case 'close_form':
  789. $this->elementsHtml .= "</form>\n";
  790. break;
  791. case 'label':
  792. $this->elementsHtml .= "<label for=\"". $res['attributes']['id'] ."\"".
  793. $res['attributes']['lbl_attributes'] .">";
  794. break;
  795. case 'text':
  796. if ( !empty($res['attributes']['label']) ) {
  797. $this->setElementHtml( array( 'type' => 'label',
  798. 'attributes' => array( 'id' => $res['attributes']['id'],
  799. 'label' => $res['attributes']['label'],
  800. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  801. $this->elementsHtml .= $res['attributes']['label'];
  802. if ( $this->labelMode == 'nowrapp' ) {
  803. $this->elementsHtml .= "</label>\n";
  804. }
  805. }
  806. $this->elementsHtml .= "<input type=\"text\" id=\"". $res['attributes']['id'] ."\" ".
  807. "name=\"". $res['attributes']['name'] ."\" ".
  808. "value=\"". $res['attributes']['value'] ."\" ".
  809. "size=\"". $res['attributes']['size'] ."\" ".
  810. "maxlength=\"" . $res['attributes']['maxlength'] ."\"".
  811. $res['attributes']['elem_attributes'] ." />";
  812. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  813. $this->elementsHtml .= "</label>\n";
  814. }
  815. break;
  816. case 'password':
  817. if ( !empty($res['attributes']['label']) ) {
  818. $this->setElementHtml( array( 'type' => 'label',
  819. 'attributes' => array( 'id' => $res['attributes']['id'],
  820. 'label' => $res['attributes']['label'],
  821. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  822. $this->elementsHtml .= $res['attributes']['label'];
  823. if ( $this->labelMode == 'nowrapp' ) {
  824. $this->elementsHtml .= "</label>\n";
  825. }
  826. }
  827. $this->elementsHtml .= "<input type=\"password\" id=\"". $res['attributes']['id'] ."\" ".
  828. "name=\"". $res['attributes']['name'] ."\" ".
  829. "value=\"". $res['attributes']['value'] ."\" ".
  830. "size=\"". $res['attributes']['size'] ."\" ".
  831. "maxlength=\"" . $res['attributes']['maxlength'] ."\"".
  832. $res['attributes']['elem_attributes'] ." />";
  833. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  834. $this->elementsHtml .= "</label>\n";
  835. }
  836. break;
  837. case 'checkbox':
  838. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  839. $this->setElementHtml( array( 'type' => 'label',
  840. 'attributes' => array( 'id' => $res['attributes']['id'],
  841. 'label' => $res['attributes']['label'],
  842. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  843. }
  844. $this->elementsHtml .= "<input type=\"checkbox\" id=\"". $res['attributes']['id'] ."\" ".
  845. "name=\"". $res['attributes']['name'] ."[". $res['attributes']['value'] ."]\" ".
  846. "value=\"". $res['attributes']['value'] ."\" ";
  847. if ( isset($this->submittedData[$res['attributes']['name']][$res['attributes']['value']]) ||
  848. ($res['attributes']['check'] == 1 && empty($this->submittedData) ) ) {
  849. $this->elementsHtml .= "checked=\"checked\"";
  850. }
  851. $this->elementsHtml .= $res['attributes']['elem_attributes'] ." />";
  852. if ( !empty($res['attributes']['label']) && $this->labelMode == 'nowrapp' ) {
  853. $this->setElementHtml( array( 'type' => 'label',
  854. 'attributes' => array( 'id' => $res['attributes']['id'],
  855. 'label' => $res['attributes']['label'],
  856. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  857. }
  858. if ( !empty($res['attributes']['label']) ) {
  859. $this->elementsHtml .= $res['attributes']['label']. "</label>";
  860. }
  861. break;
  862. case 'textarea':
  863. if ( !empty($res['attributes']['label']) ) {
  864. $this->setElementHtml( array( 'type' => 'label',
  865. 'attributes' => array( 'id' => $res['attributes']['id'],
  866. 'label' => $res['attributes']['label'],
  867. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  868. $this->elementsHtml .= $res['attributes']['label'];
  869. if ( $this->labelMode == 'nowrapp' ) {
  870. $this->elementsHtml .= "</label>\n";
  871. }
  872. }
  873. $this->elementsHtml .= "<br /><textarea id=\"". $res['attributes']['id'] ."\" ".
  874. "name=\"". $res['attributes']['name'] ."\" ".
  875. "rows=\"". $res['attributes']['rows'] ."\" ".
  876. "cols=\"". $res['attributes']['cols'] ."\" ".
  877. $res['attributes']['elem_attributes'] .">".
  878. $res['attributes']['value'].
  879. "</textarea>\n";
  880. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  881. $this->elementsHtml .= "</label>\n";
  882. }
  883. break;
  884. case 'hidden':
  885. $this->elementsHtml .= "\n<input type =\"hidden\" ".
  886. "name=\"". $res['attributes']['name'] ."\" ".
  887. "value=\"". $res['attributes']['value'] ."\" />\n";
  888. break;
  889. case 'submit':
  890. if ( !empty($res['attributes']['label']) ) {
  891. $this->setElementHtml( array( 'type' => 'label',
  892. 'attributes' => array( 'id' => $res['attributes']['id'],
  893. 'label' => $res['attributes']['label'],
  894. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  895. if ( $this->labelMode == 'nowrapp' ) {
  896. $this->elementsHtml .= "</label>";
  897. }
  898. }
  899. $this->elementsHtml .= "<input type=\"submit\" id=\"". $res['attributes']['id'] ."\" ".
  900. "name=\"". $res['attributes']['name'] ."\" ".
  901. "value=\"". $res['attributes']['value'] ."\" ".
  902. $res['attributes']['elem_attributes'] ." />";
  903. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  904. $this->elementsHtml .= "</label>";
  905. }
  906. break;
  907. case 'reset':
  908. if ( !empty($res['attributes']['label']) ) {
  909. $this->setElementHtml( array( 'type' => 'label',
  910. 'attributes' => array( 'id' => $res['attributes']['id'],
  911. 'label' => $res['attributes']['label'],
  912. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  913. if ( $this->labelMode == 'nowrapp' ) {
  914. $this->elementsHtml .= "</label>";
  915. }
  916. }
  917. $this->elementsHtml .= "<input type=\"reset\" id=\"". $res['attributes']['id'] ."\" ".
  918. "name=\"". $res['attributes']['name'] ."\" ".
  919. "value=\"". $res['attributes']['value'] ."\" ".
  920. $res['attributes']['elem_attributes'] ." />";
  921. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  922. $this->elementsHtml .= "</label>";
  923. }
  924. break;
  925. case 'radio':
  926. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  927. $this->setElementHtml( array( 'type' => 'label',
  928. 'attributes' => array( 'id' => $res['attributes']['id'],
  929. 'label' => $res['attributes']['label'],
  930. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  931. }
  932. $this->elementsHtml .= "<input type=\"radio\" id=\"". $res['attributes']['id'] ."\" ".
  933. "name=\"". $res['attributes']['name'] ."\" ".
  934. "value=\"". $res['attributes']['value'] ."\" ";
  935. if ( (isset($this->submittedData[$res['attributes']['name']]) &&
  936. $this->submittedData[$res['attributes']['name']] == $res['attributes']['value']) ||
  937. ($res['attributes']['check'] == 1 && empty($this->submittedData) ) ) {
  938. $this->elementsHtml .= "checked=\"checked\"";
  939. }
  940. $this->elementsHtml .= $res['attributes']['elem_attributes'] ." />";
  941. if ( !empty($res['attributes']['label']) && $this->labelMode == 'nowrapp' ) {
  942. $this->setElementHtml( array( 'type' => 'label',
  943. 'attributes' => array( 'id' => $res['attributes']['id'],
  944. 'label' => $res['attributes']['label'],
  945. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  946. }
  947. if ( !empty($res['attributes']['label']) ) {
  948. $this->elementsHtml .= $res['attributes']['label'] ."</label>\n";
  949. }
  950. break;
  951. case 'select':
  952. if ( !empty($res['attributes']['label']) ) {
  953. $this->setElementHtml( array( 'type' => 'label',
  954. 'attributes' => array( 'id' => $res['attributes']['id'],
  955. 'label' => $res['attributes']['label'],
  956. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  957. $this->elementsHtml .= $res['attributes']['label'];
  958. if ( $this->labelMode == 'nowrapp' ) {
  959. $this->elementsHtml .= "</label>\n";
  960. }
  961. }
  962. $this->elementsHtml .= "<select name=\"". $res['attributes']['name'] ."\" ".
  963. "id=\"". $res['attributes']['id'] ."\" ".
  964. "size=\"". $res['attributes']['size'] ."\"";
  965. if ( $res['attributes']['multiple'] == 1 ) {
  966. $this->elementsHtml .= "multiple=\"multiple\"";
  967. }
  968. $this->elementsHtml .= $res['attributes']['elem_attributes'] .">";
  969. foreach ( $res['attributes']['data'] as $data ) {
  970. if ( $data['type'] == 'optgroup_open' || $data['type'] == 'optgroup_close' ) {
  971. $this->setElementHtml($data);
  972. }
  973. if ( $data['type'] == 'option' ) {
  974. $this->elementsHtml .= "\n<option value=\"". $data['attributes']['value'] ."\"";
  975. if ( $data['attributes']['value'] == $res['attributes']['selected'] ) {
  976. $this->elementsHtml .= " selected=\"selected\"";
  977. }
  978. $this->elementsHtml .= ">". $data['attributes']['label'] ."</option>";
  979. }
  980. }
  981. $this->elementsHtml .= "\n</select>";
  982. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  983. $this->elementsHtml .= "</label>\n";
  984. }
  985. break;
  986. case 'paragraph':
  987. $this->elementsHtml .= "<p". $res['attributes']['elem_attributes'] .">".
  988. $res['attributes']['value'] ."</p>\n";
  989. break;
  990. case 'container':
  991. $this->elementsHtml .= "<div". $res['attributes']['elem_attributes'] .">".
  992. $res['attributes']['value'] ."</div>\n";
  993. break;
  994. case 'file':
  995. if ( !empty($res['attributes']['label']) ) {
  996. $this->setElementHtml( array( 'type' => 'label',
  997. 'attributes' => array( 'id' => $res['attributes']['id'],
  998. 'label' => $res['attributes']['label'],
  999. 'lbl_attributes' => $res['attributes']['lbl_attributes'] ) ) );
  1000. $this->elementsHtml .= $res['attributes']['label'];
  1001. if ( $this->labelMode == 'nowrapp' ) {
  1002. $this->elementsHtml .= "</label>\n";
  1003. }
  1004. }
  1005. $this->elementsHtml .= "<input type=\"file\" id=\"". $res['attributes']['id'] ."\" ".
  1006. "name=\"". $res['attributes']['name'] ."\" ".
  1007. $res['attributes']['elem_attributes'] ." />";
  1008. if ( !empty($res['attributes']['label']) && $this->labelMode == 'wrapp' ) {
  1009. $this->elementsHtml .= "</label>\n";
  1010. }
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. /**
  1016. * Spécifie si le formulaire a été envoyé ou non
  1017. *
  1018. * @return boolen renvoi vrai si le formulaire a été soumis, falsse sinon
  1019. * @access public
  1020. */
  1021. function isSubmitted() {
  1022. if ( !empty($this->submittedData) ) {
  1023. # fonction qui permettrait d'effectuer un traitement
  1024. $this->manageSubmittedData();
  1025. return true;
  1026. }
  1027. return false;
  1028. }
  1029. /**
  1030. * Effectue un traitement sur les valeurs soumises du formulaire
  1031. *
  1032. * @access private
  1033. */
  1034. function manageSubmittedData() {
  1035. # si besoin...
  1036. }
  1037. /**
  1038. * Ajoute un message en début du formulaire dans un paragraphe
  1039. *
  1040. * Peut être utilisé pour des messages d'erreur par exemple
  1041. *
  1042. * @param string $msg message à afficher
  1043. * @param $container balise contenant le message. Peut prendre les valeurs 'p' pour un paragraphe ou 'd' pour une div. Vaut 'p' par défaut
  1044. * @param $elem_attributes attributs pouvant être ajoutés à la balise du message pour les css (class, id ou style inline) ou du javascript
  1045. * @access public
  1046. */
  1047. function addMsgAtTop( $msg, $container = 'p', $elem_attributes = '' ) {
  1048. $attributes['value'] = $msg;
  1049. $attributes['elem_attributes'] = '';
  1050. if ( !empty($elem_attributes) ) {
  1051. $attributes['elem_attributes'] = ' '. $elem_attributes;
  1052. }
  1053. if ( $container == 'p' ) {
  1054. $type = 'paragraph';
  1055. } elseif ( $container == 'd' ) {
  1056. $type = 'container';
  1057. }
  1058. $res = array( 'type' => $type, 'attributes' => $attributes);
  1059. array_unshift( $this->elements, $res);
  1060. }
  1061. /**
  1062. * Ajoute une valeur soumise à la liste de celles à vérifier
  1063. *
  1064. * Permet de définir les variables à tester parmis celles présentent
  1065. * dans le formulaire en précisant le type de vérification
  1066. *
  1067. * @param string $name nom de l'élément à vérifier
  1068. * @param string $dataType type de vérification à effectuer sur cet élément. Voir la méthode {@link verifyData()} pour la liste des types
  1069. * @param string $option options possibles à passer pour le type de vérification à effectuer
  1070. * @access public
  1071. */
  1072. function addDataToVerif($name, $dataType, $option = '' ) {
  1073. if ( array_key_exists( trim( $name ), $this->submittedData ) ) {
  1074. $nb = count($this->verifData);
  1075. $this->verifData[$nb]['name'] = trim( strtolower( $name ) );
  1076. $this->verifData[$nb]['value'] = trim($this->submittedData[$name]);
  1077. $this->verifData[$nb]['dataType'] = trim( strtolower( $dataType ) );
  1078. $this->verifData[$nb]['option'] = trim( strtolower( $option ) );
  1079. } else {
  1080. $this->addMsgAtTop( 'impossible de verifier le champs <em>'. $name .'</em>. il n\'existe pas dans le formulaire' );
  1081. }
  1082. }
  1083. /**
  1084. * Vérifie les variables spécifiées par la méthode {@link addDataToVerif()}
  1085. *
  1086. * @return boolean Renvoi vrai si les données sont valides, faux sinon
  1087. * @access public
  1088. */
  1089. function isValidData() {
  1090. $error = false;
  1091. foreach( $this->verifData as $data ) {
  1092. switch ( $data['dataType'] ) {
  1093. case 'notnull':
  1094. if ( empty($data['value']) ) {
  1095. $this->addMsgAtTop( 'le champ <em>'. $data['name'] .'</em> est requis. Veuillez le remplir svp' );
  1096. $error = true;
  1097. }
  1098. break;
  1099. case 'maxlength':
  1100. if ( empty($data['option']) ) {
  1101. $this->addMsgAtTop( 'Veuillez préciser la longueur maximale demandée pour le champ <em>'. $data['name'] .'</em>' );
  1102. $error = true;
  1103. } elseif ( strlen( $data['value'] ) > $data['option'] ) {
  1104. $this->addMsgAtTop( 'le champ <em>'. $data['name'] .'</em> est trop long. (longueur max: '. $data['option'] .')' );
  1105. $error = false;
  1106. }
  1107. break;
  1108. case 'minlength':
  1109. if ( empty($data['option']) ) {
  1110. $this->addMsgAtTop( 'Veuillez préciser la longueur minimale demandée pour le champ <em>'. $data['name'] .'</em>' );
  1111. $error = true;
  1112. } elseif ( strlen( $data['value'] ) < $data['option'] ) {
  1113. $this->addMsgAtTop( 'le champ <em>'. $data['name'] .'</em> est trop court. (longueur min: '. $data['option'] .')' );
  1114. $error = true;
  1115. }
  1116. break;
  1117. case 'email':
  1118. if ( !ereg( "^.+@.+\\..+$", $data['value'] ) ) {
  1119. $this->adMsgAtTop( 'le champ <em>'. $data['name'] .'</em> doit etre une adresse email valide svp');
  1120. $error = true;
  1121. }
  1122. break;
  1123. default:
  1124. $this->adMsgAtTop( 'le type de vérification demandé n\'existe pas');
  1125. break;
  1126. }
  1127. }
  1128. if ($error == true) {
  1129. return false;
  1130. }
  1131. return true;
  1132. }
  1133. /**
  1134. * Modifie le type de mise en page du label avec l'élément de formulaire auquel il est associé
  1135. *
  1136. * @param string $mode type d'affichage : 'wrapp' ou 'nowrapp'
  1137. * @access public
  1138. */
  1139. function setLabelMode( $mode ) {
  1140. if ( $mode == 'wrapp' || $mode == 'nowrapp' ) {
  1141. $this->labelMode = $mode;
  1142. }
  1143. }
  1144. }
  1145.  
  1146.  
  1147. ?>

Documentation generated on Wed, 29 Dec 2004 14:09:28 +0100 by phpDocumentor 1.3.0RC3