diff --git a/src/app/components/dashboard/components/input_form.php b/src/app/components/dashboard/components/input_form.php index bcf88ad0a20f2488a0d176b8c90ef78c30ca71d2..4021561536706df12f8f9248846bd370514de0bd 100644 --- a/src/app/components/dashboard/components/input_form.php +++ b/src/app/components/dashboard/components/input_form.php @@ -47,10 +47,18 @@ <p class="sh3"><?= $this->data["INPUT_FORM_COVER_TEXT"] ?? "" ?></p> <div> - <img id="cover-image" width="200" height="200" src="<?= STORAGE_URL . ($this->data["url_thumbnail"] ?? "/images/placeholder.jpeg") ?> " alt="cover image"> + <img id="cover-image" width="200" height="200" src="<?= isset($this->data["url_thumbnail"]) ? ( + (isset($_GET["premium"]) && $_GET["premium"] == "true") ? "http://localhost:3000/images/" . $this->data["url_thumbnail"] : STORAGE_URL . $this->data["url_thumbnail"] + ) : (STORAGE_URL . "/images/placeholder.jpeg") ?>" alt="cover image"> <button id="change-cover-btn">Change Cover</button> <input name="image-input" type="file" id="image-input" accept="image/*" class="hidden"> </div> + + <?php if ($this->data["INPUT_FORM_SHOW_PREMIUM_BUTTON"] ?? false) : ?> + <button id="is-premium-btn">Not Premium</button> + <?php endif; ?> + + <input id="is-premium-input" type="text" class="hidden" disabled value="<?= $this->data["INPUT_FORM_IS_PREMIUM"] ?? "false" ?>" /> </div> <div id="overlay-form" class="overlay hidden"></div> diff --git a/src/app/components/dashboard/components/tambah_episode_button.php b/src/app/components/dashboard/components/tambah_episode_button.php index 048ebc86329987b56449a8f2529b640ccf32d0f9..2082f85c0a8747882ebe72b0ff3f9b8ba49201f2 100644 --- a/src/app/components/dashboard/components/tambah_episode_button.php +++ b/src/app/components/dashboard/components/tambah_episode_button.php @@ -1,4 +1,4 @@ -<a href="/public/dashboard/add-episode?id_podcast=<?= $this->data["id_podcast"] ?? "" ?>"> +<a href="/public/dashboard/add-episode?id_podcast=<?= $this->data["id_podcast"] ?? "" ?>&premium=<?= $_GET["premium"] ?>"> <button class="tambah-episode"> <img src="<?= BASE_URL ?>/images/dashboard/upload_icon.svg" alt="" /> diff --git a/src/app/components/dashboard/layout.php b/src/app/components/dashboard/layout.php index b642c54dce626674ae69fbabc9da9cd411dae995..38a3fe7a9ca1813598ecd9821e31fe2dac6578c7 100644 --- a/src/app/components/dashboard/layout.php +++ b/src/app/components/dashboard/layout.php @@ -1,59 +1,44 @@ -<!-- <!DOCTYPE html> -<html lang="en"> +<?php include(dirname(__DIR__) . "/common/toast.php") ?> -<head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> +<div id="overlay-layout" class="overlay hidden"></div> - <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/globals.css"> - <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/dashboard/layout.css"> - <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/dashboard/pages/main.css"> - <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/dashboard/pages/episode.css"> - <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/dashboard/components/button.css"> - - <script type="module" src="<?= BASE_URL ?>/javascript/dashboard/layout.js" defer></script> - <script type="module" src="<?= BASE_URL ?>/javascript/toast.mjs" defer></script> - <title>Main Dashboard</title> -</head> - -<body> --> - <?php include(dirname(__DIR__) . "/common/toast.php") ?> - - <div id="overlay-layout" class="overlay hidden"></div> - - <main> - <div class="dashboard-nav"> - <div> - <p id="dashboard-link" class="sh4">Dashboard</p> - <p id="episode-link" class="sh4">Episode</p> - </div> +<main> + <div class="dashboard-nav"> + <div> + <p id="dashboard-link" class="sh4">Dashboard</p> + <p id="episode-link" class="sh4">Episode</p> + </div> - <div class="choose-container"> - <button id="choose-podcast-btn" class=""> - <p class="b3">Choose Podcast</p> - </button> + <div class="choose-container"> + <button id="choose-podcast-btn" class=""> + <p class="b3">Choose Podcast</p> + </button> - <ul id="podcast-choices" class="hidden"> - <?php foreach ($this->data["podcasts"] as $podcast) : ?> - <li> - <a href="/public/dashboard-main?id_podcast=<?= $podcast->id_podcast ?>"> - <p class="b3"><?= $podcast->title ?></p> - </a> - </li> - <?php endforeach; ?> + <ul id="podcast-choices" class="hidden"> + <?php foreach ($this->data["premium_podcasts"] as $podcast) : ?> <li> - <a href="/public/dashboard/add-podcast"> - <p class="b3">Add Podcast</p> + <a href="/public/dashboard-main?id_podcast=<?= $podcast["id_podcast"] ?>&premium=true"> + <p class="b3"><?= $podcast["title"] ?></p> </a> </li> - </ul> - </div> + <?php endforeach; ?> + <?php foreach ($this->data["podcasts"] as $podcast) : ?> + <li> + <a href="/public/dashboard-main?id_podcast=<?= $podcast->id_podcast ?>&premium=false"> + <p class="b3"><?= $podcast->title ?></p> + </a> + </li> + <?php endforeach; ?> + <li> + <a href="/public/dashboard/add-podcast"> + <p class="b3">Add Podcast</p> + </a> + </li> + </ul> </div> + </div> - <div class="dashboard-nav-line"></div> - - <section id="dashboard-section"></section> - </main> -<!-- </body> + <div class="dashboard-nav-line"></div> -</html> --> \ No newline at end of file + <section id="dashboard-section"></section> +</main> \ No newline at end of file diff --git a/src/app/components/dashboard/pages/episode.php b/src/app/components/dashboard/pages/episode.php index b054d7e897f363d997516781f35ab62cfd5e728a..97d1b71b757ab6291351a1acc9c62bd11c6fea6b 100644 --- a/src/app/components/dashboard/pages/episode.php +++ b/src/app/components/dashboard/pages/episode.php @@ -4,16 +4,41 @@ </div> <ul> - <?php foreach ($this->data["episodes"] as $idx => $episode) : ?> + <?php foreach ($this->data["premium_episodes"] as $idx => $episode) : ?> <li> <div> <p class="episode-number"><?= $idx + 1 + (($this->data["page"] - 1) * 4) ?></p> + <img width="75" height="75" src="http://localhost:3000/images/<?= $episode["url_thumbnail"] ?>" alt=""> + <p class="b2"><?= $episode["title"] ?></p> + </div> + + <div> + <a href="<?= BASE_URL ?>/dashboard/edit-episode?id_podcast=<?= $this->data["id_podcast"] ?? "" ?>&id_episode=<?= $episode["id_episode"] ?>&premium=true"> + <div> + <img width="16" height="16" src="<?= BASE_URL ?>/images/dashboard/edit_icon.svg" alt=""> + <p>Edit</p> + </div> + </a> + + <button data-id="<?= $episode["id_episode"] ?>" class="delete-episode-btn"> + <div> + <img width="16" height="18" src="<?= BASE_URL ?>/images/dashboard/trash_icon.svg" alt=""> + <p>Delete</p> + </div> + </button> + </div> + </li> + <?php endforeach; ?> + <?php foreach ($this->data["episodes"] as $idx => $episode) : ?> + <li> + <div> + <p class="episode-number"><?= $idx + 1 + count($this->data["premium_episodes"]) + (($this->data["page"] - 1) * 4) ?></p> <img width="75" height="75" src="<?= STORAGE_URL . $episode->url_thumbnail ?>" alt=""> <p class="b2"><?= $episode->title ?></p> </div> <div> - <a href="<?= BASE_URL ?>/dashboard/edit-episode?id_podcast=<?= $this->data["id_podcast"] ?? "" ?>&id_episode=<?= $episode->id_episode ?>"> + <a href="<?= BASE_URL ?>/dashboard/edit-episode?id_podcast=<?= $this->data["id_podcast"] ?? "" ?>&id_episode=<?= $episode->id_episode ?>&premium=false"> <div> <img width="16" height="16" src="<?= BASE_URL ?>/images/dashboard/edit_icon.svg" alt=""> <p>Edit</p> diff --git a/src/app/components/dashboard/pages/main.php b/src/app/components/dashboard/pages/main.php index b99486ae19f2e98f450870cbb7a622fb301430bf..2f3db0b203f0fc05c0b916c5270c59e2bebecd7a 100644 --- a/src/app/components/dashboard/pages/main.php +++ b/src/app/components/dashboard/pages/main.php @@ -3,22 +3,23 @@ <!-- Podcast Cards --> <div class="podcast-card"> <div class="card-header-container"> - <img width="200" height="200" class="podcast-thumbnail-img" src="<?= STORAGE_URL . ($this->data["podcast"]->url_thumbnail ?? "") ?>" alt=""> + <!-- <?= print_r($this->data["podcast"]) ?> --> + <img width="200" height="200" class="podcast-thumbnail-img" src="<?= (isset($_GET["premium"]) && $_GET["premium"] == "true" ? "http://localhost:3000/images/" : STORAGE_URL) . ($this->data["podcast"]->url_thumbnail ?? $this->data["podcast"]["url_thumbnail"]) ?>" alt=""> <div class="podcast-description"> <div class="podcast-category"> - <p><?= $this->data["podcast"]->category ?? "" ?></p> + <p><?= $this->data["podcast"]->category ?? $this->data["podcast"]["category"] ?></p> </div> - <h3><?= $this->data["podcast"]->title ?? "" ?></h3> - <p class="b5"><?= $this->data["podcast"]->description ?? "" ?></p> + <h3><?= $this->data["podcast"]->title ?? $this->data["podcast"]["title"] ?></h3> + <p class="b5"><?= $this->data["podcast"]->description ?? $this->data["podcast"]["description"] ?></p> </div> </div> <div class="button-container"> <?php include(dirname(__DIR__) . "/components/tambah_episode_button.php") ?> - <a href="/public/dashboard/edit-podcast?id_podcast=<?= $this->data["id_podcast"] ?? "" ?>"> + <a href="/public/dashboard/edit-podcast?id_podcast=<?= $this->data["id_podcast"] ?? $this->data["podcast"]["id_podcast"] ?>&premium=<?= $_GET["premium"] ?? "false" ?>"> <button class="edit-button"> <img src="<?= BASE_URL ?>/images/dashboard/edit_icon.svg" alt="" /> diff --git a/src/app/controllers/dashboard/delete_episode.php b/src/app/controllers/dashboard/delete_episode.php index d4710e1d4d6a89d3ef9720372eeaef79ba6825fb..6c0541fecc6db924245b6f8ebb3ea010d2c5997d 100644 --- a/src/app/controllers/dashboard/delete_episode.php +++ b/src/app/controllers/dashboard/delete_episode.php @@ -22,6 +22,38 @@ class DeleteEpisodeController return; } + // Check is premium + if ($_GET["premium"] == "true") { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/episode/" . $_GET["id_episode"]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["message"]; + + if ($resMessage != "success") { + http_response_code(400); + header("Content-Type: application/json"); + echo json_encode(["message" => "bad request"]); + + return; + } + + http_response_code(200); + header("Content-Type: application/json"); + echo json_encode(["message" => "success"]); + + return; + } + $episodeModel = new EpisodeModel(); $oldEpisode = $episodeModel->getById($_GET["id_episode"]); $episodeModel->deleteEpisode($_GET["id_episode"]); diff --git a/src/app/controllers/dashboard/delete_podcast.php b/src/app/controllers/dashboard/delete_podcast.php index f7f4bfd5c4af5d6129bce4929aefc6831607f63e..6e0aa0980ebde01c7c7efe5da5525c8ff31a1a4a 100644 --- a/src/app/controllers/dashboard/delete_podcast.php +++ b/src/app/controllers/dashboard/delete_podcast.php @@ -22,6 +22,38 @@ class DeletePodcastController return; } + // Check is premium + if ($_GET["premium"] == "true") { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/" . $_GET["id_podcast"]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["message"]; + + if ($resMessage != "success") { + http_response_code(400); + header("Content-Type: application/json"); + echo json_encode(["message" => "bad request"]); + + return; + } + + http_response_code(200); + header("Content-Type: application/json"); + echo json_encode(["message" => "success"]); + + return; + } + $podcastModel = new podcastModel(); $oldPodcast = $podcastModel->getById($_GET["id_podcast"]); $podcastModel->deletePodcast($_GET["id_podcast"]); diff --git a/src/app/controllers/dashboard/get_add_episode.php b/src/app/controllers/dashboard/get_add_episode.php index c4e449dd0b1a6e310a6a80dd935d6b7996cfa815..61e8d128942ee33ead6c44aa19aa1b9b047f5893 100644 --- a/src/app/controllers/dashboard/get_add_episode.php +++ b/src/app/controllers/dashboard/get_add_episode.php @@ -37,10 +37,9 @@ class GetAddEpisodeController $data = [ "INPUT_FORM_TITLE" => "Create New Episode", "INPUT_FORM_SHOW_AUDIO_INPUT" => true, - // "INPUT_FORM_SHOW_CATEGORY_INPUT" => true, "INPUT_FORM_COVER_TEXT" => "Episode Cover", "INPUT_FORM_SUBMIT_TEXT" => "Save Episode", - // "INPUT_FORM_DELETE_TEXT" => "Hapus Podcast", + "INPUT_FORM_IS_PREMIUM" => $_GET["premium"], "INPUT_FORM_TITLE_TEXT" => "Episode title", "INPUT_FORM_DESCRIPTION_TEXT" => "Episode description", "INPUT_FORM_TYPE" => "add-episode", diff --git a/src/app/controllers/dashboard/get_add_podcast.php b/src/app/controllers/dashboard/get_add_podcast.php index c8d348ba97402d8c2c111eb4c895a747d1b8416b..f3c73d9403067acce43f23d2c397cb56a8e7827a 100644 --- a/src/app/controllers/dashboard/get_add_podcast.php +++ b/src/app/controllers/dashboard/get_add_podcast.php @@ -28,15 +28,15 @@ class GetAddPodcastController $data = [ "INPUT_FORM_TITLE" => "Create New Podcast", - // "INPUT_FORM_SHOW_AUDIO_INPUT" => true, "INPUT_FORM_SHOW_CATEGORY_INPUT" => true, "INPUT_FORM_COVER_TEXT" => "Podcast Cover", "INPUT_FORM_SUBMIT_TEXT" => "Save Podcast", - // "INPUT_FORM_DELETE_TEXT" => "Hapus Podcast", "INPUT_FORM_TITLE_TEXT" => "Podcast title", "INPUT_FORM_DESCRIPTION_TEXT" => "Podcast description", "INPUT_FORM_TYPE" => "add-podcast", - "categories" => ["comedy", "sports", "technology"], + "INPUT_FORM_SHOW_PREMIUM_BUTTON" => true, + "INPUT_FORM_IS_PREMIUM" => "false", + "categories" => ["comedy", "horror", "technology"], "id_user" => $userId, ]; diff --git a/src/app/controllers/dashboard/get_edit_episode.php b/src/app/controllers/dashboard/get_edit_episode.php index 744a34ab0dfebbed57a87355ded77980867a5926..398a6a7d9427bbb8921636f212dfb16625b091c8 100644 --- a/src/app/controllers/dashboard/get_edit_episode.php +++ b/src/app/controllers/dashboard/get_edit_episode.php @@ -38,9 +38,32 @@ class GetEditEpisodeController $idEpisode = ""; $episode = null; + $resMessage = null; + if (!isset($_GET["id_episode"])) { (new NotFoundController())->call(); return; + } else if ($_GET["premium"] == "true") { + $idEpisode = $_GET["id_episode"]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/episode/" . $idEpisode); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["episode"]; + if (!$resMessage["id_podcast"] || $resMessage["id_podcast"] != $idPodcast) { + (new NotFoundController())->call(); + return; + } } else { $idEpisode = $_GET["id_episode"]; @@ -55,10 +78,11 @@ class GetEditEpisodeController "INPUT_FORM_TITLE" => "Edit Episode", "INPUT_FORM_COVER_TEXT" => "Episode Cover", "INPUT_FORM_SUBMIT_TEXT" => "Save Episode", - "INPUT_FORM_TITLE_TEXT" => $episode->title ?? "", - "INPUT_FORM_DESCRIPTION_TEXT" => $episode->description ?? "", + "INPUT_FORM_TITLE_TEXT" => $_GET["premium"] == "true" ? $resMessage["title"] : $episode->title, + "INPUT_FORM_DESCRIPTION_TEXT" => $_GET["premium"] == "true" ? $resMessage["description"] : $episode->description, "INPUT_FORM_TYPE" => "edit-episode", - "url_thumbnail" => $episode->url_thumbnail ?? "", + "INPUT_FORM_IS_PREMIUM" => $_GET["premium"], + "url_thumbnail" => $_GET["premium"] == "true" ? $resMessage["url_thumbnail"] : $episode->url_thumbnail, "id_user" => $userId, "id_podcast" => $idPodcast, ]; diff --git a/src/app/controllers/dashboard/get_edit_podcast.php b/src/app/controllers/dashboard/get_edit_podcast.php index 662a3d34cf714a70994a824f3139689abdda31cc..37e23bd969c63b1ee45fe1773ce84e9dcd221c89 100644 --- a/src/app/controllers/dashboard/get_edit_podcast.php +++ b/src/app/controllers/dashboard/get_edit_podcast.php @@ -24,33 +24,58 @@ class GetEditPodcastController require_once __DIR__ . "/../../views/dashboard/dash_form.php"; + $podcastModel = new PodcastModel(); $userId = $_SESSION["user_id"]; $idPodcast = ""; + $podcast = null; + $resMessage = null; + if (!isset($_GET["id_podcast"])) { (new NotFoundController())->call(); return; + } else if ($_GET["premium"] == "true") { + $idPodcast = $_GET["id_podcast"]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/" . $idPodcast); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["podcast"]; + if (!$resMessage["id_user"] || $resMessage["id_user"] != $_SESSION["user_id"]) { + (new NotFoundController())->call(); + return; + } } else { $idPodcast = $_GET["id_podcast"]; - } - $podcastModel = new PodcastModel(); - $podcast = $podcastModel->getById($idPodcast); - if (!$podcast || $podcast->id_podcast != $idPodcast) { - (new NotFoundController())->call(); - return; + $podcast = $podcastModel->getById($idPodcast); + if (!$podcast || $podcast->id_user != $_SESSION["user_id"]) { + (new NotFoundController())->call(); + return; + } } $data = [ "INPUT_FORM_TITLE" => "Edit Podcast", "INPUT_FORM_COVER_TEXT" => "Episode Cover", "INPUT_FORM_SUBMIT_TEXT" => "Save Podcast", - "INPUT_FORM_TITLE_TEXT" => $podcast->title ?? "", + "INPUT_FORM_TITLE_TEXT" => $_GET["premium"] == "true" ? $resMessage["title"] : $podcast->title, "INPUT_FORM_DELETE_TEXT" => "Hapus Podcast", - "INPUT_FORM_DESCRIPTION_TEXT" => $podcast->description ?? "", + "INPUT_FORM_DESCRIPTION_TEXT" => $_GET["premium"] == "true" ? $resMessage["description"] : $podcast->description, "INPUT_FORM_TYPE" => "edit-podcast", - "url_thumbnail" => $podcast->url_thumbnail ?? "", + "INPUT_FORM_IS_PREMIUM" => $_GET["premium"], + "url_thumbnail" => $_GET["premium"] == "true" ? $resMessage["url_thumbnail"] : $podcast->url_thumbnail, "id_user" => $userId, "id_podcast" => $idPodcast, ]; diff --git a/src/app/controllers/dashboard/get_episode.php b/src/app/controllers/dashboard/get_episode.php index 3afdfeb1b3b6840dfa2cc47b58f549bc5c5cb6c3..e4be4ba697db32b458dc9a5c454ca12fb87fe8b6 100644 --- a/src/app/controllers/dashboard/get_episode.php +++ b/src/app/controllers/dashboard/get_episode.php @@ -24,6 +24,27 @@ class GetDashboardEpisodeController if (!isset($_GET["id_podcast"])) { (new NotFoundController())->call(); return; + } else if ($_GET["premium"] == "true") { + $idPodcast = $_GET["id_podcast"]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/" . $idPodcast); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["podcast"]; + if (!$resMessage["id_podcast"] || $resMessage["id_user"] != $_SESSION["user_id"]) { + (new NotFoundController())->call(); + return; + } } else { $idPodcast = $_GET["id_podcast"]; @@ -56,12 +77,13 @@ class GetDashboardEpisodeController } $data = [ + "premium_episodes" => $resMessage["PremiumEpisodes"] ?? [], "episodes" => $episodes, "page_count" => $pageCount, "url_thumbnail" => $episodes[0]->url_thumbnail ?? "", "id_user" => $userId, "id_podcast" => $idPodcast, - "page" => $page + "page" => $page, ]; $view = new DashboardEpisodeView($data); diff --git a/src/app/controllers/dashboard/get_layout.php b/src/app/controllers/dashboard/get_layout.php index c964a46d78955e369d5ad01132a90cb2ede8c5d0..c6d120335ee5af11aa0077158667d3a602adb12d 100644 --- a/src/app/controllers/dashboard/get_layout.php +++ b/src/app/controllers/dashboard/get_layout.php @@ -24,11 +24,28 @@ class GetDashboardLayoutController require_once __DIR__ . "/../../views/dashboard/layout.php"; + // Get all premium podcasts + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/by-user/" . $_SESSION["user_id"]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["podcasts"]; + $podcastModel = new PodcastModel(); $podcasts = $podcastModel->getUserPodcasts($_SESSION["user_id"]) ?? []; $data = [ - "podcasts" => $podcasts + "podcasts" => $podcasts, + "premium_podcasts" => $resMessage ]; $view = new DashboardLayoutView($data); diff --git a/src/app/controllers/dashboard/get_main.php b/src/app/controllers/dashboard/get_main.php index 9541c9643e01705a137bda87d9c8ef129eff2954..a79ac4d1923d9450dabc3c8a400a6f79dc6845a2 100644 --- a/src/app/controllers/dashboard/get_main.php +++ b/src/app/controllers/dashboard/get_main.php @@ -21,9 +21,31 @@ class GetDashboardMainController $userId = $_SESSION["user_id"]; $podcast = null; + $resMessage = null; if (!isset($_GET["id_podcast"])) { (new NotFoundController())->call(); return; + } else if ($_GET["premium"] == "true") { + $idPodcast = $_GET["id_podcast"]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/" . $idPodcast); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["podcast"]; + if (!$resMessage["id_podcast"] || $resMessage["id_user"] != $_SESSION["user_id"]) { + (new NotFoundController())->call(); + return; + } } else { $idPodcast = $_GET["id_podcast"]; $podcast = $podcastModel->getById($idPodcast); @@ -40,7 +62,7 @@ class GetDashboardMainController } $data = [ - "podcast" => $podcast, + "podcast" => $podcast ?? $resMessage, "episodes" => $episodes, "url_thumbnail" => $episodes[0]->url_thumbnail ?? "", "id_user" => $userId, diff --git a/src/app/controllers/dashboard/get_user_podcast.php b/src/app/controllers/dashboard/get_user_podcast.php index 468f593e0451fc898e836d672e0b102774775fd8..c2e15f1bc304d2bad6ed0cbcc7ae910c67bace0c 100644 --- a/src/app/controllers/dashboard/get_user_podcast.php +++ b/src/app/controllers/dashboard/get_user_podcast.php @@ -13,12 +13,28 @@ class GetUserPodcastController return; } + // Get all premium podcasts + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/by-user/" . $_SESSION["user_id"]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + $resMessage = json_decode($output, TRUE)["podcasts"]; + $podcastModel = new PodcastModel(); $podcasts = $podcastModel->getUserPodcasts($_SESSION["user_id"]) ?? []; http_response_code(200); header("Content-Type: application/json"); - echo json_encode(["podcasts" => $podcasts]); + echo json_encode(["podcasts" => $podcasts, "premium_podcasts" => $resMessage]); return; } diff --git a/src/app/controllers/dashboard/post_add_episode.php b/src/app/controllers/dashboard/post_add_episode.php index f9f81ac0376adaaa08febedf4167779d55f1d749..99aaf16c7b53803c50cd80ab60c9742d5598ea28 100644 --- a/src/app/controllers/dashboard/post_add_episode.php +++ b/src/app/controllers/dashboard/post_add_episode.php @@ -68,6 +68,52 @@ class PostAddEpisodeController $imageFileName = "/images/" . md5(uniqid(mt_rand(), true)) . IMAGE_MAP[$imageMimeType]; move_uploaded_file($_FILES["imageFile"]["tmp_name"], __DIR__ . "/../../storage" . $imageFileName); + // Check is premium + if ($_POST["isPremium"] == "true") { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/episode"); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt( + $ch, + CURLOPT_POSTFIELDS, + [ + "title" => $_POST["title"], + "description" => $_POST["description"], + "idPodcast" => $_POST["idPodcast"], + "imageFile" => curl_file_create(__DIR__ . "/../../storage" . $imageFileName), + "audioFile" => curl_file_create(__DIR__ . "/../../storage" . $audioFileName), + ] + ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + unlink(__DIR__ . "/../../storage" . $imageFileName); + unlink(__DIR__ . "/../../storage" . $audioFileName); + + $resMessage = json_decode($output, TRUE)["message"]; + + if ($resMessage != "success") { + http_response_code(400); + header("Content-Type: application/json"); + echo json_encode(["message" => "bad request"]); + + return; + } + + http_response_code(201); + header("Content-Type: application/json"); + echo json_encode(["message" => "success"]); + + return; + } + $episodeModel = new EpisodeModel(); $episodeModel->saveEpisode($_POST["idPodcast"], $_POST["title"], $_POST["description"], $imageFileName, $audioFileName); diff --git a/src/app/controllers/dashboard/post_add_podcast.php b/src/app/controllers/dashboard/post_add_podcast.php index 32745ef18ffbe6cc1f87e64744b4dbee952693a4..2d02b1ba038861471558bbcaca78694c279b3111 100644 --- a/src/app/controllers/dashboard/post_add_podcast.php +++ b/src/app/controllers/dashboard/post_add_podcast.php @@ -45,6 +45,51 @@ class PostAddPodcastController $imageFileName = "/images/" . md5(uniqid(mt_rand(), true)) . IMAGE_MAP[$imageMimeType]; move_uploaded_file($_FILES["imageFile"]["tmp_name"], __DIR__ . "/../../storage" . $imageFileName); + // Check is premium + if ($_POST["isPremium"] == "true") { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast"); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + curl_setopt( + $ch, + CURLOPT_POSTFIELDS, + [ + "title" => $_POST["title"], + "description" => $_POST["description"], + "idUser" => $_SESSION["user_id"], + "imageFile" => curl_file_create(__DIR__ . "/../../storage" . $imageFileName), + "category" => $_POST["category"] + ] + ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + unlink(__DIR__ . "/../../storage" . $imageFileName); + + $resMessage = json_decode($output, TRUE)["message"]; + + if ($resMessage != "success") { + http_response_code(400); + header("Content-Type: application/json"); + echo json_encode(["message" => "bad request"]); + + return; + } + + http_response_code(201); + header("Content-Type: application/json"); + echo json_encode(["message" => "success"]); + + return; + } + $podcastModel = new PodcastModel(); $podcastModel->savePodcast($_SESSION["user_id"], $_POST["title"], $_POST["description"], $_POST["category"], $imageFileName); diff --git a/src/app/controllers/dashboard/post_edit_episode.php b/src/app/controllers/dashboard/post_edit_episode.php index 2581d9720c8ffab1b89fb5370a185eec1afb804c..6bea488f9c31585ab83b5208b35e62f3b90e92e9 100644 --- a/src/app/controllers/dashboard/post_edit_episode.php +++ b/src/app/controllers/dashboard/post_edit_episode.php @@ -43,16 +43,67 @@ class PostEditEpisodeController $episodeModel = new EpisodeModel(); $oldEpisode = $episodeModel->getById($_POST["idEpisode"]); - $imageFileName = $oldEpisode->url_thumbnail; + $imageFileName = $oldEpisode->url_thumbnail ?? ""; // Store image file in server storage (if provided) if (isset($_POST["updateCover"]) && $_POST["updateCover"]) { - unlink(__DIR__ . "/../../storage" . $oldEpisode->url_thumbnail); + if ($oldEpisode) { + unlink(__DIR__ . "/../../storage" . $oldEpisode->url_thumbnail); + } $imageMimeType = mime_content_type($_FILES["imageFile"]["tmp_name"]); $imageFileName = "/images/" . md5(uniqid(mt_rand(), true)) . IMAGE_MAP[$imageMimeType]; move_uploaded_file($_FILES["imageFile"]["tmp_name"], __DIR__ . "/../../storage" . $imageFileName); } + // Check is premium + if ($_POST["isPremium"] == "true") { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/episode/" . $_POST["idEpisode"]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + $data = [ + "title" => $_POST["title"], + "description" => $_POST["description"], + "updateCover" => $_POST["updateCover"] ?? "false", + ]; + if (isset($_POST["updateCover"]) && $_POST["updateCover"]) { + $data["imageFile"] = curl_file_create(__DIR__ . "/../../storage" . $imageFileName); + } + curl_setopt( + $ch, + CURLOPT_POSTFIELDS, + $data + ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + if (isset($_POST["updateCover"]) && $_POST["updateCover"]) { + unlink(__DIR__ . "/../../storage" . $imageFileName); + } + + $resMessage = json_decode($output, TRUE)["message"]; + + if ($resMessage != "success") { + http_response_code(400); + header("Content-Type: application/json"); + echo json_encode(["message" => "bad request"]); + + return; + } + + http_response_code(201); + header("Content-Type: application/json"); + echo json_encode(["message" => "success"]); + + return; + } + $episodeModel->updateEpisode($_POST["idEpisode"], $_POST["title"], $_POST["description"], $imageFileName); http_response_code(201); diff --git a/src/app/controllers/dashboard/post_edit_podcast.php b/src/app/controllers/dashboard/post_edit_podcast.php index f84fc4f52592932ea0ad441fab4f07df742e28ed..6b351498afef1ab0a82a3203a32430b0905218c2 100644 --- a/src/app/controllers/dashboard/post_edit_podcast.php +++ b/src/app/controllers/dashboard/post_edit_podcast.php @@ -43,16 +43,67 @@ class PostEditPodcastController $podcastModel = new PodcastModel(); $oldPodcast = $podcastModel->getById($_POST["idPodcast"]); - $imageFileName = $oldPodcast->url_thumbnail; + $imageFileName = $oldPodcast->url_thumbnail ?? ""; // Store image file in server storage (if provided) if (isset($_POST["updateCover"]) && $_POST["updateCover"]) { - unlink(__DIR__ . "/../../storage" . $oldPodcast->url_thumbnail); + if ($oldPodcast) { + unlink(__DIR__ . "/../../storage" . $oldPodcast->url_thumbnail); + } $imageMimeType = mime_content_type($_FILES["imageFile"]["tmp_name"]); $imageFileName = "/images/" . md5(uniqid(mt_rand(), true)) . IMAGE_MAP[$imageMimeType]; move_uploaded_file($_FILES["imageFile"]["tmp_name"], __DIR__ . "/../../storage" . $imageFileName); } + // Check is premium + if ($_POST["isPremium"] == "true") { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, "http://tubes-rest-service:3000/podcast/" . $_POST["idPodcast"]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "apikey: " . $_ENV["REST_PHP_KEY"], + ]); + $data = [ + "title" => $_POST["title"], + "description" => $_POST["description"], + "updateCover" => $_POST["updateCover"] ?? "false", + ]; + if (isset($_POST["updateCover"]) && $_POST["updateCover"]) { + $data["imageFile"] = curl_file_create(__DIR__ . "/../../storage" . $imageFileName); + } + curl_setopt( + $ch, + CURLOPT_POSTFIELDS, + $data + ); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + + $output = curl_exec($ch); + curl_close($ch); + + if (isset($_POST["updateCover"]) && $_POST["updateCover"]) { + unlink(__DIR__ . "/../../storage" . $imageFileName); + } + + $resMessage = json_decode($output, TRUE)["message"]; + + if ($resMessage != "success") { + http_response_code(400); + header("Content-Type: application/json"); + echo json_encode(["message" => "bad request"]); + + return; + } + + http_response_code(201); + header("Content-Type: application/json"); + echo json_encode(["message" => "success"]); + + return; + } + $podcastModel->updatePodcast($_POST["idPodcast"], $_POST["title"], $_POST["description"], $imageFileName); http_response_code(201); diff --git a/src/public/javascript/dashboard/inputForm.js b/src/public/javascript/dashboard/inputForm.js index 0fa4c2be74dad4b2a16ea88784ecb3c5c4467c26..31226f093d445bbccde95ee871bc3e6c9463ae50 100644 --- a/src/public/javascript/dashboard/inputForm.js +++ b/src/public/javascript/dashboard/inputForm.js @@ -16,12 +16,14 @@ const changeCoverButtonEl = document.getElementById("change-cover-btn"); const saveButtonEl = document.getElementById("save-btn"); const categoryButtonEl = document.getElementById("category-input-btn"); const deleteButtonEl = document.getElementById("delete-btn"); +const isPremiumButtonEl = document.getElementById("is-premium-btn"); const audioInputEl = document.getElementById("audio-input"); const judulInputEl = document.getElementById("judul-input"); const descriptionInputEl = document.getElementById("description-input"); const imageInputEl = document.getElementById("image-input"); const categoryInputEl = document.getElementById("category-input"); +const isPremiumInputEl = document.getElementById("is-premium-input"); const fileNameEl = document.getElementById("file-name"); const fileNameContainerEl = document.querySelector(".file-name-container"); @@ -154,7 +156,10 @@ deleteButtonEl && } const xhr1 = new XMLHttpRequest(); - xhr1.open("DELETE", `/public/dashboard/podcast?id_podcast=${idPodcast}`); + xhr1.open( + "DELETE", + `/public/dashboard/podcast?id_podcast=${idPodcast}&premium=${isPremiumInputEl.value}` + ); xhr1.send(null); xhr1.onreadystatechange = function () { @@ -167,6 +172,18 @@ deleteButtonEl && }; }); +isPremiumButtonEl && + isPremiumButtonEl.addEventListener("click", (e) => { + e.preventDefault(); + + isPremiumInputEl.value = + isPremiumInputEl.value === "false" ? "true" : "false"; + isPremiumButtonEl.innerText = + isPremiumInputEl.value === "false" ? "Not Premium" : "Premium"; + + isPremiumButtonEl.classList.toggle("premium-selected"); + }); + // Handle submit form saveButtonEl.addEventListener("click", (e) => { e.preventDefault(); @@ -179,6 +196,7 @@ saveButtonEl.addEventListener("click", (e) => { const title = judulInputEl.value; const description = descriptionInputEl.value; const category = categoryInputEl?.value; + const isPremium = isPremiumInputEl.value; // Limit file size to only 10 MB if (audioFile?.size > 10 * 1024 * 1024) { @@ -238,6 +256,7 @@ saveButtonEl.addEventListener("click", (e) => { formData.append("title", title); formData.append("description", description); formData.append("idPodcast", idPodcast); + formData.append("isPremium", isPremium); xhr.send(formData); break; @@ -279,6 +298,7 @@ saveButtonEl.addEventListener("click", (e) => { "description", description || descriptionInputEl.placeholder ); + formData.append("isPremium", isPremium); xhr.send(formData); break; @@ -318,6 +338,7 @@ saveButtonEl.addEventListener("click", (e) => { formData.append("title", title); formData.append("description", description); formData.append("category", category); + formData.append("isPremium", isPremium); xhr.send(formData); break; @@ -358,6 +379,7 @@ saveButtonEl.addEventListener("click", (e) => { "description", description || descriptionInputEl.placeholder ); + formData.append("isPremium", isPremium); xhr.send(formData); break; diff --git a/src/public/javascript/dashboard/layout.mjs b/src/public/javascript/dashboard/layout.mjs index bd6b475741668298f6d22f1e14e629ec9b60aaad..6d8fbc618bacefa58fbcd8f6d330cd092d742752 100644 --- a/src/public/javascript/dashboard/layout.mjs +++ b/src/public/javascript/dashboard/layout.mjs @@ -17,6 +17,7 @@ export function handleDashboard() { let idPodcast = new URLSearchParams(window.location.search).get("id_podcast"); let page = new URLSearchParams(window.location.search).get("page"); let podcasts = []; + let isPremium = new URLSearchParams(window.location.search).get("premium"); // Initial request to fetch user podcasts const xhr = new XMLHttpRequest(); @@ -27,7 +28,15 @@ export function handleDashboard() { if (xhr.readyState === 4 && xhr.status === 200) { const resJson = JSON.parse(xhr.response); - podcasts = resJson?.podcasts; + podcasts = + resJson?.podcasts.length > 0 + ? resJson?.podcasts + : resJson?.premium_podcasts; + + if (!isPremium) { + isPremium = resJson?.podcasts.length > 0 ? "false" : "true"; + } + // Redirect if the user doesnt have any podcast if (podcasts.length === 0) { window.location.replace("/public/dashboard/add-podcast"); @@ -54,7 +63,7 @@ export function handleDashboard() { "GET", `/public/dashboard/internal/${lastURL}?id_podcast=${idPodcast}${ lastURL === "dashboard-episode" ? `&page=${page || 1}` : "" - }` + }&premium=${isPremium}` ); xhr2.send(null); @@ -84,7 +93,7 @@ export function handleDashboard() { "", `/public/${lastURL}?id_podcast=${idPodcast}${ lastURL === "dashboard-episode" ? `&page=${page || 1}` : "" - }` + }&premium=${isPremium}` ); } }; @@ -111,7 +120,7 @@ export function handleDashboard() { history.pushState( {}, "", - `http://localhost:8080/public/dashboard-episode?id_podcast=${idPodcast}&page=1` + `http://localhost:8080/public/dashboard-episode?id_podcast=${idPodcast}&page=1&premium=${isPremium}` ); dashboardLink.classList.toggle("nav-active"); @@ -120,7 +129,7 @@ export function handleDashboard() { const xhr = new XMLHttpRequest(); xhr.open( "GET", - `/public/dashboard/internal/dashboard-episode?id_podcast=${idPodcast}&page=1` + `/public/dashboard/internal/dashboard-episode?id_podcast=${idPodcast}&page=1&premium=${isPremium}` ); xhr.send(null); @@ -147,14 +156,17 @@ export function handleDashboard() { history.pushState( {}, "", - `http://localhost:8080/public/dashboard-main?id_podcast=${idPodcast}` + `http://localhost:8080/public/dashboard-main?id_podcast=${idPodcast}&premium=${isPremium}` ); dashboardLink.classList.toggle("nav-active"); episodeLink.classList.toggle("nav-active"); const xhr1 = new XMLHttpRequest(); - xhr1.open("GET", `/public/dashboard/internal/dashboard-main?id_podcast=${idPodcast}`); + xhr1.open( + "GET", + `/public/dashboard/internal/dashboard-main?id_podcast=${idPodcast}&premium=${isPremium}` + ); xhr1.send(null); xhr1.onreadystatechange = function () { @@ -175,7 +187,7 @@ export function handleDashboard() { } const xhr1 = new XMLHttpRequest(); - xhr1.open("DELETE", `/public/dashboard/episode?id_episode=${idEpisode}`); + xhr1.open("DELETE", `/public/dashboard/episode?id_episode=${idEpisode}&premium=${isPremium}`); xhr1.send(null); xhr1.onreadystatechange = function () { @@ -185,7 +197,7 @@ export function handleDashboard() { const xhr2 = new XMLHttpRequest(); xhr2.open( "GET", - `/public/dashboard/internal/dashboard-episode?id_podcast=${idPodcast}&page=1` + `/public/dashboard/internal/dashboard-episode?id_podcast=${idPodcast}&page=1&premium=${isPremium}` ); xhr2.send(null); diff --git a/src/public/styles/dashboard/components/input-form.css b/src/public/styles/dashboard/components/input-form.css index 6570ee81c61d9ca83ebbdb5568f860d07d34384c..ab2049b349301d3c006960a3199aa6d322613fb3 100644 --- a/src/public/styles/dashboard/components/input-form.css +++ b/src/public/styles/dashboard/components/input-form.css @@ -207,6 +207,23 @@ option { background-color: var(--ORANGE); } +#is-premium-btn { + text-align: center; + border-radius: 16px; + border: 2px var(--PINK-1) solid; + padding: 12px 24px 10px; + font-size: 14px; + font-weight: 700; + text-transform: uppercase; + margin-top: 32px; +} + +.premium-selected { + background-color: #398044; + border: none !important; + padding: 12px 28px 10px !important; +} + @media (max-width: 768px) { #input-form { width: 100%;