<?php

$jouraff = date("Ymd", mktime(0,0,0,$mois,$jour-1+$j,$annee));
$jouraffvisu = date("Y-m-d", mktime(0,0,0,$mois,$jour-1+$j,$annee));
$jouraffvisuD = date("D", mktime(0,0,0,$mois,$jour-1+$j,$annee));
$horstart = substr($plageH[$h],0,2).substr($plageH[$h],3,2).'00';
$horend = substr($plageHend[$h],0,2).substr($plageHend[$h],3,2).'00';
$dtstart = $jouraff.'T'.$horstart;
$dtend = $jouraff.'T'.$horend;
// Récupération des données BDD de event et briques :
$tabevent_select = 
"SELECT event.dtstart, event.dtend, event.week, event.hor_id, event.seq_id, event.hor_name, event.duration, 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, espevent.uid, espevent.espace_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[$h]."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
$tabevent = mysqli_query($bdd,$tabevent_select);
$tabevent_nb = mysqli_num_rows($tabevent);

if($tabevent_nb==1) {
	$tabevent_info = mysqli_fetch_assoc($tabevent);
	$deb = substr($tabevent_info['dtstart'],9,2).':'.substr($tabevent_info['dtstart'],11,2).':'.substr($tabevent_info['dtstart'],13,2);
	$end = substr($tabevent_info['dtend'],9,2).':'.substr($tabevent_info['dtend'],11,2).':'.substr($tabevent_info['dtend'],13,2);
	if(@$checkprec_nb==0&&($tabevent_info['dtstart']>$dtstart)) {
		$hinter_inter=strtotime($deb)-strtotime($plageDebut[$h]);
		$inter_height = (($hinter_inter/60)*0.85);
		$edt_table .= '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$inter_height.'px;border:0;"></div>';
	}
	else if($deb!=$finprec_test&&$finprec_test!='::') {
		$hinter_inter=strtotime($plageDebut[$h])-strtotime($finprec_test);
		$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;border:0;"></div>';
	}
	else if($finprec_test=='::') {
		$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);
				$edt_table .= '<div class="td recept_close" style="background-color:'.substr($cren_colors['valeur'],14,7).';height:'.$inter_height.'px;border:0;"></div>';
			}
		}
	}
}
else  {
	$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($bef_info['fin'])-strtotime($plageDebut[$h]);
			$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>';
		}
	}
}

// Affichage de la cellule :
if($tabevent_nb==1) {
	// texte libre :
	if($tabevent_info['textelibre']!="") {
		if($colorsys_info['valeur']=='niv'&&$colortex_info['valeur']=='1') {
			$colorniv = null;
			if($nivs_info['valeur']=="PER") { 
				foreach ($sorted_caracs as $prefixe => $niveau) {
				    // Si la division commence par ce préfixe, on a trouvé le niveau
				    if (strpos($briksup['textelibre'], $prefixe) === 0) {
				        $colorniv = $niveau;
				        break; // On arrête la boucle une fois le niveau trouvé
				    }
				}
				if($colorniv!==null&&@$colorniv!='r') {
					$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
					$color_info = mysqli_fetch_assoc($color);
					$bgcolor = $color_info['bgcolor'];
					$nocolniv = '0';
				}
				else { $nocolniv = '1'; }
			}
			else {
				$colorniv = substr($tabevent_info['textelibre'],0,1);
				if($colorniv!='6'&&$colorniv!='5'&&$colorniv!='4'&&$colorniv!='3'&&$colorniv!='2'&&$colorniv!='1'&&$colorniv!='t'&&$colorniv!='T'&&$colorniv!='a') { $nocolniv = '1'; }
				else {
					$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
					$color_info = mysqli_fetch_assoc($color);
					$bgcolor = $color_info['bgcolor'];
					$nocolniv = '0';
				}
			} 			
		}
		else { $nocolniv = '1'; }
		if($nocolniv=='1') {
			if($tabevent_info['close']=='1') {
				$bgcolor = substr($cren_colors['valeur'],7,7);
			}
			else {
				$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = 'r'");
				$color_info = mysqli_fetch_assoc($color);
				$bgcolor = $color_info['bgcolor'];
			}
		}
		$classe=0;
		$edt_cell = '<div id="inbrik'.$h.$j.$usercode.$espid.'" class="td recept_open';
		$edt_cell .= '" style="background-color:'.$bgcolor.';height:';
		if(@$tabevent_info['specifique'] == '1') {
			$deb = substr($tabevent_info['dtstart'],9,2).':'.substr($tabevent_info['dtstart'],11,2).':'.substr($tabevent_info['dtstart'],13,2);
			$fin = substr($tabevent_info['dtend'],9,2).':'.substr($tabevent_info['dtend'],11,2).':'.substr($tabevent_info['dtend'],13,2);
			$hinter=strtotime($fin)-strtotime($deb);
			$heightspe = ($hinter/60)*0.85;
			$edt_cell .= $heightspe;
		}
		else if(@$checkpost_nb==1) { $edt_cell .= $heightspe; }
		else if(@$checkprec_info['specifique']==1) { $edt_cell .= $heightspe; }
		else { $heightspe = $heighthor[$h]; $edt_cell .= $heighthor[$h]; }
		$edt_cell .= 'px;"';
		include('edt_cellule_resavisu.php');
		$edt_cell .= '><div class="cartedt';
		$edt_cell .= '" style="border:0;';
		if(@$heightspe<30||@$heighthor[$h]<30) { $edt_cell .= ' font-size:0.6em;line-height:5px;padding-top:-5px;'; }
		$edt_cell .= '" name="'.$tabevent_info['brik_id'].'">';
		if(strlen($tabevent_info['textelibre'])>16||(@$heightspe<30||@$heighthor[$h]<30)) { 
			$edt_cell .= '<span class="txtlibre_2"'; }
		else { $edt_cell .= '<span class="txtlibre_1"'; }
		$edt_cell .= '>'.$tabevent_info['textelibre'].'</span>';
		if(@$heightspe<30||@$heighthor[$h]<30) { $edt_cell .= '</div>'; } 
		else { include('edt_brikoptions.php'); }
	}
	// classe :
	else {
		$classe=1;
		$seq = mysqli_query($bdd,"SELECT seq_id, seq_nom, SUBSTR(seq_nom,1,13) FROM ".$prefix."_sequences WHERE seq_id ='".$tabevent_info['seq_id']."'");
		$seq_info = mysqli_fetch_assoc($seq);
		$seq_nomaff = @mb_ereg_replace("[?]","",mb_convert_encoding($seq_info['SUBSTR(seq_nom,1,13)'],"utf-8"));
		$divaff = mb_ereg_replace("[?]","",mb_convert_encoding($tabevent_info['SUBSTR(brik.division,1,9)'],"utf-8"));
		if($colorsys_info['valeur']=='niv') {
			$colorniv = null;
			if($nivs_info['valeur']=="PER") { 
				foreach ($sorted_caracs as $prefixe => $niveau) {
				    // Si la division commence par ce préfixe, on a trouvé le niveau
				    if (strpos($tabevent_info['division'], $prefixe) === 0) {
				        $colorniv = $niveau;
				        break; // On arrête la boucle une fois le niveau trouvé
				    }
				}
				if($colorniv!==null&&@$colorniv!='r') {
					$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
					$color_info = mysqli_fetch_assoc($color);
					$bgcolor = $color_info['bgcolor'];
				}
			}
			else {
				$colorniv = substr($tabevent_info['division'],0,1);
				if($colorniv!='6'&&$colorniv!='5'&&$colorniv!='4'&&$colorniv!='3'&&$colorniv!='2'&&$colorniv!='1'&&$colorniv!='t'&&$colorniv!='T') { $colorniv = 'a'; }
				$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
				$color_info = mysqli_fetch_assoc($color);
				$bgcolor = $color_info['bgcolor'];
			}			
		}
		else if($colorsys_info['valeur']=='cod') {
			$color = mysqli_query($bdd,"SELECT bgcolor_code FROM ".$prefix."_edtcode WHERE code = '".$tabevent_info['code']."'");
			$color_info = mysqli_fetch_assoc($color);
			$bgcolor = $color_info['bgcolor_code'];
		}
		if($bgcolor=='') { $bgcolor = "#ffffff"; }
		$edt_cell = '<div id="inbrik'.$h.$j.$usercode.$espid.'" class="td recept_open';
		$edt_cell .= '" style="background-color:'.$bgcolor.';height:';
		if(@$tabevent_info['specifique'] == '1') {
			$deb = substr($tabevent_info['dtstart'],9,2).':'.substr($tabevent_info['dtstart'],11,2).':'.substr($tabevent_info['dtstart'],13,2);
			$fin = substr($tabevent_info['dtend'],9,2).':'.substr($tabevent_info['dtend'],11,2).':'.substr($tabevent_info['dtend'],13,2);
			$hinter=strtotime($fin)-strtotime($deb);
			$heightspe = ($hinter/60)*0.85;
			$edt_cell .= $heightspe;
		}
		else if(@$checkpost_nb==1) { $edt_cell .= $heightspe; }
		else if(@$checkprec_info['specifique']==1) { $edt_cell .= $heightspe; }
		else { $heightspe = $heighthor[$h]; $edt_cell .= $heighthor[$h]; }
		$edt_cell .= 'px;"';
		include('edt_cellule_resavisu.php');
		$edt_cell .= '><div><div class="cartedt';
		$edt_cell .= '" style="border:0;';
		if(@$heightspe<30||@$heighthor[$h]<30) { $edt_cell .= ' font-size:0.6em;line-height:5px;'; }
		$edt_cell .= '" name="'.$tabevent_info['brik_id'].'">';
		if(@$seq_info['seq_nom']!=""||@$tabevent_info['summary']!="") {
			$edt_cell .= '<a class="infobulle" style="text-decoration:none;" aria-label="';
			if(@$seq_info['seq_nom']!="") {
				$edt_cell .= $seq_info['seq_nom'];
			}
			if($tabevent_info['summary']!="") {
				if(@$seq_info['seq_nom']!=""&&@$tabevent_info['summary']!="") {
					$edt_cell .= '&nbsp;: ';
				}
				$edt_cell .= $tabevent_info['summary'];
			}
			$edt_cell .= '">';
		}
		$edt_cell .= '<span ';
		if($seq_nomaff==""&&(@strlen($briksup['division'])<=5||@$briksup['code']=="")) 
			{ $edt_cell .= ' class="txtlibre_1"'; }
		$edt_cell .= '>';
		if(strlen($tabevent_info['division'])>5) {
			$edt_cell .= $tabevent_info['division'];
			if($tabevent_info['code']!=""&&(@$heightspe>30||@$heighthor[$h]>30)) {
				$edt_cell .= '<br>'.$tabevent_info['code'].'';
			}
		}
		else {
			$edt_cell .= $tabevent_info['division'];
			if($tabevent_info['code']!="") {
				$edt_cell .= ' - '.$tabevent_info['code'].'';
			}
			if($seq_nomaff!=""&&(@$heightspe>30||@$heighthor[$h]>30)) {
				$edt_cell .= '<br>'.$seq_nomaff.'';
				if($seq_nomaff!=$seq_info['seq_nom']) {
					$edt_cell .= '...';
				}
			}
		}
		$edt_cell .= '</span>';
		if(@$seq_info['seq_nom']!=""||@$tabevent_info['summary']!="") {
			$edt_cell .= '</a>';
		}
		$edt_cell .= '</div>';
		if(@$heightspe<30||@$heighthor[$h]<30) { $edt_cell .= '</div>'; } 
		else { include('edt_brikoptions.php'); }
	}
	if(@$checkpost_nb==0&&($tabevent_info['dtend']<$dtend)) {
		$postdiv=1;
	}
	else { $postdiv=0; }
}
else { 
	$postdiv=0;
	$classe=0;
	$tabevent_info['close']=0;
	if(@$checkpost_nb==1) { } else if(@$checkprec_info['specifique']==1) { }
	else { $heightspe = 50; }
	$edt_cell = '<div id="inbrik'.$h.$j.$usercode.$espid.'" class="td recept_open';
	if($heightspe>30||$heighthor[$h]>30) { $edt_cell .= ' pdfmarg '; }
	if($heightspe<30||$heighthor[$h]<30) { $edt_cell .= ' openmsgsped '; }
	$edt_cell .= '" style="background-color:'.substr($cren_colors['valeur'],0,7).';height:';
	if(@$checkpost_nb==1) { $edt_cell .= $heightspe; }
	else if(@$checkprec_info['specifique']==1) { $edt_cell .= $heightspe; }
	else { $edt_cell .= $heighthor[$h]; $heightspe = 50; }
	$edt_cell .= 'px;"';
	include('edt_cellule_resavisu.php');
	$edt_cell .= '>';
	$edt_cell .= '<span class="openmsg ';
	if($heightspe<30||$heighthor[$h]<30) { $edt_cell .= ' openmsgspe '; }
	$edt_cell .= '"';
	$edt_cell .= '>'.$openmsg_txt.'</span>'; }
	$edt_cell .= '</div>';
	$edt_cellinput = '<input type="hidden" name="dtstart'.$h.$j.$usercode.$espid.'" value="'.$dtstart.'" />
	<input type="hidden" name="dtend'.$h.$j.$usercode.$espid.'" value="'.$dtend.'" />
	<input type="hidden" name="week'.$h.$j.$usercode.$espid.'" value="'.$week.'" />
	<input type="hidden" name="hor_id'.$h.$j.$usercode.$espid.'" value="'.$plage_horid[$h].'" />
	<input type="hidden" name="hor_name'.$h.$j.$usercode.$espid.'" value="'.$plage_horname[$h].'" />
	<input type="hidden" id="ipbrik'.$h.$j.$usercode.$espid.'" name="brik'.$h.$j.$usercode.$espid.'" value="" />';
	
	// Script pour enregistrer les données dans la cellule et dans l'input associé
	if(@$nb_allbriks>0) {
		$edt_cellscript = '<script>
		
		var td'.$h.$j.$usercode.$espid.' = document.getElementById("inbrik'.$h.$j.$usercode.$espid.'");
		
		// création d\'une fonction qui affiche du texte
		window.brik_func'.$h.$j.$usercode.$espid.' = function(){
			if (typeof brik_contenu !== "undefined") {
				td'.$h.$j.$usercode.$espid.'.innerHTML = brik_contenu;
			}
			document.getElementById("ipbrik'.$h.$j.$usercode.$espid.'").value = td'.$h.$j.$usercode.$espid.'.innerHTML;
			document.getElementById("brik'.$briksup['brik_id'].'").style = "background-color:'.$bgcolor.';border:0;"
			
		}
																
		// affichage du texte au click
		td'.$h.$j.$usercode.$espid.'.onclick = brik_func'.$h.$j.$usercode.$espid.';
								
		</script>';
	}

	if(isset($_POST['nbweeks'])) {
		$nbweeks = $_POST['nbweeks'];
	}
	else {
		$nbweeks = 1;
	}
	if(!isset($nbweeks)) {
		$nbweeks = 1;
	}

	// Traitement de la validation pour la semaine :
	if(isset($_POST['edtu'.$userid.'e'.$espid.'_valid'])) {
		$_SESSION['reload']=1;
		$dday = date("Ymd");
		if(htmlentities($_POST['brik'.$h.$j.$usercode.$espid.''])!=""&&htmlentities($_POST['brik'.$h.$j.$usercode.$espid.''])!=htmlentities($openmsg_txt)) {
			// Récupération de l'id de la brique
			$html = htmlentities($_POST['brik'.$h.$j.$usercode.$espid.'']);
			$idbrikdeb = strstr($html, 'name=');
			$sdiv_find1 = '&'; // on recherche le & avant l'id
			$sdiv_pos1 = strpos($idbrikdeb, $sdiv_find1, 3); // on définit la position du 1er &
			$sdiv_find2 = '&'; // on recherche le & après l'id
			$sdiv_pos2 = strpos($idbrikdeb, $sdiv_find2, 8); // on définit la position du 2e &
			$idbrikend = $sdiv_pos2 - $sdiv_pos1 - 6;
			$idbrik = substr($idbrikdeb, 11, $idbrikend);
			$week = $_POST['week'.$h.$j.$usercode.$espid.''];
			
			// Récupération de la séquence associée
			$seq_select = "SELECT seq.seq_id FROM ".$prefix."_sequences seq 
			INNER JOIN ".$prefix."_edtbrik brik
			ON seq.seq_id = brik.seq_id 
			WHERE brik.brik_id = '".$idbrik."' ";
			$seq = mysqli_query($bdd,$seq_select);
			$seq_nb = mysqli_num_rows($seq);
			if($seq_nb==1) {
				$seq_info = mysqli_fetch_assoc($seq);
				$seq_id = $seq_info['seq_id'];
			}
			else {
				$seq_id = 0;
			}			
			
			for($i=1;$i<($nbweeks+1);$i++) {
				if($i==1) {
					// Vérification que le créneau soit déjà pris ou non par un événement
					$verif_select = "SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."'";
				}
				else {
					$dayson = 7*($i-1);
					$jourplus = date("Ymd", mktime(0,0,0,$mois,$jour-1+$j+$dayson,$annee));
					$moisplus = date("oW", mktime(0,0,0,$mois,$jour-1+$j+$dayson,$annee));
					$dtstartplus = $jourplus.'T'.$horstart;
					$dtendplus = $jourplus.'T'.$horend;
					// Vérification que le créneau soit déjà pris ou non par un événement
					$verif_select = "SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."'";
				}
				$verif = mysqli_query($bdd,$verif_select);
				$nb_verif = mysqli_num_rows($verif);
				// Gestion de l'insert ou de l'update
				if($nb_verif==0) { // SI AUCUN EVENEMENT SUR CRENEAU
					include('edt_insert.php');
				}
				else if($nb_verif>0) { // SI EVENT SUR CRENEAU
					// Vérification que le créneau soit déjà pris ou non par un événement pour le même user dans le même espace
					if($i==1) {
						$verif_select = "SELECT * FROM ".$prefix."_edtevent event 
						INNER JOIN ".$prefix."_userevent userevent 
						ON event.uid = userevent.uid 
						INNER JOIN ".$prefix."_espaces_event espevent 
						ON event.uid = espevent.uid 
						WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
					}
					else {
						$verif_select = "SELECT * FROM ".$prefix."_edtevent event 
						INNER JOIN ".$prefix."_userevent userevent 
						ON event.uid = userevent.uid 
						INNER JOIN ".$prefix."_espaces_event espevent 
						ON event.uid = espevent.uid 
						WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
					}
					$verif = mysqli_query($bdd,$verif_select);
					$nb_verif = mysqli_num_rows($verif);
					if($nb_verif==0) { // SI EVENT POUR AUTRE USER OU AUTRE ESPACE
						// Vérification que le créneau soit déjà pris ou non par un événement avec même brique
						if($i==1) {
							$verif_select = "SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND brik_id = '".$idbrik."'";
						}
						else {
							$verif_select = "SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND brik_id = '".$idbrik."'";
						}
						$verif = mysqli_query($bdd,$verif_select);
						$nb_verif = mysqli_num_rows($verif);
						if($nb_verif==0) { // SI BRIQUE DIFFERENTE
							include('edt_insert.php');
						}
						if($nb_verif>0) { // SI MEME BRIQUE
							if($i==1) {
								// récupération de l'uid pour passage dans table ".$prefix."_userevent :
								$uidup_select = "SELECT uid FROM ".$prefix."_edtevent WHERE dtstart = '".$_POST['dtstart'.$h.$j.$usercode.$espid.'']."' AND dtend = '".$_POST['dtend'.$h.$j.$usercode.$espid.'']."' AND hor_id = '".$_POST['hor_id'.$h.$j.$usercode.$espid.'']."' AND week = '".$_POST['week'.$h.$j.$usercode.$espid.'']."' AND brik_id = '".$idbrik."' AND $seq_id = '".$seq_id."'";
							}
							else {
								// récupération de l'uid pour passage dans table ".$prefix."_userevent :
								$uidup_select = "SELECT uid FROM ".$prefix."_edtevent WHERE dtstart = '".$dtstartplus."' AND dtend = '".$dtendplus."' AND hor_id = '".$_POST['hor_id'.$h.$j.$usercode.$espid.'']."' AND week = '".$moisplus."' AND brik_id = '".$idbrik."' AND $seq_id = '".$seq_id."'";
							}
							$uidup = mysqli_query($bdd,$uidup_select);
							$uid_info = mysqli_fetch_assoc($uidup);
							$uid = $uid_info['uid'];
							include('edt_userevent.php');
						}
					}
					else if($nb_verif>0) { // SI EVENT POUR CET USER OU ESPACE
						// Vérification que le créneau soit déjà pris ou non par un événement avec même brique et même user et même espace
						if($i==1) {
							$verif_select = "SELECT * FROM ".$prefix."_edtevent event 
							INNER JOIN ".$prefix."_userevent userevent 
							ON event.uid = userevent.uid 
							INNER JOIN ".$prefix."_espaces_event espevent 
							ON event.uid = espevent.uid 
							WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."' AND brik_id = '".$idbrik."'";
						}
						else {
							$verif_select = "SELECT * FROM ".$prefix."_edtevent event 
							INNER JOIN ".$prefix."_userevent userevent 
							ON event.uid = userevent.uid 
							INNER JOIN ".$prefix."_espaces_event espevent 
							ON event.uid = espevent.uid 
							WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."' AND brik_id = '".$idbrik."'";
						}
						$verif = mysqli_query($bdd,$verif_select);
						$nb_verif = mysqli_num_rows($verif);
						if($nb_verif==0) { // SI CELA CONCERNE UNE AUTRE BRIQUE 
							if($verrouillage==0||substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)>=$dday) { 
							// on ne peut modifier que le jour même ou à l'avenir
								// Select pour récupération de l'id de l'événement
								if($i==1) {
									$event_select = "SELECT * FROM ".$prefix."_edtevent event
									INNER JOIN ".$prefix."_userevent userevent 
									ON event.uid = userevent.uid 
									INNER JOIN ".$prefix."_espaces_event espevent 
									ON event.uid = espevent.uid 
									WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
									$event = mysqli_query($bdd,$event_select);
								}
								else {
									$event_select = "SELECT * FROM ".$prefix."_edtevent event
									INNER JOIN ".$prefix."_userevent userevent 
									ON event.uid = userevent.uid 
									INNER JOIN ".$prefix."_espaces_event espevent 
									ON event.uid = espevent.uid 
									WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
									$event = mysqli_query($bdd,$event_select);
								}
								$event_info = mysqli_fetch_assoc($event);
								$check=0;
								$user = mysqli_query($bdd,"SELECT user_id FROM ".$prefix."_userevent GROUP BY user_id");
								if($userid!=0) { 
									if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_userevent WHERE user_id = '0' AND uid = '".$event_info['uid']."'"))>0) {
										$check = 1;
										mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event_info['uid']."' AND user_id = '".$userid."'");	
									}
								}
								foreach($user as $user_info) {
									if($user_info['user_id'] != $userid) { 
										if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_userevent WHERE user_id = '".$user_info['user_id']."' AND uid = '".$event_info['uid']."'"))>0) {
											$check = 1;
											mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event_info['uid']."' AND user_id = '".$userid."'");
											break;
										}
									}
								}
								$esp = mysqli_query($bdd,"SELECT espace_id FROM ".$prefix."_espaces GROUP BY espace_id");
								if($espid !=0) { 
									if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces_event WHERE espace_id = '0' AND uid = '".$event_info['uid']."'"))>0) {
										$check = 1;
										mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event_info['uid']."' AND espace_id = '".$espid."'");	
									}
								}
								foreach($esp as $esp_info) {
									if($esp_info['espace_id'] != $espid) { 
										if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces_event WHERE espace_id = '".$esp_info['espace_id']."' AND uid = '".$event_info['uid']."'"))>0) {
											$check = 1;
											mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event_info['uid']."' AND espace_id = '".$espid."'");	
											break;
										}
									}
								}
								if($check==1) {
									include('edt_insert.php');
								}
								else if($check==0) {
									mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET brik_id = '".$idbrik."', seq_id = '".$seq_id."' WHERE uid = '".$event_info['uid']."'");
								}
							}
						}
					}
				}
			}
		}
		else if(htmlentities($_POST['brik'.$h.$j.$usercode.$espid.''])==htmlentities($openmsg_txt)) {
			for($i=1;$i<($nbweeks+1);$i++) {
				if($i==1) {
					// Vérification que le créneau soit déjà pris ou non par un événement pour le user ou espace
					$verif_select = "SELECT * FROM ".$prefix."_edtevent event
					INNER JOIN ".$prefix."_userevent userevent 
					ON event.uid = userevent.uid 
					INNER JOIN ".$prefix."_espaces_event espevent 
					ON event.uid = espevent.uid 
					WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
					$verif = mysqli_query($bdd,$verif_select);
					$nb_verif = mysqli_num_rows($verif);
				}
				else {
					$dayson = 7*($i-1);
					$jourplus = date("Ymd", mktime(0,0,0,$mois,$jour-1+$j+$dayson,$annee));
					$moisplus = date("oW", mktime(0,0,0,$mois,$jour-1+$j+$dayson,$annee));
					$dtstartplus = $jourplus.'T'.$horstart;
					$dtendplus = $jourplus.'T'.$horend;
					// Vérification que le créneau soit déjà pris ou non par un événement
					$verif_select = "SELECT * FROM ".$prefix."_edtevent event
					INNER JOIN ".$prefix."_userevent userevent 
					ON event.uid = userevent.uid 
					INNER JOIN ".$prefix."_espaces_event espevent 
					ON event.uid = espevent.uid 
					WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
					$verif = mysqli_query($bdd,$verif_select);
					$nb_verif = mysqli_num_rows($verif);
				}
			
				// Gestion du delete
				if($nb_verif==1) {
					if($verrouillage==0||substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)>=$dday) { 
					// on ne peut supprimer que le jour même ou à l'avenir
						if($i==1) {
							// Select pour récupération de l'id de l'événement
							$event_select = "SELECT * FROM ".$prefix."_edtevent event
							INNER JOIN ".$prefix."_userevent userevent 
							ON event.uid = userevent.uid 
							INNER JOIN ".$prefix."_espaces_event espevent 
							ON event.uid = espevent.uid 
							WHERE dtstart LIKE '".substr($_POST['dtstart'.$h.$j.$usercode.$espid.''],0,8)."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
						}
						else {
							// Select pour récupération de l'id de l'événement
							$event_select = "SELECT * FROM ".$prefix."_edtevent event
							INNER JOIN ".$prefix."_userevent userevent 
							ON event.uid = userevent.uid 
							INNER JOIN ".$prefix."_espaces_event espevent 
							ON event.uid = espevent.uid 
							WHERE dtstart LIKE '".$jourplus."%' AND hor_name = '".$_POST['hor_name'.$h.$j.$usercode.$espid.'']."' AND userevent.user_id = '".$userid."' AND espevent.espace_id = '".$espid."'";
						}
						$event = mysqli_query($bdd,$event_select);
						$event_info = mysqli_fetch_assoc($event);
						$check = 0;
						$user = mysqli_query($bdd,"SELECT user_id FROM ".$prefix."_userevent GROUP BY user_id");
						if($userid!=0) { 
							if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_userevent WHERE user_id = '0' AND uid = '".$event_info['uid']."'"))>0) {
								$check = 1;
								mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event_info['uid']."' AND user_id = '".$userid."'");	
							}
						}
						foreach($user as $user_info) {
							if($user_info['user_id'] != $userid) { 
								if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_userevent WHERE user_id = '".$user_info['user_id']."' AND uid = '".$event_info['uid']."'"))>0) {
									$check = 1;
									mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event_info['uid']."' AND user_id = '".$userid."'");
									break;
								}
							}
						}
						$esp = mysqli_query($bdd,"SELECT espace_id FROM ".$prefix."_espaces GROUP BY espace_id");
						if($espid !=0) { 
							if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces_event WHERE espace_id = '0' AND uid = '".$event_info['uid']."'"))>0) {
								$check = 1;
								mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event_info['uid']."' AND espace_id = '".$espid."'");	
							}
						}
						foreach($esp as $esp_info) {
							if($esp_info['espace_id'] != $espid) { 
								if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces_event WHERE espace_id = '".$esp_info['espace_id']."' AND uid = '".$event_info['uid']."'"))>0) {
									$check = 1;
									mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event_info['uid']."' AND espace_id = '".$espid."'");	
									break;
								}
							}
						}
						if($check==0) {
							mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$event_info['uid']."'");
							mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event_info['uid']."' AND user_id = '".$userid."'");
							mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event_info['uid']."' AND espace_id = '".$espid."'");
						}
					}
				}
			}
		}
	$checkvide = mysqli_query($bdd, "SELECT uid FROM ".$prefix."_edtevent WHERE brik_id = 0");
	foreach($checkvide as $checkf) {
		mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$checkf['uid']."'");
		mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$checkf['uid']."'");
		mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$checkf['uid']."'");
	}
}
if(isset($edt_cellscript)) {
	$edt_cellweb = $edt_cell.$edt_cellinput.$edt_cellscript;
} else { $edt_cellweb = $edt_cell.$edt_cellinput; }

?>
