<?php
include("../config.php3");
include($BIB."bib_generic.php3");

################################
# SQL FUNCTIONS
################################
function insertPeriod($ShowTime, $EndTime, $rot)
{
	main_db_query ("INSERT INTO QPeriods (Name, ShowTime, EndTime, Number) ".
	"VALUES ('Period ".$ShowTime."', '".$ShowTime."', '".$EndTime."', '".$rot."' )");
}	

function selectContainers($ShowTime, $EndTime)
{
	return main_db_query("SELECT * FROM QContainers WHERE EndTime>='".$EndTime."' AND ShowTime<='".$ShowTime."'");
}

function selectCategories($ShowTime, $EndTime, $containerId)
{
	return main_db_query("SELECT CategoryId FROM QCategories WHERE Container='".$containerId."' AND ShowTime>='".$ShowTime."' AND EndTime<='".$EndTime."'");
}

function insertCategory($ShowTime, $EndTime, $containerId, $Name)
{
	$aDate = date("Y-m-d H:i:s", strtotime($EndTime) - 3600);
	
	$period = db_fetch_array(main_db_query("SELECT * FROM QPeriods WHERE ShowTime<'".$aDate."' AND EndTime>'".$aDate."'"));
	main_db_query ("INSERT INTO QCategories (Name, ShowTime, EndTime, Container, PeriodId) ".
	"VALUES ('".$Name." ".date("d/m", strtotime($aDate))."', '".$ShowTime."', '".$EndTime."', '".$containerId."', '".$period[Id]."')");
	
	echo "INSERT INTO QCategories (Name, ShowTime, EndTime, Container, PeriodId) ".
	"VALUES ('".$Name." ".date("d/m", strtotime($aDate))."', '".$ShowTime."', '".$EndTime."', '".$containerId."', '".$period[Id]."')";
}
################################


$SEC_PAR_PERIODE=60*60*24; // 1 jour


# pour un container donné, crée les catégories puis les motions adéquates
function build($nPeriods,$EndTime,$ShowTime,$Rotation)
{
	for ($i=0;$i<$nPeriods;$i++)
	{
		$rot = ($i%$Rotation)+1;
		
		insertPeriod($ShowTime[$i], $EndTime[$i], $rot);
		
		// on parcourt les containers
		//echo "Requ : "."SELECT * FROM Containers WHERE EndTime>='".$EndTime[$i]."' AND ShowTime<='".$ShowTime[$i]."'<br>";
		$ContainerRows = selectContainers($ShowTime[$i], $EndTime[$i]);
		
		echo "nb de containers ok : ".db_num_rows($ContainerRows)."<br>";
		while ($ContainerRow = db_fetch_array($ContainerRows))
		{
			echo "Cont : ".$ContainerRow[Name]."<br>";
			// et si une catégorie n'existe pas on la crée
			$Categories = selectCategories(date("Y-m-d H:i:s", strtotime($ShowTime[$i]) + $ContainerRow['ShowTimeOffset']), date("Y-m-d H:i:s", strtotime($ShowTime[$i]) + $ContainerRow['EndTimeOffset']), $ContainerRow['Id']);
			if (db_num_rows($Categories) == 0)
			{
				insertCategory(date("Y-m-d H:i:s", strtotime($ShowTime[$i]) + $ContainerRow['ShowTimeOffset']), date("Y-m-d H:i:s", strtotime($ShowTime[$i]) + $ContainerRow['EndTimeOffset']), $ContainerRow['Id'], $ContainerRow[Name]);
			}
		}
	}
	
	return "<br>Génération de ".$nPeriods." périodes terminée<br>";
}

#####################################
# MAIN                              #
#####################################

if (!isset($nPeriods))
	$nPeriods = 5;
if (!isset($PeriodLen))
	$PeriodLen = 7;

$arr = getdate();
if (!isset($minutes))
	$minutes = 30;//$arr[minutes];
if (!isset($hour))
	$hour = 0;//$arr[hours];
if (!isset($day))
	$day = $arr[mday];
if (!isset($month))
	$month = $arr[mon];
if (!isset($year))
	$year = $arr[year];

switch ($submit)
{

	case "Valider":
		# on place tous les EndTime / ShowTime dans un tableau
		for($r=0;$r<$nPeriods;$r++)
		{
			$rot = ($r%$Rotation)+1;
			
			$var = "ShowTime".$r;
			$ShowTime[$r] = $$var;

			$var = "EndTime".$r;
			$EndTime[$r] = $$var;
			
			echo "ShowTime:".$ShowTime[$r]." EndTime:".$EndTime[$r]." Rot:".$rot."<br>";
		}
		echo build($nPeriods,$EndTime,$ShowTime,$Rotation);
	break;

	case "Rafraichir":
		# quand on rafraichit on applique les règles liées aux dates
		$iDate = mktime($hour,$minutes,0,$month,$day,$year);
		for($r=0;$r<$nPeriods;$r++)
		{	
			$var = "ShowTime".$r;
			$$var = date("Y-m-d H:i:s",$iDate);
			
			$iDate += $SEC_PAR_PERIODE*$PeriodLen;
			
			$var = "EndTime".$r;
			$$var = date("Y-m-d H:i:s",$iDate);
		}
	break;
}
?>
<html>
<link rel="stylesheet" href="admin.css">
<body rightmargin=0>
<div align=center class=trTitle>Générer des périodes (Semaines)</div>
<br>

<table>
<form>
<tr>
	<td width=40%>Nombre de périodes:</td>
	<td><input type=text name=nPeriods value="<?php echo $nPeriods ?>"></td>
</tr>
<tr>
	<td>Durée d'une période (en jours):</td>
	<td><input type=text name=PeriodLen value="<?php echo $PeriodLen ?>"></td>
</tr>
<tr>
	<td>Rotation (m6_quiz, correspond au nombre de périodes pour constituer un tournoi. Si un tournoi = 4 semaine, on met 4):</td>
	<td><input type=text name=Rotation value="<?php echo $Rotation ?>"></td>
</tr>
<tr>
	<td>Date de commencement :</td>
	<td>
		année <input size=6 type=text name=year value="<?php echo $year ?>">
		mois <input size=2 type=text name=month value="<?php echo $month ?>">
		jour <input size=2 type=text name=day value="<?php echo $day ?>">
		heure <input size=2 type=text name=hour value="<?php echo $hour ?>">
		minutes <input size=2 type=text name=minutes value="<?php echo $minutes ?>">
	</td>
</tr>
<tr>
	<td><input type=submit name=submit value="Rafraichir"></td>
</tr>
</table>
<br><br>
<?php
echo "<table>\n";
for($r=0;$r<$nPeriods;$r++)
{
	$rot = ($r%$Rotation)+1;
	$var = "ShowTime".$r;
	echo "<tr>\n";
		echo "<td>\n";
			echo "période n°".($r+1)." (".$rot.") >> ouverture :";
		echo "</td>\n";
		echo "<td>\n";
			echo "<input type=text name='".$var;
			echo "' value='".$$var."'>";
		echo "</td>\n";
	echo "</tr>\n";
	
	$var = "EndTime".$r;
	echo "<tr>\n";
		echo "<td>\n";
			echo "période n°".($r+1)." >> <b>fermeture</b> :";
		echo "</td>\n";
		echo "<td>\n";
			echo "<input type=text name='".$var;
			echo "' value='".$$var."'>\n";
		echo "</td>\n";
	echo "</tr>\n";
	echo "<tr><td height=10></td></tr>";
}
echo "</table>\n";
?>
<input type=submit name=submit value="Valider">
</form>