diff --git a/app/api/anime_list/add.php b/app/api/anime_list/add.php index 62083159ab46f8f59069acf43336857e34d3aeba..c9bcb5882a028983c2594410e01cc8cd7da9531e 100644 --- a/app/api/anime_list/add.php +++ b/app/api/anime_list/add.php @@ -4,4 +4,28 @@ require_once(BASE_DIR.'/models/Anime_List.php'); $al = new Anime_List(); -?> \ No newline at end of file +if (isset($_GET['client_id']) && isset($_GET['anime_id'])) { + $client_id = $_GET['client_id']; + $anime_id = $_GET['anime_id']; + + $data = [ + 'client_id' => $client_id, + 'anime_id' => $anime_id, + 'user_score' => null, + 'progress' => null, + 'watch_status' => null, + 'review' => null + ]; + + if ($al->insertAnimeList($data)) { + if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) { + header("Location: " . $_SERVER['HTTP_REFERER'] . "/?message=Added Succesfully"); + } else { + header("Location: /?message=Added Succesfully"); + } + exit(); + } else { + header('Location: /?error=Failed to add'); + } +} +?> diff --git a/app/api/anime_list/delete.php b/app/api/anime_list/delete.php index 62083159ab46f8f59069acf43336857e34d3aeba..f3dc9e37abfd51c1dfb3f5befc64cb85839fb36f 100644 --- a/app/api/anime_list/delete.php +++ b/app/api/anime_list/delete.php @@ -2,6 +2,20 @@ require_once(dirname(__DIR__,2).'/define.php'); require_once(BASE_DIR.'/models/Anime_List.php'); -$al = new Anime_List(); +if (isset($_GET['id'])) { + $id = $_GET['id']; + $al = new Anime_List(); + + if ($al->deleteAnimeList($id)) { + if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) { + header("Location: " . $_SERVER['HTTP_REFERER'] . "/?message=Deleted Succesfully"); + } else { + header("Location: /?message=Deleted Succesfully"); + } + exit(); + } else { + header('Location: /?error=Failed to delete ' . $id); + } +} ?> \ No newline at end of file diff --git a/app/api/anime_list/edit.php b/app/api/anime_list/edit.php index 62083159ab46f8f59069acf43336857e34d3aeba..ae1357965fc691422620ca6d274bc5794c239ceb 100644 --- a/app/api/anime_list/edit.php +++ b/app/api/anime_list/edit.php @@ -3,5 +3,50 @@ require_once(dirname(__DIR__,2).'/define.php'); require_once(BASE_DIR.'/models/Anime_List.php'); $al = new Anime_List(); +if ($_SERVER["REQUEST_METHOD"] == "POST") { + if (!isset($_POST['list_id']) || empty($_POST['list_id'])) { + echo "No ID provided. Cannot proceed with edit."; + exit(); + } + $data = [ + 'list_id' => $_POST['list_id'], + 'client_id' => $_POST['client_id'], + 'anime_id' => $_POST['anime_id'] + ]; + if (isset($_POST['user_score'])) { + $data['user_score'] = $_POST['user_score']; + } else { + $data['user_score'] = null; + } + + if (isset($_POST['progress'])) { + $data['progress'] = $_POST['progress']; + } else { + $data['progress'] = null; + } + + if (isset($_POST['watch_status'])) { + $data['watch_status'] = $_POST['watch_status']; + } else { + $data['watch_status'] = null; + } + + if (isset($_POST['review'])) { + $data['review'] = $_POST['review']; + } else { + $data['review'] = null; + } + + + // Call the updateStudio method + $result = $al->updateAnimeList($data); + + if ($result) { + header("Location: /?client/detail/" . $_POST['client_id'] . "/?message=Edited Succesfully"); + exit(); + } else { + header('Location: /?error=Failed to edit'); + } +} ?> \ No newline at end of file diff --git a/app/api/client/edit.php b/app/api/client/edit.php index a71d2ad858e817a6f933c030d6243291a8addf1f..425495c2000b51120243bcdd7b5024c7916bb383 100644 --- a/app/api/client/edit.php +++ b/app/api/client/edit.php @@ -16,7 +16,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { 'username' => $_POST['username'], 'email' => $_POST['email'], 'password' => $_POST['password'], - 'admin_status' => $_POST['admin_status'] == "true" ? true : false + 'admin_status' => $_POST['admin_status'] ]; // Check if birthdate is set and not empty @@ -89,7 +89,11 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { $result = $c->updateClient($data); if ($result) { - header("Location: /?admin"); + if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) { + header("Location: " . $_SERVER['HTTP_REFERER']); + } else { + header("Location: /?admin"); // Fallback if there's no referrer + } exit(); } else { echo "Failed to edit client. Please try again."; diff --git a/app/models/anime_list.php b/app/models/anime_list.php index 977c1e8f417200ec8c843fb3268cf50918af0d4a..7184f4cf8d9db01a5867b6f62ee8859c3e0e0bb8 100644 --- a/app/models/anime_list.php +++ b/app/models/anime_list.php @@ -41,7 +41,7 @@ class Anime_List{ foreach($data as $key => $value){ $data[$key] = $this->db->processDataType($value); } - $this->db->query('UPDATE ' . $this->table . 'SET client_id = '.$data['client_id'].', anime_id = '.$data['anime_id'].', user_score = '.$data['user_score'].', progress = '.$data['progress'].', watch_status = '.$data['watch_status'].', review = '.$data['review'].' WHERE list_id = '. $data['list_id']); + $this->db->query('UPDATE ' . $this->table . ' SET client_id = '.$data['client_id'].', anime_id = '.$data['anime_id'].', user_score = '.$data['user_score'].', progress = '.$data['progress'].', watch_status = '.$data['watch_status'].', review = '.$data['review'].' WHERE list_id = '. $data['list_id']); $this->db->execute(); return ($this->db->countRow() != 0); // if countRow == 0, query fails diff --git a/app/public/handler/animeList.js b/app/public/handler/animeList.js new file mode 100644 index 0000000000000000000000000000000000000000..d1d3ec78ff84ad0d92d39510a5e81932b2525f20 --- /dev/null +++ b/app/public/handler/animeList.js @@ -0,0 +1,26 @@ +document.addEventListener('DOMContentLoaded', function() { + document.querySelectorAll('.add-list-btn').forEach(function(button) { + button.addEventListener('click', function(e) { + e.preventDefault(); + + let clientId = e.target.getAttribute('data-client-id'); + let animeId = e.target.getAttribute('data-anime-id'); + + window.location.href = + `/api/anime_list/add.php?client_id=${clientId}&anime_id=${animeId}`; + }); + }); +}); + +document.addEventListener('DOMContentLoaded', function() { + document.querySelectorAll('.remove-list-btn').forEach(function(button) { + button.addEventListener('click', function(e) { + e.preventDefault(); + + let listId = e.target.getAttribute('data-list-id'); + + window.location.href = + `/api/anime_list/delete.php?id=${listId}`; + }); + }); +}); \ No newline at end of file diff --git a/app/views/Client/list.php b/app/views/Client/list.php index 4aa3969e631ba31f0a1b38522645ecc370e73219..54314b874c23f6cf846e3853ec17196347aaad8e 100644 --- a/app/views/Client/list.php +++ b/app/views/Client/list.php @@ -3,13 +3,19 @@ require_once(dirname(__DIR__,2).'/define.php'); require_once(BASE_DIR.'/views/includes/header.php'); require_once(BASE_DIR.'/models/Client.php'); +require_once(BASE_DIR.'/models/Anime.php'); require_once(BASE_DIR.'/models/Anime_List.php'); +$a = new Anime(); $c = new Client(); $al = new Anime_List(); $id = $data['id']; -$isUserOwn = $al->getAnimeListByID($id)['client_id'] == $c->getClientByUsername($_SESSION['username'])['client_id']; +$list = $al->getAnimeListByID($data['id']); +$client = $c->getClientByID($list['client_id']); +$anime = $a->getAnimeByID($list['anime_id']); + +$isUserOwn = $list['client_id'] == $c->getClientByUsername($_SESSION['username'])['client_id']; ?> @@ -23,6 +29,7 @@ $isUserOwn = $al->getAnimeListByID($id)['client_id'] == $c->getClientByUsername( <link rel="stylesheet" href="../../public/style/global.css"> <link rel="stylesheet" href="../../public/style/list.css"> <script src='/public/handler/navbar.js'></script> + <script src='/public/handler/animeList.js'></script> </head> <body> @@ -30,22 +37,32 @@ $isUserOwn = $al->getAnimeListByID($id)['client_id'] == $c->getClientByUsername( if ($isUserOwn){ echo " <h1> Anime List ID $id </h1> + <h2> + Client: $client[username] <br> + Anime: $anime[title] + </h2> <form class='form-vertical' action='/api/anime_list/edit.php' method='post' enctype='multipart/form-data'> - <!-- Hidden input for client_id --> - <input type='hidden' id='editClientId' name='client_id'> + <input type='hidden' id='editListId' name='list_id' value=$id> + <input type='hidden' id='editClientId' name='client_id' value=$client[client_id]> + <input type='hidden' id='editAnimeId' name='anime_id' value=$anime[anime_id]> <label for='editUserScore'>User Score</label> - <input type='text' id='editUserScore' name='user_score'> + <input type='number' id='editUserScore' name='user_score' min='1' max='10' value=$list[user_score]> <label for='editProgress'>Progress:</label> - <input type='text' id='editProgress' name='progress'> + <input type='number' id='editProgress' name='progress' min='1' max=$anime[episodes] value=$list[progress]> - <label for='editWatchStatus'>WatchStatus:</label> - <input type='text' id='editWatchStatus' name='watch_status' required> + <label for='editWatchStatus'>Watch Status:</label> + <select id='editWatchStatus' name='watch_status'> + <option value='WATCHING'" . ($list['watch_status'] == 'WATCHING' ? " selected" : "") . ">Watching</option> + <option value='COMPLETED'" . ($list['watch_status'] == 'COMPLETED' ? " selected" : "") . ">Completed</option> + <option value='ON-HOLD'" . ($list['watch_status'] == 'ON-HOLD' ? " selected" : "") . ">On Hold</option> + <option value='DROPPED'" . ($list['watch_status'] == 'DROPPED' ? " selected" : "") . ">Dropped</option> + <option value='PLAN TO WATCH'" . ($list['watch_status'] == 'PLAN TO WATCH' ? " selected" : "") . ">Plan to Watch</option> + </select> <label for='editReview'>Review:</label> - <input type='text' id='editReview' name='review'> - + <textarea id='editReview' name='review' placeholder='Review'>$list[review]</textarea> <input type='submit' value='Update Client'> </form> diff --git a/app/views/anime/detail.php b/app/views/anime/detail.php index 6ccd27f1428103a9fc6391b886f170a4f24a5eb9..792aac76016047dfaf05bcbfeeb05e3905e45409 100644 --- a/app/views/anime/detail.php +++ b/app/views/anime/detail.php @@ -22,6 +22,7 @@ $reviews = $a->getReviewsByAnimeID($id); $genres = $g->getAllGenreIDByAnimeID($id); $client_id = $c->getClientByUsername($_SESSION['username'])['client_id']; +$list = $al->getAnimeListByAnimeClientID($id, $client_id) ?> @@ -32,6 +33,7 @@ $client_id = $c->getClientByUsername($_SESSION['username'])['client_id']; <link rel="stylesheet" href="../../public/style/global.css"> <link rel="stylesheet" href="../../public/style/anime.css"> <script src='/public/handler/navbar.js'></script> + <script src='/public/handler/animeList.js'></script> </head> @@ -90,10 +92,16 @@ $client_id = $c->getClientByUsername($_SESSION['username'])['client_id']; <center> <?php - if (!$al->getAnimeListByAnimeClientID($id, $client_id)){ - echo'<button class="add-list-btn">Add to My List</button>'; + if (!$list){ + echo" + <button class='add-list-btn' + data-anime-id='$anime[anime_id]' + data-client-id='$client_id'> + Add to My List + </button> + "; } else { - echo'<button class="remove-list-btn">Remove from My List</button>'; + echo"<button class='remove-list-btn' data-list-id=$list[list_id]>Remove from My List</button>"; } ?> </center>