<?php

echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mpa4 { background-color: #fff; }
		#mpa4 td { border: 3px #7D7777 solid; }
		#mpa3 td { border: 0; }
		</style><?php	
		$dday = date("Y-m-d");
		$date = date("Y-m-d");
		$datef = new DateTime($dday);
		
		//traitement de la prolongation du calendrier:
		include('./db/proc/calprolonge.php');

		echo '<h1>'.PARAC_CAL1.'</h1>';
		
		//récupération des calendriers scolaires
		require_once('./db/proc/caloffsco.php');
		
		//traitement de la mise à plat du calendrier:
		if(isset($_POST['allopen'])) {
			if(isset(${$codepays.'_all_'.$_POST['anneereinit'].'_cles'}[0])) {
				mysqli_query($bdd,"UPDATE ".$prefix."_calendrier SET statut = 1 WHERE date >= '".${$codepays.'_all_'.$_POST['anneereinit'].'_cles'}[0]."' AND date <= '".${$codepays.'_all_'.$_POST['anneereinit'].'_cles'}[2]."'");
			}
			else { 
				mysqli_query($bdd,"UPDATE ".$prefix."_calendrier SET statut = 1 WHERE date >= '".substr($_POST['anneereinit'],0,4)."-09-02' AND date <= '".substr($_POST['anneereinit'],4,4)."-08-31'");
			}
		}
				
		echo '<h2>'.PARAC_CAL2.'</h2>';
		
		if(isset($_POST['demis'])) {
			$demis = mysqli_query($bdd,"SELECT * FROM ".$prefix."_hordemis");
			$tableau = array();
			while($demis_info = mysqli_fetch_assoc($demis))
			{
				$tableau[] = $demis_info['demijour'];
			}
			foreach($tableau as $activ_item) {
				$soluce = 0;
				$array = array_keys($_POST);
				foreach($array as $checked_item) {
					if($checked_item==$activ_item) {
						mysqli_query($bdd,"UPDATE ".$prefix."_hordemis SET statut = 1 WHERE demijour = '".$activ_item."'");
						$soluce = 1;
					}
				}
				if($soluce==0) {
					mysqli_query($bdd,"UPDATE ".$prefix."_hordemis SET statut = 0 WHERE demijour = '".$activ_item."'");
					$soluce=0;
				}
			}
		}
		
		echo '<p class="maxwidth">'.PARAC_CAL3.'</p>';
		
		echo '<div style="overflow: auto;  white-space: nowrap;"><form method="POST" action="" name="demis">
		<table class="paramform"><tr><td><b>'.PARAC_CAL4.'</b></td>';
		foreach($jourName as $tabDay) {
			echo '<td>'.$tabDay.'</td>';
		}
		echo '<tr class="center"><td><b>'.PARAC_CAL5.'</b></td>';
		$demis = mysqli_query($bdd,"SELECT * FROM ".$prefix."_hordemis ORDER BY id");
		foreach($demis as $demis_info) {
			echo '<td><input id="'.$demis_info['demijour'].'" type="checkbox" name="'.$demis_info['demijour'].'"'; 
			if ($demis_info['statut']==1) { $check = 'checked'; } else { $check = 0; }
			echo ' '.$check.'/></td>';
			if ($demis_info['id']=='7') {
				echo '</tr>
				<tr class="center"><td><b>'.PARAC_CAL6.'</b></td>';
			}
			if ($demis_info['id']=='14') {
				echo '</tr>';
			}
		}
		echo '</table><br><span class="midecart"><input class="bouton boutparam" type="submit" name="demis" value="'.VALID.'" /></span></form></div>';
		
		echo '<h2>'.PARAC_CAL7.'</h2>';
		//traitement
		if(isset($_POST['autoclose'])) {
			include('./db/proc/param_calendrier_autoclose.php');
		}
		//formulaire
		echo '<p class="maxwidth">'.PARAC_CAL8.'</p>';
		$payscheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT pays FROM ".$prefix."_etab"));
		if(@$payscheck['pays']=='aut'||!isset($payscheck['pays'])) {
			echo PARAC_CAL9;
		}
		else {
			echo '<form method="POST" action="" name="acad">';
			if($payscheck['pays']=='bel') {
				echo '<table class="paramform"><tr><td>'.PARAC_CAL10.'</td><td>'.PAYS_BEL.' <input type="hidden" name="acad" value="bel" />';
			}
			else if($payscheck['pays']=='mon') {
				echo '<table class="paramform"><tr><td>'.PARAC_CAL10.'</td><td>'.PAYS_MON.' <input type="hidden" name="acad" value="mon" />';
			}
			else if($payscheck['pays']=='swi') {
				echo '<table class="paramform"><tr><td>'.PARAC_CAL10.'</td><td>'.PAYS_SWI.' <input type="hidden" name="acad" value="swi" />';
			}
			else if($payscheck['pays']=='fnc') {
				echo '<table class="paramform"><tr><td>'.PARAC_CAL10.'</td><td>'.PAYS_FNC.' <input type="hidden" name="acad" value="fnc" />';
			}
			else if($payscheck['pays']=='civ') {
				echo '<table class="paramform"><tr><td>'.PARAC_CAL10.'</td><td>'.PAYS_CIV.' <input type="hidden" name="acad" value="civ" />';
			}
			else {
				echo '<table class="noborder"><tr><td>'.PARAC_CAL11.'</td><td>';
				include('./include/acadschoix.php');
			}
			echo '</td></tr><tr><td>'.YEAR.DOTS.'</td><td><div class="box"><select name="annee">';
			if($payscheck['pays']=='fnc'||$payscheck['pays']=='civ') {
				for($i=2040;$i>2021;$i--) {
					if($i<=date('Y')) {
						echo '<option value="'.$i.'"';
						if(date('Y')==$i) { echo ' selected '; }
						echo '>'.$i.'</option>';
					}
				}
			} else {
				for ($i = 2040; $i >= (date('Y') - 3); $i--) {
				    if ($i <= date('Y')) {
				        // Définir l'année scolaire courante
				        $start_year = $i;
				        $end_year = $i + 1;

				        // Vérifier si on est dans l'année scolaire actuelle
				        $current_year = date('Y');
				        $current_month = date('m');
				        $current_year_school = ($current_month >= 9) ? $current_year : $current_year - 1; // Si on est après septembre, l'année scolaire correspond à l'année en cours, sinon à l'année précédente

				        // Vérifier si l'année scolaire correspond à l'année scolaire actuelle
				        $is_selected = ($current_year_school == $start_year);

				        // Affichage de l'option avec la sélection
				        echo '<option value="' . $start_year . $end_year . '"';
				        if ($is_selected) {
				            echo ' selected';
				        }
				        echo '>' . $start_year . '/' . $end_year . '</option>';
				    }
				}
			}
			echo '</select></div></td></tr>
			<tr><td colspan="2" style="text-align:right;"><input class="bouton boutparam" type="submit" name="autoclose" value="'.PARAC_CAL12.'" /></td></tr></table>
			</form>';
		}
		
		echo '<h2>'.PARAC_CAL13.'</h2><div style="overflow: auto;  white-space: nowrap;">';
		include('./db/proc/param_calendrier_aff.php');

		echo '<br><h2>'.PARAC_CAL19.'</h2>';
		//formulaire:
		echo '<p style="max-width: 700px;">'.PARAC_CAL20.'</p>';
		echo '<form method="POST" action="" name="reinit">
		<table class="noborder"><tr><td>'.YEAR.DOTS.'</td><td><div class="box"><select name="anneereinit">';
		if($payscheck['pays']=='fnc'||$payscheck['pays']=='civ') {
			for($i=2040;$i>2021;$i--) {
				if($i<=date('Y')) {
					echo '<option value="'.$i.'"';
					if(date('Ym')>(date('Y')-1).'12') { echo ' selected '; }
					echo '>'.$i.'</option>';
				}
			}
		} else {
			for($i=2040;$i>(date('Y')-2);$i--) {
				if($i<=date('Y')) {
					echo '<option value="'.$i.($i+1).'"';
					if(date('Ym')>date('Y').'12') { echo ' selected '; }
					echo '>'.$i.'/'.($i+1).'</option>';
				}
			}
		}
		echo '</select></div></td></tr>
		<tr><td colspan="2" style="text-align:right;"><input class="bouton boutparam" type="submit" name="allopen" value="'.REINIT.'" /></td></tr></table>
		</form>';
		
		echo '<h2>'.PARAC_CAL21.'</h2>';
		//formulaire:
		echo '<p style="max-width: 700px;">'.PARAC_CAL22.$formattedDate.PARAC_CAL24.'</p><form method="POST" action=""><input class="bouton boutparam paddvalid1" type="submit" name="prolonger" value="'.PARAC_CAL23.'"></form>';
		
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="../index.php">'.ACCUEIL.'</a></p>';
}
?>
