<?php


	error_reporting(0);
	header( 'content-type: text/html; charset=utf-8' );


	echo "-------------------------------";
	echo "<br/>";
	echo "Post processing du fichier csv (parse) ...";
	echo "<br/>";
	
	$the_big_array = []; 
	
	$joueur = array();

	if (($handle = fopen("toto.csv", "r")) !== FALSE) 
	{
		/*while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
		{
			$num = count($data);
			echo "<p> $num champs à la ligne $row: <br /></p>\n";
			$row++;
        
			for ($c=0; $c < $num; $c++) 
			{
				echo $data[$c] . "<br />\n";
			}
		}*/
    
	
		while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) 
		{
			$the_big_array[] = $data;		
		}
	
		fclose($handle);
	}


	for ($i = 0; $i < count($the_big_array); $i++) 
	{
		
		$pieces = explode("/", $the_big_array[$i][0] );
		
		//echo "<pre>";
		//var_dump($pieces);
		//echo "</pre>";
		
		if( count($pieces) != 3 )
		{
			echo "BUG";
			exit();
		}	
		else
		{
			
			
		
			
			
			$str_jour = $pieces[0];
			
//			echo "Valeur : " + $str_jour;
//			echo "<br/>";
			
//			echo strlen($str_jour);
//			echo "<br/>";
						
			if( strlen($str_jour) == 1)
			{
				$str_jour = "0" .$str_jour; 
			}				
			else
			{
				$str_jour = $str_jour; 
			}				
			
//			echo $str_jour;
//			echo "<br/>";
			
			
			if ($pieces[1] == "Jan") 
			{
				$str_mois = "01";
			} 
			elseif ($pieces[1] == "Feb") 
			{
				$str_mois = "02";
			} 
			elseif ($pieces[1] == "Mar") 
			{
				$str_mois = "03";
			}
			elseif ($pieces[1] == "Apr") 
			{
				$str_mois = "04";
			}
			elseif ($pieces[1] == "May") 
			{
				$str_mois = "05";
			}
			elseif ($pieces[1] == "Jun") 
			{
				$str_mois = "06";
			}
			elseif ($pieces[1] == "Jul") 
			{
				$str_mois = "07";
			}
			elseif ($pieces[1] == "Aug") 
			{
				$str_mois = "08";
			}
			elseif ($pieces[1] == "Sep") 
			{
				$str_mois = "09";
			}
			elseif ($pieces[1] == "Oct") 
			{
				$str_mois = "10";
			}
			elseif ($pieces[1] == "Nov") 
			{
				$str_mois = "11";
			}
			elseif ($pieces[1] == "Dec") 
			{
				$str_mois = "12";
			}
			
			$int_annee = intval($pieces[2]) + 2000;
			
			$str_annee = strval($int_annee);

		}			
		
		//echo $str_jour ."/" . $str_mois . "/" . $str_annee;
		//echo "<br/>";
		
		$the_big_array[$i][0] = $str_jour ."/" . $str_mois . "/" . $str_annee;
		$the_big_array[$i][1] = ucfirst($the_big_array[$i][1]);
		$the_big_array[$i][2] = ucfirst($the_big_array[$i][2]);
		
		$joueur[] = ucfirst($the_big_array[$i][1]);
		$joueur[] = ucfirst($the_big_array[$i][2]);
			
	}
	
	
	$sans_doublon = array_unique($joueur);
	$sans_doublon = array_values($sans_doublon);
	// ordonner par ordre alphabetique
	usort($sans_doublon, "strcasecmp");
	
	$nb_joueurs = count($sans_doublon);
	
	echo " --------- LISTE DES JOUEURS identifiés " .$nb_joueurs ." ---------------------" ."<br>";
	echo "<pre>";
	print_r($sans_doublon);
	echo "<br>";
	
	echo " Extraction des parties : " .count($the_big_array);
	echo "<br/>";

	
	echo "<pre>";
	var_dump($the_big_array);
	echo "</pre>";

	echo " Fin du traitement: Aucunes erreurs identifiées.  "; 
	echo "<br/>";

	//$file = fopen("student.txt","a");
	//fwrite($file,$address.PHP_EOL);
    //fclose($file);
    //echo "Adding student successfully";
	
	
	$fp = fopen('file.csv', 'w');

	$cpt = 1;

	foreach ($the_big_array as $fields) 
	{
		
		if( $cpt !=  count($the_big_array) )
		{
			$str_build = $fields[0] .";" .$fields[1] .";" .$fields[2] .";" .$fields[3] .";" .$fields[4] .";" .PHP_EOL;
			echo $str_build;
			echo "<br/>";
			fwrite($fp, $str_build);
		
		//fputcsv($fp, $fields);
		}
		else // last line 
		{
			$str_build = $fields[0] .";" .$fields[1] .";" .$fields[2] .";" .$fields[3] .";" .$fields[4] .";" ;
			echo $str_build;
			echo "<br/>";
			fwrite($fp, $str_build);
		}

		$cpt = $cpt + 1;		
	}

	fclose($fp);


?>