<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if (($_SESSION['user_droits'][2] ?? 0) < 2) {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mdt1 { background-color: #fff; }
		#mdt1 td { border: 3px #E77015 solid; }
		</style><?php
		echo '<script language="javascript">
		function toggleDiv(divid){
			if(document.getElementById(divid).style.display == \'none\'){
				document.getElementById(divid).style.display = \'block\';
			}else{
				document.getElementById(divid).style.display = \'none\';
			}
		}
		</script>';

		// Gestion du calendrier pour l'année en cours
		require_once('./db/proc/caloffsco.php');
		
		echo '<div class="h1"><h1>'.EDT1.'</h1></div>';
				
		$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires");
		$hor_nb = mysqli_num_rows($hor);
		
		// S'il n'y a pas d'horaires configurés :
		
		if($hor_nb==0) {
			echo '<p><a href="param.php?page=horaires">'.FREQ_HORNEED.'</a>.';
		}
	
		else {
			$cren_colors = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_crencolors'"));
			
			if(isset($_POST['weekpick'])) {
				$_GET['week'] = date("oW", strtotime($_POST['weekpick']));
				$_SESSION['weekpick'] = $_POST['weekpick'];
				echo '<script language="Javascript">
				<!--
				document.location.replace("edt.php?page=create&week='.$_GET['week'].'");
				// -->
				</script>';
			}
			if(isset($_POST['backweek'])) {
				$_GET['week'] = date("oW");
				$_SESSION['weekpick'] = date('Y-m-d');
				echo '<script language="Javascript">
				<!--
				document.location.replace("edt.php?page=create&week='.$_GET['week'].'");
				// -->
				</script>';
			}

			// Gestion des dates :
			include('./db/proc/edt_week.php');
			
			// Gestion impression et Gestion masquage des briques :
			echo '<style type="text/css" media="print">
				body {
				webkit-transform: rotate(-90deg);
				-moz-transform: rotate(-90deg);
				filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);';
				if($nbJour==5) {
					// anciennes valeurs : 960 et 860
					echo 'height:960px;
					width:755px;';
				}
				else if($nbJour==6) {
					// anciennes valeurs : 1110 et 910
					echo 'height:1110px;
					width:893px;';
				}
				else if($nbJour==7) {
					// anciennes valeurs : 1160 et 1110
					echo 'height:1160px;
					width:1031px;';
				}	
				echo '}
			</style>
			<script language="javascript">
				function toggleDiv(divid){
					if(document.getElementById(divid).style.display == "none"){
						document.getElementById(divid).style.display = "block";
					}else{
						document.getElementById(divid).style.display = "none";
					}
				}
			</script>';
			
			// Récupération du texte enregistré pour ouverture hors séance :
			$openmsg = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_openmsg'");
			$openmsg_info = mysqli_fetch_assoc($openmsg);
			$openmsg_txt = $openmsg_info['valeur'];
			$individu=0;
				
			// Affichage de la semaine choisie :
			echo '<div id="titreMois" name="noprint">
				<br><table class="edt_grille">
					<tr>';
					if($num_week==1||$num_week=='01') {
						$an_prev=($annee-1);
						$sem_prev = date("oW", mktime(0,0,0,12,28,$an_prev));
						echo '<td><a href="edt.php?page=create&week='.$sem_prev.'"><i class="fas fa-arrow-alt-circle-left arrows_edt"></i></a></td>';
					}
					else {
						echo '<td><a href="edt.php?page=create&week='.($week-1).'"><i class="fas fa-arrow-alt-circle-left arrows_edt"></i></a></td>';
					}
					echo '<td><b>'.WEEK.' '.$num_week.'</b></td>';
					$_SESSION['pdfweek'] = date('Y').$num_week;
					$an_next=($annee+1);
					$sem_test = date('d/m/Y', mktime(0,0,0,12,28,$annee));
					$sem_test_j = substr($sem_test,0,2);;
					$sem_test_m = substr($sem_test,3,2);;
					$sem_test_y = substr($sem_test,6,4);;
					$fin_j = substr($dateFinSemaineFr,0,2);
					$fin_m = substr($dateFinSemaineFr,3,2);
					$fin_y = substr($dateFinSemaineFr,6,4);
					if($fin_m==1||$fin_m==12) {
						if(($sem_test_y==$fin_y&&$fin_j>28&&$fin_m!=1)||($fin_y>$sem_test_y)) {
							$an_next=($annee+1);
							$sem_next = date("oW", mktime(0,0,0,01,04,$an_next));
							echo '<td><a href="edt.php?page=create&week='.$sem_next.'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
						}
						else {
							echo '<td><a href="edt.php?page=create&week='.($week+1).'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
						}
					}													
					else {
						echo '<td><a href="edt.php?page=create&week='.($week+1).'"><i class="fas fa-arrow-alt-circle-right arrows_edt"></i></a></td>';
					}
					echo '<td><form method="POST" action="">'.EDT_BUTTON1.' <input type="date" class="inptxt" id="weekpick" name="weekpick" value="';
					if(isset($_SESSION['weekpick'])) {
						if($_SESSION['weekpick']!="") {
							echo $_SESSION['weekpick'];
						}
						else { echo date('Y-m-d'); }
					}
					else { echo date('Y-m-d'); }
					echo '" />&nbsp; <input class="bouton boutedt" type="submit" value="'.VALID.'" /></form></td>';
					echo '</tr>';
					if($week!=date('oW')) {
						echo '<tr><td colspan="3"></td><td><form method="POST" action=""><input class="bouton boutedt" type="submit" name="backweek" value="'.EDT_BUTTON2.'" /></form></td></tr>';
					}
					echo '<tr><td colspan="3">'.FROM2.' '.$dateDebSemaineFr.' '.TO2.' '.$dateFinSemaineFr.'</td><td class="w400 right">';
					if (($_SESSION['user_droits'][2] ?? 0) == '2') {
						echo '<a onmousedown="toggleDiv(\'edt_briques\');"><i>'.EDT_BUTTON3.'</i></a>';
					}
					echo '</td></tr>
				</table>
			</div>';
			
			include('./db/proc/calprolonge.php');

			// Gestion des créneaux :
			$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires ORDER BY num_creneau");
			$hor_nb = mysqli_num_rows($hor);
			$horform_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_formhor'"));
			$plageH = array();
			$plageDebut = array();
			$plageFin = array();
			$heighthor = array();
			$tabheight = 0;
			foreach($hor as $hor_info) {	
				$plageH[] = substr($hor_info['debut'],0,5);
				$plageHend[] = substr($hor_info['fin'],0,5);
				$plageDebut[] = $hor_info['debut'];
				$plageFin[] = $hor_info['fin'];
				$hFin[] = substr($hor_info['fin'],0,2).substr($hor_info['fin'],3,2).substr($hor_info['fin'],6,2);
				$hDebut[] = substr($hor_info['debut'],0,2).substr($hor_info['debut'],3,2).substr($hor_info['debut'],6,2);
				$plage_horid[] = $hor_info['hor_id'];
				$plage_horname[] = $hor_info['hor_name'];
				$plage_demijour[] = substr($hor_info['hor_name'],0,1);
				$hinter=strtotime($hor_info['fin'])-strtotime($hor_info['debut']);
				$num_creneau[] = $hor_info['num_creneau'];
				$heighthor[] = ($hinter/60)*0.85;
			}
			foreach($daysOfWeek as $key => $val) {
				$key = strtoupper(substr($key,0,1)).substr($key,1,2);
				foreach($hor as $hor_info) {
					if($freq_gestion_jourscren['valeur']==0) {
						$plage_open[$key][] = substr($hor_info['hor_freq'],2,1);
					}
					else {
						$plage_open[$key][] = @intval(substr($hor_info['hor_freq'], strpos($hor_info['hor_freq'], $key) + strlen($key), 3));
					}
				}
			}
			 
			function translateMonth($month, $months_lang) {
			    return isset($months_lang[$month]) ? $months_lang[$month] : $month;
			}

			$nom_mois = translateMonth($nom_mois, $months_lang);
			$dateDebSemaine_m = translateMonth($dateDebSemaine_m, $months_lang);
			$dateFinSemaine_m = translateMonth($dateFinSemaine_m, $months_lang);
			 
			echo '<br/>';
			
			// EDT CDI

			$entete_tit = $_SESSION['lieu_nom'].' - ';
			$entete_titw = FROM2.' '.$dateDebSemaine_j.'';
				if($dateDebSemaine_j==1) {
					$entete_titw .= FIRST;
				}
				if($dateDebSemaine_m!=$dateFinSemaine_m){
					$entete_titw .= ' '.$dateDebSemaine_m.'';
				}
				$entete_titw .= ' '.TO2.' '.$dateFinSemaine_j.'';
				if($dateFinSemaine_j==1) {
					$entete_titw .= FIRST;
				}
				$entete_titw .= ' '.$dateFinSemaine_m;
			$entete_tit .= $entete_titw;
			echo '<div id="grilleSem">
				<h2>'.$entete_tit.'</h2>
			</div>';
			
			if (($_SESSION['user_droits'][2] ?? 0) == '2') {
				include('./db/proc/edt_affbriks.php');
			}

			// Voir s'il y a verrouillage des briques des jours précédents
			$brik_verr = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_brikverr'"));
			$verrouillage = $brik_verr['valeur'];

			// Chargement des options de titre et message
			$edtmsgopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_msgopt'"));
			$edtmsgopt_infos = explode(";",$edtmsgopt['valeur']);
			$edttitleopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_titopt'"));
			$edttitleopt_infos = explode(";",$edttitleopt['valeur']);

			// Affichage du tableau formulaire CDI
			echo '<form method="POST" action="" name="edtcdi">';
			include('./db/proc/edt_table.php');
			
			echo '<div name="noprint" style="clear:both;">';
			if (($_SESSION['user_droits'][2] ?? 0) == '2') {	
				$dupli = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_duplicate'"));
				echo '<p class="paddl">';
				if($dupli['valeur']==1) {
					echo EDT_BRIK6.' <input type="number" name="nbweeks" min="1" max="52" value="1" />';
				}
				echo '&nbsp; <input class="bouton boutedt" type="submit" name="edtu0e0_valid" value="'.VALID.'" /></p>';
			}
			echo '</div></form>';

			echo '<p class="maxwidth"><span name="noprint"><b>'.EDTC_CREA5.'</b></span>';
			$jourP1 = date("Y-m-d", strtotime($dateDebSemaine));
			$jourP1date = new DateTime($jourP1);
			$jourP2 = date("Y-m-d", strtotime($dateFinSemaine));
			$jourP2date = new DateTime($jourP2);
			$dayhour = 1; // pour activer le comptage dans calweeknb
			include('./db/proc/calweeknb.php');
			echo '<br>'.($nb_fullet-$briques_close_nb-$briques_seance_nb).' ';
			if(($nb_fullet-$briques_close_nb-$briques_seance_nb)>1) {
				echo EDTC_CREA1;
			}
			else { echo EDTC_CREA2; }
			 
			echo SEAN3.'.<br>'.$briques_seance_nb.' '.SEAN;
			if($briques_seance_nb>1) { echo PLURIEL; }
			echo ' ('.DONT.' '.($briques_seance_nb-$briques_noseq_nb).' '.EDTC_CREA6.').<br>';
			echo $nb_briks_close;
			if($nb_briks_close>1) { echo ' '.EDTC_CREA3.'.'; }
			else { echo ' '.EDTC_CREA4.'.'; }
			echo '</p>';

			//Message à afficher sous l'EDT	
			if($edttitleopt_infos[0]=='1') {
				$tit_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_tit'"));
				if($tit_info['valeur']!="") {
					$tit_info['valeur'] = str_replace('[week]', $entete_titw, $tit_info['valeur']);
					echo '<p class="maxwidth"><span name="noprint"><b>'.EDTC_CREA7.'</b></span><br>'.nl2br(htmlspecialchars($tit_info['valeur'])).'<span name="noprint"><br><a href="edt.php?page=param"><i>'.EDTC_CREA8.'</i></a></span></p>';
					$_SESSION['edt_tit'] = $tit_info['valeur'];
				}
				else { 
					echo '<p class="maxwidth"><span name="noprint"><b>'.TITLE.DOTS.'</b></span><br>'.$entete_tit.'<span name="noprint"><br><a href="edt.php?page=param"><i>'.EDTC_CREA8.'</i></a></span></p>';
					$_SESSION['edt_tit'] = $entete_tit; 
				}
			}
			else { $_SESSION['edt_tit'] = ""; }
			@$edtmsgopt_agenda = explode("-",@$edtmsgopt_infos[3]);
			$eventsaff = '';
			foreach(@$edtmsgopt_agenda as $agenda) {
				$blocn = mysqli_query($bdd,"SELECT * FROM ".$prefix."_agenda WHERE SUBSTRING(options, 7, 1) = '1' AND (date1 >= '".date('Y-m-d')."' OR date2 >= '".date('Y-m-d')."') AND agenda_id = '".$agenda."' ORDER BY date1 ASC");
				if(mysqli_num_rows($blocn)>0) {
					$blocnote = mysqli_fetch_assoc($blocn);
					$date1 = DateTime::createFromFormat('Y-m-d', $blocnote['date1']);
					$date2 = DateTime::createFromFormat('Y-m-d', $blocnote['date2']);
					$eventsaff .= '<br>';
					if($blocnote['date2']!='0000-00-00') {
						$eventsaff .= FROM21.' '.$date1->format('d/m/Y').' '.TO2.' '.$date2->format('d/m/Y').DOTS.' ';
					}
					else {
						$eventsaff .= LE.' '.$date1->format('d/m/Y').DOTS.' ';
					}
					$eventsaff .= $blocnote['titre'];					
				}
			}
			if($edtmsgopt_infos[0]=='1'||$eventsaff!='') {
				$_SESSION['edt_msg'] = '';
				$openmsg_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'edt_msg'"));
				if($openmsg_info['valeur']!="") {
					$_SESSION['edt_msg'] .= $openmsg_info['valeur'];
				}
				if($eventsaff!='') {
					$_SESSION['edt_msg'] .= $eventsaff;
				}
				echo '<p class="maxwidth"><span name="noprint"><b>'.EDTC_CREA9.'</b></span><br>'.$_SESSION['edt_msg'].'<span name="noprint"><br><a href="edt.php?page=param"><i>'.EDTC_CREA8.'</i></a></span></p>';
			}
			else { $_SESSION['edt_msg'] = ""; }
			$_SESSION['edtbotmsgsize'] = 'font-size:'.$edtmsgopt_infos[1].'em;';
			$_SESSION['edtbotmsgcolor'] = 'color:'.$edtmsgopt_infos[2].';';
			$_SESSION['edttitsize'] = 'font-size:'.$edttitleopt_infos[1].'em;';
			$_SESSION['edttitcolor'] = 'color:'.$edttitleopt_infos[2].';';

			//Impression et extractions
			$specific_extract = 0;
			include('db/proc/edt_extract.php');
			echo '<hr class="edt">';
			$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires ORDER BY num_creneau");
			$hor_nb = mysqli_num_rows($hor);
			
			$nb_espedt = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE edtopt = '1'"));
			if ($nb_espedt>0) {
				echo '<div name="noprint"><br>';
				// BOUCLE AVEC EDT POUR CHAQUE USER AVEC DROITS DE MODIF
				$espaces = mysqli_query($bdd,"SELECT espace_id, espace_nom, edtopt, edtoptresa FROM ".$prefix."_espaces");
				foreach($espaces as $espaces_info) {
					if($espaces_info['edtopt']==1) {
						include('./db/proc/edt_espaces.php');	
						$specific_extract = 1;
						include('db/proc/edt_extract.php');	
						echo '<hr class="edt">';	
					}
				}
				echo '</div>';					
			}

			$nb_edt = mysqli_num_rows(mysqli_query($bdd,"SELECT edt FROM ".$prefix."_user WHERE edt = '1'"));
			if ($nb_edt>0) {
				include('./db/proc/edt_multi.php');
			}

			?>
			<script type="text/javascript">
				function StatusBrik(brik,act){
				  $.ajax({
				    type: "POST",
				    url: "js/idl/edt_brikup.php",
				    data: { event: event, act: act},
				    
				   });
				}
			</script>
			<?php
		}
		if(@$_SESSION['reload']==1) {
			unset($_SESSION['reload']);
			echo '<script language="Javascript">
			<!--
			document.location.replace("edt.php?page=create&week='.$week.'");
			// -->
			</script>';
		}
		if(@$_SESSION['pdf']==1) {
			unset($_SESSION['pdf']);
			echo '<script language="Javascript">
			<!--
			document.location.replace("./include/pdf_loaders/edt_pdf.php");
			// -->
			</script>';
		}
	}
}
	
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>
