From abc00dcca219754d505477fd3df0d3080bec8d2d Mon Sep 17 00:00:00 2001 From: p1907961 Date: Sat, 16 Jan 2021 16:12:28 +0100 Subject: [PATCH] Added HotelManager Availability page --- controller/accomodation.php | 31 ++++++++- models/AccomodationLoad.php | 122 ++++++++++++++++++++++++++++++++++++ src/func.php | 9 +++ view/date_accomodation.php | 39 +++++++++++- view/manager.php | 2 +- 5 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 models/AccomodationLoad.php diff --git a/controller/accomodation.php b/controller/accomodation.php index 194d0ef..5314279 100644 --- a/controller/accomodation.php +++ b/controller/accomodation.php @@ -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(); diff --git a/models/AccomodationLoad.php b/models/AccomodationLoad.php new file mode 100644 index 0000000..d43ce3c --- /dev/null +++ b/models/AccomodationLoad.php @@ -0,0 +1,122 @@ +$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; + } +} \ No newline at end of file diff --git a/src/func.php b/src/func.php index 9f4ce9a..46b422d 100644 --- a/src/func.php +++ b/src/func.php @@ -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; diff --git a/view/date_accomodation.php b/view/date_accomodation.php index d132d8a..73f6966 100644 --- a/view/date_accomodation.php +++ b/view/date_accomodation.php @@ -2,8 +2,43 @@ require_once('template/head.php'); ?>
-
- + +
+
+

Gestion des disponibilités de "getName() ?>"

+
+ $vol) { + if ($rowCount == 0) { + echo '
'; + } + ?> +
+ +
+ Nb de Lits + +
+
+ Nb de Chambres + +
+
+ '; + $rowCount = 0; + } + } + ?> +
+
+ +
getAddress()) . ', ' . $hotel->getPostalCode() ?> getType())?> getServices() ? htmlspecialchars(join(',',$hotel->getServices())) : "Aucun" ?> - 0 + getId()))?> Editer une reservation