<?php
#############################################################################
# add_categories.php3                                                       #
# ajout de categories                                                       #
# Orienté --> MATCHS                                               		    #
#############################################################################
include("../config.php3");
include($BIB."bib_generic.php3");
include($BIB."bib_curl.php3");
# constants
$ADD_CATEGORIES_PAGE = $ADMIN_URL. "add_categories.php3";

# si aucune date n'a été entrée,on affiche la date actuelle dans les champs texte
$current_european_date = date("Y-m-d H:i:s", my_local_time());
if (!isset($ShowTime))
	$ShowTime = $current_european_date;
if (!isset($EndTime))
	$EndTime = $current_european_date;


?>
<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Gestion des catégories</div>
<br>
<?php

switch($submit)
{
	case "Transférer les sélections":
		$squery = "SELECT * FROM Categories ORDER BY EndTime DESC";
		$rowsCategories=main_db_query($squery);
		while($rowCategory=db_fetch_array($rowsCategories))
		{
			if( $$rowCategory['CategoryId'] == 1 )
			{

				# traitement de la categorie
				$name = urlencode($rowCategory['Name']);
				$endTime = urlencode($rowCategory['EndTime']);
				$showTime = urlencode($rowCategory['ShowTime']);
				$image = urlencode($rowCategory['Image']);
				
				# traitement = 1 => traitement de la categorie chez EA;
				
				$paramCategoryURL = "traitement=".$TRAITEMENT['Category']."&categoryId=".$rowCategory['CategoryId']."&category=".$rowCategory['Category']."&name=".$name."&endTime=".$endTime."&showTime=".$showTime."&container=".$rowCategory['Container']."&image=".$image;
				
				$urlCategory = $url_admin_remote."add_categories.php3?".$paramCategoryURL;
				
				$sLogCategory = VG_file($urlCategory);
				
				echo "<b><div class=smallboldDark><font color=green>".$sLogCategory."</font></div></b><br />"."\n";
				# traitement des matchs de la categories
				$squeryMatchs = "SELECT * FROM Matchs WHERE Category = ".$rowCategory['CategoryId'];
				$rowsMatchs = main_db_query($squeryMatchs);
				/*foreach ($tabLogCategory as $lineCategory_num => $lineCategory)
				{
	  					echo "<b><font color=green>".htmlspecialchars($lineCategory)."</font></b><br />"."\n";
	  					
				}*/
				echo "<br><div class=smallboldDark><b>Matchs correspondants : </b></div>\n \n";
				
				while($rowMatch=db_fetch_array($rowsMatchs))
				{
					$date = urlencode($rowMatch['Date']);
					$team1 = urlencode($rowMatch['Team1']);
					$team2 = urlencode($rowMatch['Team2']);
					$showTime = urlencode($rowMatch['ShowTime']);
					
					
					$paramMatchURL = "traitement=".$TRAITEMENT['Match']."&matchId=".$rowMatch['MatchId']."&result=0&date=".$date."&team1=".$team1."&team2=".$team2."&showTime=".$showTime."&category=".$rowMatch['Category']."&etat=".$rowMatch['etat'];
					
					$urlMatch = $url_admin_remote."add_categories.php3?".$paramMatchURL;
					$sLogMatch = VG_file($urlMatch);
					
					echo "<div class=smallboldDark><font color=black>".$sLogMatch."</font></div><br />"; 
					
					/*foreach ($tabLogMatch as $lineMatch_num => $lineMatch)
	  				{
	  					echo "<font color=black>".htmlspecialchars($lineMatch)."</font><br />";
	  				
					}*/
				}
				echo "<hr>\n \n";
					
			}
			
		}
		echo "<b><div class=smallboldDark>GENERATION DES PRONOS DANS LA HOME </div></b><br>";
		$urlPronos = $url_admin_remote."pronosHome_generator.php3?generator=1";
		$sLogPronos = VG_file($urlPronos);
		echo $sLogPronos;
		echo "<hr>";
		echo "<b><div class=smallboldDark>GENERATION DES RESULTATS DANS LA HOME </div></b><br>";
		$urlResults = $url_admin_remote."resultsHome_generator.php3?generator=1";
		$sLogResults = VG_file($urlResults);
		echo $sLogResults;
	break;
	
	case "Valider":
		if ($CategoryId!=0)
		{
			# check si la questionId existe parmi les pari deja effectué
			if (db_num_rows(main_db_query("SELECT CategoryId FROM Categories WHERE CategoryId='".$CategoryId."'"))>0)
			{
				main_db_query("UPDATE Categories SET Category='".$Category."', Container='".$ContainerId."',Name='".$Name."',EndTime='".$EndTime."',ShowTime='".$ShowTime."', Image='".$Image."' WHERE CategoryId='".$CategoryId."'");
			}
		}
		else
		{
			main_db_query("INSERT INTO Categories (Category,Container,Name,EndTime,ShowTime, Image) VALUES ('".$Category."','".$ContainerId."','".$Name."','".$EndTime."','".$ShowTime."', '".$Image."')");
		}
		break;
	
	case "Infos":
		$row = db_fetch_array(main_db_query("SELECT * FROM Categories WHERE CategoryId='".$CategoryId."'"));
		$Category=$row[Category];
		$ContainerId = $row[Container];
		$Name=$row[Name];
		$ShowTime=$row[ShowTime];
		$EndTime=$row[EndTime];
		$Image = $row[Image];
		break;
	
	case "Supprimer":
		# on verifie d'abord qu'il n'y a pas de matchs liés à cette categorie
		$row1 = db_fetch_array(main_db_query("SELECT MatchId FROM Matchs WHERE Category='".$CategoryId."'"));
		$row2 = db_fetch_array(main_db_query("SELECT CategoryId FROM Categories WHERE Category='".$CategoryId."'"));
		
		if ($row1 or $row2)
			echo "<font color=red><b>Impossible à supprimer !!! Cette categorie contient des matchs (ou des catégories) !</b></font><br><br><br>";
		else
			main_db_query("DELETE FROM Categories WHERE CategoryId='".$CategoryId."'");
		break;

}
?>
<table>
<form method="post" action="<?php echo $ADD_CATEGORIES_PAGE ?>" name="main">
		<input type=hidden name="modif" value="1">
		<tr>
			<td>
				Categorie N° :
			</td>
			<td>
				<input type="text" name="CategoryId" value="<?php echo $CategoryId ?>"> (0 pour ajouter une catégorie)
			</td>
		</tr>
		<tr>
			<td>
				Categorie père :
			</td>
			<td>
				<input type="text" name="Category" value="<?php echo $Category ?>"> (0 pour pas de père)
			</td>
		</tr>
		<tr>
			<td>
				Container :
			</td>
			<td>
				<select name="ContainerId">
				<?php
					$squery = "SELECT * FROM Containers WHERE EndTime>'$current_european_date'";
					$containers = main_db_query($squery);
					//debug($squery);
					//asort($containers);
					while($container = db_fetch_array($containers))
					{
						if ($container[Id]==$ContainerId)
							echo "<option value=$container[Id] selected>".$container[Name];
						else
							echo "<option value=$container[Id]>".$container[Name];
					}
					echo "</select>";
				?>
			</td>
		</tr>
		<tr>
			<td>
				Nom :
			</td>
			<td>
				<input type="text" name="Name" value="<?php echo $Name ?>">
			</td>
		</tr>
		<tr>
			<td>
				Date d'affichage :
			</td>
			<td>
				<input type="text" name="ShowTime" value="<?php echo $ShowTime ?>"> AAAA-MM-JJ HH:MM:SS
			</td>
		</tr>
		<tr>
			<td>
				<b>Date limite :</b>
			</td>
			<td>
				<input type="text" name="EndTime" value="<?php echo $EndTime ?>"> AAAA-MM-JJ HH:MM:SS
			</td>
		</tr>
		<tr>
			<td>
				Image :
			</td>
			<td>
			<select name="Image">
			<?php
				# contenu du répertoire
				$iconesDir = "IconesCategories/";
				$fichiers = dir($GAME_DIR."Images/".$iconesDir);
				$i=0;
				while ($fichier=$fichiers->read())
				{
					if ($fichier!="." && $fichier!="..")
					{
						$liste[$i]=$fichier;
						$i++;
					}
				}
				$imax=$i;
				sort($liste);
				for ($i=0;$i<$imax;$i++)
				{
					echo "<option value=".$iconesDir.$liste[$i];
					if (($iconesDir.$liste[$i])==$Image)
						echo " selected";
					echo ">".$iconesDir.$liste[$i];
					echo "</option>";
				}
			?>
			</select>

			</td>
		</tr>
</table>
<br><br>
    <input type=submit name=submit value="Infos">&nbsp;&nbsp;&nbsp;
    <input type=submit name=submit value="Valider">&nbsp;&nbsp;&nbsp;
    <input type=submit name=submit value="Supprimer"><br>
	</form>
<b>Catégories:</b>
<br>N : Categorie N°, P : Categorie père, passer le pointeur sur l'image pour avoir son nom...

<form method="post" action="<?php echo $ADD_CATEGORIES_PAGE; ?>" name="transfertEA">
<table border=1>
<tr>
<td colspan=8 align=right>
<select name="url_admin_remote">
	<option value="http://www.jeudepronostic.com/Adm/">EA</option>
	<option value="http://www.pronostics.fc.banquepopulaire.fr/Adm/">Banque populaire</option>
	<option value="http://m6lejeu.viralgames.com/Adm/">M6 interne</option>
	<option value="http://sfrcdm06.viralgames.com/Adm/">SFR interne</option>
</select>
<input type=submit name=submit value="Transférer les sélections">
</td>
</tr>
<tr class=trTitle>
		<td>N°</td>
		<td>P</td>
		<td>Container</td>
		<td>Nom</td>			
		<td>Date d'affichage</td>
		<td>Date fermeture</td>
		<td>Image</td>
		<td>Sélection pour EA</td>
	</tr>
	
<?php
$rows=main_db_query("SELECT * FROM Containers");
while($row=db_fetch_array($rows))
	$tabContainers[$row['Id']] = $row['Name'];
	
$rows=main_db_query("SELECT * FROM Categories  ORDER BY EndTime DESC");
while($row=db_fetch_array($rows))
{
	if (time_diff_now($row[ShowTime]) > 0 and time_diff_now($row[EndTime]) < 0)
	{
		echo "<tr bgcolor=yellow>";
		$disabled = "";
	}
	elseif($row['EndTime'] < $current_european_date)
	{
		echo "<tr bgcolor=gainsboro>";
		# si la categorie est passee, on bloque la selection permettant le trandfert chez EA
		$disabled = "DISABLED";
	}
	else
	{
		echo "<tr bgcolor=beige> ";	
		$disabled = "";
	}
		

	echo "<td>".$row[CategoryId]."</td>";
	echo "<td>".$row[Category]."</td>";
	echo "<td>".$tabContainers[$row[Container]]."</td>";
	echo "<td>".stripslashes($row[Name])."</td>";
	echo "<td>".$row[ShowTime]."</td>";
	echo "<td><b>".$row[EndTime]."</b></td>";
	echo "<td><img alt=\"".$row[Image]."\" src='".$IMAGES_DIR.$row[Image]."'></td>";
	echo "<td align=center><INPUT ".$disabled." TYPE=\"CHECKBOX\" NAME=\"".$row[CategoryId]."\" VALUE=\"1\"><BR></td>";
	echo "</tr>\n";
}
    ?>
</table>
</form>


</body>
</html>