-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalendar.php
178 lines (147 loc) · 5.5 KB
/
calendar.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
<?php
require_once("classes/connection.php");
require_once("classes/sql.php");
require_once("classes/utils.php");
require("classes/components.php");
/// This must come first when we need access to the current session
session_start();;
Components::pageHeaderAlt("Book Appointment", ["style"], ["mobile-nav"]);
/**
* Function to build a calendar for a given month and year, displaying available appointment slots.
*/
function build_calendar($month, $year){
$bookings = array();
/**
* Set up information about the days of the week and the first day of the month.
*/
$daysOfWeek = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat','Sun');
$firstDayOfMonth = mktime(0,0,0,$month,1,$year);
$numberDays = date('t',$firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
if($dayOfWeek==0){
$dayOfWeek = 6;
}else{
$dayOfWeek = $dayOfWeek-1;
}
/**
* Generates a calendar for booking appointments centered around the current date.
*/
$dateToday = date('Y-m-d');
$calendar = "<center>Book Appointment</center>";
$prev_month = date('m', mktime(0,0,0,$month-1,1,$year));
$prev_year = date('Y', mktime(0,0,0,$month-1,1,$year));
$next_month = date('m', mktime(0,0,0,$month+1,1,$year));
$next_year = date('y', mktime(0,0,0,$month+1,1,$year));
/**
* Generate a calendar HTML string for the specified month and year.
*/
$calendar .= "<center><h2>$monthName $year</h2>";
$calendar .= "<a class='btn btn-primary btn-xs' href='?month=".$prev_month."&year=".$prev_year."'>Prev Month</a>";
$calendar .= "<a class='btn btn-primary btn-xs' href='?month=".date('m')."&year=".date('Y')."'>Current Month</a>";
$calendar .= "<a class='btn btn-primary btn-xs' href='?month=".$next_month."&year=".$next_year."'>Next Month</a>";
$calendar .= "<br><table class='table table-bordered'>";
$calendar .= "<tr>";
/**
* Generate table header cells for each day of the week.
*/
foreach($daysOfWeek as $day){
$calendar .= "<th class='day-names'>$day</th>";
}
/**
* Generates a calendar table row with empty cells for days before the first day of the month.
*/
$calendar .= "</tr><tr>";
$currentDay = 1;
if($dayOfWeek > 0){
for($k = 0; $k < $dayOfWeek; $k++){
$calendar .= "<td class='empty'></td>";
}
}
/**
* Pads a string to a certain length with another string on the left.
*/
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
while($currentDay <= $numberDays){
if($dayOfWeek == 7){
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
$currentDayRel = str_pad($currentDay,2,"0", STR_PAD_LEFT);
$date = "$year-$month-$currentDayRel";
$dayName = strtolower(date('l',strtotime($date))); /// Get the day name in lowercase
/// Check if the current day is Sunday
if($dayName == 'sunday'){
$calendar .= "<td class='day-row closed'><h4>$currentDay</h4><a class='btn btn-danger btn-xs'>Closed</a></td>";
} else {
/// Otherwise, normal cell
if($date < date('Y-m-d')){
$calendar .= "<td class='day-row booked'><h4>$currentDay</h4><a class='btn btn-danger btn-xs'>N/A</a></td>";
} else {
$totalbookings = checkSlots($date);
if($totalbookings == 18){
$calendar .= "<td class='day-row booked'><h4>$currentDay</h4><a class='btn btn-danger btn-xs'>All Booked</a></td>";
} else{
$availableSlots = 18 - $totalbookings;
$calendar .= "<td class='day-row'><h4>$currentDay</h4><a href='book.php?date=$date' class='btn btn-success btn-xs'>Book</a>
<small><i>$availableSlots slots left</i></small>";
}
}
}
$currentDay++;
$dayOfWeek++;
}
/**
* Generate empty table cells for the remaining days in the week after a given day.
*/
if($dayOfWeek < 7){
$remainingDays = 7 - $dayOfWeek;
for($i=0; $i < $remainingDays; $i++){
$calendar.="<td class='empty'></td>";
}
}
$calendar .= "</tr></table>";
$calendar .= "</center>";
return $calendar;
}
/**
* Check the total number of bookings for a given date.
*
* @param string $date The date for which bookings need to be checked.
* @return int The total number of bookings for the given date.
*/
function checkSlots($date){
$conn = Connection::connect();
$stmt = $conn->prepare(SQL::$getBookingsByDate);
$stmt->bindParam(1, $date, PDO::PARAM_STR);
$totalbookings = 0;
if ($stmt->execute()) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$totalbookings = count($result);
}
return $totalbookings;
}
?>
<main class="content-wrapper calendar-content">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$dateComponents = getdate();
if(isset($_GET['month'])&&($_GET['year'])){
$month = $_GET['month'];
$year = $_GET['year'];
} else {
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
}
echo build_calendar($month, $year);
?>
</div>
</div>
</div>
</main>
<?php
Components::pageFooter();
?>