From 6be44eaff9c57c08d45e623137c24522c5f7270f Mon Sep 17 00:00:00 2001
From: Naufal-Nalendra <16521240@std.stei.itb.ac.id>
Date: Sun, 8 Oct 2023 15:25:16 +0700
Subject: [PATCH] feat: edit author for author admin

---
 app/controllers/admin.php       |  21 ++++++++++-----------
 app/models/AuthorModel.php      |   6 ++++++
 app/views/admin/authoradmin.php |   4 ++--
 mysql/#innodb_redo/#ib_redo9    | Bin 3276800 -> 3276800 bytes
 public/js/authoradmin.js        |   3 +--
 5 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/app/controllers/admin.php b/app/controllers/admin.php
index 60bfca6..e7e1085 100644
--- a/app/controllers/admin.php
+++ b/app/controllers/admin.php
@@ -111,22 +111,21 @@ class Admin extends Controller {
         if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin') {
             if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                 // Get values from the input form
-                $newAuthorName = $_POST['newAuthorName']; // Assuming you have an input field for author name
-                // Additional author-related fields can be added here
-                $oldAuthorName = $_POST['authorName']; // Assuming you have a hidden input for the old author name
-    
-                // Check if the new author name doesn't already exist in the database
-                $data['authors'] = $this->model('AuthorModel')->getAllAuthors();
+                $newAuthorName = $_POST['newAuthorName']; 
+
+                $oldAuthorName = $_POST['authorName']; 
+
+                $data['authors'] = $this->model('AuthorModel')->getAllAuthor();
                 $authorNames = array_column($data['authors'], "author_name");
     
                 if (!in_array($newAuthorName, $authorNames, true)){
-                    // Update the author's information in the database using your AuthorModel
-                    $this->model('AuthorModel')->updateAuthor($oldAuthorName, $newAuthorName); // Adjust this according to your model's method
+                    
+                    $this->model('AuthorModel')->updateAuthor($oldAuthorName, $newAuthorName); 
                 }
             }
-            // Fetch the list of authors (optional)
-            $data['authors'] = $this->model('AuthorModel')->getAllAuthors();
-            $this->view('admin/authoradmin', $data); // Adjust the view file and path accordingly
+
+            $data['authors'] = $this->model('AuthorModel')->getAllAuthor();
+            $this->view('admin/authoradmin', $data); 
         } else {
             $this->view('login/login');
         }
diff --git a/app/models/AuthorModel.php b/app/models/AuthorModel.php
index 8684fd6..e2bff15 100644
--- a/app/models/AuthorModel.php
+++ b/app/models/AuthorModel.php
@@ -21,4 +21,10 @@ class AuthorModel
         $this->database->query('SELECT name FROM author');
         return $this->database->resultSet();
     }
+    public function updateAuthor($currentUsername, $newUsername){
+        $this->database->query('UPDATE ' . $this->table . ' SET name = :newName WHERE name = :oldName');
+        $this->database->bind('newName', $newUsername);
+        $this->database->bind('oldName', $currentUsername);
+        $this->database->execute();
+    }
 }
\ No newline at end of file
diff --git a/app/views/admin/authoradmin.php b/app/views/admin/authoradmin.php
index 280a628..2283b6c 100644
--- a/app/views/admin/authoradmin.php
+++ b/app/views/admin/authoradmin.php
@@ -32,7 +32,7 @@
                     </tr>
                 </thead>
                 <tbody>
-                    <?php foreach ($data['user'] as $row): ?>
+                    <?php foreach ($data['authors'] as $row): ?>
                         <?php
                             $id = $row['id'];
                             $name = $row['name'];
@@ -51,7 +51,7 @@
                             </td>
                             <div class="edit-popup" id="editpopup">
                                 <form id="editForm" method="post" action='<?php echo BASEURL; ?>/admin/editAuthor'>
-                                    <input type="hidden" name="username" value="<?php echo $name; ?>">
+                                    <input type="hidden" name="authorName" value="<?php echo $name; ?>">
                                     <div class="add-input">
                                         <input type="text" id="username" name="newAuthorName" placeholder="Enter new username.." required>
                                     </div>
diff --git a/mysql/#innodb_redo/#ib_redo9 b/mysql/#innodb_redo/#ib_redo9
index 6669a0938a75cba049a46e91ee04e95773f80369..00b0647c4097e84234df80c55cf00055d2bd4aaa 100644
GIT binary patch
delta 196
zcmXBIIZgs$0D#f_!yuomF03lz3d0VGcQP@00ji0`cNA+93a&uqEwq7(%2E=F*Ss{{
zg!EupiZa`e)+)ri5P#R-Ue{rf6r(t7-xtUH+5f(LB0(a_NGw@7k(?a<RPs`gqLid8
z6*-f0xsXe#N=@q0kfyZcO0MNb+Hxx$xs!W&kVkovXX#2$66wo8hBA_|Ok^svFXn&$
D&R#N=

delta 199
zcmWN_J5B;o06@{anE|f|eo*|NAc!a`@>z;;4fNC|Bom6e0umc-tsxe6&|1;C3Bm#>
zr8vz=)8CM8O$$-H`WSqNWF3;>eDV4d-dg!64y(`im%rw4)*FRnG+E}xNRUWkNhBj#
z*^%w<N>279F9q3`138o<Dax^&NJ&oROwQ#(%5o`JQjw}$OHJx>BMrHgrrgQBwB$h^
Q<w@G|EFI}e?~DHCA1cQ*Qvd(}

diff --git a/public/js/authoradmin.js b/public/js/authoradmin.js
index d6c0412..8dcf57e 100644
--- a/public/js/authoradmin.js
+++ b/public/js/authoradmin.js
@@ -19,9 +19,8 @@ function closeAddPopup(){
     pw.value ='';
 }
 
-function editUser(username, password){
+function editUser(username){
     user.value = username;
-    pw.value = password;
 
     editpopup.classList.add("open-edit-popup");
     ol.classList.add("open-overlay");
-- 
GitLab