forked from OnlineLibrary252Project/OnlineLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
231 lines (170 loc) · 7.93 KB
/
Copy pathindex.php
File metadata and controls
231 lines (170 loc) · 7.93 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
$title="Main Page";
require_once 'template/header.php';
require_once 'classes/User.php';
//setcookie('username','Nawaf',time()+30*24*60*60);
$errors=[];
$user = new User();
if ($_SERVER['REQUEST_METHOD'] == 'POST' && ( isset($_POST["Addbook_copies"]) || isset($_POST["Minusbook_copies"]) ) ){
if(isset( $_POST["Addbook_copies"] )){
$copies = $_POST["Addbook_copies"] + 1;
}
elseif (isset( $_POST["Minusbook_copies"] )){
if(empty( $_POST["Minusbook_copies"] ))array_push($errors,"There is no copies to reduce");
$copies = $_POST["Minusbook_copies"] - 1;
}
if(!count($errors)){
$st = $mysqli->prepare("update books set copies = ? where id = ?");
$st -> bind_param("ii", $copies, $IdToDelete);
$IdToDelete = $_POST['book_id'];
$st -> execute();
if($st->error) echo $st->error; else echo "<script>location.href='index.php'</script>";
}
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_POST["borrow"] ) ){
$user_id= $_SESSION['user_id'];
$book_id = $_POST['book_id'];
$NumberOfTotalPending = $mysqli -> query("select status from requests where user_id=$user_id and status='pending' ")->fetch_all(MYSQLI_ASSOC);
$NumberOfTotalPending = count($NumberOfTotalPending);
$HasPendingSameBook = $mysqli -> query("select * from requests where user_id=$user_id and book_id=$book_id and status='pending' ")->fetch_all(MYSQLI_ASSOC);
$NumberOfSamePending = count($HasPendingSameBook);
#Check if the user has 3 pending request ( for three diffrent books)
if($NumberOfTotalPending>=3){
array_push($errors,"Can't Send a new request while you have 3 pending requests");
}else if($NumberOfSamePending>0){ #Check if the user has pending request ( for the same book )
array_push($errors,"Can't Send a new request while you a pending one to the same book");
}else {
$timenow = date("Y-m-d H:i:s", strtotime("+2 hours") );
$st = $mysqli->prepare("insert into requests (user_id, book_id, requestDate) values(?, ?, ?) ");
$st -> bind_param("dds", $user_id, $book_id,$timenow);
$st -> execute();
if($st->error) echo $st->error;
else{
$_SESSION['success_message'] = "Request sent successfully";
echo "<script>location.href='index.php'</script>";
}
}
}
if($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['search']) ){ ?>
<?php
$NameToSearch = $_GET['search'];
$books = $mysqli->query("select * from books where name like '%$NameToSearch%' ");
?>
<div class="row">
<?php foreach ($books as $book) {
$subject_id = (int)$book["subject_id"];
$subject_name = $mysqli->query("select subject_name from subjects where id = $subject_id")->fetch_assoc();
$subject_name = $subject_name['subject_name'];
?>
<div class="col-md4">
<div class="card mb-3">
<div class="card-body">
<div class="custom-card-image" style="background-image: url('<?php echo $config['app_url'].$book['image']; ?> ')"></div>
<div class="">Name: <?php echo $book["name"]; ?></div>
<div class="">Subject: <?php echo $subject_name; ?></div>
<div class="">Author: <?php echo $book["author"]; ?></div>
<div class="">Number Of Available Copies:
<?php echo $book["copies"]; ?>
<?php // Request Button appears to the user only (Request to borrow a book)
if($user->isLoggedIn() && $_SESSION['user_role']=='user' && $book["copies"]>0){ ?>
<div class="">
<form action="" method="post" style="display: inline">
<input type="hidden" name="book_id" value="<?php echo $book["id"]; ?>">
<button onclick="confirm('Are you sure?')" class="btn btn-primary" type="submit" name="borrow">Request to Borrow</button>
</form>
</div>
<?php } ?>
</div>
<?php
if ($book["link"]) {
if ($user->isLoggedIn()) { ?>
<div class=""> <a href="<?php echo $book["link"]; ?>" target="_blank">Link</a> </div>
<?php } else { ?>
<div class=""> Log in to see the link</div>
<?php }
}else { ?>
<div class=""> Sorry, This book isn't available online</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
<?php
die();
}
?>
<h1>Welcome to our website</h1>
<?php include 'template/errors.php'; ?>
<!-- Search Form -->
<form action="" method="GET">
<input id="search" type="text" name="search" placeholder="Book Name">
<input id="submit" type="submit" value="Search">
</form>
<?php $books = $mysqli->query("select * from books"); ?>
<div class="row">
<?php foreach ($books as $book) {
$subject_id = (int)$book["subject_id"];
$subject_name = $mysqli->query("select subject_name from subjects where id = $subject_id")->fetch_assoc();
$subject_name = $subject_name['subject_name'];
?>
<div class="col-md4">
<div class="card mb-3">
<div class="card-body">
<div class="custom-card-image" style="background-image: url('<?php echo $config['app_url'].$book['image']; ?> ')"></div>
<div class="">Name: <?php echo $book["name"]; ?></div>
<div class="">Subject: <?php echo $subject_name; ?></div>
<div class="">Author: <?php echo $book["author"]; ?></div>
<div class="">Number Of Available Copies:
<?php
// Reduce Button appears to the admin only (copies)
if($user->isLoggedIn() && $_SESSION['user_role']=='admin'){ ?>
<form action="" method="post" style="display: inline" >
<input type="hidden" name="book_id" value="<?php echo $book["id"]; ?>">
<input type="hidden" name="Minusbook_copies" value="<?php echo $book["copies"]; ?>">
<button onclick='return confirm("Are you sure?")' class="btn btn-danger" type="submit">-</button>
</form>
<?php } ?>
<?php echo $book["copies"]; ?>
<?php // Request Button appears to the user only (Request to borrow a book)
if($user->isLoggedIn() && $_SESSION['user_role']=='user' && $book["copies"]>0){ ?>
<div class="">
<form action="" method="post" style="display: inline">
<input type="hidden" name="book_id" value="<?php echo $book["id"]; ?>">
<button onclick="confirm('Are you sure?')" class="btn btn-primary" type="submit" name="borrow">Request to Borrow</button>
</form>
</div>
<?php } ?>
<?php
// Add Button appears to the admin only (copies)
if($user->isLoggedIn() && $_SESSION['user_role']=='admin'){ ?>
<form action="" method="post" style="display: inline" >
<input type="hidden" name="book_id" value="<?php echo $book["id"]; ?>">
<input type="hidden" name="Addbook_copies" value="<?php if($book["copies"])echo $book["copies"];else echo 0; ?>">
<button onclick='return confirm("Are you sure?")' class="btn btn-success" type="submit">+</button>
</form>
<?php } ?>
</div>
<?php
if ($book["link"]) {
if ($user->isLoggedIn()) { ?>
<div class=""> <a href="<?php echo $book["link"]; ?>" target="_blank">Link</a> </div>
<?php } else { ?>
<div class=""> Log in to see the link</div>
<?php }
}else { ?>
<div class=""> Sorry, This book isn't available online</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
<br><br>
<?php
echo "<br><br><br><br>";
unset($Service); // call the destructor to close the class
?>
<?php
$mysqli->close();
require_once 'template/footer.php'; ?>