<?php

	session_start();
	
	$_SESSION['database']="local";
	
	if($_SESSION['database']=="local")
	  {
	     $db_host    = "localhost";
	     $db_user   = "root";
	     $db_pass   = "";
     	     $db_name   = "boutique";

	 }
	
	if($_SESSION['database']=="public")
	  {
  	     $db_host    = "sql2";
   	     $db_user   = "analogde";
 	     $db_pass   = "rainbow";
    	     //$db_type   = "mysql";
	     $db_name   = "analogde";
	 }

?>


<?php

function supprimer_caracteres($chaine)
{
 	return( strtr( $chaine, "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn" ) );
} 

function compacter($str)
{
	$str =supprimer_caracteres($str);
	
	// remplace les apostrophes par des underscore
	$result = str_replace( "'" , "_", $str);
	
	// remplace les espace par des underscore
	$result = str_replace( " " , "_", $result);
	
	return $result;
}

?>

<?php
	
	if($_POST)
	  {
	  	/*if($_POST['filename'] == "")
	  	  { //echo "Il manque le nom de la catégorie !";
	  	    exit("Il manque le nom de la catégorie !");
	  	  }	
	  
	  	if(empty($_POST['text']))
	  	  { //echo "La zone contenant les critéres de filtrage est vide !";
	  	    exit("La zone contenant les critéres de filtrage est vide !");
	  	  }*/	
	  
	  
	    if($_POST['filename'] != "" && !empty($_POST['text']))
	      {	 // sauvegarde du textarea	
    		$file_name = $_POST['filename'];
    		$file_contents = $_POST['text'];
    		
    		//echo "++++ " .strlen($file_contents); 
    
    	   	$fo = fopen($file_name, 'w');
    	   	fputs($fo, $file_contents);
    	   	fclose($fo); 
    	   
  	   	$point = ".";
  		$position = strpos($file_name, $point);
		$sans_extension = substr($file_name, 0, $position);
		$categorie = $sans_extension;

	   	//echo $sans_extension;	

		// lire le fichier ligne par ligne
		$contenu_array = file($file_name);
	 
	 	$motif_titre = '(*)';
 	 	$remplacement = "";
		
	 	$tab_filtre = array();
	 	$tab_association = array();
	 	
	 	$connexion = mysql_connect($db_host,$db_user,$db_pass); 
		mysql_select_db($db_name, $connexion);   
		$requete = "SELECT * FROM correspondance WHERE categorie = '$categorie' ";
		$result = mysql_query($requete);
		
		if(mysql_num_rows($result)!=0) 
		  { echo "La catégorie existe dèja !";
                  echo "<br>";
		  }	
	 
	 	else {
		 	for($i=0; $i<=count($contenu_array)-1; $i++)
		    	   {   
			      	      if(preg_match('`\*`', $contenu_array[$i]))
				  	{  $str = str_replace($motif_titre, $remplacement, $contenu_array[$i]); 
				           $str = strtolower($str);
				           $association = addslashes($str);
				           $tab_association[] = $association;
					   $filtre = compacter($str);
				          // echo $filtre;
				          // echo "<br>";
				           $tab_filtre[]= $filtre; 
				           
				           $connexion = mysql_connect($db_host,$db_user,$db_pass); 
					   mysql_select_db($db_name, $connexion);
				  	   $sql = "SELECT * FROM correspondance WHERE filtre = '$filtre' ";
			    	           $req = mysql_query($sql);
	
					  if(mysql_num_rows($req) == 0)
		   			    {
	     					$connexion = mysql_connect($db_host,$db_user,$db_pass); 
	     					mysql_select_db($db_name, $connexion);
						$sql = "INSERT INTO correspondance( id, categorie, filtre, association) VALUES('', '$categorie' ,'$filtre', '$association')"; 
	 					$requete = mysql_query($sql);
	 					echo $str ."-> Enregistrement : OK";
	 					echo "<br>";
	 					
	 					
		  			    }
				       else {
	     		                      echo $association ."-> Enregistrement existe !";
	     		                      echo "<br>";
	 					
		                            }
				           
				        }
	 	   	   }
		 
				$connexion = mysql_connect($db_host,$db_user,$db_pass); 
				mysql_select_db($db_name, $connexion);
		 		$sql = "SELECT * FROM correspondance ";
		 		$requete = mysql_query($sql);
		 	
		 		$tab1 =array();
				$tab2 =array();
			
				while($data = mysql_fetch_array($requete))
			     	     {	$tab1[] =  $data['filtre']; 
			        	$tab2[] = $data['association'];
			     	     }
		
				/*print_r($tab1);
			
				echo "<br>";
				echo "<br>";
			
				print_r($tab2);
			
				echo "<br>";
				echo "<br>";*/
			
				/*echo $tab1[5];
				echo "<br>";
				echo $tab2[5];
				echo "<br>";*/
			
				for($i=0; $i<=count($tab1)-1; $i++)
			           {  if($i == 0)
			           	 {$chaine = $tab1[$i] ." varchar(30) NOT NULL,";
					 }
				      if($i != 0)
					 {$chaine = $chaine ." " .$tab1[$i] ." varchar(30) NOT NULL,";
					 }
					
			           }
			           
			        $connexion = mysql_connect($db_host,$db_user,$db_pass); 
				mysql_select_db($db_name, $connexion);   
			           
				$p1 = " CREATE TABLE $categorie (
		  		id tinyint(4) unsigned NOT NULL AUTO_INCREMENT,
		  		id_catalogue varchar(20) NOT NULL,
		  		designation varchar(50) NOT NULL DEFAULT '',
		  		reference varchar(20) NOT NULL,
		  		marque varchar(20) NOT NULL,
		  		fournisseur varchar(20) NOT NULL,
		  		garantie varchar(30) NOT NULL,
		  		dispo varchar(20) NOT NULL,
		  		prix_ttc decimal(7,2) NOT NULL,
		  		prix_ht decimal(7,2) NOT NULL,
		  		remise varchar(20) NOT NULL,
				tva decimal(5,2) NOT NULL,
		  		descriptif text NOT NULL,
		  		caracteristiques text NOT NULL,
		  		image varchar(20) NOT NULL,
		  		stock varchar(20) NOT NULL,
		  		status varchar(20) NOT NULL,
		  		remarques text NOT NULL,
		  		liens text NOT NULL,
		  		heure varchar(20) NOT NULL,
				date varchar(20) NOT NULL, "; 
		  	 	
		  	 	$p2 = " PRIMARY KEY (id) ) ";
			       
			        $requete = $p1 .$chaine .$p2; 	    
			        
			        //echo $requete;   
			       
				mysql_query($requete);	     
				
				$connexion = mysql_connect($db_host,$db_user,$db_pass); 
				mysql_select_db($db_name, $connexion);   
				$requete = "SELECT * FROM correspondance WHERE categorie = '$categorie' ";
				$result = mysql_query($requete);
				
				$cpt = 0;
				
				while($data = mysql_fetch_array($result))
			     	{	//echo  "+++ " .$data['filtre']; 
			       		$cpt = $cpt +1;
			     	}
				
						
				echo "Total : " .$cpt;
		
		
		}	
		
	   }	
	}	
		
?> 		


	<form action="" method="post">
	<input type="text" name="filename" /><br />
	<textarea name="text" rows="10" cols="50"></textarea><br /><br />
	<input type="Submit">
	</form>
	



<!--	
	<form action="" method="post">
	<input type="text" name="filename" /><br />
	<textarea cols='118' name="txtbox" rows='15' class='textbox'><?php echo $data; ?></textarea>
	<input type="Submit">
	</form>
-->