<?php
// $Id: export.class.php,v 1.1 2008-11-24 11:13:41 fraynaud Exp $

class Export
{
	var $db;
	
	function Export (&$db)
	{
		$this -> db = $db;
	}
	
	function FromResultatToResultatElection ($election)
	{
		$tab_bureaux = $this -> InfoBureaux ($election);
		$tab_candidats = $this -> InfoCandidats ($election);
		$nb_candidat = count ($tab_candidats);
		$tab = $this -> FillResultat ($election, $nb_candidat, $tab_bureaux, $tab_candidats);
		$resultat = $this -> Merge ($election, $tab, $tab_bureaux);
		$this -> ToCsv ($resultat, "../tmp/".$election.".csv", ";");

	}
	function InfoBureaux ($election)
	{
		$sql = "select bureau from resultat where election = '".$election."'";
		$res = $this -> db -> query ($sql);
		if (DB :: isError ($res))
			die ($res -> getMessage ()." ".$sql);
		$tmp = array ();
		while ($row =& $res -> fetchRow (DB_FETCHMODE_ASSOC))
			array_push ($tmp, $row);
		$tab = array ();
		foreach ($tmp as $bureau)
		{
			$sql = "select bureau_libelle, canton from bureau where bureau = '".$bureau ['bureau']."'";
			$res = $this -> db -> query ($sql);
			if (DB :: isError ($res))
				die ($res -> getMessage ()." ".$sql);
			$row =& $res -> fetchRow (DB_FETCHMODE_ASSOC);
			$test = array_merge ($bureau, $row);
			$tab [$bureau ['bureau']] = $test;
		}
		return $tab;
	}
	
	function InfoCandidats ($election)
	{
		$sql = "select election_candidat_ordre, candidat from election_candidat where election = '".$election."'";
		$res = $this -> db -> query ($sql);
		if (DB :: isError ($res))
			die ($res -> getMessage ()." ".$sql);
		$tmp = array ();
		while ($row =& $res -> fetchRow (DB_FETCHMODE_ASSOC))
		{
			if ($row ['candidat'] >0)
				array_push ($tmp, $row);
		}
		$tab = array ();
		foreach ($tmp as $candidat)
		{
			$sql = "select candidat_nom, candidat_prenom, parti, groupe from candidat where candidat = '".$candidat ['candidat']."'";
			$res = $this -> db -> query ($sql);
			if (DB :: isError ($res))
				die ($res -> getMessage ()." ".$sql);
			$row =& $res -> fetchRow (DB_FETCHMODE_ASSOC);
			$test = array_merge ($candidat, $row, array ('resultat' => "", 'taux' => ""));
			$tab [$candidat ['election_candidat_ordre']] = $test;
		}
		return $tab;
	}
	
	function FillResultat ($election, $nb_candidat, $tab_bureaux, $tab_candidats)
	{
		$tab = array ();
		foreach ($tab_bureaux as $bureaux)
		{
			$tab [$bureaux ['bureau']] = array_merge ($bureaux, array (0 => $tab_candidats));
		}
		foreach ($tab as $bureau)
		{
			$sql = "select exprime";
			for ($i = 1; $i <= $nb_candidat; $i++)
				$sql .= ", candidat".$i;
			$sql .= " from resultat where election = '".$election."' and bureau = '".$bureau ['bureau']."'";
			$res = $this -> db -> query ($sql);
			if (DB :: isError ($res))
				die ($res -> getMessage ()." ".$sql);
			while ($row =& $res -> fetchRow (DB_FETCHMODE_ASSOC))
			{
				for ($i = 1; $i <= $nb_candidat; $i++)
				{
					$tab [$bureau['bureau']][0][$i]['resultat'] = $row ['candidat'.$i.''];
					if ($row['exprime'] != 0)
						$tab [$bureau['bureau']][0][$i]['taux'] = ($row ['candidat'.$i])/$row['exprime'];
					else
						$tab [$bureau['bureau']][0][$i]['taux'] = 0;
				}
			}
		}
		return $tab;
	}
	
	function Merge ($election, $tab, $tab_bureaux)
	{
		$tmp = array ();
		foreach ($tab as $elem)
		{
			foreach ($elem [0] as $test)
				array_push ($tmp, array_merge (array ('election' => $election), $tab_bureaux[$elem['bureau']], $test));
		}
		return $tmp;
	}
	
	function ToCsv ($resultat, $file, $separation)
	{
		$pfic = fopen ($file, "w");
		if (isset ($resultat [0]))
		{
			$string = "";
			foreach ($resultat [0] as $key =>$titre)
				$string .= $key.$separation;
			$string .= "\n";
			fwrite ($pfic, $string);
		}
		foreach ($resultat as $ligne)
		{
			$string = "";
			foreach ($ligne as $elem)
			{
				$string .= $elem.$separation;
			}
			$string .= "\n";
			fwrite ($pfic, $string);
		}
		fclose ($pfic);
	}
}
?>