1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Accommodation_Management/controller/accomodation.php

155 lines
7.8 KiB
PHP
Raw Permalink Normal View History

2020-12-18 20:29:24 +01:00
<?php
2021-01-14 19:01:32 +01:00
if ($_SESSION['USER']->isLoggedIn() && $_SESSION['USER']->getType() == 'AccomodationOwner') {
2021-01-11 17:30:13 +01:00
$accomodationType = ['Hôtel', 'Gîte', 'Camping', 'Villa en location'];
2021-01-05 17:53:26 +01:00
$alert = '';
2021-01-11 17:30:13 +01:00
$availableServices = AccomodationServices::getAll();
$re_name = '/^[a-zA-Z -\'?*éàèôê0-9"()+&]{1,}$/';
$re_cp = '/[0-9]{5}/';
if (isset($path[1])) {
2020-12-18 20:29:24 +01:00
switch ($path[1]) {
case 'add':
if (!$_SESSION['USER']->getAccomodationId()) {
2021-01-05 17:53:26 +01:00
/*
* Creating Accomodation
*/
2021-01-11 17:30:13 +01:00
if (isset(
2021-01-05 17:53:26 +01:00
$_POST['accomodationName'],
$_POST['postalCode'],
$_POST['address'],
$_POST['accomodationType']
2021-01-11 17:30:13 +01:00
)) {
2021-01-05 17:53:26 +01:00
/*
* Checking inputs
*/
2021-01-11 17:30:13 +01:00
if (!preg_match($re_name, $_POST['accomodationName']) || !preg_match($re_cp, $_POST['postalCode']) || ((isset($_POST["accomodationService"]) && gettype($_POST['accomodationService']) === 'array' && array_diff($_POST["accomodationService"], $availableServices)))) {
$alert = alert('danger', 'Le formulaire est invalide');
} else {
if (Accomodation::insertUser($_SESSION['USER'], array(
2021-01-05 17:53:26 +01:00
"name" => $_POST['accomodationName'],
"address" => $_POST['address'],
"postalCode" => $_POST['postalCode'],
2021-01-11 17:30:13 +01:00
"type" => $_POST['accomodationType'])) != false) {
2021-01-05 17:53:26 +01:00
/*
* Update users information
*/
2021-01-11 17:30:13 +01:00
$_SESSION['USER']->refresh();
2021-01-05 17:53:26 +01:00
/*
* Insert ok
*/
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
$accomodation->setServices($_POST["accomodationService"]);
2021-01-11 17:30:13 +01:00
$alert = alert('success', 'Ajout du logement réussi.');
} else {
$alert = alert('danger', 'Erreur lors de l\'insertion du logement.');
2021-01-05 17:53:26 +01:00
}
}
}
$services = AccomodationServices::fetch();
2021-01-11 17:30:13 +01:00
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
} else {
redirect();
}
2020-12-18 20:29:24 +01:00
break;
case 'edit':
2021-01-11 17:30:13 +01:00
if ($_SESSION['USER']->getAccomodationId()) {
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
if (isset(
$_POST['accomodationName'],
$_POST['postalCode'],
$_POST['address'],
$_POST['accomodationType']
)) {
if (!preg_match($re_name, $_POST['accomodationName']) || !preg_match($re_cp, $_POST['postalCode']) || ((isset($_POST["accomodationService"]) && gettype($_POST['accomodationService']) === 'array' && array_diff($_POST["accomodationService"], $availableServices)))) {
$alert = alert('danger', 'Le formulaire est invalide');
} else {
Accomodation::update(array(
"name" => $_POST['accomodationName'],
"address" => $_POST['address'],
"postalCode" => $_POST['postalCode'],
"type" => $_POST['accomodationType']), array(['id','=',$accomodation->getId()]));
$accomodation = Accomodation::fetchByUser($_SESSION['USER']);
$accomodation->setServices($_POST["accomodationService"]);
$alert = alert('success', 'Le logement a été modifié.');
}
$accomodation->setServices($_POST["accomodationService"]);
}
2021-01-05 17:53:26 +01:00
$services = AccomodationServices::fetch();
2021-01-11 17:30:13 +01:00
require_once(VIEW_PATH . $path[1] . '_' . $path[0] . '.php');
} else {
redirect();
}
2020-12-18 20:29:24 +01:00
break;
2021-01-11 17:30:13 +01:00
case 'date':
2021-01-16 16:12:28 +01:00
if ($_SESSION['USER']->getAccomodationId()) {
$allRange = AccomodationLoad::getByAccomodationId($_SESSION['USER']->getAccomodationId());
/*
* Calcul des minimum / disponibilités
*/
$minimum = AccomodationLoad::getRange();
$reservations = AccomodationReservation::fetchByAccomodationId($_SESSION['USER']->getAccomodationId());
forEach($reservations as $reservation){
$start = date_create($reservation->getStartDate());
$end = date_create($reservation->getEndDate());
$end->add(new DateInterval('P1D'));
$period = new DatePeriod(
$start,
new DateInterval('P1D'),
$end
);
foreach ($period as $key => $value) {
if(in_array($value->format('Y-m-d'), array_keys($minimum))){
$minimum[$value->format('Y-m-d')]['rooms'] += $reservation->getRoomCount();
$minimum[$value->format('Y-m-d')]['beds'] += $reservation->getPeopleCount();
}
}
}
2021-01-16 16:12:28 +01:00
/*
* Formulaire POST
*/
$filteredPost = [];
$error = false;
2021-01-16 16:12:28 +01:00
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'])){
if($minimum[$parts[0]][$col] > (int)$value){
$error = true;
$alert = alert('danger','Impossible de réduire le compte de \'' .$col. '\' pour le ' . dateToFrench($parts[0], "j F Y") . ' due a des reservations déjà enregistrées.');
}else {
$filteredPost[$parts[0]][$col] = (int)$value;
}
2021-01-16 16:12:28 +01:00
}
}
}
if(count($filteredPost) > 0 && $error == false){
2021-01-16 16:12:28 +01:00
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();
}
2021-01-11 17:30:13 +01:00
break;
2020-12-18 20:29:24 +01:00
default:
redirect();
}
2021-01-11 17:30:13 +01:00
} else {
2020-12-18 20:29:24 +01:00
redirect();
}
2021-01-11 17:30:13 +01:00
} else {
2020-12-18 20:29:24 +01:00
redirect('login');
}