From 8f7c5be0c7782b427218fde9368baa68c462fca6 Mon Sep 17 00:00:00 2001 From: Kenneth Ezekiel <88850771+KenEzekiel@users.noreply.github.com> Date: Fri, 6 Oct 2023 09:30:37 +0700 Subject: [PATCH] feat: delete film --- public/css/styles.css | 34 +++++- src/controllers/UpdateFilmController.php | 132 ++++++++++++++--------- src/repositories/FilmRepository.php | 45 +++++--- src/services/FilmService.php | 42 ++++---- views/delete-film.php | 76 +++++++++++++ views/home.php | 7 +- views/update-film.php | 11 +- 7 files changed, 257 insertions(+), 90 deletions(-) create mode 100644 views/delete-film.php diff --git a/public/css/styles.css b/public/css/styles.css index f429ff8..409f2ac 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -240,8 +240,7 @@ textarea { .popup div { display: flex; flex-direction: row; - justify-content: space-between; - gap: 30px; + justify-content: center; } .button-delete { @@ -257,6 +256,7 @@ textarea { font-weight: bold; margin-top: 20px; margin-bottom: 20px; + margin-right: 5px; align-self: stretch; } @@ -279,6 +279,7 @@ textarea { font-weight: bold; margin-top: 20px; margin-bottom: 20px; + margin-left: 5px; align-self: stretch; } @@ -323,6 +324,35 @@ textarea { padding-bottom: 5px; } +.update-film { + width: 50%; +} + +.button-delete-update { + border:none; + border-radius: 8px; + background: #eb5050; + color: white; + width: 100%; + padding-top: 10px; + padding-bottom: 10px; + transition: all 0.5s; + cursor: pointer; + font-weight: bold; + margin-bottom: 20px; + align-self: stretch; +} + +.button-delete-update:hover {background-color: #ff9898} + +.button-delete-update:active { + background-color: #b63131; +} + +.no-margin-bot { + margin-bottom: 0px; +} + @media only screen and (max-width: 800px) { .form-container { width: 100%; diff --git a/src/controllers/UpdateFilmController.php b/src/controllers/UpdateFilmController.php index 38fde26..26fe2cd 100644 --- a/src/controllers/UpdateFilmController.php +++ b/src/controllers/UpdateFilmController.php @@ -46,9 +46,16 @@ class UpdateFilmController extends BaseController return; } + if (!isset($urlParams['film_id'])) { + parent::redirect("/"); + } $film_id = $urlParams['film_id']; $film = $this->service->getById($film_id); + if (!$film) { + parent::redirect("/"); + } $data = []; + $data['film_id'] = $film_id; $data['title'] = $film->title; $data['released_year'] = $film->released_year; $data['director'] = $film->director; @@ -56,7 +63,12 @@ class UpdateFilmController extends BaseController $data['cast'] = $film->cast; $data['genre'] = $film->genre; - parent::render($data, "update-film", "layouts/base"); + if (isset($urlParams['action'])) { + $data['action'] = $urlParams['action']; + parent::render($data, 'delete-film', 'layouts/base'); + } else { + parent::render($data, "update-film", "layouts/base"); + } } protected function post($urlParams) @@ -69,64 +81,88 @@ class UpdateFilmController extends BaseController $film_id = $urlParams['film_id']; $film = $this->service->getById($film_id); - // Get data - $data = []; - $data['film_id'] = $film->film_id; - $data['title'] = $_POST['title']; - $data['released_year'] = $_POST['released-year']; - $data['director'] = $_POST['director']; - $data['description'] = $_POST['description']; - $data['cast'] = $_POST['cast']; - $data['genre'] = $_POST['genre']; - - // Check if file is valid - if ($_FILES['image-path']['error'] == UPLOAD_ERR_NO_FILE) { - $data['image_path'] = $film->image_path; - } else { - if ($_FILES['image-path']['error'] == UPLOAD_ERR_OK) { - $image_tmp = $_FILES['image-path']['tmp_name']; - $image_name = $_FILES['image-path']['name']; - move_uploaded_file($image_tmp, __DIR__ . "/../../public/files/images/" . $image_name); - $image_path = "/public/files/images/" . $image_name; - - if (!$this->is_image($image_name)) { - throw new BadRequestException("Image file format is not valid"); + // Delete or update + if (isset($_POST['delete_confirm'])) { + // Delete page + if ($_POST['delete_confirm'] == 'yes') { + // Confirm delete + $response = $this->service->deleteById($film->film_id); + if ($response == 1) { + $msg = "film $film->title deleted successfully"; } + // Unset the parameters + unset($urlParams['action']); + unset($urlParams['film_id']); + unset($urlParams['delete_confirm']); + parent::redirect("/", ["Msg" => $msg]); } else { - throw new BadRequestException("Image file is not valid"); + unset($urlParams['action']); + unset($urlParams['delete_confirm']); + unset($urlParams['title']); + parent::redirect("/update-film", $urlParams); } - } - - if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_NO_FILE) { - $data['trailer_path'] = $film->trailer_path; + } else if (isset($_POST['action'])) { + // Delete button clicked + parent::redirect("/update-film", ['action' => $_POST['action'], 'film_id' => $film_id, 'title' => $film->title]); } else { - if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_OK) { - $trailer_tmp = $_FILES['trailer-path']['tmp_name']; - $trailer_name = $_FILES['trailer-path']['name']; - move_uploaded_file($trailer_tmp, __DIR__ . "/../../public/files/trailers/" . $trailer_name); - $trailer_path = "/public/files/trailers/" . $trailer_name; - - if (!$this->is_trailer($trailer_name)) { - throw new BadRequestException("Trailer file format is not valid"); + // Update + // Get data + $data = []; + $data['film_id'] = $film->film_id; + $data['title'] = $_POST['title']; + $data['released_year'] = $_POST['released-year']; + $data['director'] = $_POST['director']; + $data['description'] = $_POST['description']; + $data['cast'] = $_POST['cast']; + $data['genre'] = $_POST['genre']; + // Check if file is valid + if ($_FILES['image-path']['error'] == UPLOAD_ERR_NO_FILE) { + $data['image_path'] = $film->image_path; + } else { + if ($_FILES['image-path']['error'] == UPLOAD_ERR_OK) { + $image_tmp = $_FILES['image-path']['tmp_name']; + $image_name = $_FILES['image-path']['name']; + move_uploaded_file($image_tmp, __DIR__ . "/../../public/files/images/" . $image_name); + $image_path = "/public/files/images/" . $image_name; + + if (!$this->is_image($image_name)) { + throw new BadRequestException("Image file format is not valid"); + } + } else { + throw new BadRequestException("Image file is not valid"); } + } + + if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_NO_FILE) { + $data['trailer_path'] = $film->trailer_path; } else { - throw new BadRequestException("Trailer Format is not valid"); + if ($_FILES['trailer-path']['error'] == UPLOAD_ERR_OK) { + $trailer_tmp = $_FILES['trailer-path']['tmp_name']; + $trailer_name = $_FILES['trailer-path']['name']; + move_uploaded_file($trailer_tmp, __DIR__ . "/../../public/files/trailers/" . $trailer_name); + $trailer_path = "/public/files/trailers/" . $trailer_name; + + if (!$this->is_trailer($trailer_name)) { + throw new BadRequestException("Trailer file format is not valid"); + } + } else { + throw new BadRequestException("Trailer Format is not valid"); + } } - } - // Call service - $filmModel = new FilmModel(); - $filmModel->constructFromArray($data); - $response = $this->service->update($filmModel); - if ($response) { - $msg = "Successfully updated film!"; + // Call service + $filmModel = new FilmModel(); + $filmModel->constructFromArray($data); + $response = $this->service->update($filmModel); + if ($response) { + $msg = "Successfully updated film!"; + } + // Render response + parent::redirect("/", ["Msg" => $msg]); } - - // Render response - parent::redirect("/", ["Msg" => $msg]); } catch (Exception $e) { $msg = $e->getMessage(); - parent::render(["errorMsg" => $msg], "create-film", "layouts/base"); + parent::render(["errorMsg" => $msg], "update-film", "layouts/base"); } } } diff --git a/src/repositories/FilmRepository.php b/src/repositories/FilmRepository.php index bec39a1..e423cf7 100644 --- a/src/repositories/FilmRepository.php +++ b/src/repositories/FilmRepository.php @@ -3,6 +3,7 @@ namespace app\repositories; use app\base\BaseRepository; +use app\models\FilmModel; use PDO; class FilmRepository extends BaseRepository @@ -28,10 +29,16 @@ class FilmRepository extends BaseRepository return $this->findOne(['film_id' => [$film_id, PDO::PARAM_INT]]); } - public function getAllBySearchAndFilter($word, $order = 'title', $isDesc= false, $genre = 'all', - $released_year = 'all', $pageNo = 1, $limit = 10) - { - $where = []; + public function getAllBySearchAndFilter( + $word, + $order = 'title', + $isDesc = false, + $genre = 'all', + $released_year = 'all', + $pageNo = 1, + $limit = 10 + ) { + $where = []; if (isset($genre) and !empty($genre) and $genre != 'all') { $where['genre'] = [$genre, PDO::PARAM_STR, 'LIKE']; @@ -48,23 +55,31 @@ class FilmRepository extends BaseRepository public function countRowBySearchAndFilter($word, $genre = 'all', $released_year = 'all') { - $where = []; + $where = []; - if (isset($genre) and !empty($genre) and $genre != 'all') { - $where['genre'] = [$genre, PDO::PARAM_STR, 'LIKE']; - } - if (isset($released_year) and !empty($released_year) and $released_year != 'all') { - $where['released_year'] = [$released_year, PDO::PARAM_INT]; - } - if (isset($word) and !empty($word)) { - $where['title'] = [$genre, PDO::PARAM_STR, 'LIKE', ['director']]; - } + if (isset($genre) and !empty($genre) and $genre != 'all') { + $where['genre'] = [$genre, PDO::PARAM_STR, 'LIKE']; + } + if (isset($released_year) and !empty($released_year) and $released_year != 'all') { + $where['released_year'] = [$released_year, PDO::PARAM_INT]; + } + if (isset($word) and !empty($word)) { + $where['title'] = [$genre, PDO::PARAM_STR, 'LIKE', ['director']]; + } - return $this->countRow($where); + return $this->countRow($where); } public function getAllCategoryValues($category) { return $this->getDistinctValues($category); } + + public function deleteById($film_id) + { + $film = $this->getById($film_id); + $filmModel = new FilmModel(); + $filmModel->constructFromArray($film); + return $this->delete($filmModel); + } } diff --git a/src/services/FilmService.php b/src/services/FilmService.php index 9bc6307..8750dcd 100644 --- a/src/services/FilmService.php +++ b/src/services/FilmService.php @@ -88,26 +88,30 @@ class FilmService extends BaseService public function searchAndFilter($word, $order, $isDesc, $genre, $released_year, $page = 1) { - $data = null; - $word = strtolower(trim($word)); - $response = $this->repository->getAllBySearchAndFilter($word, $order, $isDesc, $genre, $released_year , $page); - $films = []; - foreach ($response as $resp) { - $film = new FilmModel(); - $films[] = $film->constructFromArray($resp); - } - $data['films'] = $films; - - $row_count = $this->repository->countRowBySearchAndFilter($word, $genre, $released_year); - $total_page = ceil($row_count/10); - $data['total_page'] = $total_page; - - return $data; + $data = null; + $word = strtolower(trim($word)); + $response = $this->repository->getAllBySearchAndFilter($word, $order, $isDesc, $genre, $released_year, $page); + $films = []; + foreach ($response as $resp) { + $film = new FilmModel(); + $films[] = $film->constructFromArray($resp); } + $data['films'] = $films; - public function getAllCategoryValues($category) - { - return $this->repository->getAllCategoryValues($category); - } + $row_count = $this->repository->countRowBySearchAndFilter($word, $genre, $released_year); + $total_page = ceil($row_count / 10); + $data['total_page'] = $total_page; + + return $data; + } + + public function getAllCategoryValues($category) + { + return $this->repository->getAllCategoryValues($category); + } + public function deleteById($film_id) + { + return $this->repository->deleteById($film_id); + } } diff --git a/views/delete-film.php b/views/delete-film.php new file mode 100644 index 0000000..e69a4e7 --- /dev/null +++ b/views/delete-film.php @@ -0,0 +1,76 @@ +<div class="form-container update-film"> + <h2 class="header-title">Update Film</h2> + <p class="error-msg"><?php if (isset($errorMsg)) { + echo "$errorMsg"; + } ?></p> + <form class="form" method="post" enctype="multipart/form-data"> + <div class="form-group"> + <label for="title">Title</label> + <br> + <input class="input" type="text" id="title" name="title" value="<?= $title ?>" required> + </div> + <div class="form-group"> + <label for="released-year">Released Year</label> + <br> + <input class="input" type="text" id="released-year" name="released-year" value="<?= $released_year ?>" required> + </div> + <div class="form-group"> + <label for="director">Director</label> + <br> + <input class="input" type="text" id="director" name="director" value="<?= $director ?>" required> + </div> + <div class="form-group"> + <label for="description">Description</label> + <br> + <textarea class="input" type="text" id="description" name="description" required><?= $description ?></textarea> + </div> + <div class="form-group"> + <label for="cast">Cast</label> + <br> + <input class="input" type="text" id="cast" name="cast" value="<?= $cast ?>" required> + </div> + <div class="form-group"> + <label for="genre">Genre</label> + <br> + <input class="input" type="text" id="genre" name="genre" value="<?= $genre ?>" required> + </div> + <div class="form-group"> + <label for="image-path">Image</label> + <br> + <input class="input" type="file" id="image-path" name="image-path"> + </div> + <div class="form-group"> + <label for="trailer-path">Trailer</label> + <br> + <input class="input" type="file" id="trailer-path" name="trailer-path"> + </div> + <div class="form-group"> + <button class="button" ctype="submit">Update</button> + </div> + </form> + <form class="form" method="post" enctype="multipart/form-data"> + <input type='hidden' name='action' value='delete'> + <input type='hidden' name='film_id' value='$film_id'> + <button type='submit' class='button-delete'>Delete</button> + + </form> +</div> +<div class="overlay"> + <div class="popup"> + <h1 class="header-title">Are you sure?</h1> + <p class="error-msg"><?php if (isset($errorMsg)) { + echo "$errorMsg"; + } ?></p> + <p>Film <?= $title ?> will be deleted forever!</p> + <div> + <form method='post' enctype="multipart/form-data"> + <input type='hidden' name='delete_confirm' value='yes'> + <button type="submit" class="button-delete ">Delete</button> + </form> + <form method='post' enctype="multipart/form-data"> + <input type='hidden' name='delete_confirm' value='no'> + <button type="submit" class="button-cancel">Cancel</button> + </form> + </div> + </div> +</div> \ No newline at end of file diff --git a/views/home.php b/views/home.php index d48d814..c11658b 100644 --- a/views/home.php +++ b/views/home.php @@ -5,10 +5,6 @@ <p>write your opinion about films</p> <p><?php - if (isset($Msg)) { - echo "<p>$Msg</p>"; - } - if (isset($_SESSION['user_id'])) { if ($_SESSION['role'] == "admin") { echo "<br> Hi, Admin!"; @@ -16,6 +12,9 @@ echo "<br> Hi, User!"; } } + if (isset($Msg)) { + echo "<br><br><p>$Msg</p>"; + } ?></p> <div> <?php diff --git a/views/update-film.php b/views/update-film.php index 5d08719..f3060a9 100644 --- a/views/update-film.php +++ b/views/update-film.php @@ -1,4 +1,4 @@ -<div class="form-container"> +<div class="form-container update-film"> <h2 class="header-title">Update Film</h2> <p class="error-msg"><?php if (isset($errorMsg)) { echo "$errorMsg"; @@ -45,7 +45,14 @@ <input class="input" type="file" id="trailer-path" name="trailer-path"> </div> <div class="form-group"> - <button class="button" ctype="submit">Add</button> + <button class="button no-margin-bot" ctype="submit">Update</button> + </div> + </form> + <form class="form" method="post" enctype="multipart/form-data"> + <div class='form-group'> + <input type='hidden' name='action' value='delete'> + <input type='hidden' name='film_id' value='$film_id'> + <button type='submit' class='button-delete-update'>Delete</button> </div> </form> </div> \ No newline at end of file -- GitLab