From 48675d877c9c543850817db28eefe293b59ded6c Mon Sep 17 00:00:00 2001 From: Naufal-Nalendra <16521240@std.stei.itb.ac.id> Date: Sun, 8 Oct 2023 15:40:39 +0700 Subject: [PATCH] feat: delete author with constraint fail --- app/controllers/admin.php | 12 +++ app/models/AuthorModel.php | 15 ++- app/views/admin/authoradmin.php | 169 +++++++++++++++++--------------- mysql/#innodb_redo/#ib_redo9 | Bin 3276800 -> 3276800 bytes public/js/authoradmin.js | 4 + 5 files changed, 114 insertions(+), 86 deletions(-) diff --git a/app/controllers/admin.php b/app/controllers/admin.php index e7e1085..a35a3a2 100644 --- a/app/controllers/admin.php +++ b/app/controllers/admin.php @@ -130,5 +130,17 @@ class Admin extends Controller { $this->view('login/login'); } } + 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); + } + $data['authors'] = $this->model('AuthorModel')->getAllAuthor(); + $this->view('admin/authoradmin', $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 e2bff15..44774dd 100644 --- a/app/models/AuthorModel.php +++ b/app/models/AuthorModel.php @@ -8,11 +8,6 @@ class AuthorModel { $this->database = new Database; } - public function insertAuthor($newAuthorName){ - $this->database->query('INSERT IGNORE INTO author (id, name) VALUES (NULL, :newAuthorName)'); - $this->database->bind('newAuthorName', $newAuthorName); - $this->database->execute(); - } public function getAllAuthor(){ $this->database->query('SELECT * FROM author'); return $this->database->resultSet(); @@ -21,10 +16,20 @@ class AuthorModel $this->database->query('SELECT name FROM author'); return $this->database->resultSet(); } + public function insertAuthor($newAuthorName){ + $this->database->query('INSERT IGNORE INTO author (id, name) VALUES (NULL, :newAuthorName)'); + $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'); $this->database->bind('newName', $newUsername); $this->database->bind('oldName', $currentUsername); $this->database->execute(); } + public function deleteAuthor($authorName){ + $this->database->query('DELETE FROM ' . $this->table . ' WHERE name = :name'); + $this->database->bind('name', $authorName); + $this->database->execute(); + } } \ No newline at end of file diff --git a/app/views/admin/authoradmin.php b/app/views/admin/authoradmin.php index 2283b6c..ee2d10b 100644 --- a/app/views/admin/authoradmin.php +++ b/app/views/admin/authoradmin.php @@ -1,95 +1,102 @@ <!DOCTYPE html> - <head> - <meta charset="UTF-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta name = "viewport" content="width=device-width, initial-scale=1.0"> - <title> Admin Page </title> - <link rel ="stylesheet" href="<?php echo BASEURL; ?>/style/sidebar.css"> - <link rel ="stylesheet" href="<?php echo BASEURL; ?>/style/authoradmin.css"> - <link href='https://fonts.googleapis.com/css?family=Italianno' rel='stylesheet'> - <link href='https://fonts.googleapis.com/css?family=Hanuman' rel='stylesheet'> - <link href='https://unpkg.com/boxicons@2.0.9/css/boxicons.min.css' rel='stylesheet'> - </head> - <body> - <?php - include(__DIR__ . '/sidebar.php'); - ?> - - <div class="top-section"> - <h1 class="title">Authors</h1> - <button class="add-btn" onclick="openAddPopup()">Add User</button> - </div> - - <div class="overlay" id="overlay"></div> - <div style="overflow-x: auto;"> - <table id="users"> - <!-- Judul-judul kolom --> - <thead> + +<head> + <meta charset="UTF-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title> Admin Page </title> + <link rel="stylesheet" href="<?php echo BASEURL; ?>/style/sidebar.css"> + <link rel="stylesheet" href="<?php echo BASEURL; ?>/style/authoradmin.css"> + <link href='https://fonts.googleapis.com/css?family=Italianno' rel='stylesheet'> + <link href='https://fonts.googleapis.com/css?family=Hanuman' rel='stylesheet'> + <link href='https://unpkg.com/boxicons@2.0.9/css/boxicons.min.css' rel='stylesheet'> +</head> + +<body> + <?php + include(__DIR__ . '/sidebar.php'); + ?> + + <div class="top-section"> + <h1 class="title">Authors</h1> + <button class="add-btn" onclick="openAddPopup()">Add User</button> + </div> + + <div class="overlay" id="overlay"></div> + <div style="overflow-x: auto;"> + <table id="users"> + <!-- Judul-judul kolom --> + <thead> + <tr> + <th>ID</th> + <th>Name</th> + <th>Action</th> + </tr> + </thead> + <tbody> + <?php foreach ($data['authors'] as $row) : ?> + <?php + $id = $row['id']; + $name = $row['name']; + + ?> <tr> - <th>ID</th> - <th>Name</th> - <th>Action</th> - </tr> - </thead> - <tbody> - <?php foreach ($data['authors'] as $row): ?> - <?php - $id = $row['id']; - $name = $row['name']; - - ?> - <tr> - <td><?php echo $id; ?></td> - <td><?php echo $name; ?></td> - <td> + <td><?php echo $id; ?></td> + <td><?php echo $name; ?></td> + <td> <button class="edituser" onclick="editUser('<?php echo $name; ?>')"> Edit </button> <button class="deleteuser" onclick="deleteUser()"> 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; ?>"> - <div class="add-input"> - <input type="text" id="username" name="newAuthorName" placeholder="Enter new username.." required> - </div> - </form> - <div class="add-submission"> - <button type="submit" class="submit-button" onclick="submitForm()">Submit</button> - <button class="cancel-button" onclick="closeEditPopup()">Cancel</button> - </div> + </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; ?>"> + <div class="add-input"> + <input type="text" id="username" name="newAuthorName" placeholder="Enter new username.." required> + </div> + </form> + <div class="add-submission"> + <button type="submit" class="submit-button" onclick="submitForm()">Submit</button> + <button class="cancel-button" onclick="closeEditPopup()">Cancel</button> </div> - + </div> - <div class="delete-popup" id="deletepopup"> - <p> - Are you sure want to delete this user? - </p> - <div class="add-submission"> - <button class="submit-button">Delete</button> - <button class="cancel-button" onclick=closeDeletePopup()>Cancel</button> - </div> + + <div class="delete-popup" id="deletepopup"> + <p> + 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; ?>"> + </form> + <div class="add-submission"> + <button class="submit-button" onclick="deleteForm()">Delete</button> + <button class="cancel-button" onclick=closeDeletePopup()>Cancel</button> </div> - </tr> - <?php endforeach ?> - </tbody> - </table> - </div> - - <div class="add-popup" id="addpopup"> - <form id="addForm" method="post" action='<?php echo BASEURL; ?>/admin/addAuthor'> - <div class="add-input"> - <input type="text" id="username" name="newAuthorName" placeholder="Enter new username.." required> - </div> - </form> - <div class="add-submission"> - <button type="submit" class="submit-button" onclick="addForm()">Submit</button> - <button class="cancel-button" onclick="closeAddPopup()">Cancel</button> - </div> + </div> + + </tr> + <?php endforeach ?> + </tbody> + </table> + </div> + + <div class="add-popup" id="addpopup"> + <form id="addForm" method="post" action='<?php echo BASEURL; ?>/admin/addAuthor'> + <div class="add-input"> + <input type="text" id="username" name="newAuthorName" placeholder="Enter new username.." required> </div> + </form> + <div class="add-submission"> + <button type="submit" class="submit-button" onclick="addForm()">Submit</button> + <button class="cancel-button" onclick="closeAddPopup()">Cancel</button> + </div> + </div> + + <script src="<?php echo BASEURL; ?>/js/authoradmin.js"></script> +</body> - <script src="<?php echo BASEURL; ?>/js/authoradmin.js"></script> - </body> </html> \ No newline at end of file diff --git a/mysql/#innodb_redo/#ib_redo9 b/mysql/#innodb_redo/#ib_redo9 index 00b0647c4097e84234df80c55cf00055d2bd4aaa..c6b73588fa4e6f29862345cad2321e9998eb5e24 100644 GIT binary patch delta 196 zcmXBIyHNsB06<aRvcg3H0YUjF_ybYCHpEy5(GJ0F!48O-F`xw_N}z%b3Cx5Uf|Hyi z`3=d*rW9q<%>Pe_(-80LyTdjths7ukY3A#k-<w5mg#?KtE3sTiPV#d4f?Ud#6s06( zxt5Alr6xC0mxkQRoiwE-ZRyCpJV;l1@+eR8EHCmZeHqA`4CP%uWF%vm$W&%BU-S8Q E1jz$3TmS$7 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= diff --git a/public/js/authoradmin.js b/public/js/authoradmin.js index 8dcf57e..861ef8c 100644 --- a/public/js/authoradmin.js +++ b/public/js/authoradmin.js @@ -37,6 +37,10 @@ function addForm(){ const form = document.getElementById("addForm"); form.submit(); } +function deleteForm(){ + const form = document.getElementById("deleteForm"); + form.submit(); +} function deleteUser(){ deletepopup.classList.add("open-delete-popup"); -- GitLab