From 3b83a89ab47bcbd6220173845e282bfd135228ee Mon Sep 17 00:00:00 2001
From: Kenneth Dave <dave.bahana@gmail.com>
Date: Mon, 9 Oct 2023 12:36:53 +0700
Subject: [PATCH] fix: add & edit in admin page

---
 app/controllers/admin.php     | 13 +++++++------
 app/models/AuthorModel.php    |  6 +++---
 app/models/BookModel.php      |  6 +++---
 app/views/admin/bookadmin.php | 10 +++++-----
 app/views/admin/useradmin.php | 16 +++++++---------
 public/js/authoradmin.js      |  2 --
 public/js/bookadmin.js        | 12 +++++++-----
 public/js/useradmin.js        | 15 +++++++++------
 8 files changed, 41 insertions(+), 39 deletions(-)

diff --git a/app/controllers/admin.php b/app/controllers/admin.php
index f5d11a9..cc3e338 100644
--- a/app/controllers/admin.php
+++ b/app/controllers/admin.php
@@ -87,9 +87,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
@@ -114,6 +114,7 @@ class Admin extends Controller {
                 $selectedAuthor = $_POST["selectedAuthor"];
                 $selectedCategory = $_POST["selectedCategory"];
                 $newContent = $_POST["newContent"];
+                $bookid = $_POST["bookid"];
 
                 $data['book'] = $this->model('BookModel')->getAllBook();
                 $array = array_column($data['book'], "title");
@@ -127,7 +128,7 @@ class Admin extends Controller {
 
                 // Cek title udah ada atau belum, atau emang gk diganti
                 if(!in_array($newTitle, $array, true) || $newTitle === $oldTitle){
-                    $this->model('BookModel')->updateBook($oldTitle, $newTitle, $selectedAuthor, $newContent, $selectedCategory);
+                    $this->model('BookModel')->updateBook($bookid, $newTitle, $selectedAuthor, $newContent, $selectedCategory);
                 }
 
                 $data['book'] = $this->model('BookModel')->getAllBookAdmin();
@@ -143,11 +144,11 @@ class Admin extends Controller {
         if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin') {
             if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                 // Dapetin nilai dari input formnye ya
+                $oldUsername = $_POST['username'];
                 $newUsername = $_POST['newUsername'];
                 $newPassword = $_POST['newPassword'];
-                $userId = $_POST['userid'];
+                $userId = $_POST['userId'];
                 $data['user'] = $this->model('UserModel')->getAllUser();
-
                 // Bikin kolom array list username buat pengecekkan
                 $array = array_column($data['user'], "username");
 
@@ -155,8 +156,8 @@ class Admin extends Controller {
                 if(empty($newUsername) || empty($newPassword)){
                     $this->view('admin/useradmin', $data);
                 }
-                // Janlups, cek dulu ada di db ato g ye gan
-                if (!in_array($newUsername, $array, true)){
+                // Cek ada di db ato g + cek kalo usernamenya gk di edit, berarti password doang
+                if (!in_array($newUsername, $array, true) || $newUsername === $oldUsername){
                    $this->model('UserModel')->updateUser($userId, $newUsername, $newPassword);
                 }
 
diff --git a/app/models/AuthorModel.php b/app/models/AuthorModel.php
index 44774dd..f259633 100644
--- a/app/models/AuthorModel.php
+++ b/app/models/AuthorModel.php
@@ -21,10 +21,10 @@ class AuthorModel
         $this->database->bind('newAuthorName', $newAuthorName);
         $this->database->execute();
     }
-    public function updateAuthor($currentUsername, $newUsername){
-        $this->database->query('UPDATE ' . $this->table . ' SET name = :newName WHERE name = :oldName');
+    public function updateAuthor($authorid, $newUsername){
+        $this->database->query('UPDATE ' . $this->table . ' SET name = :newName WHERE author.id = :authorid');
         $this->database->bind('newName', $newUsername);
-        $this->database->bind('oldName', $currentUsername);
+        $this->database->bind('authorid', $authorid);
         $this->database->execute();
     }
     public function deleteAuthor($authorName){
diff --git a/app/models/BookModel.php b/app/models/BookModel.php
index 91a9146..835e96e 100644
--- a/app/models/BookModel.php
+++ b/app/models/BookModel.php
@@ -51,9 +51,9 @@ class BookModel
         $this->database->bind('pickedCategory', $pickedCategory);
         $this->database->execute();
     }
-    public function updateBook($currentTitle, $newTitle, $selectedAuthor, $newContent, $selectedCategory){
-        $this->database->query('UPDATE book SET title = :newTitle, content = :newContent, author_id = (SELECT id FROM author WHERE author.name = :selectedAuthor), category = :newCategory WHERE title = :currentTitle');
-        $this->database->bind('currentTitle', $currentTitle);
+    public function updateBook($bookid, $newTitle, $selectedAuthor, $newContent, $selectedCategory){
+        $this->database->query('UPDATE book SET title = :newTitle, content = :newContent, author_id = (SELECT id FROM author WHERE author.name = :selectedAuthor), category = :newCategory WHERE book.id = :bookid');
+        $this->database->bind('bookid', $bookid);
         $this->database->bind('newTitle', $newTitle);
         $this->database->bind('newContent', $newContent);
         $this->database->bind('selectedAuthor', $selectedAuthor);
diff --git a/app/views/admin/bookadmin.php b/app/views/admin/bookadmin.php
index b823bd9..8d33f0a 100644
--- a/app/views/admin/bookadmin.php
+++ b/app/views/admin/bookadmin.php
@@ -19,7 +19,7 @@
         
         <div class="top-section">
             <h1 class="title">Book</h1>
-            <button class="add-btn" onclick="openAddPopup()">Add Book</button>
+            <button class="add-btn" onclick="addBook()">Add Book</button>
         </div>
         
         <div class="overlay" id="overlay"></div>
@@ -52,7 +52,7 @@
                             <td><?php echo $category;?></td>
                             <td><?php echo $author_name;?></td>
                             <td>
-                                <button class="editbook" onclick="editBook('<?php echo $title; ?>', '<?php echo $category; ?>', '<?php echo $author_name; ?>', '<?php echo $content; ?>')">
+                                <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()>
@@ -62,8 +62,8 @@
 
                             <div id="editpopup" class="edit-popup">
                                 <form id="editForm" method="post" action='<?php echo BASEURL; ?>/admin/editBook'>
-                                <input type="hidden" name="oldTitle" value="<?php echo $title; ?>">
-                                <input type="hidden" name="bookid" value="<?php echo $id; ?>">
+                                <input type="hidden" id = "bookid" name="bookid" value="<?php echo $id; ?>">
+                                <input type="hidden" id = "oldtitle" name="oldTitle" value="<?php echo $id; ?>">
                                     <div class="horizontal-component">
                                         <div class="title-input">
                                             <input type="text" id='title' name="newTitle" placeholder="Enter new title..." class="input-text">
@@ -114,7 +114,7 @@
         <div class="add-popup" id="addpopup">
             <form id="addForm" method="post" action='<?php echo BASEURL; ?>/admin/addBook'>
                 <div class="horizontal-component">
-                    <div class="title-input" id="titleadd">
+                    <div class="title-input">
                         <input type="text" id='titleadd' name="addTitle" placeholder="Add title..." class="input-text">
                     </div>
                     <select class="author-input" id="authoradd" name="pickedAuthor">
diff --git a/app/views/admin/useradmin.php b/app/views/admin/useradmin.php
index 3ed1edd..39ae02b 100644
--- a/app/views/admin/useradmin.php
+++ b/app/views/admin/useradmin.php
@@ -39,20 +39,21 @@
                             $password = $row['password'];
                         ?>
                         <tr>
+                        
                             <td><?php echo $id; ?></td>
                             <td><?php echo $username; ?></td>
                             <td>
-                            <button class="edituser" onclick="editUser('<?php echo $username; ?>', '<?php echo $password; ?>')">
+                            <button class="edituser" onclick="editUser('<?php echo $username; ?>', '<?php echo $password; ?>', '<?php echo $id; ?>')">
                                 Edit
                             </button>
                             <button class="deleteuser" onclick="deleteUser()">
                                 Delete
                             </button>
                             </td>
-                            <div class="edit-popup" id="editpopup">
+                            <div id="editpopup" class="edit-popup">
                                 <form id="editForm" method="post" action='<?php echo BASEURL; ?>/admin/editUser'>
-                                    <input type="hidden" name="username" value="<?php echo $username; ?>">
-                                    <input type="hidden" name="userid" value="<?php echo $id; ?>">
+                                    <input type="hidden" id="olduser" name="username">
+                                    <input type="hidden" id="userId" name="userId">
                                     <div class="add-input">
                                         <input type="text" id="username" name="newUsername" placeholder="Enter new username.." required>
                                     </div>
@@ -61,12 +62,10 @@
                                     </div>
                                 </form>
                                 <div class="add-submission">
-                                    <button type="submit" class="submit-button" onclick="submitForm()">Submit</button>
+                                    <button type="submit" class="submit-button" onclick="submitForm()">Update</button>
                                     <button class="cancel-button" onclick="closeEditPopup()">Cancel</button>
-                                    </div>
+                                </div>
                             </div>
-        
-
                             <div class="delete-popup" id="deletepopup">
                                 <p>
                                     Are you sure want to delete this user?
@@ -81,7 +80,6 @@
                 </tbody>
             </table>
         </div>
-        
             <div class="add-popup" id="addpopup">
                 <form id="addForm" method="post" action='<?php echo BASEURL; ?>/admin/addUser'>
                     <div class="add-input">
diff --git a/public/js/authoradmin.js b/public/js/authoradmin.js
index 861ef8c..32a2b79 100644
--- a/public/js/authoradmin.js
+++ b/public/js/authoradmin.js
@@ -51,8 +51,6 @@ function deleteUser(){
 function closeEditPopup(){
     editpopup.classList.remove("open-edit-popup");
     ol.classList.remove("open-overlay");
-    user.value ='';
-    pw.value ='';
 }
 function closeDeletePopup(){
     deletepopup.classList.remove("open-delete-popup");
diff --git a/public/js/bookadmin.js b/public/js/bookadmin.js
index ddc0f20..2b4b46e 100644
--- a/public/js/bookadmin.js
+++ b/public/js/bookadmin.js
@@ -1,18 +1,18 @@
 //Add Book
 
-let addpopup = document.getElementById("addpopup");
+const addpopup = document.getElementById("addpopup");
 const title = document.getElementById("title");
 const content = document.getElementById("content");
 const author = document.getElementById("author");
 const category =document.getElementById("category");
 const titleadd = document.getElementById("titleadd");
 const categoryadd = document.getElementById("categoryadd");
+const idbook = document.getElementById("bookid");
+const titleold = document.getElementById("oldtitle");
 let ol = document.getElementById("overlay");
 
 
-function openAddPopup(){
-    titleadd='';
-    categoryadd='';
+function addBook(){
     addpopup.classList.add("open-add-popup");
     ol.classList.add("open-overlay");
 }
@@ -30,11 +30,13 @@ function closeAddPopup(){
 let editpopup = document.getElementById("editpopup");
 let deletepopup = document.getElementById("deletebook");
 
-function editBook(booktitle, bookcategory, bookauthor, bookcontent){
+function editBook(booktitle, bookcategory, bookauthor, bookcontent, bookid){
+    titleold.value = booktitle;
     title.value = booktitle;
     category.value = bookcategory;
     author.value = bookauthor;
     content.value = bookcontent;
+    idbook.value = bookid;
 
 
     editpopup.classList.add("open-edit-popup");
diff --git a/public/js/useradmin.js b/public/js/useradmin.js
index d6c0412..5ebf3c1 100644
--- a/public/js/useradmin.js
+++ b/public/js/useradmin.js
@@ -1,9 +1,8 @@
 let addpopup = document.getElementById("addpopup")
 let ol =document.getElementById("overlay")
-const user = document.getElementById("username")
-const pw = document.getElementById("password")
+const editpopup = document.getElementById("editpopup");
+
 
-let editpopup = document.getElementById("editpopup")
 
 let deletepopup = document.getElementById("deletepopup")
 
@@ -19,9 +18,15 @@ function closeAddPopup(){
     pw.value ='';
 }
 
-function editUser(username, password){
+function editUser(username, password, userid){
+    const user = document.getElementById("username");
+    const pw = document.getElementById("password");
+    const iduser = document.getElementById("userId");
+    const olduser = document.getElementById("olduser");
+    olduser.value = username;
     user.value = username;
     pw.value = password;
+    iduser.value = userid;
 
     editpopup.classList.add("open-edit-popup");
     ol.classList.add("open-overlay");
@@ -48,8 +53,6 @@ function deleteUser(){
 function closeEditPopup(){
     editpopup.classList.remove("open-edit-popup");
     ol.classList.remove("open-overlay");
-    user.value ='';
-    pw.value ='';
 }
 function closeDeletePopup(){
     deletepopup.classList.remove("open-delete-popup");
-- 
GitLab