Je cherche a créer une application de réservations de chambres d'hôtels.
Voici le code actuel qui marche très bien.
Mais, car il y a un mais, j'aimerais que lorsque une chambre est déjà occupée, pour des dates déjà enregistrées elle ne puisse plus être réservée pour ces dates et que la réservation se fasse sur une autre chambre du même hôtel pour ces mêmes dates.
Tables:
bookings
id | client_id | arrival_date | departure_date | booking_date | room_id
clients
id | name | email
resorts
id | name | address
-------------------
1 | hotel A | adresse A
-------------------
2 | hotel A | adresse B
------------------
3 | hotel A | adresse C
------------------
4 | hotel A | adresse D
rooms
id | resort_id | number
-------------------------
1 | 1 | 1
-------------------------
2 | 1 | 2
-------------------------
3 | 2 | 3
-------------------------
4 | 2 | 4
-------------------------
5 | 3 | 5
-------------------------
6 | 3 | 6
-------------------------
7 | 4 | 7
-------------------------
8 | 4 | 8
index.php
<?php
require('Class/Client.php');
require('Class/Clientmanager.php');
require('Class/Booking.php');
require('Class/Bookingmanager.php');
if (isset($_POST['submitForm']))
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// clients informations ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
$name = $_POST['name'];
$email = $_POST['email'];
//création d'un objet client
$client_data = array('name' => $name, 'email' => $email);
$client = new Client($client_data);
//affectation dans la varibale $db de la connexion
$db = new PDO('mysql:host=localhost;dbname=booking;charset=UTF8', 'root', '');
//instanciation de la classe clientManager, nous créons un objet clientManager
//la connexion PDO est passée en paramètre au constructeur
$clientManager = new clientManager($db);
//appel à la méthode addClient, nous passons un ojet en argument.
$client_id = $clientManager->addClient($client);
var_dump($name);
var_dump($email);
var_dump($client_id);
echo '<br>';
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// bookings informations ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
//création d'un objet booking
$booking_data = array('client_id' => (int) $client_id);
$booking = new Booking($booking_data);
$bookingManager = new bookingManager($db);
$booking_id = $bookingManager->addBooking($booking);
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// rooms informations ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
$resort_id = $_POST['resort'];
$rooms = $bookingManager->getRoomsByResortId($resort_id);
var_dump($rooms);
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Accueil</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="wrapper">
<h1>Booking Application</h1>
<nav class="main-nav" role="navigation">
<ul>
<li><a href="index.php" role="menuitem">Accueil</a></li>
<li><a href="resorts.php" role="menuitem">Hôtels</a></li>
</ul>
</nav>
<section>
<div class="formulaire">
<?php if(isset($error_msg)) : ?>
<p class="error_msg"><?php echo $error_msg ?></p>
<?php endif ?>
<?php if(isset($success_msg)) : ?>
<p class="success_msg"><?php echo $success_msg ?></p>
<?php endif ?>
<form method="post" action="index.php">
<h2>Réservez votre hôtel</h2>
<p>
Votre nom: <input type="text" name="name" value="<?php $name ?>" placeholder="Nom">
</p>
<p>
Votre email: <input type="email" name="email" value="" placeholder="votre@mail">
</p>
<p>
Votre hôtel:<select name="resort">
<option value=""></option>
<?php
$resorts = array(1 => 'Atlantis The Palm, Dubaï 5*', 'Burj Al Arab, Dubaï 7*', 'Krabi La Playa, Thaïlande 4*', 'Four Seasons, Bora Bora 4*', 'Atlantis Paradise Island, Bahamas 4*');
foreach ($resorts as $resort_id => $resort)
{
echo '<option value=" '.$resort_id.' ">' .$resort. '</option>';
}
?>
</select>
</p>
<p>
Du: <input class="mr-30" type="date" name="arrival" value="">
Au: <input class="mr-30" type="date" name="departure" value="">
</p>
<p>
<?php $today = date("Y-m-d") ?>
<input type="hidden" name="bookingCreation" value="<?php echo $today ?>" >
</p>
<p>
<input class="submit-bt" type="submit" name="submitForm" value="Réserver">
</p>
</form>
</div>
</section>
</div>
</body>
</html>
Client.php
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// ///////////////////////
/////////////// CLIENT ///////////////////////
/////////////// ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Client
{
private $_name;
private $_email;
public function __construct(array $clientData)
{
$this->setName($clientData['name']);
$this->setEmail($clientData['email']);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// setters ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
public function setName($name)
{
if (is_string($name))
{
//$name = $_POST['name'];
$this->_name = $name;
}
}
public function setEmail($email)
{
if (is_string($email))
{
//$email = $_POST['email'];
$this->_email = $email;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// getters ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
public function getName()
{
return $this->_name;
}
public function getEmail()
{
return $this->_email;
}
}
?>
Clientmanager.php
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// ///////////////////////
/////////////// CLIENT MANAGER ///////////////////////
/////////////// ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class clientManager
{
private $_db;
public function __construct($db)
{
$this->setDb($db);
}
public function setDb(PDO $dbh)
{
$this->_db = $dbh;
}
//insertion
public function addclient(Client $client)
{
$sql = 'INSERT INTO clients (name, email) VALUES (:name, :email)';
$stmnt = $this->_db->prepare($sql);
$name = trim($client->getName());
$email = trim($client->getEmail());
$stmnt->bindParam(':name', $name);
$stmnt->bindParam(':email', $email);
if ($stmnt->execute())
{
return $this->_db->lastInsertId();
}
return false;
//gestion des erreurs SQL
$errors = $stmnt->errorInfo();
if ($errors[0] = '00000')
{
echo 'Erreur SQL ' . $errors[2];
}
else
{
echo 'Votre réservation a bien été enregistrée. (clientManager)';
}
}
}
?>
Booking.php
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// ///////////////////////
/////////////// BOOKING ///////////////////////
/////////////// ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Booking
{
private $_client_id;
private $_arrival;
private $_departure;
private $_today;
private $_room_id;
public function __construct(array $bookingData)
{
$this->setClientId($bookingData['client_id']);
$this->setArrival();
$this->setDeparture();
$this->setToday();
$this->setRoomId();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// setters ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
public function setClientId($client_id)
{
if ((is_int($client_id)) AND ($client_id > 0))
{
$this->_client_id = $client_id;
}
}
public function setArrival()
{
$arrival = $_POST['arrival'];
$this->_arrival = $arrival;
}
public function setDeparture()
{
$departure = $_POST['departure'];
$this->_departure = $departure;
}
public function setToday()
{
$today = date("Y-m-d");
$this->_today = $today;
}
public function setRoomId()
{
try
{
$dbh = new PDO('mysql:host=localhost;dbname=booking;charset=UTF8', 'root', '');
}
catch(Exception $e)
{
echo 'Message erreur SQL : ' .$e->getMessage(). '<br>';
exit;
}
$resort_id = trim($_POST['resort']);
$sql = 'SELECT number FROM rooms AS o
INNER JOIN resorts AS r
ON r.id=o.resort_id
WHERE r.id="'.$resort_id.'" ';
$stmnt = $dbh->prepare($sql);
$stmnt->execute();
$result = $stmnt->fetchAll();
$room_id = $result[array_rand($result)]['number'];
$this->_room_id = $room_id;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// getters ////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
public function getClientId()
{
return $this->_client_id;
}
public function getArrival()
{
return $this->_arrival;
}
public function getDeparture()
{
return $this->_departure;
}
public function getToday()
{
return $this->_today;
}
public function getRoomId()
{
return $this->_room_id;
}
}
?>
Bookingmanager.php
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////// ///////////////////////
/////////////// BOOKING MANAGER ///////////////////////
/////////////// ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class bookingManager
{
private $_db;
public function __construct($db)
{
$this->setDb($db);
}
public function setDb(PDO $dbh)
{
$this->_db = $dbh;
}
//insertion
public function addBooking(Booking $booking)
{
$sql = 'INSERT INTO bookings (client_id, arrival_date, departure_date, booking_date, room_id) VALUES (:client_id, :arrival, :departure, :today, :room_id)';
$stmnt = $this->_db->prepare($sql); //
$clientId = $booking->getClientId();
$arrival = $booking->getArrival();
$departure = $booking->getDeparture();
$today = $booking->getToday();
$roomId = $booking->getRoomId();
$stmnt->bindParam(':client_id', $clientId);
$stmnt->bindParam(':arrival', $arrival);
$stmnt->bindParam(':departure', $departure);
$stmnt->bindParam(':today', $today);
$stmnt->bindParam(':room_id', $roomId);
$stmnt->execute();
//gestion des erreurs
$errors = $stmnt->errorInfo();
if ($errors[0] = '00000')
{
echo 'Erreur SQL ' . $errors[2];
}
else
{
//$success_msg = 'Votre réservation a bien été enregistrée. (bookingManager)';
echo 'Votre réservation a bien été enregistrée. (addBooking)';
}
}
public function getRoomsByResortId($resort_id)
{
$resort_id = trim($_POST['resort']);
$sql = 'SELECT number, arrival_date, departure_date FROM rooms AS o
LEFT JOIN bookings as b ON b.room_id=o.id
INNER JOIN resorts AS r ON r.id=o.resort_id
WHERE r.id= "'.$resort_id.'"
ORDER BY number ASC';
$stmnt = $this->_db->prepare($sql);
$stmnt->execute();
while ($row = $stmnt->fetchAll(PDO::FETCH_ASSOC))
{
$result[] = $row;
}
return $result;
//gestion des erreurs
$errors = $stmnt->errorInfo();
if ($errors[0] = '00000')
{
echo 'Erreur SQL ' . $errors[2];
}
else
{
echo 'Votre réservation a bien été enregistrée. (getRoomsByHotel)';
}
}
}
?>
D'avance merci à ceux qui pourront m'aider.