<?php
$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 ORDER BY fin DESC LIMIT 1"));
$hinter_ext=strtotime($last['fin'])-strtotime($prem['debut']);
$tabheight = (($hinter_ext/60)*0.85)-1;

$table_width = 76+(136*$nbJour);
if($nbJour==7) { $table_width += 2; }
else if($nbJour==5) { $table_width -= 2; }
$total_height = $tabheight+44;
$edt_table = '<div class="edt_grille" style="display:block;width:'.$table_width.'px;background-color:'.substr($cren_colors['valeur'],14,7).';" id="html-imageu0e0">';
$edt_table .= '<div class="td titleEdt';
if(@$edttitleopt_infos[3]=='1') { $edt_table .= ' none'; }
$edt_table .= '" name="noprint" style="background-color:'.@substr($cren_colors['valeur'],21,7).';">'.@$entete_tit.'</div>';
$edt_table .= '<div style="float:left;width:60px;">';
$edt_table .= '<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_table .= '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$inter_height.'px;"></div>';
		}
	}
	$edt_table .= '<div class="th heure" style="height:'.$heighthor[$h].'px;background-color:#fff;">';
	$edt_table .= '<div';
	if($heighthor[$h]>=30) { $edt_table .= ' class="pdfmarg"'; }
	$edt_table .= '>';
	if($heighthor[$h]<20||$horform_info['valeur']=='2') { $edt_table .= '<div class="pdfmargnone"><span class="fontsmall">'; }
	if($heighthor[$h]>=12.75) { 
		if($horform_info['valeur']=='1') {
			$edt_table .= $plageH[$h]; 
		}
		else if($horform_info['valeur']=='2'&&$heighthor[$h]>=14) {
			$edt_table .= $plageH[$h].'<br>'.TO.'<br>'.$plageHend[$h]; 
		}
		else if($horform_info['valeur']=='3') {
			$edt_table .= $plage_horname[$h]; 
		}
	}
	if($heighthor[$h]<20||$horform_info['valeur']=='2') { $edt_table .= '</span></div>'; }
	$edt_table .= '</div></div>';
}
$edt_table .= '</div>';

// en tête de colonne
for($j = 1; $j < $nbJour+1; $j++) {
	$edt_table .= '<div style="display:inline-block;position:relative;top:auto;float:left;width:138px;">';
	$edt_table .= '<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>';
	$dayl = date("D", mktime(0,0,0,$mois,$jour-1+$j,$annee));
	$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;
	$userid = 0;
	$espid = 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 = '0'
					OR espaces LIKE '0;%'
					OR espaces LIKE '%;0;%'
					OR espaces LIKE '%;0'
				)
		");
		$edt_table .= '<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)>0) {
			$info_msg = mysqli_fetch_assoc($info);
			$edt_table .= '<p><span>'.$info_msg['titre'].'<br>&nbsp;</span></p>';
		}
		$edt_table .= '</div>';
	}
	else if(@$jouropenverif_info['statut']==1) {
		for ($h = 0; $h < $hor_nb; $h++) {
			// Check spécifique sur la brique suivante et précédente :
			$jouraff = date("Ymd", mktime(0,0,0,$mois,$jour-1+$j,$annee));
			$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, userevent.uid, userevent.user_id  
				FROM ".$prefix."_edtevent event
				INNER JOIN ".$prefix."_edtbrik brik
				ON event.brik_id = brik.brik_id
				INNER JOIN ".$prefix."_userevent userevent 
				ON event.uid = userevent.uid 
				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 userevent.user_id = '".$userid."' AND userevent.user_id = '".$userid."' 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 = ($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, userevent.uid, userevent.user_id  
				FROM ".$prefix."_edtevent event
				INNER JOIN ".$prefix."_edtbrik brik
				ON event.brik_id = brik.brik_id
				INNER JOIN ".$prefix."_userevent userevent 
				ON event.uid = userevent.uid 
				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 userevent.user_id = '".$userid."' 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);
				//$finprec = 0;
				@$finprec_test = substr($checkprec_info['dtend'],9,2).':'.substr($checkprec_info['dtend'],11,2).':'.substr($checkprec_info['dtend'],13,2);
				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 = 0;
				$userid = 0;
				$espid = 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_table .= '<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 debut <= '12:00:00' AND hor_name NOT LIKE 'A%' 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
						$espid = 0;			
						include('./db/proc/edt_cellule.php');
						$edt_table .= $edt_cellweb;
						if(@$postdiv==1) {
							$hinter_inter=strtotime($plageFin[$h])-strtotime($end);
							$inter_height = (($hinter_inter/60)*0.85);
							$edt_table .= '<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_table .= '<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_table .= $closeheight.'px;';
					if(!isset($plage_horname[$posth])) { $edt_table .= 'clear:both;'; }
					$edt_table .= '"></div>';
					$loss = 8;
					$hideh=0;
				}
				else { $hideh++; }
			}
		}
	}
	$edt_table .= '</div>';
}

$edt_table .= '</div>';
$edt_tablepdf = $edt_table;
$_SESSION['edt_tablepdfu'.$userid.'e'.$espid] = $edt_tablepdf;
echo $edt_table;

?>