<html>
<!--    cibl&eacute; par:  choix-ref1.php

		cible:  	../menu.php
 -->

<!-- param&egrave;tre &eacute;mis:        -session_id         le num&eacute;ro de la session

	 param&egrave;tre re&ccedil;evable :  -session_id         le num&eacute;ro de la session
 -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Gestion des branches</title>
</head>
<body>

<?php 

require("../outils/gui.php");
require("../outils/mise_en_page.php");
require("../outils/session.php");

//V&eacute;rifier si la session est valide.
$Bsession_id = $_GET["Bsession_id"];
verifierSession($Bsession_id);

//V&eacute;rifier les droits de l'utilisateur.
$privilege_base = lirePrivilege($Bsession_id);
if ($privilege_base != 2) 
	die("Vous n'avez pas les droits requis!");


// *****************************************************************************************************
// &eacute;tape 0 *********************************************************************************************
// menu de base ****************************************************************************************
// *****************************************************************************************************

if (empty ($etape))
{
	// liste des cafeteria actives
	$sql = "select base_ref1cafeteria.num_cafeteria, base_ref1cafeteria.nom, 
	               base_ref1cafeteria.base 
			from base_ref1cafeteria
			where base_ref1cafeteria.actif = 'O'
		 	order by base_ref1cafeteria.nom asc;";

	$result = mysql_query($sql) 
		or die("Ne peut acc&eacute;der &agrave; la table base_ref1cafeteria!<br>".$sql);


print("<center><table cellspacing=0 cellpadding=0 width=750px border=0>");
print("<tr><td bgcolor=white width=27px><img src=coin-haut-gauche.gif></td>");
print("<td bgcolor=#636300><center><font face=Arial,Helvetica size=3 color=white><b>Gestion de la caf&eacute;t&eacute;ria</b></td>");
print("<td bgcolor=white width=27px><img src=coin-haut-droit.gif></td>");
print("</tr></table>");	

print("<center><table cellspacing=0 cellpadding=0 width=750px border=0>");

	print ("<tr><td bgcolor=#E7EFCE><b>Liste des Caf&eacute;t&eacute;ria actives</b><BR>");

	print ("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"ref1cafeteria_o\">
			<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
			<input type=\"hidden\" name=\"etape\" value=1>
			<select name=\"num_cafeteria\">");

	while ($row = mysql_fetch_row ($result))
	{
		for ($i = 0; $i < mysql_num_fields ($result); $i++)
		{
			print("<option value=\"");
			print(htmlspecialchars($row[$i++]));
			print("\">");
			printf("%s,  %s", htmlspecialchars ($row[$i++]), htmlspecialchars ($row[$i]));
			print("</option><br>");
		}
	}

	mysql_free_result($result);

	print ("</select><input type=\"submit\" value=\"Consulter\"><BR></form>");

	print ("Cette section permet de s&eacute;lectionner une caf&eacute;t&eacute;ria active dans le menu d&eacute;roulant et d'y apporter 
            des modifications. Chaque &eacute;l&eacute;ment du menu d&eacute;roulant se compose de son nom et de l'abr&eacute;viation de la base. 
			Une fois l'&eacute;l&eacute;ment s&eacute;lectionn&eacute;, cliquez sur \"Consulter\" afin de visualiser et modifier les donn&eacute;es.");

	print("</tr></td><tr><td bgcolor=#D6E4AC><BR>");

	// liste des cafeteria inactives

	$sql = "select base_ref1cafeteria.num_cafeteria, base_ref1cafeteria.nom, 
	               base_ref1cafeteria.base 
			from base_ref1cafeteria
			where base_ref1cafeteria.actif = 'N'
		 	order by base_ref1cafeteria.nom asc;";

	$result = mysql_query($sql) 
		or die("Ne peut acc&eacute;der &agrave; la table base_ref1cafeteria!<br>".$sql);

	$num = mysql_numrows($result);

	print ("<b>Liste des caf&eacute;t&eacute;ria inactives</b><br>");

	if (!empty ($num))
	{
		print ("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"ref1cafeteria_n\">
				<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
				<input type=\"hidden\" name=\"etape\" value=1>
				<select name=\"num_cafeteria\">");

		while ($row = mysql_fetch_row ($result))
		{
			for ($i = 0; $i < mysql_num_fields ($result); $i++)
			{
				print ("<option value=\"");
				printf ("%s", htmlspecialchars ($row[$i++]));
				print ("\">");
				printf ("%s, %s", htmlspecialchars ($row[$i++]), htmlspecialchars ($row[$i]));
				print ("</option><br>");
			}
		}

		print("</select><input type=\"submit\" value=\"Consulter\"></form><br>");

		print ("Cette section permet de s&eacute;lectionner une caf&eacute;t&eacute;ria inactive dans le menu d&eacute;roulant et d'y apporter 
    	        des modifications. Chaque &eacute;l&eacute;ment du menu d&eacute;roulant se compose de son nom et de l'abr&eacute;viation de la base. 
				Une fois l'&eacute;l&eacute;ment s&eacute;lectionn&eacute;, cliquez sur \"Consulter\" afin de visualiser et modifier les donn&eacute;es.");
	}
	else
	{
		print ("Il n'y a aucune caf&eacute;t&eacute;ria inactive.");
	}

	mysql_free_result ($result);

	print("</td></tr><tr><td bgcolor=#E7EFCE><BR><BR>");

// pour des nouvelles activit&eacute;es

    print ("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\">");
	print ("<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">");
	print ("<input type=\"hidden\" name=\"etape\" value=2>");
	print ("<input id=\"frm_nouvelle_activite\" type=\"submit\" value=\"Nouvelle caf&eacute;t&eacute;ria\">");
	print ("</form>");
	
	print("</td></tr></table>");
	
print("<center><table cellspacing=0 cellpadding=0 width=750px border=0>");
print("<tr><td bgcolor=white width=27px><img src=coin-bas-gauche.gif></td>");
print("<td bgcolor=#636300 width=746px></td>");
print("<td bgcolor=white width=27px><img src=coin-bas-droit.gif></td>");
print("</tr></table>");	



// bouton de rtour au menu
	print ("<div align=\"center\">");
    print ("<form action=\"choix-ref1.php?Bsession_id=".$Bsession_id."\" method=\"post\">");
	print ("<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">");
	print ("<input id=\"frm_menu\" type=\"submit\" value=\"Retour au menu\">");
	print ("</form></div>");

//	pageDebug("&Eacute;tape 0");
} 

// *****************************************************************************************************
// &eacute;tape 1 *********************************************************************************************
// &eacute;diter un secteur existant **************************************************************************
// *****************************************************************************************************
if ($etape == 1)
	{
	// faire les modifications dans la table base_ref1activite *****************************************
	if (!empty ($mod))
		{
		// modifier le champ nom dans base_ref1cafeteria 
		if (!empty ($nom))
			{
			$sql = "update base_ref1cafeteria
				set base_ref1cafeteria.nom = '$nom',
					base_ref1cafeteria.mis_a_jour = now()
           	    WHERE base_ref1cafeteria.num_cafeteria = '$num_cafeteria';";

			$result = mysql_query($sql) 
				or die("ne peut faire la modification.<br>".$sql);
			}

		// modifier le champ base dans base_ref1cafeteria 
		if (!empty ($base))
			{
			$sql = "update base_ref1cafeteria
				set base_ref1cafeteria.base = '$base',
					base_ref1cafeteria.mis_a_jour = now()
           	    WHERE base_ref1cafeteria.num_cafeteria = '$num_cafeteria';";

			$result = mysql_query($sql) 
				or die("ne peut faire la modification.<br>".$sql);
			}

		// modifier le champ actif dans base_ref1cafeteria 
		if (!empty ($actif))
			{
			$sql = "update base_ref1cafeteria
				set base_ref1cafeteria.actif = '$actif',
					base_ref1cafeteria.mis_a_jour = now()
           	    WHERE base_ref1cafeteria.num_cafeteria = '$num_cafeteria';";

			$result = mysql_query($sql) 
				or die("ne peut faire la modification.<br>".$sql);
			}

		// modifier le champ compte_budget dans base_ref1cafeteria 
		if (!empty ($compte_budget))
			{
			$sql = "update base_ref1cafeteria
				set base_ref1cafeteria.compte_budget = '$compte_budget',
					base_ref1cafeteria.mis_a_jour = now()
           	    WHERE base_ref1cafeteria.num_cafeteria = '$num_cafeteria';";

			$result = mysql_query($sql) 
				or die("ne peut faire la modification.<br>".$sql);
			}

		}

	// remplir les variables ******************************************************************************
	$sql = "SELECT base_ref1cafeteria.nom, base_ref1cafeteria.base, 
				   base_ref1cafeteria.actif, base_ref1cafeteria.compte_budget  
    	    FROM base_ref1cafeteria
       	    WHERE base_ref1cafeteria.num_cafeteria = '$num_cafeteria';";

	// Ex&eacute;cute la requ&ecirc;te et mettre les r&eacute;sultats dans $result
	$result = mysql_query($sql) 
		or die ("ne peut lire les donn&eacute;es.<br>".$sql);

	// obtenir le nombre d'enregistrement. 0 si invalide, 1 si valide.
	$num = mysql_numrows($result);

	// met la valeur dans les variables
	if ($num)
		{
		$i = 0;
		$row = mysql_fetch_row ($result);
		$nom = ($row[$i++]);
		$base = ($row[$i++]);
		$actif = ($row[$i++]);
		$compte_budget = ($row[$i]);
		}
	else
		{
		die ("Il y a un probl&egrave;me dans la base de donn&eacute;es !");
		}

	// fait le m&eacute;nage des r&eacute;sultat en m&eacute;moire
	mysql_free_result ($result);

	// afficher les r&eacute;sultats ****************************************************************************	

	print ("<div align=\"center\"><u><b><font size=\"+2\">Modification de la Caf&eacute;t&eacute;ria</font></b></u></div>");

	
print("<center><table cellspacing=0 cellpadding=0 width=750px border=0>");
print("<tr><td bgcolor=white width=27px><img src=coin-haut-gauche.gif></td>");
print("<td bgcolor=#636300><center><font face=Arial,Helvetica size=3 color=white><b>Tables de r&eacute;f&eacute;rences</b></td>");
print("<td bgcolor=white width=27px><img src=coin-haut-droit.gif></td>");
print("</tr></table>");	

	print("<table width=\"750px\" cellpadding=0 cellspacing=0 border=\"0\"><tr><td bgcolor=#E7EFCE>");

	// nom

	print ("<b>Nom de la caf&eacute;t&eacute;ria:</b>");

	print("</td><td bgcolor=#E7EFCE><div align=right>");

	print("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"mod_nom\"> 
		<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
		<input type=\"hidden\" name=\"num_cafeteria\" value=".$num_cafeteria.">
		<input type=\"hidden\" name=\"mod\" value=O>
		<input type=\"hidden\" name=\"etape\" value=1>");

	print("<input type=\"text\" value=\"".$nom."\" name=\"nom\" align=\"top\" maxlength=\"40\" size=\"40\">
			<input type=\"image\" alt=\"nom\" src=\"bouton0.gif\" border=0 width=15 height=24>
			</form>");
	print ("<td bgcolor=#E7EFCE></td>");
	print("</div></td></tr><tr><td bgcolor=#D6E4AC>");

	// base

	print ("<b>Base:</b>");
	print("</td><td bgcolor=#D6E4AC><div align=right>");

	print ("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"mod_base\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
		    <input type=\"hidden\" name=\"num_cafeteria\" value=".$num_cafeteria.">
		    <input type=\"hidden\" name=\"mod\" value=O>
		    <input type=\"hidden\" name=\"etape\" value=1>");

	print ("<select name=\"base\">
			  <option value=\"".$base."\">".$base."</option>
			  <option value=\"R\">Richelieu</option>
			  <option value=\"V\">Ville-Marie</option>
			  <option value=\"RV\">Les deux</option>
		    </select>");
		
	print ("<input type=\"image\" alt=\"base\" src=\"bouton0.gif\" border=0 width=15 height=24></form>");
	print ("<td bgcolor=#D6E4AC><b>R</b>ichelieu, <b>V</b>ille-Marie, <b>RV</b> les deux</td>");
	print("</div></td></tr><tr><td bgcolor=#E7EFCE>");

	// actif

	print ("<b>Actif:</b>");
	print("</td><td bgcolor=#E7EFCE><div align=right>");

	print ("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"mod_actif\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
		    <input type=\"hidden\" name=\"num_cafeteria\" value=".$num_cafeteria.">
		    <input type=\"hidden\" name=\"mod\" value=O>
		    <input type=\"hidden\" name=\"etape\" value=1>");

	print ("<select name=\"actif\">
			  <option value=\"".$actif."\">".$actif."</option>
			  <option value=\"N\">Non</option>
			  <option value=\"O\">Oui</option>
		    </select>");
	print ("<input type=\"image\" alt=\"actif\" src=\"bouton0.gif\" border=0 width=15 height=24></form>");
	print ("<td bgcolor=#E7EFCE><b>O</b>ui/<b>N</b>on)</td>");
	print("</div></td></tr><tr><td bgcolor=#D6E4AC>");

	// compte budgetaire

	print ("<b>Poste budg&eacute;taire:</b>");

	print("</td><td bgcolor=#D6E4AC><div align=right>");

	print("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"mod_compte_budget\"> 
		<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
		    <input type=\"hidden\" name=\"num_cafeteria\" value=".$num_cafeteria.">
		<input type=\"hidden\" name=\"mod\" value=O>
		<input type=\"hidden\" name=\"etape\" value=1>");

	print("<input type=\"text\" value=\"".$compte_budget."\" name=\"compte_budget\" align=\"top\" maxlength=\"4\" size=\"4\">
			<input type=\"image\" alt=\"compte_budget\" src=\"bouton0.gif\" border=0 width=15 height=24>
			</form>");

	print("</div></td><td bgcolor=#D6E4AC></td></tr></table>");
	
print("<center><table cellspacing=0 cellpadding=0 width=750px border=0>");
print("<tr><td bgcolor=white width=27px><img src=coin-bas-gauche.gif></td>");
print("<td bgcolor=#636300 width=746px></td>");
print("<td bgcolor=white width=27px><img src=coin-bas-droit.gif></td>");
print("</tr></table>");	
	

//*****************************************************************************************************
// Bas de page 
//*****************************************************************************************************
	print ("<br>");
	print ("<hr><br>");

	}

// *****************************************************************************************************
// &eacute;tape 2 *********************************************************************************************
// demande les info pour cr&eacute;er la branche **************************************************************
// *****************************************************************************************************
if ($etape == 2)
{
	print ("<div align=\"center\"><u><b><font size=\"+2\">Nouvelle caf&eacute;t&eacute;ria</font></b></u></div>");
	print("<p>");

	print ("<form action=\"ref1cafeteria.php?Bsession_id=".$Bsession_id."\" method=\"post\" name=\"creation\"> 
			<input type=\"hidden\" name=\"Bsession_id\" value=".$Bsession_id.">
			<input type=\"hidden\" name=\"etape\" value=3>");

	print("<table width=350>");

	print("<tr><td><b>");
	print ("<b>Nom</b>");
	print("</b></td><td><div align=right>");
	print("<input type=\"text\" value=\"".$nom."\" name=\"nom\" align=\"top\" maxlength=\"40\" size=\"40\">");
	print("</div></td></tr>");


	print ("<tr><td>");	
	print ("<b>Base</b></td>");
	print ("<td><div align=right>");
	print ("<select name=\"base\">
			  <option value=\"R\">Richelieu</option>
			  <option value=\"V\">Ville-Marie</option>
			  <option value=\"RV\">Les deux</option>
			</select>");
	print("</div></td></tr>");

	print("<tr><td><b>");
	print ("<b>Poste budg&eacute;taire:</b>");
	print("</b></td><td><div align=right>");
	print("<input type=\"text\" value=\"".$compte_budget."\" name=\"compte_budget\" align=\"top\" maxlength=\"4\" size=\"4\">");
	print("</div></td>");

	print ("<tr><td>");	
	print ("<b>Actif</b></td>");
	print ("<td><div align=right>");
	print ("<select name=\"actif\">
			  <option value=\"O\">Oui</option>
			  <option value=\"N\">Non</option>
			</select>");
	print("</div></td></tr>");
	
	print ("<td>&nbsp;</td><td><br>");
	print("<div align=right><input type='submit' value='Soumettre'></div>");

	//<input type=\"image\" alt=\"etape3\" src=\"bouton0.gif\" border=0 width=15 height=24>
	print("</td></tr></table></form>");

//	pageDebug("&Eacute;tape 2");
}

//FIXME: Un reload ajoute une seconde fois les informations !
// *****************************************************************************************************
// &eacute;tape 3 *********************************************************************************************
// cr&eacute;e le secteur dans la table ref1branche ***********************************************************
// *****************************************************************************************************
if ($etape == 3)
{
	print ("<div align=\"center\"><b>Activit&eacute; cr&eacute;&eacute;e avec succ&egrave;s</b></div>");
	print("<p>");

	// mettre les info dans la table ref1branche
	$sql = "insert into base_ref1cafeteria 
		    set nom = '$nom',
				base = '$base',
				actif = '$actif',
				compte_budget = $compte_budget,
				mis_a_jour = now();";

	$result = mysql_query($sql) 
		or die("Ne peut faire l'ajout!<br>".$sql);

	$sql = "select base_ref1cafeteria.num_cafeteria
			from base_ref1cafeteria
			where base_ref1cafeteria.nom = '$nom'
			  and base_ref1cafeteria.base = '$base'
			  and base_ref1cafeteria.compte_budget = '$compte_budget';";

	$result = mysql_query($sql) 
		or die("Ne peut lire dans ref1branche!<br>".$sql);
	$row = mysql_fetch_row ($result);
	$num_cafeteria = ($row[0]);


	
}
?>

</body>
</html>
