<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 activit&eacute;s</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 activit&eacute;s actives
	$sql = "select base_ref1activites.num_activite, base_ref1activites.nom, 
	               base_ref1activites.base 
			from base_ref1activites
			where base_ref1activites.actif = 'O'
		 	order by base_ref1activites.nom asc;";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die("Ne peut acc&eacute;der &agrave; la table ref1branche!<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>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("<center><table cellspacing=0 cellpadding=0 width=750px border=0>");
	print("<tr><td bgcolor=#E7EFCE>");

	print ("<b>Liste des activit&eacute;s actives<BR></b>");

	print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"ref1branche_o\">
			<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
			<input type=\"hidden\" name=\"etape\" value=1>
			<select name=\"num_activite\">");

	while ($row = mysqli_fetch_row($result))
	{
		for ($i = 0; $i < (($___mysqli_tmp = mysqli_num_fields($result)) ? $___mysqli_tmp : false); $i++)
		{
			print("<option value=\"");
			print(htmlspecialchars($row[$i++]));
			print("\">");
			printf("%s,  %s", htmlspecialchars ($row[$i++]), htmlspecialchars ($row[$i]));
			print("</option><br>");
		}
	}

	((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);

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

	print ("Cette section permet de s&eacute;lectionner une activit&eacute; 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("<BR><BR></td></tr><tr><td bgcolor=#D6E4AC>");

	// liste des activit&eacute;s inactives

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

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die("Ne peut acc&eacute;der &agrave; la table ref1branche!<br>".$sql);

	$num = mysqli_num_rows($result);

	print ("<b>Liste des activit&eacute;s inactives</b><br>");

	if (!empty ($num))
	{
		print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"ref1branche_n\">
				<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
				<input type=\"hidden\" name=\"etape\" value=1>
				<select name=\"num_activite\">");

		while ($row = mysqli_fetch_row($result))
		{
			for ($i = 0; $i < (($___mysqli_tmp = mysqli_num_fields($result)) ? $___mysqli_tmp : false); $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 activit&eacute; 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 activit&eacute; inactive.");
	}
	
	((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);

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

// pour des nouvelles activit&eacute;es

    print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\">");
	print ("<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">");
	print ("<input type=\"hidden\" name=\"etape\" value=2>");
	print ("<input id=\"frm_nouvelle_activite\" type=\"submit\" value=\"Nouvelle activit&eacute;\">");
	print ("</form></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 ("<hr>");
	print ("<div align=\"center\">");
    print ("<form action=\"choix-ref1.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\">");
	print ("<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">");
	print ("<input id=\"frm_menu\" type=\"submit\" value=\"Retour au menu des constantes\">");
	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_ref1activites 
		if (!empty ($nom))
			{
			$sql = "update base_ref1activites
				set base_ref1activites.nom = '$nom',
					base_ref1activites.mis_a_jour = now()
           	    WHERE base_ref1activites.num_activite = '$num_activite';";

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

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

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

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

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

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

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

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

			$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("ne peut faire la modification.<br>".$sql);
			}

		}

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

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

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

	// met la valeur dans les variables
	if ($num)
		{
		$i = 0;
		$row = mysqli_fetch_row($result);
		$nom = ($row[$i++]);
		$animation = ($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
	((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);

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

	
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>Modification de l'activit&eacute;</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 l'activit&eacute;:</b>");

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

	print("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_nom\"> 
		<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		<input type=\"hidden\" name=\"num_activite\" value=".$num_activite.">
		<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>");

	// animation

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

	print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_animation\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_activite\" value=".$num_activite.">
		    <input type=\"hidden\" name=\"mod\" value=O>
		    <input type=\"hidden\" name=\"etape\" value=1>");

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

	// base

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

	print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_base\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_activite\" value=".$num_activite.">
		    <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=#E7EFCE><b>R</b>ichelieu, <b>V</b>ille-Marie, <b>RV</b> les deux</td>");
	print("</div></td></tr><tr><td bgcolor=#D6E4AC>");

	// actif

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

	print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_actif\"> 
		    <input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_activite\" value=".$num_activite.">
		    <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=#D6E4AC><b>O</b>ui/<b>N</b>on)</td>");
	print("</div></td></tr><tr><td bgcolor=#E7EFCE>");

	// compte budgetaire

	print ("<b>Poste budgetaire:</b>");

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

	print("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"mod_compte_budget\"> 
		<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
		    <input type=\"hidden\" name=\"num_activite\" value=".$num_activite.">
		<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("</td><td bgcolor=#E7EFCE>");		

	print("</div></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>");
// Boutons pour les renvois vers d'autres pages
	print("<table border=0 width=100%>");
   		print("<td width=45% align=right>"); // afiche le bouton de retour &agrave; la gestion des activit&eacute;s
		print("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");
	   			print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	   			print("<input type=hidden name=etape value=0>");
	   			print("<input type=submit name=back value=\"Gestion des activit&eacute;s\">");
	    print("</form>");
	print("</td>");
	print("<td width=5% align=center>"."&nbsp"."</td>"); //Espacement
	print("<td width=45% align=left>"); //Afficher le bouton de retour au menu principal.
		print("<form action=\"../menu-base.php?Bsession_id=".$_GET['Bsession_id']."\" method=post>");  
	   		 	print("<input type=hidden name=Bsession_id value=$Bsession_id>");
	  		 	print("<input type=submit name=back value=\"    Menu Principal    \">");
		print("</form>");
	print("</td>");
	print ("</table>");
	}

// *****************************************************************************************************
// &eacute;tape 2 *********************************************************************************************
// demande les info pour cr&eacute;er l'activit&eacute; **************************************************************
// *****************************************************************************************************
if ($etape == 2)
{

print("<center><table cellspacing=0 cellpadding=0 width=400px 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>Nouvelle activit&eacute;</b></td>");
print("<td bgcolor=white width=27px><img src=coin-haut-droit.gif></td>");
print("</tr></table>");	

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

	print ("<form action=\"ref1activites.php?Bsession_id=".$_GET['Bsession_id']."\" method=\"post\" name=\"creation\"> 
			<input type=\"hidden\" name=\"Bsession_id\" value=".$_GET['Bsession_id'].">
			<input type=\"hidden\" name=\"etape\" value=3>
			<input type=\"hidden\" name=\"pargestion\" value=".$pargestion.">");


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

	print ("<tr><td bgcolor=#D6E4AC>");
	print ("<b>Animation</b></td>");
	print ("<td bgcolor=#D6E4AC><div align=left>");
	print ("<select name=\"animation\">
			  <option value=\"O\">Oui</option>
			  <option value=\"N\">Non</option>
			</select>");
	print("</div></td></tr><tr>");

	print ("<tr><td bgcolor=#E7EFCE>");	
	print ("<b>Base</b></td>");
	print ("<td bgcolor=#E7EFCE><div align=left>");
	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><tr>");

	print("<tr><td bgcolor=#D6E4AC><b>");
	print ("<b>Poste budgetaire:</b>");
	print("</b></td><td bgcolor=#D6E4AC><div align=left>");
	print("<input type=\"text\" value=\"".$compte_budget."\" name=\"compte_budget\" align=\"top\" maxlength=\"4\" size=\"4\">");
	print("</div></td></tr>");

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

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

print("<center><table cellspacing=0 cellpadding=0 width=400px 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>");	
	
	
//	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_ref1activites 
		    set nom = '$nom',
			    animation = '$animation',
				base = '$base',
				actif = '$actif',
				compte_budget = $compte_budget,
				mis_a_jour = now();";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) 
		or die("Ne peut faire l'ajout!<br>".$sql);

	$sql = "select base_ref1activites.num_activite
			from base_ref1activites
			where base_ref1activites.nom = '$nom'
			  and base_ref1activites.animation = '$animation'
			  and base_ref1activites.base = '$base'
			  and base_ref1activites.compte_budget = '$compte_budget';";

	$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die("Ne peut lire dans ref1branche!<br>".$sql);
	$row = mysqli_fetch_row($result);
	$num_activite = ($row[0]);

	print("&nbsp;<p><center><table width=80%><tr><td><center>");

	
	
}
?>

</body>
</html>
