Skip to content

Commit

Permalink
create tags from admin dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Mar 23, 2024
1 parent f9c86a7 commit 6a7f1ce
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 1 deletion.
29 changes: 29 additions & 0 deletions controllers/video_tags.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);


// check if new video tag is empty
function is_new_tag_empty(string $new_tag): bool
{
if (empty($new_tag)) {
return true;
}
return false;
}


// check if video tag already exists
function is_video_tag_exists(object $pdo, string $new_tag): bool
{
$query = "SELECT * FROM video_tags WHERE tag_name = :new_tag";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":new_tag", $new_tag, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if ($result) {
return true;
}
return false;
}
40 changes: 40 additions & 0 deletions css/admin_manage_tags.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.tags {
/* border: 1px solid red; */
min-height: calc(100vh - 70px);
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
gap: 1rem;
padding: 1rem;
}

.tags__section {
border: 1px solid rgb(211, 211, 16);
border-radius: 10px;
}

.tags__section__heading {
font-size: 2rem;
}

.tags__section,
.tags__section form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 1rem;
}

.tags__btn {
padding: 0.2rem 1.25rem;
cursor: pointer;
background-color: black;
color: whitesmoke;
}

.tags__btn:hover {
background-color: rgba(0, 0, 0, 0.929);
color: rgba(245, 245, 245, 0.922);
outline: 1px solid rgba(0, 255, 255, 0.865);
}
50 changes: 50 additions & 0 deletions includes/create_tag.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$new_tag = $_POST['new_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 new tag is empty
if (is_new_tag_empty($new_tag)) {
$errors[] = "New tag cannot be empty";
}

// check if tag already exists
else if (is_video_tag_exists($pdo, $new_tag)) {
$errors[] = "Tag already exists";
}


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

// Create new video tag

create_new_video_tag($pdo, $new_tag);



header('Location: ../pages/admin_manage_tags.php?success=tag_created');
// close connection
$pdo = null;
$stmt = null;
die();
} catch (PDOException $e) {
die("Error while creating new video tag: " . $e->getMessage());
}
} else {
header("Location: ../pages/admin_manage_tags.php");
exit();
}
13 changes: 13 additions & 0 deletions models/video_tags.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);


// create new video tag
function create_new_video_tag(object $pdo, string $new_tag): void
{
$query = "INSERT INTO video_tags (tag_name) VALUES (:new_tag)";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":new_tag", $new_tag, PDO::PARAM_STR);
$stmt->execute();
}
2 changes: 1 addition & 1 deletion pages/admin_dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</div>
<h2 class="subheading subheading--bigger">Admin Actions</h2>
<section class="admin__actions">
<a href="#" class="admin__actions__link">Manage Video Tags</a>
<a href="./admin_manage_tags.php" class="admin__actions__link">Manage Video Tags</a>
<a href="#" class="admin__actions__link">Manage Users</a>
</section>
</main>
Expand Down
58 changes: 58 additions & 0 deletions pages/admin_manage_tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
require_once "../includes/config_session.inc.php";
require_once "../views/video_tags.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 - Manage Video Tags</title>
<link rel="stylesheet" href="../css/global.css" />
<link rel="stylesheet" href="../css/navbar.css" />
<link rel="stylesheet" href="../css/admin_manage_tags.css" />
</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();
}
$user_name = $_SESSION['user_username'];
?>

<main class="tags">
<section class="tags__section">
<h3 class="tags__section__heading">Create video tag</h3>
<form action="../includes/create_tag.inc.php" method="post">
<input type="text" name="new_tag" placeholder="Enter new video tag name">
<button type="submit" class="tags__btn">Create</button>
</form>
</section>
<section class="tags__section"></section>
<section class="tags__section"></section>
<section class="tags__section"></section>
</main>

<?php
check_and_print_video_tag_creation_errors();

if (isset($_GET["success"]) && $_GET["success"] === "tag_created") {
echo <<<HTML
<section class="modal modal--success">
<h1 class="modal__title">New video tag created!</h1>
<span class="modal__close modal__close--success">X</span>
</section>
HTML;
}
?>

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

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

declare(strict_types=1);


// check and print video tag creation errors
function check_and_print_video_tag_creation_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 create new 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 6a7f1ce

Please sign in to comment.