error_reporting(E_ALL);
define('JAVASCRIPT',false);
define('REP_IMAGE','./icones/');
/**
* classe Element utilisee par la classe Formulaire
*
*/
class Element{
protected $type;
protected $name;
protected $label;
protected $labelScreen;
protected $attributs;
protected $notnull;
/** Constructeur
* @param string $type balise de l'element (input, select, textarea, ...)
* @param string $name
* @param string $label chaine affiché avant le champ
* @param tab $attributs tableau associatif des attibuts
* @return none
*/
function __construct($type,$name,$label,$attributs){
$this->type=$type;
$this->name=$name;
$this->label=$label;
if($this->label==''){
$this->labelScreen='';
}else{
$this->labelScreen=$this->label.' : ';
}
$this->attributs=$attributs;
$this->attributs['notnull']=false;
}
/**
* @param string $pre code HTML a afficher avant l'element
* @param string $post code HTML a afficher apres l'element
* @return string
*/
protected function toHTMLField($pre='',$post=''){
$html='';
//traitement particulier pour l'attribut class
if(isset($this->attributs['class'])){
$this->attributs['class']=str_replace(';',' ',$this->attributs['class']);
}
switch($this->type){
case 'input':
$html.='
attributs).' >';
break;
case 'textarea':
if(isset($this->attributs['value'])){
$value=$this->attributs['value'];
unset($this->attributs['value']);
}else{
$value='';
}
$html.='';
break;
case 'select':
$list=$this->attributs['list'];
unset($this->attributs['list']);
if(isset($this->attributs['selected'])){
$selected=$this->attributs['selected'];
unset($this->attributs['selected']);
}else{
$selected=null;
}
$html.='
';
break;
case 'checkbox':
case 'radio':
$list=$this->attributs['list'];
unset($this->attributs['list']);
$disposition=$this->attributs['disposition'];
unset($this->attributs['disposition']);
if(isset($this->attributs['checked'])){
$checked=$this->attributs['checked'];
unset($this->attributs['checked']);
}else{
$checked=array();
}
//parcours de la liste
foreach($list as $key=>$value){
//selection de l'element
if(in_array($key,$checked)){
$checkedHTML=' checked="checked"';
}else{
$checkedHTML='';
}
$html.='
attributs).'>';
$html.='';
if($disposition){
$html.='';
}
}
break;
default:
exit('Error : field type "'.$this->type.'" unknown');
}
if($this->attributs['notnull']){
$html.='*';
}
return $pre.$html.$post;
}
/** renvoie l'element HTML en ligne de tableau
* @return string
*/
function toHTML(){
if(!($this->type=='input' and $this->attributs['type']=='hidden')){
$html=''.$this->toHTMLField();
}else{
$html=$this->toHTMLField();
}
$html.='';
return $html;
}
/**
* @return unknown
*/
function toHTMLTableau(){
if(!($this->type=='input' and $this->attributs['type']=='hidden')){
$html='
'.$this->labelScreen.'
'.$this->toHTMLField().'
';
}else{
$html=$this->toHTMLField();
}
return $html;
}
function __set($key,$value){
if(isset($this->attributs[$key])){
if(substr($this->attributs[$key],-1)!=';'){
$this->attributs[$key].=';';
}
$this->attributs[$key].=$value;
}else{
$this->attributs[$key]=$value;
}
}
function __isset($key){
return isset($this->attributs[$key]);
}
function __unset($key){
unset($this->attributs[$key]);
}
function getName(){
return $this->name;
}
}
class Formulaire{
private $name;
private $attributs;
private $action;
private $method;
private $elements;
private $javascript;
private $boutons;
function __construct($name){
$this->name=$name;
$this->elements=array();
$this->method='POST';
$this->action=$_SERVER['PHP_SELF'];
$this->javascript=JAVASCRIPT;
$this->boutons=true;
}
/**
* renvoie un tableau associatif sous la forme dune chaine
* @author Erwan Gallenne
* @param $tab Tableau associatif a convertir
* @since 0.9
* @return une chaine representant le tableau
*/
function tab2html($tab){
$html='';
foreach($tab as $key=>$value){
$html.=' '.$key.'="'.$value.'"';
}
return $html;
}
function javascript(){
$this->javascript=true;
}
protected function addElement($element){
if(!isset($this->elements[$element->getName()])){
$this->elements[$element->getName()]=$element;
}else{
exit('Error : name "'.$element->getName().'" used before');
}
}
public function entete(){
$html='
';
if($this->javascript){
$html.='';
}
return $html;
}
private function boutons(){
$html='
';
$html.='
';
return $html;
}
public function setAction($action){
$this->action=$action;
}
public function toHTML(){
$html=$this->entete();
foreach($this->elements as $name=>$element){
$html.=$element->toHTML();
}
if($this->boutons){
$html.=$this->boutons();
}
$html.=$this->pied();
return $html;
}
public function toHTMLTableau($attributs=array()){
$this->boutons();
$html=$this->entete();
$html.='
| '; $html.=$this->boutons(); $html.=' |
No related posts.
La liste des entrées complémentaires est établie par le module d’extension YARPP.