<?php

/* edit et enregsitre les tableaux via les class sql_php et file_config . Prend en charge la detection du type de tabelau, recherche du nom de tableau si absent. s'occupe de mettre en forme les formulaire, et d'enregitrer les donnée valide par celui ci.  */
class file_tbl_php{
	var $file;
	var $key;

	function file_tbl_php ($file,$key='',$query=''){
		$this->file=$file;
		/* Ratrape deplacement class*/
		if (!eregi("(\.\./)",$file)&&( file_exists($file) || file_exists('admin/'.$file)) ){
		 $this->file_light='admin/'.str_replace('../','',$file);
		}elseif (eregi('\.\./',$file))$this->file_light=str_replace('../','',$file);

		if (!file_exists(CHEMIN_ABS.CHEMIN_DU_SITE.$this->file_light)){ print ("<p class='erreur'>".ERROR.THE_FILES.DOES_NOT_EXIST." ".$this->file_light." ". WRITE_NOT_POSSIBLE."</p>"); return false; }
		require(CHEMIN_ABS.CHEMIN_DU_SITE.$this->file_light);

		/* key vide, recup via config_general */
		if (empty($key)) $key=recup_name_tabl_php($this->file);
		if (!empty($query)) $this->query=$query;

		/* insertion tableau liée include ou insert sql query */
		$this->key=(empty($query))? @$$key : sql('',$this->file,$key,$query,'') ;
		$this->tbl=$key;

		if(!is_array($this->key)) return false;

		$this->type=(is_array(current(each($this->key))))?'2d':'1d';
		if (defined('BO_MOD_LG_DEV'))include(CHEM_MODULES.'lg_dev/'.LANG_ADMIN.'/'.eregi_replace("((.*/)*|\.[a-z]{3})",'',$file).'.php');
	}
	/* edition du fichier, adapte type tableau*/
	function edit_file(){
		if (isset($this->type)&&$this->type=='2d') $this-> edit_file_config_2d();
		else $this-> edit_file_config_1d();
	}
	/* enreg fichier adatpé type retour  */
	function up_file($post){
		$this->post=$post;
		if ($this->type=='2d') $this-> up_file_2d();
		else $this-> up_file_1d();
	}
	function var_form($name_form,$dest,$get,$method,$html){
		$this->name_form=(empty($name_form))? 'formul' : $name_form;
		$this->dest=(empty($dest))? $_SERVER['PHP_SELF'] : $dest;
		$this->get=(empty($get))? '' : $get;
		$this->method=(empty($method))? 'POST' : strtoupper($method);
		$this->html=(empty($html))? '' : $html;
	}

	function edit_file_config_2d(){
		$_CONFIG_GEN = $this->key;
		$t="<h2>%1\$s</h2>";
		$op="<option value='%1\$s' %2\$s>%3\$s</option>";
		$p="<p>%1\$s</p>";
		$dtdd="<dt>%1\$s</dt><dd>%2\$s</dd>";
		$f_ok="<p class='ok'>%1\$s</p>";
		$arryData2=array("openwysing" => "openwysing","bbcode" => "bbcode");
		$Tooltip= "onmouseover=\"showTooltip('<div class=\'info\'>%s</div>');\" onmouseout=\"hideTooltip()\" onclick=\"hideTooltip()\"";
		$i=0;$s='';$tabindex='';
		$lg=appel_lang();
		$s.=tep_form ($this->dest,'',$this->get,$this->name_form,$this->method,false,$this->html );
		if (!is_array($_CONFIG_GEN)) { print 'tableau vide ou qui ne contient pas 2 dimensions'; return false;}

		while ($rs=each($_CONFIG_GEN))
		while ($r=each($rs['value'])){
		$ID_index=(isset($this->query)) ? $rs['value']['id']:$rs['key'] ;
		/* element on off*/
		if  (eregi("(true|false)", $r['value'])) {
			$etat= ($r['value']=='true') ? 'checked' : '' ;
			$etat_i= ($r['value']!='true') ? 'checked' : '' ;
			$s.=sprintf($dtdd,$ID_index.' '.$r['key'],php_f_radio($ID_index.'['.$r['key'].']', 'true', $etat ,$tabindex,sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'_D')))).GEN_TXT_ON
			.php_f_radio($ID_index.'['.$r['key'].']', 'false', $etat_i ,$tabindex,sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'_D')))).GEN_TXT_OFF
			.@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
			}

		/* Element uniquement 0-9*/
		elseif (eregi("^([0-9\.,]*)$", $r['value']))
			$s.=sprintf($dtdd,$ID_index.' '.$r['key'],php_f_textField($ID_index."[".$r['key']."]",100,100,$r['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'_D')))).@constant(strtoupper('ATO_'.$NOM_MODULE.'_'.$r['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
		/* url, src, href */
		elseif(eregi("^((.*/)*[a-z0-9_\.-]*\.[a-z0-9]{2,4})$", $r['value'])||eregi("^(href|src|url|img)", $r['key'])) {
			$s.='<fieldset><legend>'.$ID_index.' '.$r['key'].'</legend>';
			/* trait image */
			if (eregi("^(img)", $r['key'])){
// 				$s.=NewFct('upload_media_ajax',$action,$is_dir,$current_dir,$up_dir,$env,$_PX_config,$rel_url_files,$mode_pop);
				$s.=sprintf($p,php_f_textField($ID_index."[".$r['key']."]",100,100,$r['value'],'',sprintf($Tooltip,addslashes("<img src='".$r['value']."' alt='".$r['key']."' style='max-height:150px; max-width:150px;width:100%;' />"))).@constant(strtoupper('ATO_'.$NOM_MODULE.'_'.$r['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
			}
			/* traite url */
			else {
			$s.='<p>'.option_deplie($ID_index.$r['key'],_MENU_ENTREE_CHOIX_HREF,true).'</p>';
			$s.='<p id="'.$ID_index.$r['key'].'" style="display:none;">'.NewFct('list_site_links_interne').'</p>';
			$s.=sprintf($p,php_f_textField($ID_index."[".$r['key']."]",100,100,$r['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'_D'))).'  onfocus= "if (document.getElementById(\''.$ID_index."[".$r['key']."]".'\').value != \'\') {this.value = document.getElementById(\''.$ID_index."[".$r['key']."]".'\').value;}"').@constant(strtoupper('ATO_'.$NOM_MODULE.'_'.$r['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
			}
			$s.='</fieldset>';
		}
		/* Elemet contenat indice de lang (_fr,_en) */
		elseif  ((eregi("(.*)(_)([a-z]{2,3})$", $r['key'],$reg))AND(isset($lg[$reg[3]]))) {
			reset($lg);
			## Elment enrg
			if (strlen($r['value'])<70)
			 $s.=sprintf($dtdd,$ID_index.' '.$r['key'],php_f_textField($ID_index."[".$r['key']."_".$reg[3]."]",100,100,$r['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'_D'))))."(".$reg[3].") ".@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
			else
			  $s.=sprintf($p,$ID_index.' '.$r['key'].@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].''))." (".$reg[3].") ".'<br />'.php_f_textArea($ID_index."[".$r['key']."_".$reg[3]."]",100,3,$r['value'],'','').FORM_CHAMPS_OBLIGATOIRE);
			## Boucle recup lang absente
			while ($bcl_lang=each($lg))
			   if (!isset($_CONFIG_GEN[$ID_index][$reg[1].'_'.$bcl_lang['key']]))
			     $s.=sprintf($dtdd,$ID_index.' '.$r['key'],php_f_textField($ID_index."[".$reg[1]."_".$bcl_lang['key']."]",100,100,$r['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$reg[1].'_D'))))."(".$bcl_lang['key'].") ".@constant(strtoupper('ATO_'.$ID_index.'_'.$reg[1].'')).FORM_CHAMPS_OBLIGATOIRE);
			reset($lg);
		}
		/* Le reste sauf <> */
		elseif(!eregi("(<|>|&)",$r['value']))
		 $s.=sprintf($dtdd,$ID_index.' '.$r['key'],php_f_textField($ID_index."[".$r['key']."_".$reg[3]."]",100,100,$r['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'_D'))))."(".$reg[3].") ".@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
		/* Le reste */
		else $s.=sprintf($p,$ID_index.' '.$r['key'].@constant(strtoupper('ATO_'.$ID_index.'_'.$r['key'].'')).'<br />'.php_f_textArea($ID_index."[".$r['key']."]",50,3,$r['value'],'','').FORM_CHAMPS_OBLIGATOIRE);

		}
		## declenche enregsitrement sur nouvel appel
		$s.='<input type="hidden" name="file_tbl_php_form" value="true" />';
		$s.=sprintf($p,FORM_CHAMPS_OBLIGATOIRE_TXT);
		$s.=sprintf($p,tep_bts_form('','','SE','SR','','','',true));
		$s.="</form>\n<br />";
		print $s;
	}

	function edit_file_config_1d(){
		$_CONFIG_GEN = $this->key;
		$t="<h2>%1\$s</h2>";
		$op="<option value='%1\$s' %2\$s>%3\$s</option>";
		$p="<dd>%1\$s</dd><dt>%2\$s</dt>";
		$f_ok="<p class='ok'>%1\$s</p>";
		$arryData2=array("openwysing" => "openwysing","bbcode" => "bbcode");
		$Tooltip= "onmouseover=\"showTooltip('<div class=\'info\'>%s</div>');\" onmouseout=\"hideTooltip()\" onclick=\"hideTooltip()\"";
		$i=$tabindex=0;
		$lg=appel_lang();
		$s="<dl>\n";
		$s.=tep_form ($this->dest,'',$this->get,$this->name_form,$this->method,false,$this->html );
		if (!is_array($_CONFIG_GEN)) { print 'tableau vide ou qui ne contient pas 1 dimensions'; return false;}
		while ($rs=each($_CONFIG_GEN)){
			$tabindex++;
			 if  (eregi("(true|false)", $rs['value'])) {
				$etat= ($rs['value']=='true') ? 'checked' : '' ;
				$etat_i= ($rs['value']!='true') ? 'checked' : '' ;
				$s.=sprintf($p,$rs['key'],php_f_radio($rs['key'], 'true', $etat ,$tabindex,sprintf($Tooltip,@constant(strtoupper('ATO_'.$rs['key'].'_D')))).GEN_TXT_ON
				.php_f_radio($rs['key'], 'false', $etat_i ,$tabindex,sprintf($Tooltip,@constant(strtoupper('ATO_'.$rs['key'].'_D')))).GEN_TXT_OFF
				.@constant(strtoupper('ATO_'.$rs['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
				}
			## Elemet contenant indice de lang (_fr,_en)
			elseif  ((eregi("(.*)(_)([a-z]{2,3})$", $rs['key'],$reg))AND(isset($lg[$reg[3]]))) {
				reset($lg);
				## Elment enrg
				if (strlen($r['value'])<70)
				 $s.=sprintf($p,$rs['key'],php_f_textField($rs['key']."[".$reg[3]."]",100,100,$rs['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$rs['key'].'_D'))))."(".$reg[3].") ".@constant(strtoupper('ATO_'.$rs['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
				else
				  $s.=sprintf($p,$rs['key'],@constant(strtoupper('ATO_'.$rs['key'].''))." (".$reg[3].") ".'<br />'.php_f_textArea($rs['key']."_".$reg[3]."]",50,3,$r['value'],'','').FORM_CHAMPS_OBLIGATOIRE);
				## Boucle recup lang absente
				while ($bcl_lang=each($lg))
				   if (!isset($_Config_gen[$rs['key']][$reg[1].'_'.$bcl_lang['key']]))
				     $s.=sprintf($p,$rs['key'].' '.$r['key'],php_f_textField($rs['key']."[".$reg[1]."_".$bcl_lang['key']."]",100,100,$r['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$rs['key'].'_'.$reg[1].'_D'))))."(".$bcl_lang['key'].") ".@constant(strtoupper('ATO_'.$rs['key'].'_'.$reg[1].'')).FORM_CHAMPS_OBLIGATOIRE);
				reset($lg);
			}
			 elseif (eregi("^([a-z0-9@:. \._,/-]*)$", $rs['value']))
				$s.=sprintf($p,$rs['key'],php_f_textField($rs['key'],100,100,$rs['value'],'',sprintf($Tooltip,@constant(strtoupper('ATO_'.$rs['key'].'_D')))).@constant(strtoupper('ATO_'.$rs['key'].'')).FORM_CHAMPS_OBLIGATOIRE);
			 else $s.=sprintf($p,$rs['key'],@constant(strtoupper('ATO_'.$rs['key'])).'<br />'.php_f_textArea($rs['key'],50,3,$rs['value'],'','').FORM_CHAMPS_OBLIGATOIRE);
		}
		## declenche enregsitrement sur nouvel appel
		$s.='<input type="hidden" name="file_tbl_php_form" value="true" />';
		$s.=sprintf($p,'',FORM_CHAMPS_OBLIGATOIRE_TXT);
		$s.=sprintf($p,'',tep_bts_form('','','SE','SR','','','',true));

		$s.="</form>\n<br />";
		$s.="</dl>\n";
		print $s;
	}

	function up_file_2d(){
		/* Suppression element form autre que valeurs (file_tbl_php_form + confirm)  */
		array_splice($this->post,(count($this->post)-2));

		$err=0;
		while($r=each($this->post)) {
			if (is_array($r['value'])){
				$cat=' SET ';
				while ($rf=each($r['value']))
					if ($rf['key']=='id') $ID=$rf['value'];
					else $cat .=$rf['key'].' = \''.$rf['value'].'\' , ';

		$cat=substr($cat,0,(strlen($cat)-2));
		 $query=' UPDATE '.$this->file_light.' '.$cat.' WHERE  '.$this->tbl.' =  '.$ID;
		 $s=sql ('',$this->file,$this->tbl,$query,'');
		if ($s==false)$err++;
			}

		}
		if ($s==false)$err++;
		echo ($err>0)?false:$s;
		return ($err>0)?false:true;
	}

	function up_file_1d(){
		/* Suppression element form autre que valeurs (file_tbl_php_form + confirm)  */
		array_splice($this->post,(count($this->post)-2));
		$cat=' SET ';
		while($r=each($this->post)) {
			$cat .=$r['key'].' = \''.$r['value'].'\' , ';
		}
		$cat=substr($cat,0,(strlen($cat)-2));
		echo  $query=' UPDATE '.$this->file_light.' '.$cat ;
		echo $s=sql ('UPDATE',$this->file,$this->tbl,$query,'');
	}

}

?>