-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_operations.php
134 lines (121 loc) · 4.58 KB
/
db_operations.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
<?php
// Database connection function using Singleton pattern
function getDatabaseConnection() {
static $pdo = null;
if ($pdo === null) {
try {
// Get the current directory of the PHP script
$dbPath = __DIR__ . '/bookings.db';
// Check if the database file exists
$isNewDatabase = !file_exists($dbPath);
// Create the PDO connection to the SQLite database
$pdo = new PDO("sqlite:$dbPath");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// If the database is newly created, create the Booking table
if ($isNewDatabase) {
$createTableSQL = "
CREATE TABLE IF NOT EXISTS Booking (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
time TEXT NOT NULL,
status INTEGER NOT NULL
);
";
$pdo->exec($createTableSQL);
error_log("Database and 'Booking' table created successfully.");
}
} catch (PDOException $e) {
die("Database connection failed: " . $e->getMessage());
}
}
return $pdo;
}
function getStatusLabel($status) {
$statusLabels = [
0 => 'Pending',
1 => 'Booked',
// 2 => 'finished'
];
return $statusLabels[$status] ?? 'Unknown';
}
function deleteBooking($id) {
try {
$pdo = getDatabaseConnection();
$stmt = $pdo->prepare("DELETE FROM Booking WHERE id = :id");
$stmt->execute([':id' => (int) $id]);
echo "Booking deleted successfully.";
} catch (PDOException $e) {
error_log("Error deleting booking: " . $e->getMessage());
}
}
// Function to get bookings by status
function getBookingsByStatus($status) {
try {
$pdo = getDatabaseConnection();
if ($status === '' || $status === null) {
// If $status is empty, fetch all bookings without filtering
$stmt = $pdo->prepare("SELECT * FROM Booking WHERE date > date('now', '-1 day') ORDER BY date ASC, time ASC");
$stmt->execute();
} else {
// Otherwise, fetch bookings filtered by the given status
$stmt = $pdo->prepare("SELECT * FROM Booking WHERE status = :status ORDER BY date ASC, time ASC");
$stmt->execute([':status' => (int) $status]);
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log("Error fetching bookings: " . $e->getMessage());
return [];
}
}
// Function to create a booking
function createBooking($date, $time, $status = 0) {
try {
$pdo = getDatabaseConnection();
// Check if a booking with the same date and time already exists
$checkStmt = $pdo->prepare("SELECT COUNT(*) FROM Booking WHERE date = :date AND time = :time");
$checkStmt->execute([
':date' => $date,
':time' => $time
]);
$count = $checkStmt->fetchColumn();
if ($count > 0) {
error_log("Duplicate booking attempt: A booking already exists for date $date and time $time.");
return; // Exit the function to avoid duplicate entry
}
$stmt = $pdo->prepare("INSERT INTO Booking (date, time, status) VALUES (:date, :time, :status)");
$stmt->execute([
':date' => $date,
':time' => $time,
':status' => (int) $status
]);
} catch (PDOException $e) {
error_log("Error creating booking: " . $e->getMessage());
}
}
// Function to handle incoming requests
function handleRequest() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
if ($action === 'create') {
$date = $_POST['date'] ?? date('Y-m-d');
$time = $_POST['time'] ?? date('H:i');
createBooking($date, $time);
header("Location: index.php");
exit;
} elseif ($action === 'delete') {
$id = $_POST['id'] ?? '';
if (!empty($id)) {
deleteBooking($id);
}
header("Location: index.php");
exit;
}
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['status'])) {
$status = (int)$_GET['status']; // Casting to integer for security
$bookings = getBookingsByStatus($status);
require 'index.php'; // Re-render the page with filtered bookings
exit;
}
}
// Call the request handler
handleRequest();