-
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.
add user subscribe/unsubscribe feature
- Loading branch information
Showing
4 changed files
with
202 additions
and
1 deletion.
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,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(); | ||
} |
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,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"]); | ||
} | ||
} | ||
} |