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='
javascript){ $html.=' onSubmit="return formulaire_submit(this);"'; } $html.='>'; return $html; } public function pied(){ $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.=' '; foreach($this->elements as $name=>$element){ $html.=$element->toHTMLTableau()."\n"; } if($this->boutons){ $html.=' '; } $html.='
'; $html.=$this->boutons(); $html.='
'; $html.=$this->pied(); return $html; } private function addFieldInput($type,$name,$label,$attributs){ $attributs['type']=$type; $elem=new Element('input',$name,$label,$attributs); $this->addElement($elem); } public function addText($name,$label,$attributs=array()){ $this->addFieldInput('text',$name,$label,$attributs); } public function addPassword($name,$label,$attributs=array()){ $this->addFieldInput('password',$name,$label,$attributs); } public function addHidden($name,$value){ $this->addFieldInput('hidden',$name,'',array('value'=>$value)); } public function addTextArea($name,$label,$attributs=array()){ $element=new Element('textarea',$name,$label,$attributs); $this->addElement($element); } public function addSelect($name,$label,$list,$attributs=array()){ $element=new Element('select',$name,$label,$attributs); $element->list=$list; $this->addElement($element); } public function addSelectQuery($name,$label,$query,$attributs=array()){ $result=mysql_query($query); $list=array(); while ($row=mysql_fetch_row($result)){ $list[$row[0]]=$row[1]; } $this->addSelect($name,$label,$list,$attributs); } public function addRadio($name,$label,$list,$attributs=array(),$disposition="v"){ $attributs['disposition']=$disposition; $element=new Element('radio',$name,$label,$attributs); $element->list=$list; $this->addElement($element); } public function addCheckbox($name,$label,$list,$attributs=array(),$disposition="v"){ $attributs['disposition']=$disposition; $element=new Element('checkbox',$name,$label,$attributs); $element->list=$list; $this->addElement($element); } public function addButton($name,$label,$attributs=array()){ $this->addFieldInput('button',$name,$label,$attributs); } /** * affecte un attribut a un element * si default, on teste si une valeur existe deja * si l'attribut existe, on concatene * * @param chaine $name * @param chaine $key * @param chaine $value * @param bool�en $default * @param bool�en $force force l'affectation */ public function setFieldAttribut($name,$key,$value,$default=true,$force=false){ if($force){ unset($this->elements[$name]->$key); } if($default){ if(!isset($this->elements[$name]->$key)){ $this->elements[$name]->$key=$value; } }else{ $this->elements[$name]->$key=$value; } } public function noBoutons(){ $this->boutons=false; } public function setFieldNotNull($elements){ if(is_string($elements)){ $elements=array($elements); } $this->javascript(); foreach($elements as $name){ $this->setFieldAttribut($name,'class','notnull',false); $this->elements[$name]->notnull=true; } } public function sent(){ $resultat=true; $i=0; while($resultat and isset($this->elements[$i])){ $element=$this->elements[$i]; $resultat=Parametre::valeur($element->name); $i++; } return $resultat; } public function verif(){ $resultat=true; $i=0; } /** * renvoie vrai si une reponse est re�ue * */ public function reponse(){ switch($this->method){ case 'POST': if(isset($_POST['nom_formulaire']) and $_POST['nom_formulaire']==$this->name){ return true; }else{ $this->addHidden('nom_formulaire',$this->name); return false; } break; case 'GET': if(isset($_GET['nom_formulaire']) and $_GET['nom_formulaire']==$this->name){ return true; }else{ $this->addHidden('nom_formulaire',$this->name); return false; } break; default: exit('Method '.$this->method.' unknown'); } return true; } public function traiterReponse(){ switch($this->method){ case 'POST': $parametres=$_POST; break; case 'GET': $parametres=$_GET; break; } foreach($parametres as $key=>$value){ $parametres[$key]=htmlentities($value); } var_dump($parametres); } }

No related posts.

La liste des entrées complémentaires est établie par le module d’extension YARPP.

Laisser un commentaire

Recherche
Pub