Skip to content

Commit

Permalink
edit video, kind of works but the thumbnail is a pain still
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Apr 16, 2024
1 parent 1650d8e commit cbf6cf5
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 3 deletions.
33 changes: 33 additions & 0 deletions controllers/edit_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

// is edit video form empty
function is_edit_video_form_empty(string $title, string $description, array $tags, array $thumbnail): bool
{
return empty($title) && empty($description) && empty($tags) && empty($thumbnail);
}


// check if thumbnail is not empty and is a valid image
function is_thumbnail_file_invalid(array $thumbnail): bool
{
$thumbnail_name = $thumbnail['name'];
$thumbnail_size = $thumbnail['size'];
$thumbnail_error = $thumbnail['error'];

$thumbnail_ext = explode('.', $thumbnail_name);
$thumbnail_actual_ext = strtolower(end($thumbnail_ext));

$allowed = ['jpg', 'jpeg', 'png'];

if (in_array($thumbnail_actual_ext, $allowed)) {
if ($thumbnail_error === 0 && $thumbnail_size > 0) {
return false;
} else {
return true;
}
} else {
return true;
}
}
77 changes: 77 additions & 0 deletions includes/edit_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$new_title = trim($_POST['title']);
$new_description = trim($_POST['description']);
$new_tags_arr = $_POST['tags'];
$new_thumbnail_arr = $_FILES['thumbnail'];
$video_id = $_POST['video_id'];

try {
require_once "./db_handler.inc.php";
require_once "./config_session.inc.php";
require_once "../models/videos.inc.php";
require_once "../models/video_tags.inc.php";
require_once "../controllers/edit_video.inc.php";

$current_video_details = !empty($video_id) ? fetch_video_by_id($pdo, $video_id) : [];
$current_video_tags = get_video_tags_by_video_id($pdo, $video_id);


// Error handlers

$errors = [];



// Check for empty inputs
if (is_edit_video_form_empty($new_title, $new_description, $new_tags_arr, $new_thumbnail_arr)) {
// make sure to use local variables here
$errors["empty_input"] = "Please fill at least one field to update your profile";
}


// the fields that are still empty should be filled with the old values
if (empty($new_title)) {
$new_title = $current_video_details['video_title'];
}
if (empty($new_description)) {
$new_description = $current_video_details['video_desc'];
}
if (empty($new_tags_arr)) {
$new_tags_arr = $current_video_tags;
}

if ($errors) {
$_SESSION["errors_edit_video"] = $errors;
header('Location: ../pages/edit_video.php?video_id=' . $video_id);
die();
}

// edit video
edit_video_as_user($pdo, $video_id, $new_title, $new_description);
// update video tags
if (!empty($new_tags_arr)) {
update_video_tags($pdo, $video_id, $new_tags_arr);
}
// update video thumbnail
if (!empty($new_thumbnail_arr)) {
update_video_thumbnail($pdo, $video_id, $new_thumbnail_arr);
}

// update timestamp
update_video_timestamp($pdo, $video_id);


header('Location: ../pages/user_dashboard.php?video_updated=success');

$pdo = null;
$stmt = null;
die();
} catch (PDOException $e) {
die("Query failed: " . $e->getMessage());
}
} else {
header('Location: ../index.php');
die();
}
65 changes: 65 additions & 0 deletions models/videos.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,68 @@ function update_video_as_admin(object $pdo, string $video_id, string $updated_ti
$stmt->bindParam(":is_active", $updated_status, PDO::PARAM_STR);
$stmt->execute();
}

// edit video as user
function edit_video_as_user(object $pdo, string $video_id, string $updated_title, string $updated_desc): void
{
// update video details
$query = "UPDATE videos SET video_title = :video_title, video_desc = :video_desc, updated_at = CURRENT_TIMESTAMP WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->bindParam(":video_title", $updated_title, PDO::PARAM_STR);
$stmt->bindParam(":video_desc", $updated_desc, PDO::PARAM_STR);
$stmt->execute();
}


// update video tags
function update_video_tags(object $pdo, string $video_id, array $updated_tags): void
{
// delete all tags associated with the video
$query = "DELETE FROM video_tag_associations WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->execute();

// insert new tags
foreach ($updated_tags as $tag) {
$query = "INSERT INTO video_tag_associations (video_id, tag_id) VALUES (:video_id, (SELECT tag_id FROM video_tags WHERE tag_name = :tag_name))";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->bindParam(":tag_name", $tag, PDO::PARAM_STR);
$stmt->execute();
}
}

// update video thumbnail
function update_video_thumbnail(object $pdo, string $video_id, array $updated_thumbnail): void
{
if (!empty($updated_thumbnail)) {

// fetch necessary detail from the thumbnail array
$thumbnail_name = $updated_thumbnail['name'];

// move the thumbnail to the uploads folder
// encode thumbnail_name into a random string
$thumbnail_name = bin2hex(random_bytes(10)) . "." . pathinfo($thumbnail_name, PATHINFO_EXTENSION);
$thumbnail_destination = "../uploads/thumbnails/" . $thumbnail_name;

move_uploaded_file($updated_thumbnail['tmp_name'], $thumbnail_destination);

// update the thumbnail in the database
$query = "UPDATE videos SET video_thumbnail = :thumbnail WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->bindParam(":thumbnail", $thumbnail_name, PDO::PARAM_STR);
$stmt->execute();
}
}

// update updated_at timestamp
function update_video_timestamp(object $pdo, string $video_id): void
{
$query = "UPDATE videos SET updated_at = CURRENT_TIMESTAMP WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->execute();
}
17 changes: 14 additions & 3 deletions pages/edit_video.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once "../includes/config_session.inc.php";
require_once "../models/videos.inc.php";
require_once "../models/video_tags.inc.php";
require_once "../views/edit_video.inc.php";
?>
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -34,13 +35,20 @@
$video_details = fetch_video_by_id($pdo, $video_id);
$video_tag_list = get_video_tags($pdo);
$current_video_tags = get_video_tags_by_video_id($pdo, $video_id);
// print $current_video_tags;
print_r($current_video_tags);
// just make a list of tags from the current video tags
$current_video_tags = array_map(function ($tag) {
return $tag['tag_name'];
}, $current_video_tags);

check_and_print_edit_video_errors();

?>

<main class="edit">
<h1 class="heading heading--small">Edit Video</h1>
<form class="edit__form" method="post">
<form class="edit__form" method="post" action="../includes/edit_video.inc.php" enctype="multipart/form-data">
<input type="text" name="title" class="edit__form__input" placeholder="Edit video title" value="<?php
if (isset($video_details['video_title'])) {
echo $video_details['video_title'];
Expand All @@ -58,15 +66,16 @@
echo '<div class="edit__form__checkbox">';
echo '<label for="' . $tag['tag_name'] . '">' . $tag['tag_name'] . '</label>';
$tagChecked = in_array($tag['tag_name'], $current_video_tags) ? 'checked' : '';
echo '<input type="checkbox" name="' . $tag['tag_name'] . '" value="' . $tag['tag_name'] . '" ' . $tagChecked . '>';
echo '<input type="checkbox" name="tags[]" value="' . $tag['tag_name'] . '" ' . $tagChecked . '>';
echo '</div>';
}
?>
</div>
<div class="edit__form__thumbnail">
<label for="thumbnail">Change or remove thumbnail:</label>
<label for="thumbnail">Change or add thumbnail:</label>
<input type="file" name="thumbnail" class="edit__form__input">
</div>
<input type="hidden" name="video_id" value="<?php echo $video_id; ?>">
<input type="submit" value="Edit video" class="edit__btn">
</form>
</main>
Expand All @@ -75,6 +84,8 @@
$('textarea').autoResize();
</script>

<script src="../js/close_modal.js"></script>

</body>

</html>
21 changes: 21 additions & 0 deletions views/edit_video.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

// check and print edit video errors
function check_and_print_edit_video_errors()
{
if (isset($_SESSION["errors_edit_video"])) {
$errors = $_SESSION["errors_edit_video"];
if (count($errors) > 0) {
echo "<section class='modal modal--error'>";
echo "<h1 class='modal__title'>Errors while updating 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_edit_video"]);
}
}
}

0 comments on commit cbf6cf5

Please sign in to comment.