#!/usr/local/bin/php
<?php
/*#!/usr/local/bin/php*/

	include('database.php');
	DB_connexion(); 

	date_default_timezone_set('Europe/Paris');

	//$fichier = '/homez.65/analogde/www/Massage/Backup/auto_db-backup-'.$infos.'.sql';
	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");
	
	$jour = date('d-m-Y');
	$heure = date("H_i_s");
	$infos = $jour.'-'.$heure;
	$fichier = "test____backup_auto_new-" .$infos .".txt";      //////// modif le 30 Juin 2018        aaa
	$chemin = '/homez.65/analogde/www/WORKAREA/WEBSITE/PROD/pipo';
	//DB_to_file($chemin, $fichier);
	//$fp = fopen("oto", 'a+'); 
	//fputs ($fp, "bkabka");
	//fclose($fp);

	
	chdir($chemin);
	
	DB_to_file($fichier);
	
	//$fichier = "backup_bis-" .$infos .".txt"; 
	//$chemin = 'homez.65/analogde/www/WORKAREA/WEBSITE/PROD/backup';
	//DB_to_file($chemin, $fichier);
	//$fp = fopen("oto", 'a+'); 
	//fputs ($fp, "bkabka");
	//fclose($fp);

	
	//chdir($chemin);
	
	//DB_to_file($fichier);
	

function DB_to_file($fichier)
{
				//chdir($chemin);
				
				$sql = "SELECT * FROM parties_new ORDER by date asc"; 
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
							
				while($row = mysql_fetch_assoc($results))
				{
					$blanc = $row['w_name'];
					$noir = $row['b_name'];
					$date = $row['date'];
					$resultat = $row['result'];
					$date_partie = date('d/m/Y', $date);
					
					if($resultat == 1)
					{
						$_score_blanc = 1;
						$_score_noir = 0;
					}
					if($resultat == 2)
					{
						$_score_blanc = 0;
						$_score_noir = 1;
					}
					if($resultat == 3)
					{
						$_score_blanc = 0.5;
						$_score_noir = 0.5;
					}
				
					$table_csv[0] = $date_partie;
					$table_csv[1] = $blanc;
					$table_csv[2] = $noir;
					$table_csv[3] = $_score_blanc;
					$table_csv[4] = $_score_noir;

					$table_fichier[] = $table_csv;
				}
				
				//echo getcwd();
				
										
				$fp = fopen($fichier, 'a+'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);
				
				//echo "Done";
				
			
}
	
	
?>
