-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_scanned.php
195 lines (182 loc) · 6.35 KB
/
book_scanned.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
185
186
187
188
189
190
191
192
193
194
195
<?php
if(!defined('ROOT')){ define('ROOT', getcwd().'/'); }
require('../../koble_til_database.php');
session_start();
$error = array(
'multiple_user_rfid' => 'Du kan bare skanne 1 bruker-enhet.',
'no_book_rfid' => 'Du må skanne minst 1 bok.',
'failed_to_lend_book' => 'Klarte ikke å låne boken.',
'no_user_rfid' => 'For å låne bøker må du skanne en bruker-enhet.',
'nonexistant_book' => 'Boken ble ikke funnet i databasen.',
'failed_to_deliver_book' => 'Klarte ikke å levere boken.',
'deliver_success' => '',
'lend_success' => '',
'only_one_action_allowed' => 'Du kan ikke låne og levere samtidig.'
);
$post_vars = array(
'obligatory' => array(
'rfid'
)
);
//Array that contains all the post information
$vars = $post->verify($post_vars);
$vars['rfid'] = trim($vars['rfid'], ";");
$rfid_arr = explode(";", $vars['rfid']);
//Require and initialize the rfid class
require 'rfid.class.php';
$rfid = new RFID();
$user = -1;
$books = array();
for($i = 0; $i < count($rfid_arr); $i++){
$res = $rfid->type($rfid_arr[$i]);
if($res[0] == 'user'){
if($user == -1){
$user = $res[1];
}else{
//Two user RFID's are selected
j_die($error['multiple_user_rfid']);
}
}else if($res[0] == 'book'){
$books[] = array('bookID' => $res[1], 'rfid' => $rfid_arr[$i], 'shelfID' => $res[2]);
}
}
if(count($books) < 1){
//No books have been selected
j_die($error['no_book_rfid']);
}
$action = 0;
/*
Deliver / lend the book
*/
$date= (new DateTime())->format('Y-m-d H:i:s');
$deliver_deadline = (new DateTime());
$deliver_deadline->modify('+1 month');
$deliver_deadline = $deliver_deadline->format('Y-m-d H:i:s');
$deliver_deadline = explode(" ", $deliver_deadline)[0]." 00:00:00";
$res = array('error' => '', 'type' => 0);
$deliver = array();
$lend = array();
require ROOT.'scan_book.class.php';
$sb = new ScanBook();
for($i = 0; $i < count($books); $i++){
if($sb->isLended($books[$i]['rfid'])){
$user = $sb->user;
//Book shall be delivered
if($res['type'] !== "lend"){
$res['type'] = 'deliver';
require ROOT.'admin/info.class.php';
$info = new Info("books", $books[$i]['bookID']);
$result = $info->getInfo();
$result['RFID'] = $books[$i]['rfid'];
$result['shelfID'] = $books[$i]['shelfID'];
$deliver[] = $result;
}else{
j_die($error['only_one_action_allowed']);
}
}else{
//Book shall be lended
//Check if user has been set
if($user === -1){
j_die($error['no_user_rfid']);
}
if($res['type'] !== "deliver"){
$res['type'] = 'lend';
require ROOT.'admin/info.class.php';
$info = new Info("books", $books[$i]['bookID']);
$result = $info->getInfo();
$result['RFID'] = $books[$i]['rfid'];
$result['shelfID'] = $books[$i]['shelfID'];
$lend[] = array(
'user' => $user,
'date' => $date,
'book' => $result
);
}else{
j_die($error['only_one_action_allowed']);
}
}
}
//Lend shiet
for($i = 0; $i < count($lend); $i++){
$insert_user_book = "INSERT INTO lib_User_Book (userID, outDate, bookRFID, deliver_deadline) VALUES
('" . $lend[$i]['user'] . "', '" . $lend[$i]['date'] . "', '" . $lend[$i]['book']['RFID'] . "', '".$deliver_deadline."')";
$insert_user_book_qry = $conn->query($insert_user_book);
if($insert_user_book_qry === TRUE){
//Success
$res['status'][] = array(
'book_info' => get_book_info($lend[$i]['book'], $deliver_deadline),
'error' => $error['lend_success']
);
}else{
//Failed lend book
$res['status'][] = array(
'book_info' => get_book_info($lend[$i]['book'], $deliver_deadline),
'error' => $error['failed_to_lend_book']
);
}
}
//Deliver all books that are supposed to be delivered
$where_st = "";
for($i = 0; $i < count($deliver); $i++){
if($where_st == ""){
$where_st = "WHERE bookRFID = '" . $deliver[$i]['RFID'] . "' ";
}else{
$where_st .= "OR bookRFID = '" . $deliver[$i]['RFID'] . "'";
}
}
if($where_st != ""){
$deliver_books = "UPDATE lib_User_Book SET inDate = '" . $date . "' " . $where_st . " ORDER BY user_book_ID DESC LIMIT 1";
$deliver_books_qry = $conn->query($deliver_books);
if ($deliver_books_qry === TRUE) {
//Success
for($i = 0; $i < count($deliver); $i++){
$res['status'][] = array(
'book_info' => get_book_info($deliver[$i], $deliver_deadline),
'error' => $error['deliver_success']
);
}
} else {
//Failed
for($i = 0; $i < count($deliver); $i++){
$res['status'][] = array(
'book_info' => get_book_info($deliver[$i], $deliver_deadline),
'error' => $error['failed_to_deliver_book']
);
}
}
}
//Get username
$res['username'] = "";
$res['userRFID'] = "";
if($user != 0){
$get_rfid = "SELECT RFID FROM lib_RFID WHERE userID = '".$user."'";
$get_rfid_qry = $conn->query($get_rfid);
if($get_rfid_qry->num_rows > 0){
if($rfid = $get_rfid_qry->fetch_assoc()){
$res['userRFID'] = $rfid['RFID'];
}
}
$get_username = "SELECT username FROM lib_User WHERE userID = '".$user."' AND active = 1";
$get_username_qry = $conn->query($get_username);
if($get_username_qry->num_rows > 0){
if($user = $get_username_qry->fetch_assoc()){
$res['username'] = $user['username'];
}
}
}
echo json_encode($res);
function get_book_info($book, $deliver_deadline){
include 'admin/list.class.php';
$list = new Lists("shelves");
$shelf_name = $list->getShelfName($book['shelfID']);
return array(
'title' => mb_convert_encoding($book['title'], "UTF-8", 'HTML-ENTITIES'),
'author' => mb_convert_encoding($book['author'], "UTF-8", 'HTML-ENTITIES'),
'ISBN10' => $book['ISBN10'],
'ISBN13' => $book['ISBN13'],
'delivery_date' => explode(" ", $deliver_deadline)[0],
'shelf' => mb_convert_encoding($shelf_name, "UTF-8", 'HTML-ENTITIES'),
'rfid' => $book['RFID']
);
}
?>