<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

$doAutentication = false;
$no_visible_elements = true;
$calLang = (isset($_GET['lang'])) ? $_GET['lang'] : NULL;
$title = (isset($_GET['title']) && $_GET['title'] == '0') ? 0 : 1;
$nav = (isset($_GET['nav']) && $_GET['nav'] == '0') ? 0 : 1;
$btn = (isset($_GET['btn']) && $_GET['btn'] == '0') ? 0 : 1;
require "../_container/container_top.php";
	require_once ('lib/Cal/activecalendar.php');
	require_once ('config/Logger.php');

	require_once ('business/TenancyMgr.php');
	require_once ('business/ReservationMgr.php');

	$tenancyMgr = new TenancyMgr();
	$reservationMgr = new ReservationMgr();

	Logger::info('BEGIN User Calendar rendering');
	
	$selectTenancy = (isset($_GET['id'])) ? $_GET['id'] : 0;
	$year = (isset($_GET['year'])) ? $_GET['year'] : date('Y');

	$startMonth = ($config['cal_start_current_month']) ? date("n") : 1;
	$monthNum = 12;
	$col = 3;

	$arrAllReservations = $reservationMgr->getReservations();

	$tenancyid = isset($_GET['id']) ? $_GET['id'] : getPost('tenancyid');
	$lang = (isset($calLang)) ? $calLang : $config['language'];

	$arrTenancy = $tenancyMgr->getTenancy($tenancyid);
	if (empty($arrTenancy)) $arrTenancy = array(new TenancyVo());

?>
<?php
$cal=new activeCalendar($year);
$cal->yearID = "year";
$cal->monthID = "startMonth";
// $cal->enableYearNav(false);
// $cal->enableMonthNav(false);

if ($config['cal_8days_week']) $cal->enable8DaysWeek();
if ($config['cal_show_week_number']) $cal->enableWeekNum();
$cal->showNoMonthDays = $config['cal_show_nomonth_days'];
$showReservOption = $config['cal_show_reserv_option'];
$cal->setFirstWeekDay($config['cal_first_weekday']); // 0=sunday, 1=Monday, 6=saturday

$cal->setMonthNames($tr->translate('calMonthNames', "default", $lang));
$cal->setDayNames($tr->translate('calDayNames', "default", $lang));

$cal->cssYearTitle = "year-title";
$cal->cssNoMonthDay = "nomonth-day";
$cal->cssMonthTitle = "month-title";
$cal->cssMonthTable = "month-table";
$cal->cssMonthDay = "month-day";
$cal->cssSaturday = "weekend-day";
$cal->cssSunday = "weekend-day";
$cal->cssWeekNum = "weeknum-day";
$cal->cssWeekDay = "week-day";
$cal->cssWeekDays = "week-days";

	// construction des tableaux des jours réservés et des saison pour l'année (+début et fin de réservation)

	$tab_tres_haute_1 = array();
	$tab_haute_1 = array();
	$tab_moyenne_1 = array();
	$tab_indispo_1 = array();

	$tab_res = array();
	$tab_res_deb = array();
	$tab_res_fin = array();
	$tab_option = array();
	$tab_option_deb = array();
	$tab_option_fin = array();
	foreach ($arrAllReservations as $reservation) {
		if ($reservation->tenancy_id == $selectTenancy) {
			$date_req = date('Y-m-d', strtotime($reservation->arrival_date));
			$datefin_req = date('Y-m-d', strtotime($reservation->departure_date));
			if ($reservation->status >= ReservationMgr::RESERV_VALIDATE){
				$tab_res_deb[] = date('Y-m-d', strtotime($date_req)); // on place chaque jours réservés dans le tableau
				$tab_res_fin[] = date('Y-m-d', strtotime($datefin_req)); // on place chaque jours réservés dans le tableau
				$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				while ($date_req < $datefin_req){
					$tab_res[] = date('Y-m-d', strtotime($date_req));
					$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				}
			}else{
				$tab_option_deb[] = date('Y-m-d', strtotime($date_req)); // on place chaque jours réservés dans le tableau
				$tab_option_fin[] = date('Y-m-d', strtotime($datefin_req)); // on place chaque jours réservés dans le tableau
				$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				while ($date_req < $datefin_req) {
					$tab_option[] = date('Y-m-d', strtotime($date_req));
					$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
				}
			}
		}
	}
	// print_r($tab_res);
	// print_r($tab_option);

foreach ($arrAllReservations as $reservation) {
	if ($reservation->tenancy_id == $selectTenancy) {
	$date_req = $reservation->arrival_date;
	$datefin_req = $reservation->departure_date;
	if ($reservation->status >= ReservationMgr::RESERV_VALIDATE){

		$d = date('Y-m-d', strtotime($date_req));
		// debut de reserv_moyenne
		if (in_array ($d, $tab_res_deb) && in_array ($d, $tab_res_fin)){$class = " booked ";}
		elseif (in_array ($d, $tab_option_fin)){$class = " option-booked ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " moyenne-booked ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " haute-booked ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " tres-haute-booked ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " indispo-booked ";}
		else{$class = " free-booked ";}

		$cal->setEvent(
			date("Y", strtotime($date_req)),
			date("m", strtotime($date_req)),
			date("d", strtotime($date_req)),
			$class,
			"#",
			""
			// $reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);

		$d = date('Y-m-d', strtotime($datefin_req));
		// debut de reserv_moyenne
		if (in_array ($d, $tab_res_deb) && in_array ($d, $tab_res_fin)){$class = " booked ";}
		elseif (in_array ($d, $tab_option_deb)){$class = " booked-option ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " booked-moyenne ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " booked-haute ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " booked-tres-haute ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " booked-indispo ";}
		else{$class = " booked-free ";}

		$cal->setEvent(
			date("Y", strtotime($datefin_req)),
			date("m", strtotime($datefin_req)),
			date("d", strtotime($datefin_req)),
			$class,
			"#",
			""
			// $reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);
	}else{
if ($showReservOption){
		$d = date('Y-m-d', strtotime($date_req));
	// if (!in_array($d, $tab_res)){
		// debut de reserv_moyenne
		// if (in_array ($d, $tab_res)){$class = " booked ";}
		if (in_array ($d, $tab_option_deb) && in_array ($d, $tab_option_fin)){$class = " option ";}
		// elseif (in_array ($d, $tab_option)){$class = " option ";}
		elseif (in_array ($d, $tab_res_fin)){$class = " booked-option ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " moyenne-option ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " haute-option ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " tres-haute-option ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " indispo-option ";}
		else{$class = " free-option ";}

		$cal->setEvent(
			date("Y", strtotime($date_req)),
			date("m", strtotime($date_req)),
			date("d", strtotime($date_req)),
			$class,
			"#",
			""
			// $reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);

		$d = date('Y-m-d', strtotime($datefin_req));
		// debut de reserv_moyenne
		// if (in_array ($d, $tab_res)){$class = " booked ";}
		if (in_array ($d, $tab_option_deb) && in_array ($d, $tab_option_fin)){$class = " option ";}
		// elseif (in_array ($d, $tab_option)){$class = " option ";}
		elseif (in_array ($d, $tab_res_deb)){$class = " option-booked ";}
		elseif (in_array ($d, $tab_moyenne_1)){$class = " option-moyenne ";}
		// debut de reserv_haute
		elseif (in_array ($d, $tab_haute_1)){$class = " option-haute ";}
		// debut de reserv_tres_haute
		elseif (in_array ($d, $tab_tres_haute_1)){$class = " option-tres-haute ";}
		// debut de reserv_indispo
		elseif (in_array ($d, $tab_indispo_1)){$class = " option-indispo ";}
		else{$class = " option-free ";}

		$cal->setEvent(
			date("Y", strtotime($datefin_req)),
			date("m", strtotime($datefin_req)),
			date("d", strtotime($datefin_req)),
			$class,
			"#",
			""
			// $reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
		);
	// }
	}
} // end $showReservOption
	$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
	while ($date_req <= date('Y-m-d', strtotime($datefin_req . ' - 1 day'))) {
		if ($reservation->status >= ReservationMgr::RESERV_VALIDATE){
			$cal->setEvent(
				date("Y", strtotime($date_req)),
				date("m", strtotime($date_req)),
				date("d", strtotime($date_req)),
				" booked ",
				"#",
				""
				// $reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
			);
		}else{
		if ($showReservOption) {
			if (!in_array($date_req, $tab_res)){
				if (in_array ($date_req, $tab_res_fin)){$class = " booked-option ";}
				elseif (in_array ($date_req, $tab_res_deb)){$class = " option-booked ";}
				else{$class = " option ";}

				$cal->setEvent(
					date("Y", strtotime($date_req)),
					date("m", strtotime($date_req)),
					date("d", strtotime($date_req)),
					$class,
					"#",
					""
					// $reservation->tenant_lastname . " " . $reservation->tenant_name . " // " . date($config['date_format'], strtotime($reservation->arrival_date)) ."->" . date($config['date_format'], strtotime($reservation->departure_date))
				);
			}
		} // end $showReservOption
		}
		$date_req = date('Y-m-d', strtotime($date_req . ' + 1 day'));
	}
}
}

?>
	<div class="container center">
		<h1><?php if ($title) echo $tr->translate('thCalformHeader', "default", $lang); ?> <strong>
			<?php
			if (empty($arrTenancy[0]->tenancy_id)) {
					$msgType = "error";
					$msg = $tr->translate("msgUserformTenancyNotFound", "default", $lang);
					echo '<div class="alert alert-'.$msgType . '">
							<strong>' . $msg . '</strong>
							</div>';		
			}else{
				if ($title) echo $arrTenancy[0]->name;
				if (isset($msgType) && $msgType == "success"){
					echo '<div class="alert alert-'.$msgType . '">
						<strong>' . $msg . '</strong>
						</div>';
					require "../_container/container_bottom.php";
					die();
				}else{
					if (isset($msgType) && $msgType == "error"){
					echo '<div class="alert alert-'.$msgType . '">
						<strong>' . $msg . '</strong>
						</div>';
					}

			echo "</strong>";
			if ($title) echo "<hr>";

			if ($userPageMaintenanceMode) {
				$msgType = "error";
				$msg = $tr->translate("msgCalformMaintenanceMode", "default", $lang);
				echo '<div class="alert alert-'.$msgType . '">
					<strong>' . $msg . '</strong>
					</div>';
				require "../_container/container_bottom.php";
				die();
			}

			?>
			
		</h1>
<div class="row-fluid">
		<div class="span12">

<?php
	echo $cal->showYear($col,$startMonth,$monthNum);
?>
			<?php if (file_exists("userform.php") && $btn) { ?>
			<div class="row-fluid span12 center">
				<button id="btnReservThisTenancy" name="btnReservThisTenancy" class="btn btn-primary input-block-level"><?php echo $tr->translate('btnReservThisTenancy', "default", $lang); ?></button>
			</div>
			<?php } ?>
		
			<?php if ($nav) { ?>
			<div class="row-fluid well span12 center">
				<div class="row-fluid span8 center">
					<div class="span6">
						<h4><?php echo $tr->translate('thCalformAvailability', "default", $lang); ?></h4>
						<div class="indispo"><?php echo $tr->translate('NotAvailable', "default", $lang); ?></div>
						<div class="option"><?php echo $tr->translate('Option', "default", $lang); ?></div>
						<div class="booked"><?php echo $tr->translate('Booked', "default", $lang); ?></div>
					</div>
				</div>
				<hr>
				<div class="row-fluid span8 center">
				<?php
					$navParam = "&id=" . $tenancyid;
					$navParam .= "&title=" . (($title) ? "1" : "0");
					$navParam .= "&nav=" . (($nav) ? "1" : "0");
					$navParam .= "&btn=" . (($btn) ? "1" : "0");
				?>
					<div class="span6">
						<h4><?php echo $tr->translate('thCalformNavigation', "default", $lang); ?></h4>
						<?php $today = date("Y");
						if ($today != $year) { ?>
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year-1; ?>&lang=<?php echo $lang; ?><?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/arrow-gauche.png" width="40" height="40" border="0" align="absmiddle"></a> 
						<?php } ?>
						<select class="input-small" id="year" name="year">
							<?php $x = 5; for($i=0; $i<=$x; $i++){ $d = $today + $i;
							?>
							<option value="<?php echo $d; ?>" <?php if ($d == $year) echo " selected"; ?>><?php echo $d; ?></option>
							<?php } ?>
						</select>
						<?php if ($year != $today + $x) { ?>
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year+1; ?>&lang=<?php echo $lang; ?><?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/arrow-droite.png" width="40" height="40" border="0" align="absmiddle"></a> 
						<?php } ?>
					</div>
					<div class="span6">
						<h4><?php echo $tr->translate('Language', "default", $lang); ?></h4>
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year; ?>&lang=fr_FR<?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/fr.png" alt="France" width="32" height="32" border="0"></a>&nbsp; 
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year; ?>&lang=en_US<?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/gb.png" alt="English" width="32" height="32" border="0"></a> 
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year; ?>&lang=de_DE<?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/de.png" alt="Germany" width="32" height="32" border="0"></a>&nbsp; 
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year; ?>&lang=it_IT<?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/it.png" alt="Italy" width="32" height="32" border="0"></a>&nbsp; 
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year; ?>&lang=es_ES<?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/es.png" alt="Spain" width="32" height="32" border="0"></a>&nbsp; 
							<a href="<?php echo $_SERVER['PHP_SELF']; ?>?year=<?php echo $year; ?>&lang=nl_NL<?php echo $navParam; ?>"><img src="<?php echo CALENDAR_IMAGE_PATH_RESS; ?>/nl.png" alt="Netherland" width="32" height="32" border="0"></a> 
					</div>
				</div>
			</div>
			<?php } ?>
		<?php } ?>
		<?php } ?>
	</div>
</div><!--/row-->
</div>

<?php Logger::info('END User Calendar rendering'); ?>
<?php require "../_container/container_bottom.php"; ?>

<script>
	$(document).ready(function(){
		$("body").css("background-color", "#<?php echo $config['color_cal_bgd']; ?>");
	});
	$("#year").change(function(){
		$().redirect(
			"<?php echo $_SERVER['PHP_SELF']; ?>",
			{
				<?php if (isset($lang)) echo "'lang': '" . $lang . "',"; ?>
				<?php if (isset($title)) echo "'title': '" . $title . "',"; ?>
				<?php if (isset($nav)) echo "'nav': '" . $nav . "',"; ?>
				<?php if (isset($btn)) echo "'btn': '" . $btn . "',"; ?>
				'year': $(this).val(),
				'id': <?php echo $selectTenancy; ?>
			},
			'GET'
		); 
	});
	$("#btnReservThisTenancy").click(function(){
		$().redirect(
			"userform.php",
			{
				<?php if (isset($lang)) echo "'lang': '" . $lang . "',"; ?>
				<?php if (isset($title)) echo "'title': '" . $title . "',"; ?>
				'id': <?php echo $selectTenancy; ?>
			},
			'GET'
		); 
	});
</script>
