Skip to content
Snippets Groups Projects
Commit 8f7c5be0 authored by Kenneth Ezekiel's avatar Kenneth Ezekiel
Browse files

feat: delete film

parent 21a6972a
Branches delete-film
Tags
No related merge requests found
......@@ -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%;
......
......@@ -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");
}
}
}
......@@ -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);
}
}
......@@ -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);
}
}
<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
......@@ -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
......
<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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment