diff --git a/src/app/components/library/playlist.php b/src/app/components/library/playlist.php index 1e9f64e74925fed71245ea08a2a5389f7e1e938f..1f5c858f1f45840c9dc70e6e84f835ccc4c7384f 100644 --- a/src/app/components/library/playlist.php +++ b/src/app/components/library/playlist.php @@ -15,6 +15,6 @@ </div> <?php else : ?> <h5> - no results found. + you don't have any playlist. </h5> <?php endif;?> \ No newline at end of file diff --git a/src/app/components/playlist/playlist.php b/src/app/components/playlist/playlist.php index be04d8bcea14396ad6ab79b23fecdc167732ef36..a4dd9e9a1ddbb6704d86f52b378d9b9d672a8ff2 100644 --- a/src/app/components/playlist/playlist.php +++ b/src/app/components/playlist/playlist.php @@ -7,6 +7,8 @@ <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/globals.css"> <!-- Page-specific CSS --> <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/library/library.css"> + <script type="module" src="<?= BASE_URL ?>/javascript/library/library.js" defer> </script> + <title>Playlist</title> </head> <body> @@ -15,7 +17,7 @@ <main> - <div class="playlist-container"> + <div class="playlist-container" data-id="<?=$_GET["playlist_id"]?>"> <?php include(dirname(__DIR__) . "/playlist/playlist_content.php")?> </div> </main> diff --git a/src/app/components/playlist/playlist_content.php b/src/app/components/playlist/playlist_content.php index fb216c4ba37de95941bb3c829e69c30ff3fb0adb..375f8971528e2ef88758f125e0438ceadf455043 100644 --- a/src/app/components/playlist/playlist_content.php +++ b/src/app/components/playlist/playlist_content.php @@ -1,4 +1,5 @@ <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/library/library.css"> +<link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'> <?php if (!empty($this->data)): ?> <div class="user-playlist"> @@ -10,6 +11,7 @@ <div class="info"> <div class="sh5"><?=$podcast['title']?> </div> </div> + <i class='bx bx-trash' data-id="<?=$podcast["id_podcast"]?>"></i> </div> <?php endforeach; ?> </div> @@ -17,4 +19,6 @@ <h5> your playlist is empty. </h5> -<?php endif;?> \ No newline at end of file +<?php endif;?> + +<script type="module" src="<?= BASE_URL ?>/javascript/library/library.js" defer> </script> diff --git a/src/app/components/podcast/page.php b/src/app/components/podcast/page.php index af5c57c113ed9d0bd8feed01e6bd3db2ea15ad95..1c127e60e244cafef64c2fba7f66a789fa82e800 100644 --- a/src/app/components/podcast/page.php +++ b/src/app/components/podcast/page.php @@ -7,7 +7,7 @@ <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/globals.css"> <link rel="stylesheet" type="text/css" href="<?= BASE_URL ?>/styles/podcast/style.css"> - <script type="text/javascript" src="<?= BASE_URL ?>/javascript/podcast/script.js" defer> + <script type="module" src="<?= BASE_URL ?>/javascript/podcast/script.js" defer> </script> <title>Main Dashboard</title> </head> @@ -38,11 +38,13 @@ <p>Add To Library</p> </button> - <ul id="library-choices" class="hidden"> + <ul id="library-choices" class="hidden" data-id-podcast="<?=$this->data["podcast"]->id_podcast ?>"> <?php foreach ($this->data["libraries"] as $library) : ?> <!-- Ini sekarang pake li tapi ntar kalo mau diganti jadi button atau a bisa juga --> <li> - <p class="b3"><?= $library ?></p> + <p class="b3 playlist" + data-id="<?= $library->id_playlist?>"> + <?= $library->title ?></p> </li> <?php endforeach; ?> </ul> diff --git a/src/app/controllers/library/get_library.php b/src/app/controllers/library/get_library.php index 60363e8d741fdfd0a1be1940f788008038f3dc4d..466cbab67861b0b6ce3f27e2e0967f6cb9d87dfe 100644 --- a/src/app/controllers/library/get_library.php +++ b/src/app/controllers/library/get_library.php @@ -5,7 +5,6 @@ class GetLibraryController public function call() { require_once __DIR__ . "/../../views/library/library_view.php"; - require_once __DIR__ . "/../../models/playlist.php"; $data = []; session_start(); @@ -22,7 +21,7 @@ class GetLibraryController $view = new LibraryView($data); $view->render(); }else{ - header("Location: "); + header("Location: " . BASE_URL . "/login"); } } diff --git a/src/app/controllers/login/get_login.php b/src/app/controllers/login/get_login.php index f1f7fd41aa629ddb6506166a96347d54cd1ee8a9..44627b028f597b1a2d46150bb2d66b8a45b41171 100644 --- a/src/app/controllers/login/get_login.php +++ b/src/app/controllers/login/get_login.php @@ -10,7 +10,8 @@ class GetLoginController if(isset($_SESSION['user_id'])){ - header("Location: http://localhost:8080/public/home?user_id=" . $_SESSION['user_id']); + header("Location: " . BASE_URL . "/home?user_id=" . $_SESSION["user_id"]); + }else{ $data = []; $view = new LoginView($data); diff --git a/src/app/controllers/login/post_login.php b/src/app/controllers/login/post_login.php index 1d1dbb1a70c84628dc63aae0e065cf7eb2aed394..b063c411299f144c64efe2be5c6f90fba8f6a25b 100644 --- a/src/app/controllers/login/post_login.php +++ b/src/app/controllers/login/post_login.php @@ -4,8 +4,7 @@ class PostLoginController { public function call() { - require_once __DIR__ . "/../../views/login/login.php"; - require_once __DIR__ . "/../../models/user.php"; + // require_once __DIR__ . "/../../views/login/login.php"; session_start(); if(isset($_POST['username']) && isset($_POST['password'])){ @@ -15,7 +14,7 @@ class PostLoginController $model = new UserModel(); try{ - $user = $model->getUser($_POST['username']); + $user = $model->getUser($username); $user = json_decode(json_encode($user), true); if(!$user){ diff --git a/src/app/controllers/playlist/post_playlist.php b/src/app/controllers/playlist/post_playlist.php new file mode 100644 index 0000000000000000000000000000000000000000..fada90eac7ea0b386395aaa40ca9bbb6518341bb --- /dev/null +++ b/src/app/controllers/playlist/post_playlist.php @@ -0,0 +1,36 @@ +<?php + +class PostPlaylistController +{ + public function call() + { + + session_start(); + if(!isset($_SESSION["user_id"])){ + header("Location: " . BASE_URL . "/login"); + + return; + } + + $idPodcast = ""; + $idPlaylist = ""; + + if(isset($_POST["id_playlist"]) && isset($_POST["id_podcast"])){ + $idPlaylist = $_POST["id_playlist"]; + $idPodcast = $_POST["id_podcast"]; + } + + $model = new PlaylistModel(); + + try{ + $model->removePodcastFromPlaylist($idPodcast, $idPlaylist); + http_response_code(201); + exit; + }catch(PDOException $e){ + $e->getMessage(); + http_response_code(200); + exit; + } + + } +} diff --git a/src/app/controllers/podcast/get_page.php b/src/app/controllers/podcast/get_page.php index 12bb94de4ff454bd2aa4b076e84e2b7208d1e561..3f82502c59c5d06caa59daa6d7e9aebf01bd2848 100644 --- a/src/app/controllers/podcast/get_page.php +++ b/src/app/controllers/podcast/get_page.php @@ -4,6 +4,7 @@ class GetPodcastPageController { public function call() { + session_start(); if (!isset($_SESSION["user_id"])) { http_response_code(403); @@ -24,14 +25,17 @@ class GetPodcastPageController $podcastModel = new PodcastModel(); $episodeModel = new EpisodeModel(); + $playlistModel = new PlaylistModel(); $podcast = $podcastModel->getById($idPodcast); $episodes = $episodeModel->getByPodcastId($idPodcast); + $playlists = $playlistModel->getUserPlaylist($_SESSION["user_id"]); + $data = [ "podcast" => $podcast, "episodes" => $episodes, - "libraries" => ["Contoh Library 1", "Contoh Library 2", "Contoh Library 3"] + "libraries" => $playlists, ]; $view = new PodcastPageView($data); diff --git a/src/app/controllers/podcast/post_page.php b/src/app/controllers/podcast/post_page.php new file mode 100644 index 0000000000000000000000000000000000000000..1a62717a81319e89024bd7aefc657ff802c15271 --- /dev/null +++ b/src/app/controllers/podcast/post_page.php @@ -0,0 +1,38 @@ +<?php + +class PostPodcastPageController +{ + public function call() + { + session_start(); + if(!isset($_SESSION["user_id"])){ + header("Location: " . BASE_URL . "/login"); + + return; + } + + $idPodcast = ""; + $idPlaylist = ""; + if(isset($_POST["id_playlist"]) && isset($_POST["id_podcast"])){ + $idPlaylist = $_POST["id_playlist"]; + $idPodcast = $_POST["id_podcast"]; + } + + $model = new PlaylistModel(); + + try{ + $model->addPodcastToPlaylist($idPlaylist, $idPodcast); + http_response_code(201); + exit; + + }catch(PDOException $e){ + // duplicate entries, violate integrity constraint (SQL code 23000) + if($e->getCode() === 23000){ + http_response_code(203); + }else{ // other type of error causing failure + http_response_code(200); + }; + exit; + } + } +} diff --git a/src/app/controllers/signup/get_signup.php b/src/app/controllers/signup/get_signup.php index 83adadb948f34900d76d4b25be1aae06a9ea124e..a680a69873413ce8b9ca1413b50e5b3011c5be9a 100644 --- a/src/app/controllers/signup/get_signup.php +++ b/src/app/controllers/signup/get_signup.php @@ -4,6 +4,8 @@ class GetSignupController { public function call() { + session_start(); + if(!isset($_SESSION['user_id'])){ require_once __DIR__ . "/../../views/signup/signup.php"; @@ -11,7 +13,7 @@ class GetSignupController $view = new SignupView($data); $view->render(); }else{ - header("Location: " . BASE_URL . "/home"); + header("Location: " . BASE_URL . "/home?user_id=" . $_SESSION["user_id"]); } } diff --git a/src/app/controllers/signup/post_signup.php b/src/app/controllers/signup/post_signup.php index cb5331b1ec84097f1f3e786ca89a5ff0ea274238..c9bafbd92257385eb0bc1add1db90306b4282a9c 100644 --- a/src/app/controllers/signup/post_signup.php +++ b/src/app/controllers/signup/post_signup.php @@ -5,7 +5,6 @@ class PostSignupController public function call() { require_once __DIR__ . "/../../views/signup/signup.php"; - require_once __DIR__ . "/../../models/user.php"; session_start(); if(!isset($_SESSION['user_id'])){ diff --git a/src/app/core/app.php b/src/app/core/app.php index af5ac7be67dadb8bc121d89b74ccc0d380c67dc8..20577232cd4c35d4e87a3529c40d66907a6bfcde 100644 --- a/src/app/core/app.php +++ b/src/app/core/app.php @@ -29,6 +29,8 @@ class App $router->get("public/podcast", new GetPodcastPageController()); $router->post("public/episode/play", new PostPlayEpisodeController()); + $router->post("public/podcast", new PostPodcastPageController()); + $router->post("public/logout", new LogoutController()); @@ -40,6 +42,7 @@ class App $router->post("public/signup", new PostSignupController()); $router->get("public/library", new GetLibraryController()); $router->get("public/playlist", new GetPlaylistController()); + $router->post("public/playlist", new PostPlaylistController()); $router->directRequest($url); } diff --git a/src/app/init.php b/src/app/init.php index 3ad64894dff473e13461768a6c246c5da5812c0f..c03e6df0137016ce290231deeef161074fc2b7b4 100644 --- a/src/app/init.php +++ b/src/app/init.php @@ -23,6 +23,8 @@ require_once __DIR__ . "/controllers/dashboard/delete_podcast.php"; require_once __DIR__ . "/controllers/podcast/get_page.php"; require_once __DIR__ . "/controllers/episode/post_play_episode.php"; +require_once __DIR__ . "/controllers/podcast/post_page.php"; + require_once __DIR__ . "/controllers/logout/logout.php"; @@ -32,6 +34,7 @@ require_once __DIR__ . "/controllers/login/post_login.php"; require_once __DIR__ . "/controllers/library/get_library.php"; require_once __DIR__ . "/controllers/playlist/get_playlist.php"; +require_once __DIR__ . "/controllers/playlist/post_playlist.php"; require_once __DIR__ . "/controllers/library/get_library.php"; require_once __DIR__ . "/controllers/signup/get_signup.php"; require_once __DIR__ . "/controllers/signup/post_signup.php"; @@ -40,3 +43,4 @@ require_once __DIR__ . "/controllers/search/get_search.php"; require_once __DIR__ . "/models/podcast.php"; require_once __DIR__ . "/models/episode.php"; require_once __DIR__ . "/models/user.php"; +require_once __DIR__ . "/models/playlist.php"; diff --git a/src/app/models/playlist.php b/src/app/models/playlist.php index e8b6b0533abb955f4ae904be2dbe05fcc04581ad..ba55193a3ebd0d41d076c6eba58a88a65414e2be 100644 --- a/src/app/models/playlist.php +++ b/src/app/models/playlist.php @@ -37,4 +37,61 @@ class PlaylistModel return $playlistPodcast; } + public function addPlaylist($id_user, $title, $id_playlist) + { + $query = " + INSERT INTO playlist (id_playlist, title, id_user) + VALUES(:id_playlist, :title, :id_user) + "; + + $this->db->query($query); + $this->db->bind("id_user", $id_user); + $this->db->bind("title", $title); + $this->db->bind("id_playlist", $id_playlist); + + $this->db->execute(); + } + + public function addPodcastToPlaylist($id_playlist, $id_podcast) + { + $query = " + INSERT INTO podcast_x_playlist (id_playlist, id_podcast) + VALUES(:id_playlist, :id_podcast) + "; + + $this->db->query($query); + $this->db->bind("id_playlist", $id_playlist); + $this->db->bind("id_podcast", $id_podcast); + + $this->db->execute(); + + } + + public function deletePlaylist($id_playlist) + { + $query = " + DELETE FROM playlist + WHERE id_playlist = :id_playlist + "; + + $this->db->query($query); + $this->db->bind("id_playlist", $id_playlist); + + $this->db->execute(); + } + + public function removePodcastFromPlaylist($id_podcast, $id_playlist) + { + $query = " + DELETE FROM podcast_x_playlist + WHERE id_playlist = :id_playlist AND id_podcast = :id_podcast + "; + + $this->db->query($query); + $this->db->bind("id_playlist", $id_playlist); + $this->db->bind("id_podcast", $id_podcast); + + $this->db->execute(); + } + } diff --git a/src/app/models/podcast.php b/src/app/models/podcast.php index 85a1a0585f42965167618bb0ffec32a41a846556..ee193721999d951ed6222d120400ee809555baf9 100644 --- a/src/app/models/podcast.php +++ b/src/app/models/podcast.php @@ -11,94 +11,85 @@ class PodcastModel public function getAllPodcast($keyword, $genre, $eps, $sort, $isAsc) { - $eps_new = ""; + $epsMin = ""; + $epsMax = ""; switch ($eps) { - case "eps-cat-1": - $eps_new = "COUNT(e) < 20"; + case "Less than 20 episodes": + $epsMin = "-1"; + $epsMax = "20"; break; - case "eps-cat-2": - $eps_new = "COUNT(e) BETWEEN 20 AND 50"; + case "20-50 episodes": + $epsMin = "20"; + $epsMax = "50"; break; - case "eps-cat-3": - $eps_new = "COUNT(e) BETWEEN 50 AND 100"; + case "50-100 episodes": + $epsMin = "50"; + $epsMax = "100"; break; - case "eps-cat-4": - $eps_new = "COUNT(e) > 100"; + case "More than 100 episodes": + $epsMin = "100"; + $epsMax = "999999999"; break; default: - $eps_new = "COUNT(e) > -1"; + $epsMin = "-1"; + $epsMax = "999999999"; break; }; - - $sort_new = ""; + //sort switch ($sort) { - case "alphabet": - $sort_new = "p.title"; - break; - case "sort": - $sort_new = "p.id"; - break; - default: - $sort_new = "p.description"; - break; - }; - - $isAsc_new = ""; - //isAsc - switch ($isAsc) { - case "true": - $isAsc_new = "ASC"; + case "alphabetical": + $query = + "SELECT p.title, p.category, p.url_thumbnail, p.description, u.name + FROM podcast AS p + NATURAL JOIN user AS u + LEFT JOIN episode AS e ON p.id_podcast = e.id_podcast + WHERE (p.title LIKE :search_value + OR u.name LIKE :search_value) + AND p.category LIKE :genre + GROUP BY p.id_podcast + HAVING COUNT(e.id_episode) BETWEEN :epsMin AND :epsMax + ORDER BY p.title + "; break; - case "false": - $isAsc_new = "DESC"; + case "date joined": + $query = + "SELECT p.title, p.category, p.url_thumbnail, p.description, u.name + FROM podcast AS p + NATURAL JOIN user AS u + LEFT JOIN episode AS e ON p.id_podcast = e.id_podcast + WHERE (p.title LIKE :search_value + OR u.name LIKE :search_value) + AND p.category LIKE :genre + GROUP BY p.id_podcast + HAVING COUNT(e.id_episode) BETWEEN :epsMin AND :epsMax + ORDER BY p.id_podcast + "; break; default: - $isAsc_new = "ASC"; + $query = + "SELECT p.title, p.category, p.url_thumbnail, p.description, u.name + FROM podcast AS p + NATURAL JOIN user AS u + LEFT JOIN episode e ON e.id_podcast=p.id_podcast + WHERE (p.title LIKE :search_value + OR u.name LIKE :search_value) + AND p.category LIKE :genre + GROUP BY p.id_podcast + HAVING COUNT(e.id_episode) BETWEEN :epsMin AND :epsMax + ORDER BY p.description + "; break; }; + - $query = - "SELECT p.title, p.category, p.url_thumbnail, p.description, u.name - FROM podcast AS p - NATURAL JOIN user AS u - INNER JOIN episode AS e ON p.id_podcast = e.id_podcast - WHERE (p.title LIKE :search_value - OR u.name LIKE :search_value) - GROUP BY p.title - HAVING :epsCount - ORDER BY :sort - "; - - $queryByGenre = - "SELECT p.title, p.category, p.url_thumbnail, p.description, u.name - FROM podcast p - NATURAL JOIN user u - INNER JOIN episode e ON p.id_podcast = e.id_podcast - WHERE (p.title LIKE :search_value - OR u.name LIKE :search_value - AND p.category = :genre) - GROUP BY p.title - HAVING :epsCount - ORDER BY :sort - "; + $this->db->query($query); - if ($genre == "") { - $this->db->query($query); - } else { - $this->db->query($queryByGenre); - } $this->db->bind("search_value", '%' . $keyword . '%'); - $this->db->bind("sort", $sort_new . $isAsc_new); - $this->db->bind("epsCount", $eps_new); - $this->db->bind("genre", $genre); + $this->db->bind("epsMin", $epsMin); + $this->db->bind("epsMax", $epsMax); + $this->db->bind("genre", $genre==""?'%':$genre); $podcasts = $this->db->fetchAll(); - - // var_dump($keyword); - // var_dump($genre); - // var_dump($eps); - // var_dump($sort); - // var_dump($isAsc); return $podcasts; } @@ -148,18 +139,18 @@ class PodcastModel return $podcasts; } - public function getPodcast($id_podcast) { - $query = " - SELECT * FROM podcast - WHERE id_podcast = :id_podcast - "; + // public function getPodcast($id_podcast) { + // $query = " + // SELECT * FROM podcast + // WHERE id_podcast = :id_podcast + // "; - $this->db->query($query); - $this->db->bind("id_podcast", $id_podcast); - $podcast = $this->db->fetchAll(); + // $this->db->query($query); + // $this->db->bind("id_podcast", $id_podcast); + // $podcast = $this->db->fetchAll(); - return $podcast; - } + // return $podcast; + // } public function getById($idPodcast) { diff --git a/src/public/javascript/library/library.js b/src/public/javascript/library/library.js new file mode 100644 index 0000000000000000000000000000000000000000..906458bd1d23dfa46bbfd6d7981687e7e3ae8074 --- /dev/null +++ b/src/public/javascript/library/library.js @@ -0,0 +1,38 @@ +"use strict" + +import { showErrorToast, showSuccessToast } from "../toast.mjs"; + + +document.addEventListener("DOMContentLoaded", (e) =>{ + + const trashEl = document.querySelectorAll('.bx-trash'); + const playlistContainer = document.querySelector('.playlist-container') + + if(trashEl.length !== 0){ + trashEl.forEach(function(trash){ + trash.addEventListener("click", (e) => { + e.preventDefault(); + + const data = new FormData(); + + const xhr = new XMLHttpRequest(); + + data.append("id_podcast", trash.dataset.id); + data.append("id_playlist", playlistContainer.dataset.id); + + xhr.open("POST", "/public/playlist", true); + + xhr.onload = function () { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 201) { + showSuccessToast("Podcast berhasil dihapus dari playlist!"); + }else{ // status code 200, some error + showErrorToast("Podcast gagal dihapus dari playlist!"); + } + } + } + xhr.send(data); + }) + }) + } +}); \ No newline at end of file diff --git a/src/public/javascript/podcast/script.js b/src/public/javascript/podcast/script.js index ba2295050a34df18f21476d295837096890ed67d..122b303623acb13f36b474574422af08c8657463 100644 --- a/src/public/javascript/podcast/script.js +++ b/src/public/javascript/podcast/script.js @@ -1,10 +1,13 @@ "use strict"; +import { showSuccessToast, showErrorToast, showInformationToast } from "../toast.mjs"; + // Get DOM elements const playButtonsEl = document.querySelectorAll(".play-button"); -const addLibraryButtonEl = document.getElementById("add-library-btn"); -const libraryChoicesEl = document.getElementById("library-choices"); -const overlayEl = document.getElementById("overlay-library"); +const addLibraryButtonEl = document.getElementById("add-library-btn") +const libraryChoicesEl = document.getElementById("library-choices") +const overlayEl = document.getElementById("overlay-library") +const playlists = document.querySelectorAll(".playlist"); // Handle play episode Array.from(playButtonsEl).forEach((el) => { @@ -42,3 +45,35 @@ overlayEl.addEventListener("click", (e) => { libraryChoicesEl.classList.toggle("hidden"); overlayEl.classList.toggle("hidden"); }); + + +if(playlists.length !== 0){ + playlists.forEach(function(playlist){ + playlist.addEventListener("click", (e) => { + e.preventDefault(); + + const data = new FormData(); + + const xhr = new XMLHttpRequest(); + + xhr.open("POST", "/public/podcast", true); + + data.append("id_playlist", playlist.dataset.id); + data.append("id_podcast", libraryChoicesEl.dataset.idPodcast); + + xhr.onload = function () { + if (xhr.readyState === XMLHttpRequest.DONE) { + if (xhr.status === 201) { + showSuccessToast("Podcast berhasil ditambahkan ke playlist!"); + }else if(xhr.status === 203){ + showInformationToast("Podcast sudah ada dalam playlist!") + } + else{ // status code 200, some other error + showErrorToast("Podcast gagal ditambahkan ke playlist!"); + } + } + } + xhr.send(data); + }) + }) +} \ No newline at end of file