<?php
include("../config.php3");
include($BIB."bib_generic.php3");
include($ARCHIVES_DIR."AllFruits.inc");
$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)
{
	# recup des matchs
	$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");
	/*
	echo "SELECT * FROM Matchs ".
		" LEFT JOIN Categories ON Categories.CategoryId=Matchs.Category ".
		" WHERE ".
		" Date > '".$Start."' AND Date<'".$Stop."'".
		" ORDER BY Date";
	*/
	# on lit le template
	$tabIn = file($CALENDAR_TEMPLATE);
	
	# ouverture du calendar.inc pour ecriture
	$f_out = fopen($CALENDAR_INC,'w');
	
	# 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]);
		else
		{
			while($match=db_fetch_array($rows))
			{
				$strDate = date_mysql($match["Date"])." à ".time_mysql($match["Date"]);
				$aDate = $match["Date"];
				
/*
				if (abs(time_diff($last_date,$aDate))>$TIME_GROUP)
					$msg .= "<br>";
*/
				if ($last_category != $match[Name])
				{
					$msg = 
						"<tr><td colspan=5 class=smallBoldDark>&nbsp;</td></tr>";
					$msg .= 
						"<tr><td colspan=5 bgcolor=".$BG_COLOR_FOND_3." class=smallBoldDark>&nbsp;&nbsp;".$match[Name]."</td></tr>";
				}
				
				$msg .= "<tr>".
					"<td class=smallDark>&nbsp;</td>".
					"<td align=right class=smallDark>".
					$match[Team1]."&nbsp;".
					"</td><td align=center class=smallBoldDark><font color=".$BG_COLOR_FOND_10.">";

				if ($match[Result])
					$msg .= $match[Result];
				else
					$msg .= "-";
				 
				$msg .= "</font></td><td align=left class=smallDark>";
				 $msg .= "&nbsp;".$match[Team2]."</td>\n";
				 $msg .= "</td><td align=left class=smallDark>";
				 $msg .= " le ".$strDate."</td></tr>\n";
				$last_date = $aDate;
				$last_category = $match[Name];
				fwrite($f_out,$msg);
				$msg="";
			}
		}
	}
	fclose($f_out);
	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=Oui>
</form>
<?php
}
?>
</body>
</html>
