<?
/**
* MiniClick
* Gestionnaire de tableau collaboratif
* Copyright (C) 2006 Guillaume Monet <gm@ibreizh.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
*
* @package miniclick
* @version 2.2.1
*/
$handle = fopen("$_POST[file]", "r") or die("Impossible de lire le fichier");

$ret = "";
while (!feof ($handle)) {
        $ligne = fgets($handle);

        $ligne = str_replace("'","\'",$ligne);
        $ligne = str_replace("\"","",$ligne);
        $ligne = str_replace("\n","",$ligne);
	
        $tableau = explode(";", $ligne);
	$count = count($tableau);
	
	$table="";
	for($i = 0 ; $i< $count;$i++){
		if ($_POST[row.$i]!=""){
			$table[$_POST[table.$i]][$_POST[row.$i]]=$tableau[$i];
		} 
	}
	
	//insertion des TABLES en cours
	foreach($table as $keytable => $valuetable) {
		
		$i++;
		$sql = "INSERT INTO $keytable (";
		$val = " VALUES (";
		foreach($table[$keytable] as $keyrow => $valuerow) {
			if($key3 != "NM"){
				$sql .= "".$key3.",";
				$val .= "'".$value3."',";
			}
			
		}
		$sql = substr($sql,0,-1);
		$val = substr($val,0,-1);
		$sql .= ")";
		$val .= ");";
		echo "$sql.$val";
		mysql_query($sql.$val) ;
		${'id_'.$keytable}=mysql_insert_id();
		
		$j=0;
		//insertion des relations CIF s'il y a 
		foreach($table[$keytable] as $keyrow => $valuerow) {
			foreach($table as $keytable2 => $valuetable2) {
			$j++;
				foreach($table[$keytable2] as $keyrow2 => $valuerow2) {
					if(($keyrow== $keyrow2) && ($i>$j) && ($keytable!=$keytable2) ){
						if ($valuerow!=""){
							$sql = "INSERT INTO $keytable2 ($keyrow2) VALUES ('$valuerow') WHERE ".ucfirst($keytable2)."_Id=".${'id_'.$keytable2}."";
						}else{
							$sql = "INSERT INTO $keytable ($keyrow) VALUES ('$valuerow2') WHERE ".ucfirst($keytable)."_Id=".${'id_'.$keytable}."";
						}
					}

			}
			
		}
	}
	
	//insertion des relations (n,n) s'il y a 
	// mettre à jour automatique la table_relation
	$table_relation=array("PROD_FACT", "REGLEMENT", "SOUSCONTRAT");
	foreach($table_relation as $keyrelation => $valuerelation) {
		$cpt=0;
		$requete = mysql_query("SHOW COLUMNS FROM  $valuerelation");
		while($result=mysql_fetch_object($requete)){
			foreach($table as $keytable => $valuetable) {
				$nom_table=substr($result->Field,0,-3); 
				if($nom_table==ucfirst($keytable)){
					$cpt++;
					$tab[$cpt]=$nom_table;
				}
				if($cpt==2){
					$sql = "INSERT INTO $valuerelation ( ".$tab[1]."_Id,".$tab[2]."_Id) VALUES ('${'id_'.$tab[1]}','${'id_'.$tab[2]}')";
				}
			}
		}
	}	
/*
	$sql = "INSERT INTO $_POST[module] (";
	$val = " VALUES (";
	for($i = 0 ; $i< $count;$i++){
		if($_POST[row.$i] != "NM"){
			$sql .= "".$_POST[row.$i].",";
			$val .= "'".$tableau[$i]."',";
		}
	}
	$sql = substr($sql,0,-1);
	$val = substr($val,0,-1);
	$sql .= ")";
	$val .= ");";
	echo "$sql.$val";
	mysql_query($sql.$val) ;*/
}
?>