Skip to content

Commit

Permalink
soft-delete video as admin
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Apr 7, 2024
1 parent e6ce2bf commit c92fb4c
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 7 deletions.
48 changes: 48 additions & 0 deletions includes/admin_delete_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$selected_video_id = $_POST['video_id'];

try {
require_once "./db_handler.inc.php";
require_once "./config_session.inc.php";
// require_once "../models/edit_profile.inc.php";
// require_once "../models/register.inc.php";
require_once "../models/videos.inc.php";
// require_once "../controllers/edit_profile.inc.php";
// require_once "../controllers/register.inc.php";


// Error handlers

$errors = [];



// Check for empty inputs
if (!empty($selected_video_id) && !does_video_exist($pdo, $selected_video_id)) {
$errors['video_id'] = 'Video does not exist';
}




if ($errors) {
$_SESSION["errors_admin_delete_video"] = $errors;
header('Location: ../pages/admin_manage_videos.php');
die();
}

// delete video as admin (soft delete)
delete_video_as_admin($pdo, $selected_video_id);
header('Location: ../pages/admin_manage_videos.php?video_delete=success');
$pdo = null;
$stmt = null;
die();
} catch (PDOException $e) {
die("Failed to delete video as admin: " . $e->getMessage());
}
} else {
header('Location: ../index.php');
die();
}
11 changes: 11 additions & 0 deletions models/videos.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,14 @@ function fetch_previous_rating_value(object $pdo, int $video_id, int $user_id):
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result["rating_value"];
}


// delete video as admin
function delete_video_as_admin(object $pdo, string $video_id): void
{
// just set is_active to N and update deleted_at timestamp
$query = "UPDATE videos SET is_active = 'N', deleted_at = CURRENT_TIMESTAMP WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->execute();
}
82 changes: 82 additions & 0 deletions pages/admin_delete_video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
require_once "../includes/db_handler.inc.php";
require_once "../includes/config_session.inc.php";
require_once "../models/videos.inc.php";
?>

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - Video Management</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" integrity="sha512-jnSuA4Ss2PkkikSOLtYs8BlYIeeIK1h99ty4YfvRPAlzr377vr3CXDb7sb7eEEBYjDtcYj+AjBH3FLv5uSJuXg==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<link rel="stylesheet" href="../css/global.css">
<link rel="stylesheet" href="../css/navbar.css" />

<style>
.table {
border: 1px solid whitesmoke;
font-size: 1.2rem;
}

form {
font-size: 1.25rem;
}

label,
input.form-control,
select.form-select,
option {
font-size: inherit;
}
</style>
</head>

<body>

<?php include_once('../includes/components/navbar.inc.php') ?>
<?php
// check is user is not logged in or is not admin
if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
// if not, redirect to home page
header('Location: ../index.php');
exit();
}
$selected_video_id = $_GET['video_id'];



?>

<div class="container mt-3">
<h1 class="mb-4 heading">Admin - Manage Videos</h1>

<div class="mb-4">
<h2>Delete video_id: <?php echo $selected_video_id; ?></h2>
<h1><?php echo 'Are you sure you want to delete this video?' ?>
</h1>
<!-- create a form with yes and no -->
<form action="../includes/admin_delete_video.inc.php" method="POST">
<input type="hidden" name="video_id" value="<?php echo $selected_video_id; ?>">
<button type="submit" name="delete_user" class="btn btn-lg btn-danger">Yes</button>
<a href="./admin_manage_users.php" class="btn btn-lg btn-primary">No</a>
</form>

</div>



</div>

<!-- Bootstrap JS (optional, only if you need Bootstrap JavaScript features) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/js/bootstrap.min.js" integrity="sha512-ykZ1QQr0Jy/4ZkvKuqWn4iF3lqPZyij9iRv6sGqLRdTPkY69YX6+7wvVGmsdBbiIfN/8OdsI7HABjvEok6ZopQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

</body>

</html>
13 changes: 6 additions & 7 deletions pages/admin_manage_videos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require_once "../includes/db_handler.inc.php";
require_once "../includes/config_session.inc.php";
require_once "../models/videos.inc.php";
// require_once "../views/admin_manage_videos.php";
require_once "../views/admin_manage_videos.php";
?>

<!DOCTYPE html>
Expand Down Expand Up @@ -40,22 +40,21 @@

$video_list = fetch_all_videos($pdo);

// check_and_print_admin_edit_user_errors();
// check_and_print_admin_delete_user_errors();
check_and_print_admin_delete_video_errors();

if (isset($_GET["user_update"]) && $_GET["user_update"] === "success") {
if (isset($_GET["video_update"]) && $_GET["video_update"] === "success") {
echo <<<HTML
<section class="modal modal--success">
<h1 class="modal__title">User updated successfully!</h1>
<h1 class="modal__title">Video updated successfully!</h1>
<span class="modal__close modal__close--success">X</span>
</section>
HTML;
}

if (isset($_GET["user_delete"]) && $_GET["user_delete"] === "success") {
if (isset($_GET["video_delete"]) && $_GET["video_delete"] === "success") {
echo <<<HTML
<section class="modal modal--success">
<h1 class="modal__title">User soft-deleted successfully!</h1>
<h1 class="modal__title">Video soft-deleted successfully!</h1>
<span class="modal__close modal__close--success">X</span>
</section>
HTML;
Expand Down
22 changes: 22 additions & 0 deletions views/admin_manage_videos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);


// check and print admin delete video errors
function check_and_print_admin_delete_video_errors()
{
if (isset($_SESSION["errors_admin_delete_video"])) {
$errors = $_SESSION["errors_admin_delete_video"];
if (count($errors) > 0) {
echo "<section class='modal modal--error'>";
echo "<h1 class='modal__title'>Errors while deleting video: </h1>";
echo "<span class='modal__close modal__close--error'>X</span>";
foreach ($errors as $error) {
echo "<p class='modal__item'>$error</p>";
}
echo "</section>";
unset($_SESSION["errors_admin_delete_video"]);
}
}
}

0 comments on commit c92fb4c

Please sign in to comment.