<?php
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2")
	{
		echo '<p>'.NOMODULE.' <a href="../index.php">'.ACCUEIL.'</a>.</p>';
	}
	else {
		$codeacad = $_POST['acad'];
		if($codeacad==""||$codeacad=="Autre"||$codeacad=="aut"||$codeacad=="civ") {
			echo '<p class="signal">'.PARAM_CAL4.'</p>';
		}
		else if($codeacad=='fnc'||$codeacad=="bel"||$codeacad=="mon"||$codeacad=="swi") {
			$codepays = $codeacad;
		}
		else if($codeacad=="La Réunion") {
			$codepays = 'reu';
		}
		else {
			$codepays = 'fra';
		}

		//Vérification de l'inscription des journées dans _calendrier :
		if(!isset(${$codepays.'_all_'.$_POST['annee'].'_cles'}[0])) {
			echo '<p class="signal">'.PARAM_CAL4.'</p>';
		}
		else {
			//récupération des codes acad selon le POST
			if($codeacad=="Besançon"||$codeacad=="Bordeaux"||$codeacad=="Clermont-Ferrand"||$codeacad=="Dijon"||$codeacad=="Grenoble"||$codeacad=="Limoges"||$codeacad=="Lyon"||$codeacad=="Poitiers") {
				$zoneacad = "zna";
			}
			else if($codeacad=="Aix-Marseille"||$codeacad=="Amiens"||$codeacad=="Normandie"||$codeacad=="Lille"||$codeacad=="Nancy-Metz"||$codeacad=="Nantes"||$codeacad=="Nice"||$codeacad=="Orléans-Tours"||$codeacad=="Reims"||$codeacad=="Rennes"||$codeacad=="Strasbourg") {
				$zoneacad = "znb";
			}
			else if($codeacad=="Créteil"||$codeacad=="Montpellier"||$codeacad=="Paris"||$codeacad=="Toulouse"||$codeacad=="Versailles") {
				$zoneacad = "znc";
			}
			// else if($codeacad=="Corse") { $zoneacad = "cor"; /* équivalent zone B */ }
			else if($codeacad=="Guadeloupe") { $zoneacad = "gua"; }
			else if($codeacad=="Guyane") { $zoneacad = "guy"; }
			else if($codeacad=="Martinique") { $zoneacad = "mar"; }
			else if($codeacad=="La Réunion") { $codepays = "reu"; $zoneacad = "all"; }
			else { $zoneacad = "all"; }

			// si besoin prolongation de 365 jours à partir de la rentrée :
			$datef = new DateTime(${$codepays.'_all_'.$_POST['annee'].'_cles'}[0]);
			for($i=0;$i<366;$i++) {
				$cal = mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date_format($datef, 'Y-m-d')."'");
				$cal_nb = mysqli_num_rows($cal);
				if($cal_nb==0) {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_calendrier (date, statut) VALUES ('".date_format($datef, 'Y-m-d')."', 1)");
				}
				$datef->modify('+1 day');
			}

			//Enregistrement de l'académie :
			$etab = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab");
			$etab_nb = mysqli_num_rows($etab);
			if($etab_nb!=0) {
				$etab_info = mysqli_fetch_assoc($etab);
				if($etab_info['pays']=='fra') {
					mysqli_query($bdd,"UPDATE ".$prefix."_etab SET academie='".$_POST['acad']."' WHERE etab_id = '".$etab_info['etab_id']."'");
				}
			}

			// fériés :
			foreach(${$codepays.'_all_'.$_POST['annee'].'_feries'} as $ferie) {
				mysqli_query($bdd,"UPDATE ".$prefix."_calendrier SET statut = 0 WHERE date = '".$ferie."'");
			}
			if(isset(${$codepays.'_'.$zoneacad.'_'.$_POST['annee'].'_feries'}[0])) {
				foreach(${$codepays.'_all_'.$_POST['annee'].'_feries'} as $ferie) {
					mysqli_query($bdd,"UPDATE ".$prefix."_calendrier SET statut = 0 WHERE date = '".$ferie."'");
				}
			}

			// périodes fermées :
			$rack=0;
			for($z=0;$z<(count(${$codepays.'_'.$zoneacad.'_'.$_POST['annee'].'_periodsclose'})/2);$z++) {
				mysqli_query($bdd,"UPDATE ".$prefix."_calendrier SET statut = 0 WHERE date >= '".${$codepays.'_'.$zoneacad.'_'.$_POST['annee'].'_periodsclose'}[$rack]."' AND date <= '".${$codepays.'_'.$zoneacad.'_'.$_POST['annee'].'_periodsclose'}[($rack+1)]."'");
				$rack+=2;		
			}

			// suppression des événements sur les jours off sur l'année scolaire
			$dayoffs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date >= '".${$codepays.'_all_'.$_POST['annee'].'_cles'}[0]."' AND date <= '".${$codepays.'_all_'.$_POST['annee'].'_cles'}[2]."' AND statut ='0'");
			foreach($dayoffs as $dayoff) {
				$events = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".date('Ymd', strtotime(str_replace("/","-",$dayoff['date'])))."%'");
				foreach($events as $event) {
					mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event['uid']."'");
					mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event['uid']."'");
					mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$event['uid']."'");
				}
			}		
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<a href="./index.php>'.ACCUEIL.'</a>.</p>';
}
