<?php
$espcode = $espaces_info['espace_id'];
$espid = $espaces_info['espace_id'];
$userid=0;
$usercode=0;
// EDT USER
echo '<div id="grilleSemu'.$userid.'e'.$espid.'" name="noprint" style="clear:both;">
<br><h2>'.$espaces_info['espace_nom'].' - du '.$dateDebSemaine_j.'';
if($dateDebSemaine_j==1) { echo 'er'; }
if($dateDebSemaine_m!=$dateFinSemaine_m){ echo ' '.$dateDebSemaine_m.''; }
echo ' au '.$dateFinSemaine_j.''; 
if($dateFinSemaine_j==1) { echo 'er'; }
echo ' '.$dateFinSemaine_m.'</h2>
</div>';
$entete_tit = $espaces_info['espace_nom'].' - ';
$entete_titw = 'du '.$dateDebSemaine_j.'';
	if($dateDebSemaine_j==1) {
		$entete_titw .= 'er';
	}
	if($dateDebSemaine_m!=$dateFinSemaine_m){
		$entete_titw .= ' '.$dateDebSemaine_m.'';
	}
	$entete_titw .= ' au '.$dateFinSemaine_j.'';
	if($dateFinSemaine_j==1) {
		$entete_titw .= 'er';
	}
	$entete_titw .= ' '.$dateFinSemaine_m;
$entete_tit .= $entete_titw;
// Affichage du tableau formulaire CDI
echo '<form method="POST" action="" name="edtu'.$userid.'e'.$espid.'">';
${'edt_tableu'.$userid.'e'.$espid} = '<div class="edt_grille"  id="html-imageu'.$userid.'e'.$espid.'" style="display:block;width:'.$table_width.'px;background-color:'.substr($cren_colors['valeur'],14,7).';" name="form">';
// en tête de colonne
${'edt_tableu'.$userid.'e'.$espid} .= '<div class="td titleEdt';
if(@$edttitleopt_infos[3]=='1') { ${'edt_tableu'.$userid.'e'.$espid} .= ' none'; }
${'edt_tableu'.$userid.'e'.$espid} .= '" name="noprint" style="background-color:'.@substr($cren_colors['valeur'],21,7).';">'.@$entete_tit.'</div>';
${'edt_tableu'.$userid.'e'.$espid} .= '<div style="float:left;width:60px;">';
${'edt_tableu'.$userid.'e'.$espid} .='<div class="th heure" style="height:22px;"><div> </div></div>';
for ($h = 0; $h < $hor_nb; $h++) {
	$precnum = $num_creneau[$h]-1;
	$bef = mysqli_query($bdd,"SELECT fin FROM ".$prefix."_horaires WHERE num_creneau = '$precnum'");
	if(mysqli_num_rows($bef)==1) {
		$bef_info = mysqli_fetch_assoc($bef);
		if($plageDebut[$h]!=$bef_info['fin']) {
			$hinter_inter=strtotime($plageDebut[$h])-strtotime($bef_info['fin']);
			$inter_height = (($hinter_inter/60)*0.85)+1;
			${'edt_tableu'.$userid.'e'.$espid} .='<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$inter_height.'px;"></div>';
		}
	}
	${'edt_tableu'.$userid.'e'.$espid} .='<div class="th heure" style="height:'.$heighthor[$h].'px;background-color:#fff;">';
	${'edt_tableu'.$userid.'e'.$espid} .='<div';
	if($heighthor[$h]>=30) { ${'edt_tableu'.$userid.'e'.$espid} .=' class="pdfmarg"'; }
	${'edt_tableu'.$userid.'e'.$espid} .='>';
	if($heighthor[$h]<20||$horform_info['valeur']=='2') { ${'edt_tableu'.$userid.'e'.$espid} .= '<div class="pdfmargnone" style="line-height: 0.6;"><span class="fontsmall">'; }
	if($heighthor[$h]>=12.75) { 
		if($horform_info['valeur']=='1') {
			${'edt_tableu'.$userid.'e'.$espid} .= $plageH[$h]; 
		}
		else if($horform_info['valeur']=='2'&&$heighthor[$h]>=30) {
			${'edt_tableu'.$userid.'e'.$espid} .= $plageH[$h].'<br>'.TO.'<br>'.$plageHend[$h]; 
		}
		else if($horform_info['valeur']=='2'&&$heighthor[$h]<30) {
			${'edt_tableu'.$userid.'e'.$espid} .= TO.' '.$plageHend[$h]; 
		}
		else if($horform_info['valeur']=='3') {
			${'edt_tableu'.$userid.'e'.$espid} .= $plage_horname[$h]; 
		}
	}
	if($heighthor[$h]<20||$horform_info['valeur']=='2') { ${'edt_tableu'.$userid.'e'.$espid} .= '</span></div>'; }
	${'edt_tableu'.$userid.'e'.$espid} .='</div></div>';
}
${'edt_tableu'.$userid.'e'.$espid} .='</div>';

for($j = 1; $j < $nbJour+1; $j++) {
	${'edt_tableu'.$userid.'e'.$espid} .='<div style="display:inline-block;position:relative;top:auto;float:left;width:138px;">';
	${'edt_tableu'.$userid.'e'.$espid} .='<div class="th thday" style="height:22px;position:relative;background-color:#fff;">'.$jourTexte[$j].' '.date("d", mktime(0,0,0,$mois,$jour-1+$j,$annee)).'</div>';
	$breaktd_edt = array();
	$jouropentest = date("Y-m-d", mktime(0,0,0,$mois,$jour-1+$j,$annee));
	$jouropenverif = mysqli_query($bdd,"SELECT statut FROM ".$prefix."_calendrier WHERE date = '".$jouropentest."'");
	$jouropenverif_info = mysqli_fetch_assoc($jouropenverif);
	if(mysqli_num_rows(mysqli_query($bdd,"SELECT statut FROM ".$prefix."_hordemis WHERE demijour LIKE '".$demijourGestion[$j]."%' AND statut = 1"))==0) {
		$jouropenverif_info['statut']=0;
	}
	$hideh=0; $hided=0;
	unset($finprec);
	unset($debpost);
	if(@$jouropenverif_info['statut']==0&&@$breaktd_edt[$j]!='close') {
		// journée fermée
		$breaktd_edt[$j] = 'close';
		$info = mysqli_query($bdd, "
			SELECT * FROM ".$prefix."_agenda 
			WHERE 
				titre != '' 
				AND SUBSTRING(options, 5, 1) = '1' 
				AND (
					-- Cas normal : entre date1 et date2
					(date1 <= '".$jouropentest."' AND date2 >= '".$jouropentest."')
					
					-- Cas sans date de fin : affiché uniquement le jour de date1
					OR (date2 = '0000-00-00' AND date1 = '".$jouropentest."')
				)
				AND (
		        espaces = '".$espid."'
		        OR espaces LIKE '".$espid.";%' 
		        OR espaces LIKE '%;".$espid.";%'
		        OR espaces LIKE '%;".$espid."'
		    )");
		${'edt_tableu'.$userid.'e'.$espid} .='<div class="td recept_close txt_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';display:inline;position:relative;height:'.$tabheight.'px;">';
		if(mysqli_num_rows($info)==1) {
			$info_msg = mysqli_fetch_assoc($info);
			${'edt_tableu'.$userid.'e'.$espid} .='<p><span>'.$info_msg['titre'].'<br>&nbsp;</span></p>';
		}
		${'edt_tableu'.$userid.'e'.$espid} .='</div>';
	}
	else if($jouropenverif_info['statut']==1) {
		for ($h = 0; $h < $hor_nb; $h++) {
			// Check spécifique sur la brique suivante :
			$jouraff = date("Ymd", mktime(0,0,0,$mois,$jour-1+$j,$annee));
			$dayl = date("D", mktime(0,0,0,$mois,$jour-1+$j,$annee));
			$userid = 0;
			$posth = $h+1;
			if(isset($plage_horname[$posth])||@$debprec>=$plageFin[$h]) {
				$tabevent_select = 
				"SELECT event.dtstart, event.dtend, event.week, event.hor_id, event.seq_id, event.hor_name, event.specifique, brik.brik_id, brik.code, brik.seq_id, brik.division, SUBSTR(brik.division,1,9), brik.textelibre, brik.summary, brik.close, espevent.uid, espevent.espace_id  
				FROM ".$prefix."_edtevent event
				INNER JOIN ".$prefix."_edtbrik brik
				ON event.brik_id = brik.brik_id
				INNER JOIN ".$prefix."_espaces_event espevent 
				ON event.uid = espevent.uid 
				WHERE event.dtstart LIKE '".$jouraff."%' AND event.dtend LIKE '".$jouraff."%' AND event.hor_name = '".$plage_horname[$posth]."' AND espevent.espace_id = '".$espid."' AND event.specifique = 1 AND event.dtstart < '".$jouraff."T".$hFin[$h]."'";
				$checkpost = mysqli_query($bdd,$tabevent_select);
				$checkpost_nb = mysqli_num_rows($checkpost);
				$debpost=99;
				if($checkpost_nb==1) {
					$checkpost_info = mysqli_fetch_assoc($checkpost);
					$debpost = substr($checkpost_info['dtstart'],9,2).':'.substr($checkpost_info['dtstart'],11,2).':'.substr($checkpost_info['dtstart'],13,2);
					$hinter=strtotime($debpost)-strtotime($plageDebut[$h]);
					$heightspe = (round(($hinter/60)*0.85));
				}
			}
			else { $checkpost_nb=0; $debpost=99; }
			if($hideh>0) { $prech = $h-1-$hideh; }
			else { $prech = $h-1; }
			if(isset($plage_horname[$prech])&&@$finprec<=@$plageFin[$h]) {
				$tabevent_select = 
				"SELECT event.dtstart, event.dtend, event.week, event.hor_id, event.seq_id, event.hor_name, event.specifique, brik.brik_id, brik.code, brik.seq_id, brik.division, SUBSTR(brik.division,1,9), brik.textelibre, brik.summary, brik.close, espevent.uid, espevent.espace_id  
				FROM ".$prefix."_edtevent event
				INNER JOIN ".$prefix."_edtbrik brik
				ON event.brik_id = brik.brik_id
				INNER JOIN ".$prefix."_espaces_event espevent 
				ON event.uid = espevent.uid 
				WHERE event.dtstart LIKE '".$jouraff."%' AND event.dtend LIKE '".$jouraff."%' AND event.hor_name = '".$plage_horname[$prech]."' AND espevent.espace_id = '".$espid."' AND event.dtend > '".$jouraff."T".$hDebut[$h]."'";
				$checkprec = mysqli_query($bdd,$tabevent_select);
				$checkprec_nb = mysqli_num_rows($checkprec);
				$checkprec_info['specifique']=0;
				$checkprec_info = mysqli_fetch_assoc($checkprec);
				if(@$checkprec_info['specifique']==1) {
					$finprec = substr($checkprec_info['dtend'],9,2).':'.substr($checkprec_info['dtend'],11,2).':'.substr($checkprec_info['dtend'],13,2);
					$hinter=strtotime($plageFin[$h])-strtotime($finprec);
					$heightspe = ($hinter/60)*0.85;
				}
			}
			else { $checkprec_nb=0; $checkprec_info['specifique']=0; $finprec_test='::'; }
			if($checkpost_nb==1&&$checkprec_info['specifique']==1) {
				$hinter=strtotime($debpost)-strtotime($finprec);
				$heightspe = ($hinter/60)*0.85;
			}

			// Mise en place des créneaux selon les différents cas :
			if(!isset($breaktd_edt[$j])) { $breaktd_edt[$j]='open'; }
			if($plage_open[$dayl][$h]==1&&$breaktd_edt[$j]!='close') {
				$usercode = "";
				$userid = 0;
				$loss = 8;
				if($plage_demijour[$h]=='M') { $demijourAP = '_am'; }
				else if($plage_demijour[$h]=='A') { $demijourAP = '_pm'; }
				else if($plage_demijour[$h]=='P'||$plage_demijour[$h]=='R'||$plage_demijour[$h]=='H'||$plage_demijour[$h]=='T'||$plage_demijour[$h]=='C') {
					if(substr($plageH[$h],0,2)<12) { $demijourAP = '_am'; }
					else if(substr($plageH[$h],0,2)>=12) { $demijourAP = '_pm'; }
				}
				$demijourverif = mysqli_query($bdd,"SELECT statut FROM ".$prefix."_hordemis WHERE demijour = '".$demijourGestion[$j].$demijourAP."'");
				$demijourverif_info = mysqli_fetch_assoc($demijourverif);
				if($demijourverif_info['statut'] == 0) {
					if($hided==0) {
						// Selon créneaux AM ou créneaux PM :
						if($demijourAP=='_am') {
							$prem = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT debut FROM ".$prefix."_horaires ORDER BY debut ASC LIMIT 1"));
							$last = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT fin FROM ".$prefix."_horaires WHERE debut < '12:00:00' AND hor_name NOT LIKE 'A%' ORDER BY fin DESC LIMIT 1"));
							$hinter=strtotime($last['fin'])-strtotime($prem['debut']);
						}
						else if($demijourAP=='_pm') {
							if($freq_gestion_jourscren['valeur']==0) {
								$prem = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT debut FROM ".$prefix."_horaires WHERE fin >= '12:00:00' AND (hor_name LIKE 'A%' OR ((hor_name LIKE 'R%' OR hor_name LIKE 'T%' OR hor_name LIKE 'H%' OR hor_name LIKE 'C%') AND hor_freq LIKE '001%' AND debut >= '12:00:00')) ORDER BY debut ASC LIMIT 1"));
							}
							else {
								$prem = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT debut FROM ".$prefix."_horaires WHERE fin >= '12:00:00' AND (hor_name LIKE 'A%' OR ((hor_name LIKE 'R%' OR hor_name LIKE 'T%' OR hor_name LIKE 'H%' OR hor_name LIKE 'C%') AND hor_freq LIKE '%".$dayl."001%' AND debut >= '12:00:00')) ORDER BY debut ASC LIMIT 1"));
							}
							$last = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT fin FROM ".$prefix."_horaires ORDER BY fin DESC LIMIT 1"));
							$hinter=strtotime($last['fin'])-strtotime($prem['debut']);
						}
						$tabdemheight = ($hinter/60)*0.85;
						// demi-journée fermée
						${'edt_tableu'.$userid.'e'.$espid} .='<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$tabdemheight.'px;"></div>';
						$hided++;
					}
					if($demijourAP=='_am') {
						$last = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT hor_name FROM ".$prefix."_horaires WHERE hor_name LIKE 'M%' ORDER BY fin DESC LIMIT 1"));
						if($last['hor_name']==$plage_horname[$h]) { $hided = 0; }
					}
				}
				else {
					$hided=0;
					if(@$debpost>$plageDebut[$h]&&@$finprec<$plageFin[$h]) {
						// créneaux ouverts
						include('./db/proc/edt_cellule.php');
						${'edt_tableu'.$userid.'e'.$espid} .=$edt_cellweb;
						if(@$postdiv==1) {
							$hinter_inter=strtotime($plageFin[$h])-strtotime($end);
							$inter_height = (($hinter_inter/60)*0.85);
							${'edt_tableu'.$userid.'e'.$espid} .='<div class="recept_open" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$inter_height.'px;border:0;clear:both;display:block;"></div>';
						}
						$hideh=0;
					}
					else { $hideh++; }								
				}
			}
			else if($plage_open[$dayl][$h]==0&&$breaktd_edt[$j]!='close'&&$hided==0) {
				// créneaux fermés
				if($debpost>$plageDebut[$h]&&@$finprec<$plageFin[$h]) {
					$precnum = $num_creneau[$h]-1;
					$bef = mysqli_query($bdd,"SELECT fin FROM ".$prefix."_horaires WHERE num_creneau = '$precnum'");
					if(mysqli_num_rows($bef)==1) {
						$bef_info = mysqli_fetch_assoc($bef);
						if($plageDebut[$h]!=$bef_info['fin']&&$bef_info['fin']>=@$finprec) {
							$hinter_inter=strtotime($plageDebut[$h])-strtotime($bef_info['fin']);
							$inter_height = (($hinter_inter/60)*0.85)+1;
							$edt_table .= '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$inter_height.'px;"></div>';
						}
					}
					${'edt_tableu'.$userid.'e'.$espid} .='<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:';
					if(@$checkpost_nb==1) { $closeheight = $heightspe; }
					else if(@$checkprec_info['specifique']==1) { $closeheight = $heightspe; }
					else { $closeheight = $heighthor[$h]; }
					//if($closeheight < 20) { $closeheight = '30'; }
					${'edt_tableu'.$userid.'e'.$espid} .=$closeheight.'px;';
					if(!isset($plage_horname[$posth])) { $edt_table .= 'clear:both;'; }
					${'edt_tableu'.$userid.'e'.$espid} .='"></div>';
					$loss = 8;
					$hideh=0;
				}
				else { $hideh++; }
			}
		}
	}
	${'edt_tableu'.$userid.'e'.$espid} .='</div>';
}
${'edt_tableu'.$userid.'e'.$espid} .='</div>';
${'edt_tablepdfu'.$userid.'e'.$espid} = ${'edt_tableu'.$userid.'e'.$espid};
$_SESSION['edt_tablepdfu'.$userid.'e'.$espid] = ${'edt_tablepdfu'.$userid.'e'.$espid};
echo ${'edt_tableu'.$userid.'e'.$espid};
echo '<div name="noprint" style="clear:both;">';
if(@$_SESSION['user_droits'][2]==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" value="1" min="1" max="52" />';
	}
	echo ' <input class="bouton boutedt" type="submit" name="edtu'.$userid.'e'.$espid.'_valid" value="'.VALID.'" /></p>';		
}
echo '</div>';
echo '</form>';
?>
