diff --git a/app/controllers/BookController.php b/app/controllers/BookController.php index 010ea116a90b4ca914777f1de2838b32bdad66a8..c857046d9ebd70fc63a61ac002fef1a921fac9de 100644 --- a/app/controllers/BookController.php +++ b/app/controllers/BookController.php @@ -11,40 +11,8 @@ class BookController extends Controller implements ControllerInterface{ try{ switch($_SERVER['REQUEST_METHOD']){ case 'GET': - $bookData = $this->model->getBooks(1); - - // User - if(isset($_SESSION['username'])){ - $userData = $this->model('UserModel'); - $user = $userData->getUserByUsername($_SESSION['username']); - $username = $user['username']; - $role = $user['role']; - $imagePath = $user['image_path']; - - $own = $userData->getMyBook($_SESSION['username']); - - $have = ['own'=>$own]; - $nav = ['username'=>$username, 'role'=> $role, 'profpic'=> $imagePath]; - } else { - $nav = ['username'=>null]; - $have = ['own'=>null]; - } - - if(!isset($_GET['page'])) { - $page = 1; - } else { - $page = $_GET['page']; - } - - $_SESSION['page'] = $page; - - $genre = ['genres'=>$this->model('GenreModel')->getAllGenres()]; - $author = ['authors'=>$this->model('AuthorModel')->getAllAuthors()]; - $dataset=['book'=>$bookData]; - $paginationData = ['totalPages' => $this->model->getTotalPages(8), 'page' => 1]; - $bookListView =$this->view('book','BookView', array_merge($dataset, $nav, $genre, $author, $have, $paginationData)); - $bookListView->render(); - break; + header("Location: /book/search/1", true, 301); + exit; } }catch (Exception $e) { http_response_code($e->getCode()); @@ -167,10 +135,6 @@ class BookController extends Controller implements ControllerInterface{ exit; } - // $editBookView = $this->view('admin', 'UpdateBookView', - // ['username' => $username,'role' => $this->model->getUserRole($username)]); - // $editBookView->render(); - exit; } @@ -253,12 +217,23 @@ class BookController extends Controller implements ControllerInterface{ switch ($_SERVER['REQUEST_METHOD']) { case 'GET': $q = ''; + if (isset($_GET['q'])) { $q = $_GET['q']; } + + if(!isset($page)) { + $page = 1; + } + + $_SESSION['page'] = $page; + $sort = isset($_GET['sort']) ? $_GET['sort'] : 'title'; $filter = isset($_GET['filter']) ? $_GET['filter'] : 'all'; - $bookData = $this->model->getByQuery($q, $_GET['sort'], $_GET['filter'], $page); + + $bookData = $this->model->getByQuery($q, $sort, $filter, $page); + $count = $this->model->bookCount($q, $sort, $filter); + $totalPages = ceil($count / BOOK_PER_PAGES); // User if(isset($_SESSION['username'])){ @@ -276,10 +251,14 @@ class BookController extends Controller implements ControllerInterface{ $nav = ['username'=>null]; $have = ['own'=>null]; } + + $q_params = "?filter=$filter&sort=$sort&q=$q"; + $genre = ['genres'=>$this->model('GenreModel')->getAllGenres()]; $author = ['authors'=>$this->model('AuthorModel')->getAllAuthors()]; $dataset=['book'=>$bookData]; - $bookListView =$this->view('book','BookView', array_merge($dataset, $nav, $genre, $author, $have)); + $paginationData = ['totalPages' => $totalPages, 'page' => $page, 'q_params' => $q_params]; + $bookListView =$this->view('book','BookView', array_merge($dataset, $nav, $genre, $author, $have, $paginationData)); $bookListView->render(); exit; default: diff --git a/app/models/BookModel.php b/app/models/BookModel.php index 3c65ae8255f8aa44a71ddb008d41a1681791e2a8..32654cb9849860dbdd12a65c5452367b0b4fc98f 100644 --- a/app/models/BookModel.php +++ b/app/models/BookModel.php @@ -252,6 +252,7 @@ class BookModel { } $stmt = $this->db->prepare($query); + echo $this->db->error_info(); $q = "%$q%"; @@ -267,6 +268,51 @@ class BookModel { $stmt->close(); return $books; } + + public function bookCount($q, $sort = 'title', $filter = 'all') { + $perPage = BOOK_PER_PAGES; + + if ($filter === 'all') { + $query = + "SELECT b.*, GROUP_CONCAT(DISTINCT a.full_name) AS authors, GROUP_CONCAT(DISTINCT g.name) AS genres + FROM book b + JOIN authored_by ab ON b.book_id = ab.book_id + JOIN author a ON ab.author_id = a.author_id + JOIN book_genre bg ON b.book_id = bg.book_id + JOIN genre g ON bg.genre_id = g.genre_id + WHERE (a.full_name LIKE ? OR b.title LIKE ?) + GROUP BY b.book_id + ORDER BY $sort"; + } else { + $query = + "SELECT b.*, GROUP_CONCAT(DISTINCT a.full_name) AS authors, GROUP_CONCAT(DISTINCT g.name) AS genres + FROM book b + JOIN authored_by ab ON b.book_id = ab.book_id + JOIN author a ON ab.author_id = a.author_id + JOIN book_genre bg ON b.book_id = bg.book_id + JOIN genre g ON bg.genre_id = g.genre_id + WHERE (a.full_name LIKE ? OR b.title LIKE ?) AND (g.name = ? OR a.full_name = ?) + GROUP BY b.book_id + ORDER BY $sort"; + } + + $stmt = $this->db->prepare($query); + echo $this->db->error_info(); + + $q = "%$q%"; + + if ($filter !== 'all') { + $this->db->bindParams($stmt, "ssss", $q, $q, $filter, $filter); + } else { + $this->db->bindParams($stmt, "ss", $q, $q); + } + + $this->db->execute($stmt); + $books = $this->db->getAllRecords($stmt); + + $stmt->close(); + return count($books); + } public function getBookByUserId($userId) diff --git a/app/pages/book/BookListPage.php b/app/pages/book/BookListPage.php index a9d05bb5f726304198029c4878af008dfeca0b4c..32a63c274d96c4d443c0c67df43e14e1a1e12337 100644 --- a/app/pages/book/BookListPage.php +++ b/app/pages/book/BookListPage.php @@ -46,8 +46,8 @@ <option value="year desc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'year desc') : ?>selected="selected"<?php endif; ?>>Year DSC</option> </optgroup> <optgroup label="Book Name"> - <option value="title-asc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title asc') : ?> selected="selected"<?php endif; ?>>Title ASC</option> - <option value="title-dsc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title dsc') : ?>selected="selected"<?php endif; ?>>Title DSC</option> + <option value="title asc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title asc') : ?> selected="selected"<?php endif; ?>>Title ASC</option> + <option value="title desc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title dsc') : ?>selected="selected"<?php endif; ?>>Title DSC</option> </optgroup> </select> <input type="text" name="q" class="search" placeholder="Search"/> @@ -112,16 +112,18 @@ $maxPage = $this->data['totalPages']; $prevPage = $page - 1; + $params = $this->data['q_params']; + $topPage = $page + 10; if ($topPage > $maxPage) { $topPage = $maxPage; } - + for ($i = 1; $i <= $topPage; $i++) { if ($i == $page) { - echo '<a href="/author/update?page=' . $i . '" class="page-btn"><b>' . $i . '</b></a>'; + echo '<a href="/book/search/' . $i . $params . '" class="page-btn"><b>' . $i . '</b></a>'; } else { - echo '<a href="/author/update?page=' . $i . '" class="page-btn"><div class="">' . $i . '</div></a>'; + echo '<a href="/book/search/' . $i . $params . '" class="page-btn"><div class="">' . $i . '</div></a>'; } } ?>