-
Notifications
You must be signed in to change notification settings - Fork 2
/
media_book.php
443 lines (339 loc) · 17.2 KB
/
media_book.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* SHOWS NON-DUPLICATE BOOKS LOGGED BY THE USER IN A GRID/GALLERY FORM. ON HOVERING ON AN ITEM THE DATE OF LOGGING IS DISPLAYED.
*
* @version PHP 8.0.12
* @since July 2022
* @author AtharvaShah
*/
session_start();
if (empty($_SESSION)) {
header("Location: login.php");
}
require "header.php";
require "connection.php";
require "functions.php";
$user_data = check_login($con);
$username = $user_data['user_name'];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
function bringLettersToFront($bkName)
{
if (substr($bkName, -5) == ', The') {
$bkName = 'The ' . substr($bkName, 0, -5);
} elseif (substr($bkName, -3) == ', A') {
$bkName = 'A ' . substr($bkName, 0, -3);
}
return $bkName;
}
?>
<!-------------------------------------------------------------------------------------
HTML PART
------------------------------------------------------------------------------------->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="shows books added by the user" />
<meta name="keywords" content="WYDRN, media book" />
<title>WYDRN - Your Books</title>
<!--Bootstrap Link-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<!--Custom Link-->
<link rel="stylesheet" href="CSS/media_book.css">
<!--Preloader Links-->
<link rel="stylesheet" href="css/preloader.css">
<link href="css/backToTop.css" rel="stylesheet">
<!-- Sweet Alert (Beautiful looking alert plugin-->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<link rel="icon" type="image/png" href="images/website/favicons/favicon-32x32.png" sizes="32x32">
<link rel="apple-touch-icon" href="images/website/favicons/apple-touch-icon.png">
<script src="js/modernizr-2.6.2.min.js"></script>
<!--PRELOADER JS-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery-1.9.1.min.js"><\/script>')
</script>
<script>
$(document).ready(function() {
setTimeout(function() {
$('body').addClass('loaded');
$('h1').css('color', '#222222');
}, 500);
});
</script>
</head>
<body class="css-selector">
<button onclick="topFunction()" id="BackToTopBtn" title="Go to top">⇑</button>
<div>
<!--PRELOADER-->
<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div id="loader-wrapper">
<div id="loader"></div>
<div class="loader-section section-left"></div>
<div class="loader-section section-right"></div>
</div>
<!--END OF PRELOADER-->
<!--ALL MEDIA ITEMS THAT WILL APPEAR AFTER PRELOADER-->
<div id="content">
<!--TITLE BANNER ANIMATED-->
<div class="my_container">
<h1 class="my-title"> YOUR BOOKS <br> <sub class="my-subtitle"><?php echo getRandomBookQuote(); ?></sub></h1>
<h1 class="my-title my-title-large">YOUR BOOKS</h1>
<div id="img-1" class="img-container">
<img class="img" src="images/website/assets/banners/books/1.jpg">
</div>
<div class="img-container second-animation">
<img class="img" src="images/website/assets/banners/books/2.jpg">
</div>
<div class="img-container third-animation">
<img class="img" src="images/website/assets/banners/books/3.jpg">
</div>
<div class="img-container fourth-animation">
<img class="img nba" src="images/website/assets/banners/books/4.jpg">
</div>
<div class="img-container fifth-animation">
<img class="img" src="images/website/assets/banners/books/5.jpg">
</div>
<div id="img-6" class="img-container sixth-animation">
<img class="img" src="images/website/assets/banners/books/6.jpg">
</div>
<div id="img-7" class="img-container seventh-animation">
<img class="img" src="images/website/assets/banners/books/7.jpg">
</div>
</div>
<!--END OF TITLE BANNER ANIMATED-->
<div class="flex">
<!-- Sorting Functionality -->
<form method="get" action="" name="sort" id="filter-select">
<select name="sortby" id="sort-by-select" onchange="this.form.submit()">
<option value="">
Sort By
</option>
<option value="added-desc">Added Date (Newest To Oldest)</option>
<option value="added-asc">Added Date (Oldest To Newest)</option>
<option value="alphabetic-asc">Book (A-Z)</option>
<option value="alphabetic-desc">Book (Z-A)</option>
<option value="author-asc">Author(A-Z)</option>
<option value="author-desc">Author (Z-A)</option>
</select>
</form>
<!-- End of Sorting Functionality -->
<!--List View Button-->
<button class="btn" onclick="window.location.href='media_list_view.php?book'"><img src="images/Icons/list-view.png"></button>
</div>
<!--Display Active Filters-->
<?php if (isset($_GET['sortby'])) { ?>
<span class="active-filter">
<img src="images/Icons/sort.png" alt="filter" height="15px" width="10px" />
<?php echo $_GET['sortby']; ?>
</span>
<?php } ?>
<!-------------------------------------------------------------------------------------
DYNAMICALLY GENERATED PHP PART
------------------------------------------------------------------------------------->
<?php
//set default sort order
$sortby = "added-desc";
$sorting = "`date`"; //default sorting is by added date;
$order = "DESC"; //default order is newest to oldest
// default sorting is by added date;
if (isset($_GET["sortby"])) {
$sortby = $_GET["sortby"];
/*************** SORT BY DATE OF LOGGING ***********/
// Newest To Oldest
if ($sortby == "added-desc") {
$sorting = "`date`";
$order = "DESC";
}
//Oldest to Newest
else if ($sortby == "added-asc") {
$sorting = "`date`";
$order = "ASC";
}
/*************** SORT BY BOOK NAME ***********/
//A-Z
else if ($sortby == "alphabetic-asc") {
$sorting = "`book`";
$order = "ASC";
}
// Z-A
else if ($sortby == "alphabetic-desc") {
$sorting = "`book`";
$order = "DESC";
}
/*************** SORT BY AUTHORs ***********/
//A-Z
else if ($sortby == "author-asc") {
$sorting = "`author`";
$order = "ASC";
}
//Z-A
else if ($sortby == "author-desc") {
$sorting = "`author`";
$order = "DESC";
}
} //end of if isset($_GET["sortby"])
// Number of entries to show in a page.
$per_page_record = 15;
// Look for a GET variable page if not found default is 1.
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page_record;
?>
<br><br>
<section class='cards-wrapper'>
<?php
//only select unique books logged by the user
$sql = "SELECT `book`, `author`, `date` FROM `data` where book != '' and username='$username' GROUP BY `book` order by " . $sorting . " " . $order . " LIMIT $start_from, $per_page_record;";
if ($query = mysqli_query($con, $sql)) {
$totalbookcount = mysqli_num_rows($query);
if ($totalbookcount > 0) {
while ($row = mysqli_fetch_assoc($query)) {
$book_name = $row['book'];
$book_author = $row['author'];
$book_logged = date("F jS, Y", strtotime($row['date']));
$stripnamebook = $stripped = str_replace(' ', '+', $book_name);
$stripnameauthor = $stripped = str_replace(' ', '+', $book_author);
//TO DISPLAY WHO HAS LOGGED THIS ITEM AMONG PEOPLE YOU ARE FOLLOWING
$followed_username = array();
$profile_pic = array();
try {
//this will throw an error if the name of the media contains an apostrophe.
$sql2 = "SELECT DISTINCT `follower_username`, `followed_username`, `book`, `profile_pic` FROM social
JOIN data ON data.username=social.followed_username
JOIN users on social.followed_username=users.user_name
WHERE social.follower_username='$username' and data.book='$book_name'
LIMIT 5";
$res = mysqli_query($con, $sql2);
while ($row = mysqli_fetch_assoc($res)) {
$followed_username[] = $row['followed_username'];
$profile_pic[] = $row['profile_pic'];
}
}
// TO CATCH APOSTROPHE ERROR AND CONTINUE
catch (mysqli_sql_exception $e) {
$error = $e->getMessage();
}
/**if poster is not downloaded, download the poster in the directory and show the image*/
//remove special characters since we are using it as a file name
$filename = preg_replace('/[^A-Za-z0-9\-]/', '', $stripnamebook);
$pseudo_poster = 'images/API/book-' . $filename . '.jpg';
if (file_exists($pseudo_poster)) {
$posterpath = $pseudo_poster;
} else {
$posterpath = BookPosterPath($stripnamebook, $stripnameauthor); // URL to download file from
$img = 'images/API/book-' . $filename . '.jpg'; // Image path to save downloaded image
file_put_contents($img, file_get_contents($posterpath)); // Save image
}
?>
<!-- // one single div tag for each movie -->
<div class='card-grid-space'>
<!-- // image tag for the movie -->
<div class='card' style='background-image:url(<?php echo $posterpath; ?>);'>
<!--Delete item on hover-->
<div class='delete-item' title="Delete Book">
<img class="delete-icon" src="images/icons/delete.png" alt="Delete">
</div>
<!---Show Logged Date on Hover-->
<div class='logged-date'><img src="images/Icons/plus.png" class="plus-icon"><?php echo $book_logged ?></div>
<!--Contains Profile Pics of Users followed by logged in user and who have added the same media -->
<?php if (!empty($profile_pic) && !empty($followed_username)) {
echo "<div class='mutual-enjoyers'>";
foreach (array_combine($profile_pic, $followed_username) as $pic => $fname) {
echo "<img src='$pic' class='mutual-enjoyer-pic' alt='$fname' title='$fname'>";
}
echo "</div>";
} ?>
</div><!--END OF CARD-->
<!--While printing the name of the book, bring A and THE to the front-->
<h1 class='moviename'><?php echo bringLettersToFront($book_name); ?>
<div class='tags'>
<div class='tag'><?php echo $book_author ?></div>
</div></h1>
</div>
<?php
}
} else { ?>
<!-- NO BOOKS LOGGED MESSAGE -->
<div class="zero-media"><img src='images/Icons/Book.svg' width='15' height='15' class='media-icon'> No books added to your account.</div>
<?php
}
}
?>
</section>
<!-------------------------------------------------------------------------------------
PAGINATION
------------------------------------------------------------------------------------->
<center>
<div class="pagination">
<?php
$query = "SELECT DISTINCT count(DISTINCT `book`) FROM `data` where book != '' and username='$username'";
$rs_result = mysqli_query($con, $query);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
echo "</br>";
// CALCULATING THE NUMBER OF PAGES
$total_pages = ceil($total_records / $per_page_record);
$pageLink = "";
// SHOW PREVIOUS BUTTON IF NOT ON PAGE 1
if ($page >= 2) {
echo "<a href='media_book.php?sortby=" . $sortby . "&page=" . ($page - 1) . "'> <span class='neonText'> ← </span> </a>";
}
// SHOW THE LINKS TO EACH PAGE IN THE PAGINATION GRID
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $page) {
$pageLink .= "<a class = 'active' href='media_book.php?page="
. $i . "'>" . $i . " </a>";
} else {
$pageLink .= "<a href='media_book.php?sortby=" . $sortby . "&page=" . $i . "'>" . $i . " </a>";
}
}
echo $pageLink;
// SHOW NEXT BUTTON IF NOT ON LAST PAGE
if ($page < $total_pages) {
echo "<a href='media_book.php?sortby=" . $sortby . "&page=" . ($page + 1) . "'> <span class='neonText'> → </span> </a>";
}
?>
</div>
<!--END OF PAGINATION ROW -->
</div>
<!--END OF MEDIA CONTENT -->
</div>
<!---END OF ENTIRE DOCUMENT (PRELOADER + MEDIA CONTENT) -->
</center>
</body>
<script src="js/backToTop.js"></script>
<script>
//Delete item on click
$('.delete-item').click(function() {
var bookName = $(this).closest('.card-grid-space').find('.moviename').text();
var parent = $(this).parent("div").parent("div");
//trim the name to remove trailing whitespace and match the name to be the same as the one in the database
bookName=bookName.substring(0, bookName.indexOf(' '));
//this is only for the book media
bookName=bookName.trim();
console.log(bookName);
$.ajax({
type: "GET",
url: "delete_item.php",
data: 'book=' + bookName,
success: function() {
parent.fadeOut('slow', function() {
$(this).remove();
});
},
error: function() {
alert('This book could not be deleted!');
}
});
});
</script>
</html>
<?php mysqli_close($con); ?>