-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
edit video, kind of works but the thumbnail is a pain still
- Loading branch information
Showing
5 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} | ||
} |