-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_rooms.php
More file actions
78 lines (67 loc) · 3.09 KB
/
fetch_rooms.php
File metadata and controls
78 lines (67 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
include_once "src/controller/ShowingController.php";
include_once "src/controller/RoomController.php";
if (isset($_POST['venueId'], $_POST['showingDate'], $_POST['showingTime'])) {
// Sanitize input
$venueId = intval(trim($_POST['venueId']));
$showingDate = htmlspecialchars(trim($_POST['showingDate']));
$showingTime = htmlspecialchars(trim($_POST['showingTime']));
// Split the showing time into start and end times
$showingTime = explode("-", $showingTime);
$showingTimeStart = new DateTime($showingTime[0]); // Start time
$showingTimeEnd = new DateTime($showingTime[1]); // End time
$showingController = new ShowingController();
$roomController = new RoomController();
// Get unavailable rooms for the venue on the given date
$unavailableRooms = $showingController->getUnavailableRoomsForAVenueAndDay($venueId, $showingDate);
if (isset($unavailableRooms['errorMessage']) && $unavailableRooms['errorMessage']) {
echo json_encode(['error' => true, 'message' => $unavailableRooms['errorMessage']]);
exit;
}
// Get all rooms for the venue
$rooms = $roomController->getRoomsByVenueId($venueId);
if (is_array($rooms) && isset($rooms['errorMessage']) && $rooms['errorMessage']) {
echo json_encode(['error' => true, 'message' => $rooms['errorMessage']]);
exit;
}
// Filter available rooms
$availableRooms = [];
foreach ($rooms as $room) {
$isUnavailable = false;
// Check if the room is in the unavailable list
foreach ($unavailableRooms as $unavailableRoom) {
if ($room->getRoomId() == $unavailableRoom['roomId']) {
// Convert unavailable times to DateTime for comparison
$unavailableStart = new DateTime($unavailableRoom['showingTime']);
$unavailableEnd = clone $unavailableStart;
$unavailableEnd->modify("+{$unavailableRoom['duration']} minutes");
// Check for overlap
if (
($showingTimeStart >= $unavailableStart && $showingTimeStart < $unavailableEnd) || // Overlaps start
($showingTimeEnd > $unavailableStart && $showingTimeEnd <= $unavailableEnd) || // Overlaps end
($showingTimeStart <= $unavailableStart && $showingTimeEnd >= $unavailableEnd) // Encloses
) {
$isUnavailable = true;
break;
}
}
}
// If the room is not unavailable, add it to the available rooms
if (!$isUnavailable) {
$availableRooms[] = [
'roomId' => $room->getRoomId(),
'roomNumber' => $room->getRoomNumber(),
];
}
}
if (empty($availableRooms)) {
echo json_encode(['success' => false, 'message' => 'No available rooms for this timeslot.']);
exit;
}
// Return available rooms
echo json_encode(['success' => true, 'availableRooms' => $availableRooms]);
exit;
} else {
echo json_encode(['success' => false, 'message' => 'Invalid input.']);
exit;
}