<?php
include_once("../config.php3");
include_once($BIB."bib_generic.php3");
include_once($BIB."bib_drapeaux.php3");
$CALENDAR_INC = $ARCHIVES_DIR."calendar.inc";
$CALENDAR_TEMPLATE = "calendar_template.inc";
$TIME_GROUP = 120000;




# marqueur pour indiquer qu'il faut placer les matches à cet endroit
$MARKER = "__MATCHES__";
?>

<!--<html>
<head></head>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>-->
<div align=center class=trTitle>Génération du calendrier</div>
<br>

<?php
if ($go==1)
{
	
	$msgComplet = "<table width=98% align=center cellpadding=0 cellspacing=0 border=0>";
	
	$current_european_date = date("Y-m-d H:i:s", my_local_time());
	
	echo "Aujourd'hui nous sommes le: ".$current_european_date."<br>";
	
	$rowsPeriods = main_db_query("SELECT * FROM Tours WHERE  ShowTime < '".$current_european_date."' AND  EndTime > '".$current_european_date."' ");
	
	$rowPeriod = db_fetch_assoc($rowsPeriods);
	
	echo "La période en cours est: ".$rowPeriod['Name']." du ".date_mysql($rowPeriod['ShowTime'])." au ".date_mysql($rowPeriod['EndTime'])."<br>";
	
	# ligne de la session
	$msgComplet .= "<tr><td class=smallDark colspan=6 ><BR><b>".addslashes($rowPeriod['Name'])."</b> du ".date_mysql($rowPeriod['ShowTime'])." au ".date_mysql($rowPeriod['EndTime'])."</td></tr>";
	
	# recup des matchs
	
	$rows=main_db_query("SELECT * FROM Matchs ".
		" LEFT JOIN Categories ON Categories.CategoryId=Matchs.Category ".
		" WHERE ".
		" Date > '".$rowPeriod['ShowTime']."' AND Date<'".$rowPeriod['EndTime']."'".
		" ORDER BY Categories.ShowTime,Matchs.Category,Date");
	
	/*$rows=main_db_query("SELECT * FROM Matchs ".
		" LEFT JOIN Categories ON Categories.CategoryId=Matchs.Category ".
		" WHERE ".
		" Date > '".$Start."' AND Date<'".$Stop."'".
		" ORDER BY Matchs.Category,Date");*/

	# on lit le template
	$tabIn = file($CALENDAR_TEMPLATE);
	
	# on écrit déjà ce qu'il y a dans le template
	for($r=0;$r<count($tabIn);$r++)
	{
		if (chop($tabIn[$r])!=$MARKER)
		{
			//fwrite($f_out,$tabIn[$r]);
			$msg .= $tabIn[$r];
		}
		else
		{
			while($match=db_fetch_assoc($rows))
			{
				#$strDate = date_mysql($match['Date'])." à ".time_mysql($match['Date']);
				$strDate = date("d/m/y", strtotime($match['Date']))." à ".time_mysql($match['Date']);
				$aDate = $match['Date'];
	
				
				if ($last_category != $match['Name'])
				{
					$msg = 
						"<tr><td colspan=6 class=smallBoldDark>&nbsp;</td></tr>";
					$msg .= 
						"<tr><td colspan=6 bgcolor=".$BG_COLOR_FOND_3." class=smallBoldDark>&nbsp;&nbsp;".addslashes($match['Name'])."</td></tr>";
				}
				
				$msg .= "<tr>".
					"<td class=smallDark>&nbsp;</td>".
					"<td align=right class=smallDark>".
					addslashes($match[Team1])."&nbsp;".
					"</td><td align=center class=smallBoldDark><font color=".$BG_COLOR_FOND_10.">";

				if ($match[Result])
					$msg .= $match[Result];
				else
					$msg .= "-";

				$backSlash = "\\\"";
				
				$msg .= "</font></td><td align=left class=smallDark>";
				$msg .= "&nbsp;".addslashes($match[Team2])."</td>\n";
				$msg .= "<td align=left class=smallBoldDark><a href=\".$";
				$msg .= "sess->link3(\"$"."BET_PAGE\", \"MatchId=".$match['MatchId']."&ContainerId=".$match['Container']."&redirect_page=\".$"."REDIRECT_PAGE).";
				$msg .= "\">";
				$msg .= "<img src=\\";
				$msg .= $backSlash;
				$msg .= "$IMAGES_DIR";
				$msg .= "plus.gif\\";
				$msg .= $backSlash;
				$msg .= " align=absmiddle border=0></a></td>";
				$msg .= "<td align=center class=smallDark>";
				$msg .= " le ".$strDate."</td></tr>\n";
				$last_date = $aDate;
				$last_category = $match['Name'];
				//fwrite($f_out,$msg);
				$msgComplet .= $msg;
				
				$msg="";
			}
		}
	}
	//fclose($f_out);
	$msgComplet .= "</table>";

	//$msgComplet = addslashes($msgComplet);
	
	//$bla = "echo \"bla\"; ";
	$contenu = " echo \"$msgComplet\"; ";
	
	$rowsUpdate = main_db_query("UPDATE Exploitation SET Contenu='".$contenu."' WHERE Fichier='Calendrier'");
	echo "<font color=red>La page du calendrier a été générée</font>";
}
else
{
$Stop = "2008-01-01 00:00:00";
?>
<form>
<!--
<b>Date début : </b>
<br>
<input type="text" name="Start" value="<?php echo $Start ?>">AAAA-MM-JJ HH:MM:SS<br>
<br><br>
<b>Date fin : </b>
<br>
<input type="text" name="Stop" value="<?php echo $Stop ?>">AAAA-MM-JJ HH:MM:SS<br>
<br><br>
-->
<input type=hidden name=go value=1>
<input type=submit value="Génerer le calendrier">
</form>
<?php
}
?>
</body>
</html>
