Skip to content

Commit

Permalink
update tags from admin dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Mar 24, 2024
1 parent 75736ad commit 0d0716e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
24 changes: 24 additions & 0 deletions controllers/video_tags.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,27 @@ function is_video_tag_exists(object $pdo, string $new_tag): bool
}
return false;
}

// check if video tag and video id are not empty
function is_video_tag_and_id_empty(string $tag_id, string $updated_tag): bool
{
if (empty($tag_id) || empty($updated_tag)) {
return true;
}
return false;
}

// check if the video tag id exists
function is_video_tag_id_exists(object $pdo, string $tag_id): bool
{
$query = "SELECT * FROM video_tags WHERE tag_id = :tag_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":tag_id", $tag_id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if ($result) {
return true;
}
return false;
}
47 changes: 47 additions & 0 deletions includes/update_tag.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

$tag_id = $_POST['tag_id'];
$updated_tag = $_POST['updated_tag'];


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

// Error handlers
$errors = [];

// check if video tag and video id are not empty
if (is_video_tag_and_id_empty($tag_id, $updated_tag)) {
$errors["errors_admin_manage_tags"] = "Tag ID and updated tag name cannot be empty";
$_SESSION['errors_admin_manage_tags'] = $errors;
}

// check if video tag id exists
else if (!is_video_tag_id_exists($pdo, $tag_id)) {
$errors["errors_admin_manage_tags"] = "Tag ID does not exist";
$_SESSION['errors_admin_manage_tags'] = $errors;
}

// update video tag
if (!empty($tag_id) && !empty($updated_tag)) {
update_video_tag($pdo, $tag_id, $updated_tag);
header('Location: ../pages/admin_manage_tags.php?success=tag_updated');
}
header('Location: ../pages/admin_manage_tags.php');

// close connection
$pdo = null;
$stmt = null;
die();
} catch (PDOException $e) {
die("Error while updating video tag: " . $e->getMessage());
}
} else {
header("Location: ../pages/admin_manage_tags.php");
exit();
}
11 changes: 11 additions & 0 deletions models/video_tags.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ function get_video_tags(object $pdo): array
$tags = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $tags;
}


// update video tag
function update_video_tag(object $pdo, string $tag_id, string $updated_tag): void
{
$query = "UPDATE video_tags SET tag_name = :updated_tag, updated_at = CURRENT_TIMESTAMP WHERE tag_id = :tag_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":tag_id", $tag_id, PDO::PARAM_INT);
$stmt->bindParam(":updated_tag", $updated_tag, PDO::PARAM_STR);
$stmt->execute();
}
18 changes: 17 additions & 1 deletion pages/admin_manage_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,21 @@
</table>
<?php endif; ?>
</section>
<section class="tags__section"></section>
<section class="tags__section">
<h3 class="tags__section__heading">Update video tag</h3>
<form action="../includes/update_tag.inc.php" method="post">
<input type="text" name="tag_id" placeholder="Enter tag id to update">
<input type="text" name="updated_tag" placeholder="Enter updated tag name">
<button type="submit" class="tags__btn">Update</button>
</form>
</section>
<section class="tags__section"></section>
</main>

<?php
check_and_print_video_tag_creation_errors();
check_and_print_video_tag_list_errors();
check_and_print_video_tag_update_errors();

if (isset($_GET["success"]) && $_GET["success"] === "tag_created") {
echo <<<HTML
Expand All @@ -82,6 +90,14 @@
</section>
HTML;
}
if (isset($_GET["success"]) && $_GET["success"] === "tag_updated") {
echo <<<HTML
<section class="modal modal--success">
<h1 class="modal__title">Tag was updated!</h1>
<span class="modal__close modal__close--success">X</span>
</section>
HTML;
}
?>

<script src="../js/close_modal.js"></script>
Expand Down
20 changes: 20 additions & 0 deletions views/video_tags.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ function check_and_print_video_tag_list_errors()
}
}
}


// check and print video tag update errors
function check_and_print_video_tag_update_errors()
{

if (isset($_SESSION["errors_admin_manage_tags"])) {
$errors = $_SESSION["errors_admin_manage_tags"];
if (count($errors) > 0) {
echo "<section class='modal modal--error'>";
echo "<h1 class='modal__title'>Unable to update tag: </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_manage_tags"]);
}
}
}

0 comments on commit 0d0716e

Please sign in to comment.