<?php
/*
// Ce module de paramétrage de calendrier est inspiré de celui du logiciel PMB, sous licence CeCILL
// développé par PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
*/
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../../index.php">'.ACCUEIL.'</a>.</p>';
	}
	else {
	include "db/proc/param_calendrier_func.php" ;

	if (isset($_POST['ouvrir']) || isset($_POST['fermer'])) {
		//date
		$date_deb = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick1'])));
		$date_fin = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick2'])));
		$_SESSION['datepick1'] = $_POST['datepick1'];
		$_SESSION['datepick2'] = $_POST['datepick2'];
		if(isset($_POST['j2'])) { $j2 = $_POST['j2']; }
		if(isset($_POST['j3'])) { $j3 = $_POST['j3']; }
		if(isset($_POST['j4'])) { $j4 = $_POST['j4']; }
		if(isset($_POST['j5'])) { $j5 = $_POST['j5']; }
		if(isset($_POST['j6'])) { $j6 = $_POST['j6']; }
		if(isset($_POST['j7'])) { $j7 = $_POST['j7']; }
		if(isset($_POST['j1'])) { $j1 = $_POST['j1']; }
		if (isset($_POST['ouvrir'])) $ouverture=1 ;
			else $ouverture=0 ; 

		$rqt_date = "SELECT if(TO_DAYS('".$date_fin."')>=TO_DAYS('".$date_deb."'),1,0) as OK";
		$resultatdate=mysqli_query($bdd,$rqt_date);
		$res=mysqli_fetch_object($resultatdate) ;
		$date_courante = $date_deb ; 
		while ($res->OK) { 
			$rqt_date = "SELECT dayofweek('".$date_courante."') as jour";
			$resultatdate=mysqli_query($bdd,$rqt_date);
			$res=mysqli_fetch_object($resultatdate) ;
			$jour = "j".$res->jour ;
			// OK : traitement
			if (isset($$jour)) {
				if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date_courante."'"))>0) {
					$rqt_date = "UPDATE ".$prefix."_calendrier SET statut=".$ouverture." WHERE date='".$date_courante."'";
					$resultatdate=mysqli_query($bdd,$rqt_date);
				}
				else {
					$rqt_date = "INSERT INTO ".$prefix."_calendrier SET statut=".$ouverture.", date='".$date_courante."'";
					$resultatdate=mysqli_query($bdd,$rqt_date);
					if (!mysqli_affected_rows($bdd)) die ("INSERT INTO ".$prefix."_calendrier failes") ;	
				}
				if($ouverture=='0') {
					$events = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".substr($date_courante,0,4).substr($date_courante,5,2).substr($date_courante,8,2)."%'");
					foreach($events as $event) {
						mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event['uid']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event['uid']."'");
						mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$event['uid']."'");
					}
				}
			}
			$rqt_date = "SELECT if(to_days(date_add('".$date_courante."', INTERVAL 1 DAY))<=TO_DAYS('".$date_fin."'),1,0) as OK, date_add('".$date_courante."', INTERVAL 1 DAY) as date_courante, dayofweek(date_add('".$date_courante."', INTERVAL 1 DAY)) as jour";
			$resultatdate=mysqli_query($bdd,$rqt_date);
			$res=mysqli_fetch_object($resultatdate) ;
			$date_courante=$res->date_courante ;
		}
	}
	if(isset($_GET['datem']) && isset($_GET['action'])) { $datem = $_GET['datem']; $action = $_GET['action']; }
	if (isset($action) && isset($datem)) {
		$rqt_date = "UPDATE ".$prefix."_calendrier SET statut=if(statut=0, 1, 0) WHERE date='$datem'";
		$resultatdate=mysqli_query($bdd,$rqt_date);
		if (!mysqli_affected_rows($bdd)) {
			$rqt_date = "INSERT INTO ".$prefix."_calendrier set statut=if('".$action."'='O', 1, 0), date='$datem'";
			$resultatdate=mysqli_query($bdd,$rqt_date);
			if (!mysqli_affected_rows($bdd)) die ("INSERT INTO ".$prefix."_calendrier failes") ;	
		}
		if($action=='F') {
			$events = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE dtstart LIKE '".substr($datem,0,4).substr($datem,5,2).substr($datem,8,2)."%'");
			foreach($events as $event) {
				mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$event['uid']."'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$event['uid']."'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$event['uid']."'");
			}
		}
		$jourP1date = new DateTime($datem);
		$daybase1 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date <> '".$datem."' ORDER BY date ASC LIMIT 1"));
		$jourP2date = new DateTime($daybase1['date']);
		$daybase3 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date <> '".$datem."' ORDER BY date DESC LIMIT 1"));
		$jourP3date = new DateTime($daybase3['date']);
		foreach (new DatePeriod($jourP1date, new DateInterval('P1D') /* pas d'un jour */, $jourP2date) as $dt) {
			$date = $dt->format('Y-m-d');
			if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."'"))==0) {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_calendrier SET statut=0, date='".$date."'");
			}
		}
		foreach (new DatePeriod($jourP3date, new DateInterval('P1D') /* pas d'un jour */, $jourP1date) as $dt) {
			$date = $dt->format('Y-m-d');
			if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."'"))==0) {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_calendrier SET statut=0, date='".$date."'");
			}
		}
	}
	?>	
	<form class='form-admin' id='calendrier' name='calendrier' method='post' action=''>
	<script>
		$( function() {
		$( ".datepick1, .datepick2" ).datepicker({
			minDate: null, 
			maxDate: null
		});
		$( ".datepick" ).datepicker();
		} );

	</script>
	<div class='form-contenu'>
		<div class='row'>
			<label class='etiquette' for='date_deb'><?php echo PARAM_CAL1;?> </label>
			<?php
			echo '<input type="text" class="datepick1 inptxt" name="datepick1" value="';
			if(isset($_SESSION['datepick1'])) {
				echo $_SESSION['datepick1'];
			}
			echo '"><br> <br>';?>
			&nbsp;
			<label class='etiquette' for='date_fin'><?php echo PARAM_CAL2;?> </label>
			<?php
			echo '<input type="text" class="datepick2 inptxt" name="datepick2" value="';
			if(isset($_SESSION['datepick2'])) {
				echo $_SESSION['datepick2'];
			}
			echo '">';?>
			</div>
			
			
		<div class='row'>
			<label class='etiquette' ><?php echo PARAM_CAL3;?> </label>
			<label class='etiquette' for='j2'><?php echo $days_name[1]; ?> </label><input id='j2' type='checkbox' name='j2' value=1 />&nbsp;
			<label class='etiquette' for='j3'><?php echo $days_name[2]; ?></label><input id='j3' type='checkbox' name='j3' value=1 />&nbsp;
			<label class='etiquette' for='j4'><?php echo $days_name[3]; ?></label><input id='j4' type='checkbox' name='j4' value=1 />&nbsp;
			<label class='etiquette' for='j5'><?php echo $days_name[4]; ?></label><input id='j5' type='checkbox' name='j5' value=1 />&nbsp;
			<label class='etiquette' for='j6'><?php echo $days_name[5]; ?></label><input id='j6' type='checkbox' name='j6' value=1 />&nbsp;
			<label class='etiquette' for='j7'><?php echo $days_name[6]; ?></label><input id='j7' type='checkbox' name='j7' value=1 />&nbsp;
			<label class='etiquette' for='j1'><?php echo $days_name[7]; ?></label><input id='j1' type='checkbox' name='j1' value=1 />&nbsp;
			    <input class="bouton boutparam" type="button" id="cocher" value="tout cocher" />
			    <script>
					var element = document.getElementById("cocher");
					element.addEventListener("click", function() {
					document.getElementById("j2").checked=true;
					document.getElementById("j3").checked=true;
					document.getElementById("j4").checked=true;
					document.getElementById("j5").checked=true;
					document.getElementById("j6").checked=true;
					document.getElementById("j7").checked=true;
					document.getElementById("j1").checked=true;
				});
				</script>
				<input class="bouton boutparam" type="button" id="decocher" value="tout décocher" />
			    <script>
					var element = document.getElementById("decocher");
					element.addEventListener("click", function() {
					document.getElementById("j2").checked=false;
					document.getElementById("j3").checked=false;
					document.getElementById("j4").checked=false;
					document.getElementById("j5").checked=false;
					document.getElementById("j6").checked=false;
					document.getElementById("j7").checked=false;
					document.getElementById("j1").checked=false;
				});
				</script>
			</div>
		</div>
	<div class='row'>
		<input type='hidden' name='loc' value='1'  />
		<input class="bouton boutparam paddvalid1" type='submit' name='ouvrir' value=' <?php echo OPEN; ?> ' />&nbsp;
		<input class="bouton boutparam" type='submit' name='fermer' value=' <?php echo CLOSE; ?> ' />&nbsp;
		<input type='hidden' name='faire' value='' />
		</div>
	</form>
	
	<?php
			
	if(isset($_GET['annee'])) { $annee = $_GET['annee']; }
	$params['link_on_day'] = $base_url ; 		
	
	if (!isset($annee)) {
		if (!$date) {
			$rqt_date = "select date_format(CURDATE(),'%Y') as annee ";
			$resultatdate=mysqli_query($rqt_date);
			$resdate=mysqli_fetch_object($resultatdate);
			$annee = $resdate->annee ;
		} else $annee = substr($date, 0,4);
		$_SESSION['annee'] = $annee;
	}
	else { $_SESSION['annee'] = $annee; }
	$gg = '<i class="fas fa-arrow-alt-circle-left arrows_edt"></i>';
	$dd = '<i class="fas fa-arrow-alt-circle-right arrows_edt"></i>';
	
	echo "<div style='width:90%;'><div style='display:inline-block;'><a href='".$base_url."&annee=".($annee-1)."'>".$gg."</a></div><div class='cal1'><a href='".$base_url."&annee=".($annee+1)."'>".$dd."</a></div>";
	
	echo "<div id='calendrier_tab'>" ;
	for ($i=1; $i<=12; $i++) {
		$mois = substr("0".$i, -2);
		$date = $annee.$mois."01" ;
		if ($i==1 || $i==5 || $i==9) echo "<div class='cal2'><div class='cal3'>";
			else echo "<div class='cal3 cal4'>";
		echo calendar_gestion($date, 0, $base_url, $base_url_mois,0);
		echo "</div>";
		if ($i==4 || $i==8 || $i==12 ) echo "</div>";
	}
	echo "</div>\n";
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="../../index.php">'.ACCUEIL.'</a>.</p>';
}
?>
