-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.php
More file actions
166 lines (144 loc) · 6.24 KB
/
book.php
File metadata and controls
166 lines (144 loc) · 6.24 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
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
<?php
/**
* Book Detail Page
* Shows full book information and reservation options
*/
$page_title = "Book Details";
require_once 'includes/header.php';
require_once 'includes/functions.php';
// Get book ID from URL
$book_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($book_id <= 0) {
header("Location: books.php");
exit();
}
// Get book details
$book = getBookById($book_id);
if (!$book) {
header("Location: books.php");
exit();
}
// Handle reservation
$message = '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reserve') {
if (!isLoggedIn()) {
header("Location: login.php");
exit();
}
$user_id = getCurrentUserId();
// Check if user already has an active reservation for this book
if (hasActiveReservation($user_id, $book_id)) {
$error = "You already have an active reservation for this book.";
} elseif ($book['available_copies'] <= 0) {
$error = "No copies available for reservation.";
} else {
// Create reservation
global $pdo;
$stmt = $pdo->prepare("INSERT INTO reservations (user_id, book_id, status) VALUES (?, ?, 'ACTIVE')");
if ($stmt->execute([$user_id, $book_id])) {
$message = "Book reserved successfully!";
// Refresh book data to show updated availability
$book = getBookById($book_id);
} else {
$error = "Failed to reserve book. Please try again.";
}
}
}
?>
<h1>Book Details</h1>
<div class="book-detail">
<!-- Placeholder for book cover -->
<img src="https://placehold.co/200x300?text=Book+Cover" alt="Book Cover" class="book-cover">
<div class="book-info">
<h2><?php echo htmlspecialchars($book['title']); ?></h2>
<p class="book-meta"><strong>Author:</strong> <?php echo htmlspecialchars($book['author']); ?></p>
<p class="book-meta"><strong>Category:</strong> <?php echo htmlspecialchars($book['category']); ?></p>
<p class="book-meta"><strong>ISBN:</strong> <?php echo htmlspecialchars($book['isbn'] ?? 'N/A'); ?></p>
<p class="book-meta"><strong>Total Copies:</strong> <?php echo $book['total_copies']; ?></p>
<p class="book-meta"><strong>Available Copies:</strong>
<?php if ($book['available_copies'] > 0): ?>
<span class="status-active"><?php echo $book['available_copies']; ?> available</span>
<?php else: ?>
<span class="status-rejected">Not available</span>
<?php endif; ?>
</p>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-error"><?php echo $error; ?></div>
<?php endif; ?>
<div class="book-description">
<h3>Description</h3>
<p><?php echo htmlspecialchars($book['description'] ?? 'No description available.'); ?></p>
</div>
<div class="book-actions">
<?php if (isLoggedIn()): ?>
<?php if ($book['available_copies'] > 0 && !hasActiveReservation(getCurrentUserId(), $book_id)): ?>
<form method="post" style="display: inline;">
<input type="hidden" name="action" value="reserve">
<input type="hidden" name="book_id" value="<?php echo $book['id']; ?>">
<button type="submit" class="btn btn-success" onclick="return confirm('Are you sure you want to reserve this book?')">Reserve (Mark as Mine)</button>
</form>
<?php elseif (hasActiveReservation(getCurrentUserId(), $book_id)): ?>
<button class="btn btn-warning" disabled>Already Reserved</button>
<p><em>You have an active reservation for this book.</em></p>
<?php else: ?>
<button class="btn btn-danger" disabled>Not Available</button>
<p><em>There are no copies available for reservation at this time.</em></p>
<?php endif; ?>
<?php else: ?>
<p><a href="login.php" class="btn btn-primary">Log in to Reserve</a></p>
<p><em>Log in to reserve this book.</em></p>
<?php endif; ?>
</div>
</div>
</div>
<div style="clear: both;"></div>
<h3>Similar Books</h3>
<div class="grid">
<?php
// Get similar books (same category, excluding current book)
global $pdo;
$stmt = $pdo->prepare("
SELECT b.*,
(b.total_copies - COALESCE(r.reserved_count, 0)) as available_copies
FROM books b
LEFT JOIN (
SELECT book_id, COUNT(*) as reserved_count
FROM reservations
WHERE status = 'ACTIVE'
GROUP BY book_id
) r ON b.id = r.book_id
WHERE b.category = ? AND b.id != ?
ORDER BY b.title ASC
LIMIT 3
");
$stmt->execute([$book['category'], $book['id']]);
$similar_books = $stmt->fetchAll();
foreach ($similar_books as $similar_book):
?>
<div class="card">
<div class="card-content">
<h3 class="card-title"><?php echo htmlspecialchars($similar_book['title']); ?></h3>
<p class="card-author">by <?php echo htmlspecialchars($similar_book['author']); ?></p>
<span class="card-category"><?php echo htmlspecialchars($similar_book['category']); ?></span>
<?php if ($similar_book['available_copies'] > 0): ?>
<span class="availability-badge available"><?php echo $similar_book['available_copies']; ?> available</span>
<?php else: ?>
<span class="availability-badge unavailable">Not available</span>
<?php endif; ?>
<div class="card-actions">
<a href="book.php?id=<?php echo $similar_book['id']; ?>" class="btn btn-primary">View Details</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($similar_books)): ?>
<div class="alert alert-info">
<p>No similar books found in this category.</p>
</div>
<?php endif; ?>
</div>
<?php require_once 'includes/footer.php'; ?>