1
0
Fork 0

Added HotelManager Availability page

This commit is contained in:
p1907961 2021-01-16 16:12:28 +01:00
parent 2f3925dd95
commit abc00dcca2
5 changed files with 199 additions and 4 deletions

View file

@ -85,7 +85,36 @@ if ($_SESSION['USER']->isLoggedIn() && $_SESSION['USER']->getType() == 'Accomoda
}
break;
case 'date':
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
if ($_SESSION['USER']->getAccomodationId()) {
$allRange = AccomodationLoad::getByAccomodationId($_SESSION['USER']->getAccomodationId());
/*
* Formulaire POST
*/
$filteredPost = [];
forEach($_POST as $key=>$value){
$parts = explode('-', $key);
$last = array_pop($parts);
$parts = array(implode('-', $parts), $last);
if(array_key_exists($parts[0],$allRange)){
/*
* Update count
*/
$col = $parts[1];
if(in_array($col, ['beds','rooms'])){
$filteredPost[$parts[0]][$col] = (int)$value;
}
}
}
if(count($filteredPost) > 0){
AccomodationLoad::setAccomodationLoad($_SESSION['USER']->getAccomodationId(), $filteredPost);
$allRange = AccomodationLoad::getByAccomodationId($_SESSION['USER']->getAccomodationId());
}
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
} else {
redirect();
}
break;
default:
redirect();

122
models/AccomodationLoad.php Normal file
View file

@ -0,0 +1,122 @@
<?php
class AccomodationLoad extends Model
{
private $data;
public function __construct($data = null)
{
parent::__construct();
$_col = get_class()::getColumns();
if($data !== null){
forEach($data as $key=>$value){
if(!key_exists($key, $_col)){
throw new Exception('Invalid data entry');
}else{
$this->data[$key] = $value;
}
}
}
return $this;
}
/*
* Getters
*/
public function getAccomodationId(): string
{
if(isset($this->data['AccomodationId']))
return $this->data['AccomodationId'];
return false;
}
public function getDate(): string
{
if(isset($this->data['date']))
return $this->data['date'];
return false;
}
public function getBedCount(): string
{
if(isset($this->data['beds']))
return $this->data['beds'];
return false;
}
public function getRoomCount(): string
{
if(isset($this->data['rooms']))
return $this->data['rooms'];
return false;
}
public static function getByAccomodationId($AccomodationId)
{
$output = AccomodationLoad::getRange();
$data = AccomodationLoad::fetch(array(['AccomodationId','=', $AccomodationId]));
forEach($data as $acomLoad){
if(key_exists($acomLoad->getDate(),$output)){
$output[$acomLoad->getDate()] = ["beds" => (int)$acomLoad->getBedCount(), "rooms" => (int)$acomLoad->getRoomCount()];
}
}
return $output;
}
/*
* Setters
*/
public static function setAccomodationLoad($AccomodationId, $data)
{
$toUpdate = [];
$toInsert = [];
$currentDBs = [];
/*
* Get in DBs
*/
$ranges = AccomodationLoad::getRange();
$dataLoad = AccomodationLoad::fetch(array(['AccomodationId','=', $AccomodationId]));
forEach($dataLoad as $acomLoad){
if(key_exists($acomLoad->getDate(),$ranges)){
$currentDBs[$acomLoad->getDate()] = ["beds" => (int)$acomLoad->getBedCount(), "rooms" => (int)$acomLoad->getRoomCount()];
}
}
/*
* Fill Update/Insert List
*/
forEach($data as $date=>$updatedValue){
if(key_exists($date, $currentDBs)){
$toUpdate[$date] = $updatedValue;
}
$toInsert[$date] = $updatedValue;
}
/*
* Building Queries
*/
// Delete the UpdateList
$q_delete = 'DELETE FROM AccomodationLoad WHERE AccomodationId = ' .(int)$AccomodationId. ' AND date IN (' .join(', ', array_map(function ($i){ return "'" . $i . "'"; },array_keys($toUpdate))). ')';
// Insert the Items
$q_insert = 'INSERT INTO AccomodationLoad VALUES ' . str_replace('%AccomodationId%' , $AccomodationId, join(', ', array_map(function ($i){ return "(" . $i . ")"; },array_map(function ($i,$toInsert){ return "%AccomodationId% , '" . $i . "', " .$toInsert['beds']. ", " . $toInsert['rooms'];},array_keys($toInsert),$toInsert))));
if(count($toUpdate) > 0){
$q = AccomodationLoad::$db->prepare($q_delete);
$q->execute();
}
if(count($toInsert) > 0){
$q = AccomodationLoad::$db->prepare($q_insert);
return($q->execute() == true);
}
return false;
}
public static function getRange(){
$dates = [date_create("2021-05-11"), date_create("2021-05-23")];
$output = [];
$period = new DatePeriod(
$dates[0],
new DateInterval('P1D'),
$dates[1]
);
foreach ($period as $key => $value) {
$output[$value->format('Y-m-d')] = ["beds" => 0, "rooms" => 0];
}
return $output;
}
}

View file

@ -26,6 +26,15 @@ function redirect($route = WEBSITE_DEFAULT_PATH){
/*
* Front-end render
*/
# Date
function dateToFrench($date, $format)
{
$english_days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
$french_days = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
$english_months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$french_months = array('janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre');
return str_replace($english_months, $french_months, str_replace($english_days, $french_days, date($format, strtotime($date) ) ) );
}
# Assets path generator
function assetsPath($path, $level = 0){
return str_repeat('../', $level) . $path;

View file

@ -2,8 +2,43 @@
require_once('template/head.php');
?>
<div class="container-fluid section">
<form action="">
<form action="" method="POST">
<div class="card border border-dark mb-3">
<div class="card-body text-dark">
<h3 class="card-title">Gestion des disponibilités de "<?= $accomodation->getName() ?>"</h3>
<div style="margin: auto;">
<?php
$rowCount = 0;
$maxPerRow = 4;
$mdCalc = ceil(12 / $maxPerRow);
foreach ($allRange as $date => $vol) {
if ($rowCount == 0) {
echo '<div class="row" style="padding-top: 15px">';
}
?>
<div class="col-md-<?= $mdCalc ?>">
<label for="<?= $date; ?>"><?=dateToFrench($date, "j F Y");?></label>
<div class="input-group mb-3">
<span class="input-group-text" id="lit<?= $date; ?>">Nb de Lits</span>
<input type="text" class="form-control" placeholder="0" aria-label="lit<?= $date; ?>" name="<?= $date; ?>-beds" value="<?=$vol['beds']?>">
</div>
<div class="input-group mb-3">
<span class="input-group-text" id="<?= $date; ?>">Nb de Chambres</span>
<input type="text" class="form-control" placeholder="0" aria-label="<?= $date; ?>" name="<?= $date; ?>-rooms" value="<?=$vol['rooms']?>">
</div>
</div>
<?php
$rowCount++;
if ($rowCount == $maxPerRow) {
echo '</div>';
$rowCount = 0;
}
}
?>
</div>
</div>
<button type="submit" class="btn-success"><h1>VALIDER</h1></button>
</div>
</form>
</div>
<?php

View file

@ -25,7 +25,7 @@ require_once('template/head.php');
<td><?=htmlspecialchars($hotel->getAddress()) . ', ' . $hotel->getPostalCode() ?></td>
<td><?=htmlspecialchars($hotel->getType())?></td>
<td><?=$hotel->getServices() ? htmlspecialchars(join(',',$hotel->getServices())) : "Aucun" ?></td>
<td>0</td>
<td><?=count(AccomodationReservation::fetchByAccomodationId($hotel->getId()))?></td>
<td><a href="<?=genURL('manager/' . $hotel->getId() . '/view')?>" class="btn btn-primary">Editer une reservation</a></td>
</tr>
<?php