<?php
/**
* 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 3.1.0
* version import 2 
* version avec start transaction; commit; rollback non activé 
* @todo Mettre en template
*/
?>
<script type="text/javascript" language="javascript" src="<?php  echo MINICLICK_HOST_URL; ?>engine/miniclic/ajax/js/tableau.js"></script>
</head>
<body>


<form i="form" name="formulaire" action="main.php?page=engine/miniclic/module_import/valider_import_include.php" method="POST">
<?php

//include("includes/connection.php");
$ret = "";

$repertoireDestination = SMARTY_CACHE_DIR;
    $nomDestination = $_FILES["monfichier"]["name"];
    
    // Pour raison de s�urit�nous ajouterons aux fichiers
    // portant une extension .php .php3, l'extension .txt
if (eregi(".php", $nomDestination)) {
	$nomDestination .= ".txt";
}

if (is_uploaded_file($_FILES["monfichier"]["tmp_name"])) {
	if (rename($_FILES["monfichier"]["tmp_name"],$repertoireDestination.$nomDestination)) {
		echo "Le fichier temporaire ".$_FILES["monfichier"]["tmp_name"]." a ��upload vers ".$repertoireDestination.$nomDestination;
	} else {
		echo "Erreur";
	}
} else {
	echo "Le fichier n'a pas ��upload (trop gros ?)";
}


$handle = fopen($repertoireDestination.$nomDestination, "r") or die("Impossible de lire le fichier");

//Creation table import
$table_bdd_miniclic=array ("ROLE", "REGLE", "RULE","MODELE_CONTRAT", "TRIGGER", "ARCHIVAGE", "FICHIER", "IMPORT", "TYPE_UTILISATEUR", "USER", "FILTRE", "CONFIG", "SEMAPHORE", "WORKFLOW", "DROIT", "DROITCLIENT", "DROITZONE", "FAITPARTI", "CONFIG", "CONFIG","MODELE_CONTRAT", "ZONE", "SOCIETE");
$sql_alltable=mysql_query("SHOW TABLES");
while ($result_champ=mysql_fetch_array($sql_alltable)){
	$import=true;
	$table_mini=false;
	for ($i=0;$i<count($table_bdd_miniclic);$i++){
		if ($result_champ["Tables_in_".$_SESSION["database"]]==$table_bdd_miniclic[$i]){
			$table_mini=true;
			break;
		}
		
	}
	// si la table n'appartient pas ?? miniclic
	if ($table_mini==false){
		echo "SHOW COLUMNS FROM ".$result_champ["Tables_in_".$_SESSION["database"]];
		$sql_columns=mysql_query("SHOW COLUMNS FROM ".$result_champ["Tables_in_".$_SESSION["database"]]);
		while ($result_columns=mysql_fetch_array($sql_columns)){
			if (!preg_match("/_Id/i",$result_columns["Field"])) {
				$import=false;
				break;
			}
		}
		if ($import==false){
			$table_bdd_import[]=$result_champ["Tables_in_".$_SESSION["database"]];
		}
	}
	
	
}


$ct = 0;
echo "<table border=\"1\">";
while (!feof ($handle) && $ct < 1) {
        $ligne = fgets($handle);

        $ligne = str_replace("'","\'",$ligne);
        $ligne = str_replace("\"","",$ligne);
        $ligne = str_replace("\n","",$ligne);
	
        $tableau = explode(";", $ligne);
	$count = count($tableau);
	
	
	
	for($i = 0 ; $i<$count;$i++){
		// SELECT du nom de la table
		$ret .= "<tr><td><select id=\"select_table$i\" name=\"table$i\" onChange='majcombo($i,$count);'>";
		
		for ($j=0;$j<count($table_bdd_import);$j++){
			
			$ret.= "<option value=\"$table_bdd_import[$j]\"";
			$ret.= ">$table_bdd_import[$j]</option>\n";

		}
		$ret.= "<option value=\"NM\" SELECTED>Not mapped</option></select></td>";
		// SELECT des champs de la table
		$ret .= "<td><select id=\"select_row$i\" name=\"row$i\">";
		$ret.= "<option value=\"NM\">Not mapped ".$_GET["num"]."</option>";
		$ret.= "</select> $_GET[action]</td>";
		//fin SELECT des champs de la table
		$ret.= "<td>$tableau[$i]</td><td><INPUT name=\"unique$i\" value=\"1\" type=\"checkbox\"></td></tr>";
		$ct++;
	}
}
echo $ret;
echo "</table>";
echo "<input type=\"hidden\" name=\"file\" value=\"$nomDestination\"><input type=\"hidden\" name=\"repertoire\" value=\"$repertoireDestination\"><input type=\"hidden\" name=\"numcols\" value=\"$count\">";
echo "<br><button type=\"submit\">Valider</button>";
?>
</>
</html>
