<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
if(isset($_SESSION['user_droits'])) {
	$jourDeb  = new DateTime($_REQUEST['jour1']);
	$jourFin  = new DateTime($_REQUEST['jour2']);
	foreach(new DatePeriod($jourDeb, new DateInterval('P7D') /* pas de 7 jours */, $jourFin) as $dtdup) {
		$datetest = $dtdup->format('Y-m-d');
		$datetest_var = $dtdup->format('Ymd');
		$dateweek = $dtdup->format('oW');
		$opencheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT statut FROM ".$prefix."_calendrier WHERE date = '".$datetest."'"));
		if($opencheck['statut']==1&&($datetest!=$_REQUEST['jour1'])) {
			if($_REQUEST['t'.$datetest_var]!='0') {
				$date_ins = $_REQUEST['t'.$datetest_var];
				$date_check = str_replace("-","",$date_ins);
				if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".$date_check."%' AND hor_name = '".$_REQUEST['hor_name']."'"))==0) {
					mysqli_query($bdd,"INSERT INTO ".$prefix."_edtevent (uid, dtstart, dtend, hor_id, hor_name, week, brik_id, seq_id, specifique, duration) VALUES ('', '".$date_check.'T'.substr($_REQUEST['dtstart'],9,6)."', '".$date_check.'T'.substr($_REQUEST['dtend'],9,6)."', '".$_REQUEST['hor_id']."', '".$_REQUEST['hor_name']."', '".$_REQUEST['t'.$dateweek]."', '".$_REQUEST['brik_id']."', '".$_REQUEST['seq_id']."', '".$_REQUEST['specifique']."', '".$_REQUEST['duration']."')");
					// récupération de l'uid pour passage dans table ".$prefix."_userevent :
					$uidup_select = "SELECT uid FROM ".$prefix."_edtevent WHERE dtstart = '".$date_check.'T'.substr($_REQUEST['dtstart'],9,6)."' AND dtend = '".$date_check.'T'.substr($_REQUEST['dtend'],9,6)."' AND hor_id = '".$_REQUEST['hor_id']."' AND week = '".$_REQUEST['t'.$dateweek]."' AND brik_id = '".$_REQUEST['brik_id']."' AND seq_id = '".$_REQUEST['seq_id']."'";
					$uidup = mysqli_query($bdd,$uidup_select);
					$uid_info = mysqli_fetch_assoc($uidup);
					$uid = $uid_info['uid'];
					if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_userevent WHERE uid = '".$uid."' AND user_id = '".$_REQUEST['usercode']."'"))==0) {
						mysqli_query($bdd,"INSERT INTO ".$prefix."_userevent (uid, user_id) VALUES ('".$uid."', '".$_REQUEST['usercode']."')");
					}
					if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces_event WHERE uid = '".$uid."' AND espace_id = '".$_REQUEST['espcode']."'"))==0) {
						mysqli_query($bdd,"INSERT INTO ".$prefix."_espaces_event (uid, espace_id) VALUES ('".$uid."', '".$_REQUEST['espcode']."')");
					}
				}
				else { //echo '<p class="signal">Conflit à contrôler le '.$date_ins.'.</p>'; 
				}
			}
		}
	}
}
mysqli_close($bdd);
?>