<?php
$daybase1 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier ORDER BY date ASC LIMIT 1"));
$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($acadexist=="La Réunion") { $wn = '33'; } 
else if($acadexist=="fnc") { $wn = '07'; } 
else if($acadexist=="civ") { $wn = '01'; } 
else if($acadexist=="swi") { $wn = '34'; }
else { $wn = '36'; }

$month = date("n");

if($acadexist=="fnc"||$acadexist=="civ") {
	$act_year = date("Y");
}
else {
	if($month<8&&$month>=1) {
		$act_year = (date("Y")-1).date("Y");
	}
	else {
		$act_year = date("Y").(date("Y")+1);
	}
}
if(isset($_POST['anneechoix'])) {	
	if($_POST['frag']=="actday"&&$_POST['fullyear']==$act_year) {
		$_SESSION['frag'] = $_POST['frag'];
	}
	else if(($_POST['frag']=="actday"&&$_POST['fullyear']!=$act_year)||$_POST['frag']=="allyea") { $_SESSION['frag']="allyea"; }
	else if(!isset($_SESSION['frag'])) { $_SESSION['frag']="allyea"; }
}
else if(!isset($_SESSION['frag'])) { 
	$_SESSION['frag']="actday"; 
}
// formulaire pour l'année
if(isset($_POST['anneechoix'])) {
	if($_POST['fullyear']==''||$_POST['fullyear']=='/') {
		$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepickstats1'])));
		if($day1<$daybase1['date']) {
			$day1=$daybase1['date'];
		}
		$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepickstats2'])));
		$_SESSION['week1'] = date("oW",strtotime($day1));
		$_SESSION['week2'] = date("oW",strtotime($day2));
		$_SESSION['dateweek1'] = $_POST['datepickstats1'];
		$_SESSION['dateweek2'] = $_POST['datepickstats2'];
		if(isset($_SESSION['anneesco'])) {
			unset($_SESSION['anneesco']);
		}
		$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['dateweek1'])));
		if($day1<$daybase1['date']) {
			$day1=$daybase1['date'];
		}
	}
	else {
		if($acadexist=="fnc") {
			$_SESSION['week1'] = $_POST['fullyear'].$wn;
			$_SESSION['week2'] = $_POST['fullyear'].'50';
			$anneesco = $_POST['fullyear'];
		}
		else if($acadexist=="civ") {
			$_SESSION['week1'] = $_POST['fullyear'].'01';
			$_SESSION['week2'] = $_POST['fullyear'].'52';
			$anneesco = $_POST['fullyear'];
		}
		else {
			$_SESSION['week1'] = substr($_POST['fullyear'],0,4).$wn;
			$_SESSION['week2'] = substr($_POST['fullyear'],4,4).'27';
			$anneesco = substr($_POST['fullyear'],0,4).'/'.substr($_POST['fullyear'],4,4);
		}
		$day1 = date("Y-m-d", strtotime(substr($_SESSION['week1'],0,4).'W'.substr($_SESSION['week1'],4,2)));
		if($day1<$daybase1['date']) {
			$day1=$daybase1['date'];
			$_SESSION['week1'] = date("oW",strtotime($day1));
		}
		$day2 = date("Y-m-d", strtotime(substr($_SESSION['week2'],0,4).'W'.substr($_SESSION['week2'],4,2).' +6 days'));
		$_SESSION['anneesco'] = $anneesco;
		require_once('caloffsco.php');
		include('calfullyear.php');
		if($_SESSION['frag']=='allyea'&&$_POST['fullyear']==$act_year) {
			$day2 = $lastday;
			$_SESSION['dateweek2'] = substr($lastday,8,2).'/'.substr($lastday,5,2).'/'.substr($lastday,0,4);
		}
		else if($_SESSION['frag']=='actday'&&$_POST['fullyear']==$act_year) {
			$day2 = date('Y-m-d');
			$_SESSION['dateweek2'] = date('d-m-Y');
		}
		else if(date('Y-m-d')<$day2&&$_POST['fullyear']==$act_year) {
			$day2 = date('Y-m-d');
			$_SESSION['dateweek2'] = date('d-m-Y');
		}
		else { 
			$_SESSION['frag']='allyea'; 
			$_SESSION['dateweek2'] = $_SESSION['datepickstats2'];
		}
	}
}
else if(isset($_SESSION['week1'])) {
	if(isset($_SESSION['anneesco'])) {
		if($acadexist=="fnc"||$acadexist=="civ") {
			$anneesco = substr($_SESSION['week1'],0,4);
			$_POST['fullyear'] = substr($_SESSION['week1'],0,4);
		}
		else {
			$anneesco = substr($_SESSION['week1'],0,4).'/'.substr($_SESSION['week2'],0,4);
			$_POST['fullyear'] = substr($_SESSION['week1'],0,4).substr($_SESSION['week2'],0,4);
		}
		require_once('caloffsco.php');
		include('calfullyear.php');
		if($_SESSION['frag']=='allyea'&&$_POST['fullyear']==$act_year) {
			$day2 = $lastday;
		}
		else if($_SESSION['frag']=='actday'&&$_POST['fullyear']==$act_year) {
			$day2 = date('Y-m-d');
		}
		else if(date('Y-m-d')<@$day2&&$_POST['fullyear']==$act_year) {
			$day2 = date('Y-m-d');
		}
		else { $_SESSION['frag']='allyea'; }
	}
	$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['dateweek1'])));
	if($day1<$daybase1['date']) {
		$day1=$daybase1['date'];
		$_SESSION['week1'] = date("oW",strtotime($day1));
	}
	$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['dateweek2'])));
}
else {
	$year1 = substr($renweek,0,4);
	if($acadexist=="fnc"||$acadexist=="civ") {
		$anneesco = $year1;
	}
	else {
		$anneesco = $year1.'/'.($year1+1);
	}
	$day1 = $rentree;
	if($day1<$daybase1['date']) {
		$day1=$daybase1['date'];
	}
	$day2 = date('Y-m-d');
	$_SESSION['week1'] = date("oW",strtotime($day1));
	$_SESSION['week2'] = date("oW",strtotime($day2));
	$_SESSION['dateweek1'] = substr($rentree,8,2).'-'.substr($rentree,5,2).'-'.substr($rentree,0,4);
	$_SESSION['dateweek2'] = date('d-m-Y');
}
if($_SESSION['frag']=='actday') {
	$info_comp = "(jusqu'au ".date("d/m/Y").")";
	$day2 = date('Y-m-d');
	$_SESSION['w2'] = date("oW",strtotime($day2));
} 
else { $info_comp = STATS_YEAR1; }

echo '<form method="POST" name="annee" action="" class="padd3">
'.STATS_YEAR2.' <div class="box"><select name="fullyear">';
$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);
echo '<option></option>';
$year = date("Y");
for($i=($year+2);$i>($year-8);$i--) {
	$checkyear = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE week >= '".$i.$wn."' AND week <= '".($i+1)."27'");
	$checkyear_nb = mysqli_num_rows($checkyear);
	if($checkyear_nb!=0) {
		if($acadexist=="fnc"||$acadexist=="civ") {
			echo '<option value="'.$i.'"'; 
			if(isset($_POST['fullyear'])) { 
				if($_POST['fullyear']==$i) { echo ' selected '; } 
			} 
			echo '>'.$i.'</option>';
		}
		else {
			echo '<option value="'.$i.''.($i+1).'"'; 
			if(isset($_POST['fullyear'])) { 
				if($_POST['fullyear']==$i.''.($i+1)) { echo ' selected '; } 
			} 
			echo '>'.$i.'/'.($i+1).'</option>';
		}
	}
}
echo '</select></div>';
echo '<input type="radio" name="frag" id="fragactday" value="actday"';
if(!isset($_POST['frag'])&&!isset($_SESSION['frag'])) { echo 'checked'; }
else if($_SESSION['frag']=='actday') { echo 'checked'; }
echo ' /><label for="fragactday">'.STATS_YEAR3.' </label>
<input type="radio" name="frag" id="fragallyea" value="allyea"';
if($_SESSION['frag']=='allyea') { echo 'checked'; }
echo ' /><label for="fragallyea">'.STATS_YEAR4.'</label> ';
echo '<br>OU du <input type="date" size="10" class="datepick1 inptxt" name="datepickstats1" value="'.$day1.'"> au <input type="date" size="10" class="datepick2 inptxt" name="datepickstats2" value="'.$day2.'">';
echo ' <input class="bouton boutedt" type="submit" value="'.VALID.'" name="anneechoix" /></form>';
?>