diff --git a/app/controllers/BookController.php b/app/controllers/BookController.php index c857046d9ebd70fc63a61ac002fef1a921fac9de..567aafeafbd8b06193889e8c2266c6e4ab5bb0dc 100644 --- a/app/controllers/BookController.php +++ b/app/controllers/BookController.php @@ -3,6 +3,7 @@ class BookController extends Controller implements ControllerInterface{ private BookModel $model; public function __construct() { + require_once __DIR__ . '/../models/UserRole.php'; $this->model = $this->model('BookModel'); } @@ -132,50 +133,61 @@ class BookController extends Controller implements ControllerInterface{ $id = (int)$params; $book = $this->model->getBookById($id); if (!isset($book)) { + header("Location: /book/", true, 301); exit; } + + $editBookView = $this->view('admin', 'UpdateBookView', $book); + $editBookView->render(); + exit; } break; case 'POST': - $uploadedImage = PROFILE_PIC_BASE; + // $fileHandler = new FileHandler(); - if (isset($_FILES['profile-pic'])) { - $fileHandler = new FileHandler(); - - $imageFile = $_FILES['profile-pic']['tmp_name']; - - $uploadedImage = $fileHandler->saveImageTo($imageFile, $_POST['username'], PROFILE_PIC_PATH); - } - - $username = $_POST['username']; - $email = $_POST['email']; - $pass = $_POST['password']; + // if (isset($_FILES['cover'])) { + // $imageFile = $_FILES['cover']['tmp_name']; + // $uploadedImage = $fileHandler->saveImageTo($imageFile, $_POST['title'], BOOK_COVER_PATH); + // } else { + // $uploadedImage = $_POST['old']['image_path']; + // } + + // if (isset($_FILES['audio'])) { + // $audioFile = $_FILES['audio']['tmp_name']; + // $duration = (int) $fileHandler->getAudioDuration($audioFile); + // $uploadedAudio = $fileHandler->saveAudioTo($audioFile, $_POST['title'], AUDIOBOOK_PATH); + // } else { + // $uploadedAudio = $_POST['old']['audio_path']; + // $duration = $_POST['old']['duration']; + // } - $this->model->addUser( - $username, $email, UserRole::Customer, $pass, $uploadedImage - ); - - http_response_code(301); - header("Location: /book/", true, 301); + $id = $params; + $old = $this->model->getBookById($id); - exit; - case 'PATCH': - if ($_SESSION['role'] != UserRole::Admin) { - $unauthorizedView = $this->view('.', 'UnauthorizedView'); - $unauthorizedView->render(); - exit; - } + $uploadedImage = $old['cover_path']; + $uploadedAudio = $old['audio_path']; + $duration = $old['duration']; - $username = $params; - $role = $_POST['role']; + $title = !empty($_POST['title']) ? $_POST['title'] : $old['title']; + $year = !empty($_POST['year']) ? (int)$_POST['year'] : $old['year']; + $summary = !empty($_POST['summary']) ? $_POST['summary'] : $old['summary']; + $price = !empty($_POST['price']) ? (int)$_POST['price'] : $old['price']; - $this->model->updateRole($username, $role); + $lang = 'English'; + if (isset($_POST['lang'])) { + $lang = $_POST['lang']; + } + + $bookId = $this->model->updateBook( + $old['book_id'], $title, $year, $summary, $price, $duration, $lang, + $uploadedAudio, $uploadedImage, + // $authors, $genres + ); - http_response_code(301); - header("Location: /user/update", true, 301); + header("Location: /book/details/$id", true, 301); exit; @@ -270,5 +282,65 @@ class BookController extends Controller implements ControllerInterface{ exit; } } + + public function delete($params = null) { + if (!isset($_SESSION['username'])) { + http_response_code(301); + header("Location: /user/login", true, 301); + exit; + } + try { + switch ($_SERVER['REQUEST_METHOD']) { + case 'GET': + if ($_SESSION['role'] != UserRole::Admin) { + $unauthorizedView = $this->view('.', 'UnauthorizedView'); + $unauthorizedView->render(); + exit; + } + + if (isset($params)) { + $book_id = $params; + + $book = $this->model->getBookById($book_id); + if (!$book) { + header("Location: /book/", true, 301); + exit; + } + + + $deleteBookView = $this->view('admin', 'DeleteBookView', $book); + $deleteBookView->render(); + + exit; + } + + break; + case 'POST': + if (isset($params)) { // editing specific book + $id = $params; + + $succ = $this->model->deleteBook($id); + + if ($succ) { + header("Location: /book/", true, 301); + } + + echo "Failed to delete book"; + break; + } + + $notFoundView = $this->view('not-found', 'NotFoundView'); + $notFoundView->render(); + + exit; + + default: + throw new RequestException('Method Not Allowed', 405); + } + } catch (Exception $e) { + http_response_code($e->getCode()); + exit; + } + } } ?> \ No newline at end of file diff --git a/app/models/BookModel.php b/app/models/BookModel.php index 32654cb9849860dbdd12a65c5452367b0b4fc98f..bc23ae44b4e5c9b8c9bca1e78385cd85ed522b7c 100644 --- a/app/models/BookModel.php +++ b/app/models/BookModel.php @@ -67,7 +67,7 @@ class BookModel { public function updateBook( int $bookId, string $title, int $year, string $summary, int $price, int $duration, string $lang, string $audio_path, string $img_path, - array $authors, array $genres + // array $authors, array $genres ): bool { $sql = "UPDATE book SET title = ?, year = ?, summary = ?, price = ?, duration = ?, @@ -85,18 +85,18 @@ class BookModel { $stmt->close(); if ($result) { - $this->removeAuthorsFromBook($bookId); - $this->removeGenresFromBook($bookId); + // $this->removeAuthorsFromBook($bookId); + // $this->removeGenresFromBook($bookId); - foreach ($authors as $authorId) { - $this->addAuthorToBook($bookId, $authorId); - } + // foreach ($authors as $authorId) { + // $this->addAuthorToBook($bookId, $authorId); + // } - foreach ($genres as $genreId) { - $this->addGenreToBook($bookId, $genreId); - } + // foreach ($genres as $genreId) { + // $this->addGenreToBook($bookId, $genreId); + // } - return true; + // return true; } return false; diff --git a/app/pages/admin/DeleteBookPage.php b/app/pages/admin/DeleteBookPage.php new file mode 100644 index 0000000000000000000000000000000000000000..b980aca3ec94e7d44119bc6b29f5f360676be4b8 --- /dev/null +++ b/app/pages/admin/DeleteBookPage.php @@ -0,0 +1,61 @@ +<!DOCTYPE html> +<html> + +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link rel="icon" sizes="180x180" href="<?= BASE_URL ?>/icon/favicon-110.png"> + <link rel="icon" type="image/png" sizes="32x32" href="<?= BASE_URL ?>/icon/favicon-32.png"> + <!-- Global CSS --> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/template/globals.css"> + <!-- Navbar CSS --> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/template/navbar.css"> + <!-- Page-specific CSS --> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/admin/list.css"> + <title>Delete Book : <? echo $this->data['title'];?></title> +</head> + +<body> + <!-- Navigation bar --> + <?php include(dirname(__DIR__) . '../../components/Navbar.php') ?> + <div class="wrapper-small"> + <div class="pad-40"> + <h1>Delete Book Page</h1> + <div class="centered"> + <form + class="center-contents" + action="/book/delete/<? echo $this->data['book_id']?>" method="POST" enctype="multipart/form-data" + > + <h2><b>Book Information</b></h2> + <p class="form-label">Book ID : <? + echo $this->data['book_id']; + ?></p> + <p class="form-label">Title : <? + echo $this->data['title']; + ?></p> + <p class="form-label">Year Published : <? + echo $this->data['year']; + ?></p> + <p class="form-label">Authors : <? + echo $this->data['authors']; + ?></p> + <p class="form-label">Genres : <? + echo $this->data['genres']; + ?></p> + <p class="form-label">Price : <? + echo $this->data['price']; + ?></p> + <p class="form-label">Summary : <? + echo $this->data['summary']; + ?></p> + + + <input type="submit" class="button green-button" value="Delete"> + + </form> + </div> + </div> + </div> +</body> + +</html> \ No newline at end of file diff --git a/app/pages/admin/UpdateBookPage.php b/app/pages/admin/UpdateBookPage.php new file mode 100644 index 0000000000000000000000000000000000000000..4eda91ceff66066ea60b953f341fb9cee98cb805 --- /dev/null +++ b/app/pages/admin/UpdateBookPage.php @@ -0,0 +1,90 @@ +<!DOCTYPE html> +<html> + +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link rel="icon" sizes="180x180" href="<?= BASE_URL ?>/icon/favicon-110.png"> + <link rel="icon" type="image/png" sizes="32x32" href="<?= BASE_URL ?>/icon/favicon-32.png"> + <!-- Global CSS --> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/template/globals.css"> + <!-- Navbar CSS --> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/template/navbar.css"> + <!-- Page-specific CSS --> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/admin/list.css"> + <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/admin/crud-page.css"> + <title>Update Book : <? echo $this->data['book_id'];?></title> +</head> + +<body> + <!-- Navigation bar --> + <?php include(dirname(__DIR__) . '../../components/Navbar.php') ?> + <div class="wrapper-small"> + <div class="pad-40"> + <h1>Update Book Page</h1> + <form + action="/book/update/<? echo $this->data['book_id']?>" method="POST" enctype="multipart/form-data" + > + <h2><b>Old Information</b></h2> + <p class="form-label">Book ID : <? + echo $this->data['book_id']; + ?></p> + <p class="form-label">Title : <? + echo $this->data['title']; + ?></p> + <p class="form-label">Year Published : <? + echo $this->data['year']; + ?></p> + <p class="form-label">Authors : <? + echo $this->data['authors']; + ?></p> + <p class="form-label">Genres : <? + echo $this->data['genres']; + ?></p> + <p class="form-label">Price : <? + echo $this->data['price']; + ?></p> + <p class="form-label">Summary : <? + echo $this->data['summary']; + ?></p> + + <input type="hidden" name="old" value=<?$this->data?>> + + <br> + <label class="form-label" for="title">New Book Title:</label><br> + <input class="form-field" type="text" id="title" name="title"> + + <br><br> + + <label class="form-label" for="year">New Book Published Year:</label><br> + <input class="form-field" type="number" id="year" name="year"> + + <br><br> + + <label class="form-label" for="genre">New Book Price:</label><br> + <input class="form-field" type="number" id="genre" name="genre"> + + <br><br> + + <label class="form-label" for="genre">New Book Summary:</label><br> + <textarea + class="text-area" type="text" id="summary" name="summary" + rows="5" cols="50" + > </textarea> + + <!-- <label class="file-upload form-label" for="cover">New Book Cover:</label> + <input type="file" id="image" name="cover" accept="image/png, image/jpeg"> + + <label class="file-upload form-label" for="audio">New Audio:</label> + <input type="file" id="audio" name="audio" accept="audio/mpeg"> --> + + <br><br> + + <input type="submit" class="button green-button" value="Update"> + + </form> + </div> + </div> +</body> + +</html> \ No newline at end of file diff --git a/app/pages/admin/UpdateSpecificAuthorPage.php b/app/pages/admin/UpdateSpecificAuthorPage.php index 0bbcfc5050746ce5c4af326c2c43070e93b366b7..9a01ec34c93f80aa8242eed8c71f1e8ff766a30f 100644 --- a/app/pages/admin/UpdateSpecificAuthorPage.php +++ b/app/pages/admin/UpdateSpecificAuthorPage.php @@ -13,7 +13,7 @@ <!-- Page-specific CSS --> <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/admin/list.css"> <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/admin/crud-page.css"> - <title>Update User : <? echo $this->data['author_id'];?></title> + <title>Update Author : <? echo $this->data['author_id'];?></title> </head> <body> diff --git a/app/pages/book/BookListPage.php b/app/pages/book/BookListPage.php index 32a63c274d96c4d443c0c67df43e14e1a1e12337..d2a1dad3c1795f39236150a8ef9a7572e15cfe1f 100644 --- a/app/pages/book/BookListPage.php +++ b/app/pages/book/BookListPage.php @@ -96,8 +96,18 @@ <input type="submit" class="button green-reverse-button" value="Buy"></input> </form> <?php elseif (isset($this->data['username']) && $this->data['role']=='admin'):?> - <a type="button" class="button green-reverse-button" >Edit</a> - <a type="button" class="button red-reverse-button" >Delete</a> + + <a type="button" class="button green-reverse-button" + href="/book/update/<?=$book['book_id']?>" + > + Edit + </a> + <a type="button" class="button red-reverse-button" + href="/book/delete/<?=$book['book_id']?>" + > + Delete + </a> + <?php endif;?> <a type="button" class="button yellow-reverse-button" href="/book/details/<?=$book['book_id']?>">Details</a> </div> diff --git a/app/views/admin/DeleteBookView.php b/app/views/admin/DeleteBookView.php new file mode 100644 index 0000000000000000000000000000000000000000..0ce27cb71050262ca707add881386bafbf9905d2 --- /dev/null +++ b/app/views/admin/DeleteBookView.php @@ -0,0 +1,16 @@ +<?php + +class DeleteBookView implements ViewInterface +{ + public function __construct($data = []) + { + $this->data = $data; + } + + public function render() + { + if (isset($this->data['book_id'])) { + require_once __DIR__ . '/../../pages/admin/DeleteBookPage.php'; + } + } +} diff --git a/app/views/admin/UpdateBookView.php b/app/views/admin/UpdateBookView.php new file mode 100644 index 0000000000000000000000000000000000000000..500256f823fa83d2c7ac469931632e428e03e4a1 --- /dev/null +++ b/app/views/admin/UpdateBookView.php @@ -0,0 +1,14 @@ +<?php + +class UpdateBookView implements ViewInterface +{ + public function __construct($data = []) + { + $this->data = $data; + } + + public function render() + { + require_once __DIR__ . '/../../pages/admin/UpdateBookPage.php'; + } +}