diff --git a/app/controllers/admin.php b/app/controllers/admin.php index cc3e3385fc24056abc1e833bb4c19dbdf6c9c1ab..ff4060eeeef9a90186e2079a4fc06118c4f4d42b 100644 --- a/app/controllers/admin.php +++ b/app/controllers/admin.php @@ -75,6 +75,8 @@ class Admin extends Controller { } } + $data['book'] = $this->model('BookModel')->getAllBookAdmin(); + $data['author'] = $this->model('AuthorModel')->getAuthor(); $this->view('admin/bookadmin', $data); } else { $this->view('error/404'); @@ -87,13 +89,9 @@ class Admin extends Controller { $newPassword = $_POST['newPassword']; $data['user'] = $this->model('UserModel')->getAllUser(); $array = array_column($data['user'], "username"); - // Cek stringnya kosong ato g - if(empty($newUsername) || empty($newPassword)){ - var_dump($newPassword); - $this->view('admin/useradmin', $data); - } - // Cek exist ato g usernya - if (!in_array($newUsername, $array, true)){ + + // Cek stringnya kosong ato g && exist ato g usernya + if (!(empty($newUsername) || empty($newPassword)) && !in_array($newUsername, $array, true)){ $this->model('UserModel')->insertUser($newUsername, $newPassword); } } @@ -118,16 +116,8 @@ class Admin extends Controller { $data['book'] = $this->model('BookModel')->getAllBook(); $array = array_column($data['book'], "title"); - - // Cek string kosong ato ga - if(empty($newTitle)){ - $data['book'] = $this->model('BookModel')->getAllBookAdmin(); - $data['author'] = $this->model('AuthorModel')->getAuthor(); - $this->view('admin/bookadmin', $data); - } - - // Cek title udah ada atau belum, atau emang gk diganti - if(!in_array($newTitle, $array, true) || $newTitle === $oldTitle){ + // Cek title kosong atau udah exist di db atau gk diganti + if(!empty($newTitle) && (!in_array($newTitle, $array, true) || $newTitle === $oldTitle)){ $this->model('BookModel')->updateBook($bookid, $newTitle, $selectedAuthor, $newContent, $selectedCategory); } @@ -152,12 +142,8 @@ class Admin extends Controller { // Bikin kolom array list username buat pengecekkan $array = array_column($data['user'], "username"); - // Cek stringnya kosong ato g - if(empty($newUsername) || empty($newPassword)){ - $this->view('admin/useradmin', $data); - } - // Cek ada di db ato g + cek kalo usernamenya gk di edit, berarti password doang - if (!in_array($newUsername, $array, true) || $newUsername === $oldUsername){ + // Cek password kosong dan user kosong atau udah exist di db atau gk diganti + if (!(empty($newUsername) || empty($newPassword)) && (!in_array($newUsername, $array, true) || $newUsername === $oldUsername)){ $this->model('UserModel')->updateUser($userId, $newUsername, $newPassword); } @@ -173,21 +159,22 @@ class Admin extends Controller { public function addAuthor(){ if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin' ){ if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $newAuthorName = $_POST['newAuthorName']; // Assuming you have an input field for author name - // Additional author-related fields can be added here + $newAuthorName = $_POST['newAuthorName']; + - // Check if the author already exists in the database + $data['authors'] = $this->model('AuthorModel')->getAllAuthor(); $authorNames = array_column($data['authors'], "author_name"); - - if (!in_array($newAuthorName, $authorNames, true)){ - // Insert the new author into the database using your AuthorModel - $this->model('AuthorModel')->insertAuthor($newAuthorName); // Adjust this according to your model's method + + // Cek exist atau kosong + if (!in_array($newAuthorName, $authorNames, true) && !empty($newAuthorName)){ + + $this->model('AuthorModel')->insertAuthor($newAuthorName); } } - // Fetch the list of authors (optional) + $data['authors'] = $this->model('AuthorModel')->getAllAuthor(); - $this->view('admin/authoradmin', $data); // Adjust the view file and path accordingly + $this->view('admin/authoradmin', $data); } else { $this->view('login/login'); } @@ -196,17 +183,17 @@ class Admin extends Controller { public function editAuthor(){ if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin') { if ($_SERVER['REQUEST_METHOD'] === 'POST') { - // Get values from the input form - $newAuthorName = $_POST['newAuthorName']; + $newAuthorName = $_POST['newAuthorName']; $oldAuthorName = $_POST['authorName']; + $authorid = $_POST['authorId']; + $data['authors'] = $this->model('AuthorModel')->getAllAuthor(); $authorNames = array_column($data['authors'], "author_name"); - - if (!in_array($newAuthorName, $authorNames, true)){ - - $this->model('AuthorModel')->updateAuthor($oldAuthorName, $newAuthorName); + + if (!in_array($newAuthorName, $authorNames, true || $newAuthorName === $oldAuthorName) && !empty($newAuthorName)){ + $this->model('AuthorModel')->updateAuthor($authorid, $newAuthorName); } } @@ -219,8 +206,8 @@ class Admin extends Controller { public function deleteAuthor(){ if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin') { if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $authorName = $_POST['authorName']; - $this->model('AuthorModel')->deleteAuthor($authorName); + $authorId = $_POST['deleteId']; + $this->model('AuthorModel')->deleteAuthor($authorId); } $data['authors'] = $this->model('AuthorModel')->getAllAuthor(); $this->view('admin/authoradmin', $data); @@ -228,5 +215,31 @@ class Admin extends Controller { $this->view('login/login'); } } + + public function deleteUser(){ + if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin') { + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $userId = $_POST['deleteId']; + $this->model('UserModel')->deleteUser($userId); + } + $data['user'] = $this->model('UserModel')->getAllUser(); + $this->view('admin/useradmin', $data); + } else { + $this->view('login/login'); + } + } + public function deleteBook(){ + if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin') { + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $bookId = $_POST['deleteId']; + $this->model('BookModel')->deleteBook($bookId); + } + $data['book'] = $this->model('BookModel')->getAllBookAdmin(); + $data['author'] = $this->model('AuthorModel')->getAuthor(); + $this->view('admin/bookadmin', $data); + } else { + $this->view('login/login'); + } + } } \ No newline at end of file diff --git a/app/models/AuthorModel.php b/app/models/AuthorModel.php index f259633a63014b15dcf506386084ee9e388641a1..7ca054277d05bdf3c296edc87b8534d30b0334c0 100644 --- a/app/models/AuthorModel.php +++ b/app/models/AuthorModel.php @@ -27,9 +27,9 @@ class AuthorModel $this->database->bind('authorid', $authorid); $this->database->execute(); } - public function deleteAuthor($authorName){ - $this->database->query('DELETE FROM ' . $this->table . ' WHERE name = :name'); - $this->database->bind('name', $authorName); + public function deleteAuthor($authorId){ + $this->database->query('DELETE FROM ' . $this->table . ' WHERE id = :id'); + $this->database->bind('id', $authorId); $this->database->execute(); } } \ No newline at end of file diff --git a/app/models/UserModel.php b/app/models/UserModel.php index 64eea7e8423bd2af9daa5a947efaa0d84451f569..33601c7b016843f06a5b828906432b0d0892eab9 100644 --- a/app/models/UserModel.php +++ b/app/models/UserModel.php @@ -44,14 +44,7 @@ class UserModel $this->database->bind('newPassword', $newPassword); $this->database->execute(); } - - public function deleteUserByName($username){ - $this->database->query('DELETE FROM user WHERE name = :username'); - $this->database->bind('name', $username); - $this->database->execute(); - } - - public function deleteUserById($id){ + public function deleteUser($id){ $this->database->query('DELETE FROM user WHERE id = :id'); $this->database->bind('id', $id); $this->database->execute(); diff --git a/app/views/admin/authoradmin.php b/app/views/admin/authoradmin.php index ee2d10b6e1629eb71519c91ac47d9388fee158e3..0d168cfbb01039f879cd990d4a12796d688acdce 100644 --- a/app/views/admin/authoradmin.php +++ b/app/views/admin/authoradmin.php @@ -19,7 +19,7 @@ <div class="top-section"> <h1 class="title">Authors</h1> - <button class="add-btn" onclick="openAddPopup()">Add User</button> + <button class="add-btn" onclick="openAddPopup()">Add Author</button> </div> <div class="overlay" id="overlay"></div> @@ -44,16 +44,17 @@ <td><?php echo $id; ?></td> <td><?php echo $name; ?></td> <td> - <button class="edituser" onclick="editUser('<?php echo $name; ?>')"> + <button class="edituser" onclick="editAuthor('<?php echo $name; ?>', '<?php echo $id;?>')"> Edit </button> - <button class="deleteuser" onclick="deleteUser()"> + <button class="deleteuser" onclick="deleteAuthor('<?php echo $id ?>')"> Delete </button> </td> <div class="edit-popup" id="editpopup"> <form id="editForm" method="post" action='<?php echo BASEURL; ?>/admin/editAuthor'> - <input type="hidden" name="authorName" value="<?php echo $name; ?>"> + <input type="hidden" id="authorid" name="authorId"> + <input type="hidden" id="oldauthor" name="authorName"> <div class="add-input"> <input type="text" id="username" name="newAuthorName" placeholder="Enter new username.." required> </div> @@ -70,7 +71,7 @@ Are you sure want to delete this author? </p> <form id="deleteForm" method="post" action='<?php echo BASEURL; ?>/admin/deleteAuthor'> - <input type="hidden" name="authorName" value="<?php echo $name; ?>"> + <input type="hidden" id = "deleteid" name="deleteId"> </form> <div class="add-submission"> <button class="submit-button" onclick="deleteForm()">Delete</button> diff --git a/app/views/admin/bookadmin.php b/app/views/admin/bookadmin.php index 8d33f0a29cb71f50c4ca780b5221a10eb37a0f23..ef96e946b3a737230d00556a04874a2a00ddc759 100644 --- a/app/views/admin/bookadmin.php +++ b/app/views/admin/bookadmin.php @@ -55,15 +55,15 @@ <button class="editbook" onclick="editBook('<?php echo $title; ?>', '<?php echo $category; ?>', '<?php echo $author_name; ?>', '<?php echo $content; ?>', '<?php echo $id?>')"> Edit </button> - <button class="deletebook" onclick=deletebook()> + <button class="deletebook" onclick="deleteBook('<?php echo $id; ?>')"> Delete </button> </td> <div id="editpopup" class="edit-popup"> <form id="editForm" method="post" action='<?php echo BASEURL; ?>/admin/editBook'> - <input type="hidden" id = "bookid" name="bookid" value="<?php echo $id; ?>"> - <input type="hidden" id = "oldtitle" name="oldTitle" value="<?php echo $id; ?>"> + <input type="hidden" id = "bookid" name="bookid"> + <input type="hidden" id = "oldtitle" name="oldTitle"> <div class="horizontal-component"> <div class="title-input"> <input type="text" id='title' name="newTitle" placeholder="Enter new title..." class="input-text"> @@ -101,8 +101,11 @@ <p> Are you sure want to delete this book? </p> + <form id="deleteForm" method="post" action='<?php echo BASEURL; ?>/admin/deleteBook'> + <input type="hidden" id = "deleteid" name="deleteId"> + </form> <div class="add-submission"> - <button class="submit-button" onclick=deleteBook() >Delete</button> + <button class="submit-button" onclick=deleteForm()>Delete</button> <button class="cancel-button" onclick=closeDeletePopup()>Cancel</button> </div> </div> diff --git a/app/views/admin/useradmin.php b/app/views/admin/useradmin.php index 39ae02b5eb484d0aa5698c37919b285e6ffd736d..c946f0ec251d3547daf1e8f15694a643ba96f1aa 100644 --- a/app/views/admin/useradmin.php +++ b/app/views/admin/useradmin.php @@ -46,7 +46,7 @@ <button class="edituser" onclick="editUser('<?php echo $username; ?>', '<?php echo $password; ?>', '<?php echo $id; ?>')"> Edit </button> - <button class="deleteuser" onclick="deleteUser()"> + <button class="deleteuser" onclick="deleteUser('<?php echo $id;?>')"> Delete </button> </td> @@ -70,8 +70,11 @@ <p> Are you sure want to delete this user? </p> + <form id="deleteForm" method="post" action='<?php echo BASEURL; ?>/admin/deleteUser'> + <input type="hidden" id = "deleteid" name="deleteId"> + </form> <div class="add-submission"> - <button class="submit-button">Delete</button> + <button class="submit-button" onclick=deleteForm()>Delete</button> <button class="cancel-button" onclick=closeDeletePopup()>Cancel</button> </div> </div> diff --git a/public/js/authoradmin.js b/public/js/authoradmin.js index 32a2b799b71256f118cc9f89f59435381bab5538..457007cf3f8e46c3da7247cc08f99dfe7123be1f 100644 --- a/public/js/authoradmin.js +++ b/public/js/authoradmin.js @@ -1,7 +1,6 @@ let addpopup = document.getElementById("addpopup") let ol =document.getElementById("overlay") -const user = document.getElementById("username") -const pw = document.getElementById("password") +const author = document.getElementById("username") let editpopup = document.getElementById("editpopup") @@ -19,8 +18,12 @@ function closeAddPopup(){ pw.value =''; } -function editUser(username){ - user.value = username; +function editAuthor(authorname, authorid){ + const id = document.getElementById("authorid") + const oldauthor = document.getElementById("oldauthor"); + oldauthor.value = authorname; + author.value = authorname; + id.value = authorid; editpopup.classList.add("open-edit-popup"); ol.classList.add("open-overlay"); @@ -42,7 +45,9 @@ function deleteForm(){ form.submit(); } -function deleteUser(){ +function deleteAuthor(id){ + const deleteid = document.getElementById("deleteid"); + deleteid.value = id; deletepopup.classList.add("open-delete-popup"); ol.classList.add("open-overlay"); diff --git a/public/js/bookadmin.js b/public/js/bookadmin.js index 2b4b46e557a71d1db5d993f0cfbeb8103f77a898..0322f58f3857079683983529755b8762c3a8288b 100644 --- a/public/js/bookadmin.js +++ b/public/js/bookadmin.js @@ -55,11 +55,19 @@ function submitAddForm(){ form.submit(); } -function deletebook(){ +function deleteBook(id){ + const deletepopup = document.getElementById("deletepopup"); + const deleteid = document.getElementById("deleteid"); + deleteid.value = id; deletepopup.classList.add("open-delete-popup"); ol.classList.add("open-overlay"); } +function deleteForm(){ + const form = document.getElementById("deleteForm"); + form.submit(); +} + function closeEditPopUp(){ editpopup.classList.remove("open-edit-popup"); ol.classList.remove("open-overlay"); diff --git a/public/js/useradmin.js b/public/js/useradmin.js index 5ebf3c126b087f3ac736d17b731d897b0dae313d..9db5267168ee753eb3427ed9cbc01bfa95871632 100644 --- a/public/js/useradmin.js +++ b/public/js/useradmin.js @@ -44,12 +44,19 @@ function addForm(){ form.submit(); } -function deleteUser(){ +function deleteUser(id){ + const deleteid = document.getElementById("deleteid"); + deleteid.value = id; deletepopup.classList.add("open-delete-popup"); ol.classList.add("open-overlay"); } +function deleteForm(){ + const form = document.getElementById("deleteForm"); + form.submit(); +} + function closeEditPopup(){ editpopup.classList.remove("open-edit-popup"); ol.classList.remove("open-overlay");