-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbook.php
184 lines (160 loc) · 6.95 KB
/
book.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/// This must come first when we need access to the current session
session_start();;
require("classes/user.php");
$username = Utils::escape($_SESSION["username"]);
$userId = Utils::escape($_SESSION["user_id"]);
$conn = Connection::connect();
if (isset($_GET['date'])) {
$date = $_GET['date'];
/// Prepare the SQL statement to fetch existing bookings for the given date
$stmt = $conn->prepare(SQL::$getBookingsByDate);
$stmt->bindParam(1, $date, PDO::PARAM_STR);
$stmt->execute();
/// Fetch the existing bookings and populate the $bookings array
$bookings = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$bookings[] = $row['booking_time']; /// Adjust 'booking_time' as needed to match your DB column
}
}
if (isset($_POST['submit'])) {
$timeslot = $_POST['timeslot'];
/// Check if the selected timeslot is available
$stmt = $conn->prepare(SQL::$getBookingsByDateAndTime);
$stmt->bindParam(1, $date, PDO::PARAM_STR);
$stmt->bindParam(2, $timeslot, PDO::PARAM_STR);
$stmt->execute();
/// If the timeslot is available, proceed with creating the booking
if (!$stmt->fetch(PDO::FETCH_ASSOC)) {
$stmt = $conn->prepare(SQL::$createBooking);
$stmt->bindParam(1, $userId, PDO::PARAM_INT);
$stmt->bindValue(2, 1, PDO::PARAM_INT);
$stmt->bindParam(3, $date, PDO::PARAM_STR);
$stmt->bindParam(4, $timeslot, PDO::PARAM_STR);
if ($stmt->execute()) {
$msg = "<div class='alert alert-success'>Booking Successful</div>";
echo $msg;
/// Redirect after 5 seconds
$_SESSION["successMessage"] = "Appointment Booked!";
echo '<script src="js\script.js"></script>';
} else {
$msg = "<div class='alert alert-danger'>Booking Failed</div>";
echo $msg;
}
} else {
$msg = "<div class='alert alert-danger'>Timeslot already booked</div>";
echo $msg;
}
}
$duration = 30;
$cleanup = 0;
$start = "09:00";
$end = "18:00";
/**
* Generate timeslots based on the given duration, cleanup time, start, and end time.
*
* @param int $duration The duration of each timeslot in minutes.
* @param int $cleanup The cleanup time between each timeslot in minutes.
* @param string $start The start time in "Y-m-d H:i:s" format.
* @param string $end The end time in "Y-m-d H:i:s" format.
* @return array An array of timeslots formatted as "start-end".
*/
function timeslots($duration, $cleanup, $start, $end) {
$start = new DateTime($start);
$end = new DateTime($end);
$interval = new DateInterval("PT".$duration."M");
$cleanupInterval = new DateInterval("PT".$cleanup."M");
$slots = array();
for ($intStart = $start; $intStart < $end; $intStart->add($interval)->add($cleanupInterval)) {
$endPeriod = clone $intStart;
$endPeriod->add($interval);
if ($endPeriod > $end) {
break;
}
$slots[] = $intStart->format("H:i")."-".$endPeriod->format("H:i");
}
return $slots;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Booking System</title>
<link rel="stylesheet" href="https:///maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<h1 class="text-center">Book for Date: <?php echo date('m/d/Y', strtotime($date)); ?></h1>
<hr>
<div class= "row msg-row">
<div class="col-md-12">
<?php echo(isset($msg))?$msg:"";?>
</div>
</div>
<div class="row timeslot-row">
<?php
/**
* Display each timeslot as a button with a specific color based on whether it is booked or not.
*/
$timeslots = timeslots($duration, $cleanup, $start, $end);
foreach($timeslots as $ts): ?>
<div class="col-md-2 col-test">
<div class="form-group group-test">
<?php $isBooked = in_array($ts, $bookings); ?>
<button class="btn <?php echo $isBooked ? 'btn-danger' : 'btn-success book'; ?>" data-timeslot="<?php echo $ts; ?>">
<?php echo $ts; ?>
</button>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Booking: <span id="slot"></span> </h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<form action="" method="post">
<div class="form-group">
<label for="">Timeslot</label>
<input required type="text" readonly name="timeslot" id="timeslot" class="form-control">
</div>
<div class="form-group">
<label for="">Name</label>
<input required type="text" readonly name="name" class="form-control" value="<?php echo $username; ?>">
</div>
<div class="form-group">
<label for="">Email</label>
<input required type="email" readonly name="email" class="form-control">
</div>
<div class="form-group pull-right">
<button class="btn btn-submit-appt btn-primary" type="submit" name="submit">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https:///ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https:///maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script>
$(".book").click(function() {
var timeslot = $(this).attr('data-timeslot');
$("#slot").html(timeslot);
$("#timeslot").val(timeslot);
$("#myModal").modal("show");
});
</script>
</body>
</html>