Skip to content

Commit

Permalink
add user subscribe/unsubscribe feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kuldp18 committed Mar 25, 2024
1 parent 675e9b7 commit 3d4f4d7
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 1 deletion.
63 changes: 63 additions & 0 deletions includes/subscriptions.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

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

$current_video_id = $_POST['video_id'];

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

$current_user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
$creator_id = fetch_creator_id($pdo, $current_video_id);

// Error handlers
$errors = [];

// check if user is logged in
if ($current_user_id === null) {
$errors[] = "Please login to subscribe";
}

// check if video exists
else if (!does_video_exist($pdo, $current_video_id)) {
$errors[] = "Video does not exist";
}

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


// user is not subscribed to creator
if (!is_user_subscribed_to_creator($pdo, $current_user_id, $creator_id) && $current_user_id !== null) {
// subcribe user to creator
if ($current_user_id !== $creator_id) {
subscribe_user_to_creator($pdo, $current_user_id, $creator_id);
header('Location: ../pages/video_page.php?video_id=' . $current_video_id . '&success=user_subscribed');
} else {
header('Location: ../pages/video_page.php?video_id=' . $current_video_id . '&error=self_subscribe');
}
}
// user is already subscribed to creator
else if (is_user_subscribed_to_creator($pdo, $current_user_id, $creator_id) && $current_user_id !== null) {
// unsubscribe user from creator
unsubscribe_user_from_creator($pdo, $current_user_id, $creator_id);
header('Location: ../pages/video_page.php?video_id=' . $current_video_id . '&success=user_unsubscribed');
}

// close connection
$pdo = null;
$stmt = null;
die();
} catch (PDOException $e) {
die("Error while subscribing: " . $e->getMessage());
}
} else {
header('Location: ../pages/video_page.php?video_id=' . $current_video_id);
exit();
}
65 changes: 65 additions & 0 deletions models/videos.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,68 @@ function increment_video_views(object $pdo, int $video_id, int $user_id): void
}
}
}

// check if video exists
function does_video_exist(object $pdo, string $video_id): bool
{
$query = "SELECT * FROM videos WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if ($result === false) {
return false;
}

return true;
}

// fetch creator id from video id
function fetch_creator_id(object $pdo, int $video_id): int
{
$query = "SELECT user_id FROM videos WHERE video_id = :video_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":video_id", $video_id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result["user_id"];
}


// subcribe user to creator in user_subscriptions table: it takes user_id and creator_id
function subscribe_user_to_creator(object $pdo, int $user_id, int $creator_id): void
{
$query = "INSERT INTO user_subscriptions (user_id, creator_id) VALUES (:user_id, :creator_id)";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":creator_id", $creator_id, PDO::PARAM_INT);
$stmt->execute();
}

// unsubscribe user from creator in user_subscriptions table: it takes user_id and creator_id
function unsubscribe_user_from_creator(object $pdo, int $user_id, int $creator_id): void
{
$query = "DELETE FROM user_subscriptions WHERE user_id = :user_id AND creator_id = :creator_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":creator_id", $creator_id, PDO::PARAM_INT);
$stmt->execute();
}

// check if user is already subscribed to creator
function is_user_subscribed_to_creator(object $pdo, int $user_id, int $creator_id): bool
{
$query = "SELECT * FROM user_subscriptions WHERE user_id = :user_id AND creator_id = :creator_id";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":user_id", $user_id, PDO::PARAM_INT);
$stmt->bindParam(":creator_id", $creator_id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if ($result === false) {
return false;
}

return true;
}
52 changes: 51 additions & 1 deletion pages/video_page.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
require_once "../includes/db_handler.inc.php";
require_once "../includes/config_session.inc.php";
require_once "../views/video_ratings.php";
require_once "../models/videos.inc.php";
?>

Expand Down Expand Up @@ -30,6 +31,7 @@
$video_views = fetch_video_views($pdo, $current_video_id);

$current_user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : null;
$creator_id = fetch_creator_id($pdo, $current_video_id);

if ($current_user_id !== null) {
increment_video_views($pdo, $current_video_id, $current_user_id);
Expand All @@ -55,7 +57,20 @@
<?php echo "@" . $username; ?>
</a>
</p>
<button class="subscribe__btn">Subscribe</button>
<form action="../includes/subscriptions.inc.php" method="post">
<input type="hidden" name="video_id" value="<?php echo $current_video_id; ?>">
<?php
if ($current_user_id !== null) {

$isSubscribed = is_user_subscribed_to_creator($pdo, $current_user_id, $creator_id);
$buttonText = $isSubscribed ? "Unsubscribe" : "Subscribe";
} else {
$buttonText = "Subscribe";
}
?>

<button class="subscribe__btn" type="submit"><?php echo $buttonText; ?></button>
</form>
</div>
<div class="player__stats__sub__right">
<p class="video__views">
Expand Down Expand Up @@ -86,9 +101,44 @@

</main>

<?php
check_and_print_video_subscription_errors();

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


if (isset($_GET["error"]) && $_GET["error"] === "self_subscribe") {
echo <<<HTML
<section class="modal modal--error">
<h1 class="modal__title">You can't subscribe yourself!</h1>
<span class="modal__close modal__close--error">X</span>
</section>
HTML;
}


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

?>




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

<script src="https://vjs.zencdn.net/8.10.0/video.min.js"></script>
</body>
Expand Down
23 changes: 23 additions & 0 deletions views/video_ratings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);


// check and print video subscription errors
function check_and_print_video_subscription_errors()
{

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

0 comments on commit 3d4f4d7

Please sign in to comment.