<?php
// données utiles pour dates :
$daybase1 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier ORDER BY date ASC LIMIT 1"));
$day1 = $rentree;
$dday = date("Y-m-d");
$day2 = date("Y-m-d");
$year = date("Y");
if(@$_GET['stat']=='periodes'){
	if(!isset($_SESSION['fully'])) {
		if($acadexist=="fnc") {
			$_POST['fullyear']=date('Y');
		}
		else if($acadexist=="civ") {
			$_POST['fullyear']=date('Y');
		}
		else {
			$_POST['fullyear']="20212022";
		}
		$_SESSION['fully'] = $_POST['fullyear'];
	}
	else {
		if(!isset($_POST['fullyear'])) {
			$_POST['fullyear'] = $_SESSION['fully'];
		}
	}
	include('calfullyear.php');
}

$academie = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab WHERE academie <> '' OR pays <> ''");
$etab_nb = mysqli_num_rows($academie);
if($etab_nb!=0) {
	$acad_info = mysqli_fetch_assoc($academie);
	if($acad_info['pays']=='fra') { $acadexist = $acad_info['academie']; }
	else { $acadexist = $acad_info['pays']; }
}
else { $acadexist = ""; }

if(!isset($_GET['stat'])) { $_GET['stat']=''; }

if(isset($_POST['newfiltres'])) {
	if(@$_POST['fullyear']!="") {
		include('calfullyear.php');
	}
	else {
		$_SESSION['fully']="";
		$_SESSION['datepickstats1'] = $_POST['datepickstats1'];
		if(date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats1'])))<$daybase1['date']) {
			$_SESSION['datepickstats1']= date('d/m/Y', strtotime(str_replace("/","-",$daybase1['date'])));
		}
		$_SESSION['datepickstats2'] = $_POST['datepickstats2'];
		if($_GET['stat']=="annees") {
			if(substr($_POST['datepickstats1'],3,2).'/'.substr($_POST['datepickstats1'],0,2)<'12/31'&&substr($_POST['datepickstats1'],3,2).'/'.substr($_POST['datepickstats1'],0,2)>'08/01') {
				$_SESSION['datepickstats1'] = $_POST['datepickstats1'].'/'.$year;
			}
			else { $_SESSION['datepickstats1'] = $_POST['datepickstats1'].'/'.($year+1); }
			if(substr($_POST['datepickstats2'],3,2).'/'.substr($_POST['datepickstats2'],0,2)<'12/31'&&substr($_POST['datepickstats2'],3,2).'/'.substr($_POST['datepickstats2'],0,2)>'08/01') {
				$_SESSION['datepickstats2'] = $_POST['datepickstats2'].'/'.$year;
			}
			else { $_SESSION['datepickstats2'] = $_POST['datepickstats2'].'/'.($year+1); }
		}
		if($_POST['datepickstats1']=="") { $_SESSION['datepickstats1'] = $rentree; }
		if($_POST['datepickstats2']=="") { $_SESSION['datepickstats2'] = $dday; }
	}
	$day1 = $_SESSION['datepickstats1'];
	$day2 = $_SESSION['datepickstats2'];
	$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats1'])));
	$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats2'])));
	$_SESSION['datestats1'] = date('d/m/Y', strtotime(str_replace("/","-",$_SESSION['datepickstats1'])));
	$_SESSION['datestats2'] = date('d/m/Y', strtotime(str_replace("/","-",$_SESSION['datepickstats2'])));
}
else if(isset($_POST['defaultdates'])) {
	if($rentree<$daybase1['date']) {
		$rentree=$daybase1['date'];
	}
	$_SESSION['datepickstats1'] = date('d/m/Y', strtotime(str_replace("/","-",$rentree)));
	$_SESSION['datepickstats2'] = date('d/m/Y', strtotime(str_replace("/","-",$dday)));
	$day1 = $_SESSION['datepickstats1'];
	$day2 = $_SESSION['datepickstats2'];
	$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats1'])));
	$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats2'])));
	$_SESSION['datestats1'] = date('d/m/Y', strtotime(str_replace("/","-",$_SESSION['datepickstats1'])));
	$_SESSION['datestats2'] = date('d/m/Y', strtotime(str_replace("/","-",$_SESSION['datepickstats2'])));
}
else if(isset($_SESSION['datestats1'])&&isset($_SESSION['datestats2'])) {
	$day1 = $_SESSION['datestats1'];
	$day2 = $_SESSION['datestats2'];
	$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datestats1'])));
	$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datestats2'])));
	$_SESSION['datestats1'] = date('d/m/Y', strtotime(str_replace("/","-",$_SESSION['datestats1'])));
	$_SESSION['datestats2'] = date('d/m/Y', strtotime(str_replace("/","-",$_SESSION['datestats2'])));
}

if($day1<$daybase1['date']) {
	$day1=$daybase1['date'];
}

$dayP1 = strtotime($day1);
$dayname1 = date('D',$dayP1);
if($dayname1=="Mon") { 
	$jourP1 = date("Y-m-d", $dayP1);
}
else {
	$jourP1 = date("Y-m-d", strtotime("Previous Monday", $dayP1));
}
$jourP1date = new DateTime($jourP1);

$dayP2 = strtotime($day2);
$dayname2 = date('D',$dayP2);
if($dayname2=="Mon") {
	$jourP2to = strtotime(date("Y-m-d",$dayP2) . " +7 day");
	$jourP2last = strtotime(date("Y-m-d",$dayP2) . " +6 day");
}
else {
	$jourP2to = strtotime(date("Y-m-d",strtotime("Previous Monday", $dayP2)) . " +7 day");
	$jourP2last = strtotime(date("Y-m-d",strtotime("Previous Monday", $dayP2)) . " +6 day");
}
$jourP2 = date('Y-m-d',$jourP2to);
$jourP2dim = date('Y-m-d',$jourP2last);
$jourP2date = new DateTime($jourP2);

$affday1=date('d/m/Y', strtotime(str_replace("/","-",$day1)));
$affday2=date('d/m/Y', strtotime(str_replace("/","-",$day2)));
$affpday1=date('d/m/Y', strtotime(str_replace("/","-",$jourP1)));
$affpday2=date('d/m/Y', strtotime(str_replace("/","-",$jourP2dim)));

// Pour les stats en évolutions
if(isset($_POST['nbyear'])){
	$_SESSION['nb_year'] = $_POST['nbyear'];
}
else if(!isset($_SESSION['nb_year'])) { $_SESSION['nb_year'] = "2a"; }
$affday3=substr($day1, 8, 2).'/'.substr($day1, 5, 2);
$affday4=substr($day2, 8, 2).'/'.substr($day2, 5, 2);
$day3=substr($day1,5,2).'/'.substr($day1,8,2);
$day4=substr($day2,5,2).'/'.substr($day2,8,2);

// gestion des choix d'activités

$verif_activ= array();

if(!isset($_SESSION['filtre_activs'])) {
	$filtre_activs = "";
}
else {
	$filtre_activs = $_SESSION['filtre_activs'];
}
if(!isset($filtre_activs)) {
	$filtre_activs = "";
}
if(isset($_POST['newfiltres'])) {
	$activ = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv ORDER BY nom_activ ASC");
	$nb_activ = mysqli_num_rows($activ);
	while($activ_info = mysqli_fetch_assoc($activ)) {
		if(isset($_SESSION[''.$activ_info['code_activ'].''])) {
			unset($_SESSION[''.$activ_info['code_activ'].'']);
		}
		if(isset($_SESSION['activ'.$activ_info['code_activ'].''])) {
			unset($_SESSION['activ'.$activ_info['code_activ'].'']);
		}
	}
	$array = array_keys($_POST);
	$nb_activcheck=0;
	foreach($array as $activ_item) {
		if(substr($activ_item,0,5)=="activ") {
			$nb_activcheck++;
		}
	}
	if($nb_activcheck==0) {
		$filtre_activs = "";
	}
	else {
		$filtre_activs = " AND (";
		if($espopt['valeur']==1) { 
			$nb_espch=0;
			foreach($array as $activ_item) {
				if(substr($activ_item,0,6)=="espace") {
					$nb_espch++;
				}
			}
			$i=4+$nb_espch; 
		}
		else { $i=4; }
		foreach($array as $activ_item) {
			$verif_activ[] = $activ_item;
			$_SESSION[str_replace("_"," ",$activ_item)] = 1;
			if($activ_item!="nbyear"&&$activ_item!="newfiltres"&&$activ_item!="fullyear"&&$activ_item!="datepickstats1"&&$activ_item!="datepickstats2"&&$activ_item!="datepickstats3"&&$activ_item!="datepickstats4"&&$activ_item!="h1"&&$activ_item!="h2") {
				if(substr($activ_item,0,6)!="espace") {
					$filtre_activs .= " code_activ = '".str_replace("_"," ",substr($activ_item,5,4))."' ";
					if($i<count($array)-1) {
						$filtre_activs .= " OR ";
					}
					$i++;
				}
			}
		}
		$filtre_activs .= " ) ";
	}
	$_SESSION['filtre_activs']=$filtre_activs;
	$filtre_activsonly = $filtre_activs;
	$_SESSION['filtre_activsonly']=$filtre_activsonly;
}
if(!isset($filtre_activsonly)) { $filtre_activsonly = ""; }
$_SESSION['verif_activ'] = $verif_activ;
// VERIF DANS MODIFS $_SESSION['verif_activonly'] = $verif_activonly;
if(isset($_POST['defaultactivs'])) {
	$filtre_activs = "";
}
// gestion des choix d'espaces
if($espopt['valeur']==1) {
	$verif_espaces= array();

	if(!isset($_SESSION['filtre_espaces'])) {
		$filtre_espaces = "";
	}
	else {
		$filtre_espaces = $_SESSION['filtre_espaces'];
	}
	if(!isset($filtre_espaces)) {
		$filtre_espaces = "";
	}
	if(isset($_POST['newfiltres'])) {
		while($espace = mysqli_fetch_assoc($espaces)) {
			if(isset($_SESSION['espace0'])) {
				unset($_SESSION['espace0']);
			}
			if(isset($_SESSION['espace'.$espace['espace_id'].''])) {
				unset($_SESSION['espace'.$espace['espace_id'].'']);
			}
		}
		$esp_array = array_keys($_POST);
		$filtre_espaces = " AND (";
		$i=1;
		$nb_espch=0;
		foreach($esp_array as $esp_item) {
			if(substr($esp_item,0,6)=="espace") {
				$nb_espch++;
			}
		}
		foreach($esp_array as $esp_item) {
			$verif_espaces[] = $esp_item;
			$_SESSION[$esp_item] = 1;
			if(substr($esp_item,0,6)=="espace") {
				if($esp_item=="espace0") {
					$filtre_espaces .= " espace_id = '0' OR espace_id IS NULL ";
				}
				else {
					$filtre_espaces .= " espace_id = '".(substr($esp_item,6,1))."' ";
				}
				if($i<$nb_espch) {
					$filtre_espaces .= " OR ";
				}
				$i++;
			}
		}
		$filtre_espaces .= " ) ";
		$_SESSION['filtre_espaces']=$filtre_espaces;
	}
	$_SESSION['verif_espaces'] = $verif_espaces;
	if(isset($_POST['defaultespaces'])) {
		$filtre_espaces = "";
	}
	$filtre_activs .= ' '.$filtre_espaces;
	$_SESSION['filtre_activs']=$filtre_activs;
}
else { $filtre_espaces=""; }

// début du formulaire : 
echo '<div name="formulaire">';

echo '<form method="POST" name="dates" action="" class="padd3">';
echo '<table class="freqform">';
$checkfreq = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq");
$checkfreq_nb = mysqli_num_rows($checkfreq);
if($_GET['stat']!='annees'){ // pour les stats sauf évolutions
	include('./include/datepicker/dp2_past.php');
	echo '<tr><td colspan="2"><b>'.FREQJ_ORDER10.'</b></td></tr>';
	if($checkfreq_nb!=0) {
		echo '<tr><td colspan="2">'.STATS_YEAR9.' <div class="box"><select name="fullyear"><option></option>';
			for($i=($year+2);$i>($year-8);$i--) {
				$checkyear = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE date_freq >= '".$i."/08/01' AND date_freq <= '".($i+1)."/07/31'");
				$checkyear_nb = mysqli_num_rows($checkyear);
				if($checkyear_nb!=0) {
					if($acadexist=="fnc"||$acadexist=="civ") {
						echo '<option value="'.($i+1).'"';
						if(@$_SESSION['fully']==($i+1)) {
							echo ' selected ';
						}
						echo '>'.($i+1).'</option>';
					}
					else {
						echo '<option value="'.$i.($i+1).'"';
						if(@$_SESSION['fully']==$i.($i+1)) {
							echo ' selected ';
						}
						echo '>'.$i.'/'.($i+1).'</option>';
					}
				}
			}
		echo '</select></div>';
		if($_GET['stat']!='periodes'){
			echo ' OU ';
		}
		echo '</td></tr>';
	}
	if($_GET['stat']!='periodes'){
		echo '<tr><td> '.FROM2.' <input type="text" size="10" class="datepick1 inptxt" name="datepickstats1" value="'.$affday1.'"></td>
		<td> '.TO2.' <input type="text" size="10" class="datepick2 inptxt" name="datepickstats2" value="'.$affday2.'"></td></tr>';
	}
	else {
		//pour tromper le calcul des arrays :
		echo '<input type="hidden" name="h1" value="" /><input type="hidden" name="h2" value="" />';
	}
}
else if($_GET['stat']=='annees') { // pour les stats évolutions
	include('./include/datepicker/dp_period.php');
	echo '<tr><td colspan="2"><b>'.STATS_YEAR10.'</b></td></tr>';
	if($checkfreq_nb!=0) {
		echo '<tr><td colspan="2">'.STATS_YEAR5.' ';
		$j=0;
		for($i=($year+2);$i>($year-8);$i--) {
			$checkyear = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE date_freq >= '".$i."/08/01' AND date_freq <= '".($i+1)."/07/31'");
			$checkyear_nb = mysqli_num_rows($checkyear);
			if($checkyear_nb!=0) {
				$j++;
			}
		}
		if($j==2) {
			echo STATS_YEAR6;
			echo '<input type="hidden" name="nbyear" />';
		}
		else if($j>2) {
			echo '<div class="box"><select name="nbyear">';
			echo '<option value="2a';
			if(isset($_SESSION['nb_year'])) { 
				if($_SESSION['nb_year']=='2a') { echo ' selected '; }
			}
			echo '">2 '.STATS_YEAR7.'</option>';
			for($k=3;$k<$j+1;$k++) {
				echo '<option value="'.$k.'p"';
				if(isset($_SESSION['nb_year'])) { 
					if($_SESSION['nb_year']==''.$k.'p') { echo ' selected '; } 
				}
				echo '>'.($k-1).' '.STATS_YEAR8.'</option>';
				echo '<option value="'.$k.'a"';
				if(isset($_SESSION['nb_year'])) { 
					if($_SESSION['nb_year']==''.$k.'a') { echo ' selected '; } 
				}
				echo '>'.$k.' '.STATS_YEAR7.'</option>';
			}
			echo '</select></div>';
		}
		echo '</td></tr>';
	}
	echo '<tr><td> '.FROM2.' <input type="text" size="10" class="datepick1" name="datepickstats1" value="'.$affday3.'"></td>
	<td> '.TO2.' <input type="text" size="10" class="datepick2" name="datepickstats2" value="'.$affday4.'"></td></tr>';
}
if($_GET['stat']!="passages") {
	echo '<tr><td colspan="2"><b>'.STATS_ACT1.'</b></td></tr>';
	$activ = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv ORDER BY nom_activ ASC");
	$nb_activ = mysqli_num_rows($activ);
	if($nb_activ==1) {
		echo '<tr><td colspan="2">'.STATS_ACT2.'</td></tr>';
	}
	else {
		echo '<tr><td colspan="2">';
		while($activ_info=mysqli_fetch_assoc($activ)) {
			if($activ_info['code_activ']=="ZERO") { }
			else {
				if(trim($filtre_activs)==""||!isset($_SESSION['filtre_activs'])) {
					echo '<input type="checkbox" id="activ'.$activ_info['code_activ'].'" name="activ'.$activ_info['code_activ'].'" value="activ'.$activ_info['code_activ'].'" checked /><label for="activ'.$activ_info['code_activ'].'">'.$activ_info['nom_activ'].'</label> ';
				}
				else if(isset($nb_activcheck)&&$nb_activcheck==0) {
					echo '<input type="checkbox" id="activ'.$activ_info['code_activ'].'" name="activ'.$activ_info['code_activ'].'" value="activ'.$activ_info['code_activ'].'" checked /><label for="activ'.$activ_info['code_activ'].'">'.$activ_info['nom_activ'].'</label> ';
				}
				else {
					echo '<input type="checkbox" id="activ'.$activ_info['code_activ'].'" name="activ'.$activ_info['code_activ'].'" value="activ'.$activ_info['code_activ'].'"';
					if(isset($_SESSION['activ'.$activ_info['code_activ'].''])&&$_SESSION['activ'.$activ_info['code_activ'].'']==1){ echo 'checked '; }
					echo ' /><label for="activ'.$activ_info['code_activ'].'">'.$activ_info['nom_activ'].'</label> ';
				}
			}
		}
		echo '</td></tr>';
	}
}
if($espopt['valeur']==1) {
	echo '<tr><td colspan="2"><b>'.STATS_ESP1.'</b></td></tr>';
	$espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
	$nb_esp = mysqli_num_rows($espaces);
	if($nb_esp==0) {
		echo '<tr><td colspan="2">'.STATS_ESP2.'</td></tr>';
	}
	else {
		echo '<tr><td colspan="2">';
		if($filtre_espaces=="") { echo '<input type="checkbox" name="espace0" value="espace0" checked />'.$esp0['valeur'].' '; }
		else { echo '<input type="checkbox" id="espace0" name="espace0" value="espace0"'; if(isset($_SESSION['espace0'])&&$_SESSION['espace0']==1){ echo 'checked '; } echo ' /><label for="espace0">'.$esp0['valeur'].'</label> '; }
		while($espace=mysqli_fetch_assoc($espaces)) {
			if($filtre_espaces=="") {
				echo '<input type="checkbox" id="espace'.$espace['espace_id'].'" name="espace'.$espace['espace_id'].'" value="espace'.$espace['espace_id'].'" checked /><label for="espace'.$espace['espace_id'].'">'.$espace['espace_nom'].'</label> ';
			}
			else {
				echo '<input type="checkbox" id="espace'.$espace['espace_id'].'" name="espace'.$espace['espace_id'].'" value="espace'.$espace['espace_id'].'"';
				if(isset($_SESSION['espace'.$espace['espace_id'].''])&&$_SESSION['espace'.$espace['espace_id'].'']==1){ echo 'checked '; }
				echo ' /><label for="espace'.$espace['espace_id'].'">'.$espace['espace_nom'].'</label> ';
			}
		}
	}
	echo '</td></tr>';
}
echo '<tr><td colspan="4" class="right"><input class="bouton boutfreq" type="submit" value="'.STATS_REQUEST.'" name="newfiltres" /> <input class="bouton boutfreq" type="submit" value="'.STATS_ACT3.'" name="defaultactivs" />  <input class="bouton boutfreq" type="submit" value="'.STATS_DEFDATES.'" name="defaultdates" /></td></tr>';

echo '</table>
</form></div>';
// fin du formulaire //
?>
