10000 Add Edit delte method · zino-coder/mvc-sample@765e690 · GitHub
[go: up one dir, main page]

Skip to content

Commit 765e690

Browse files
committed
Add Edit delte method
1 parent 664fc13 commit 765e690

File tree

5 files changed

+117
-25
lines changed

5 files changed

+117
-25
lines changed

Controllers/TaskController.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,32 @@ public function store() {
4141

4242
return header('Location: /task/index');
4343
}
44+
45+
public function edit($id) {
46+
$task = $this->model->getTaskById($id);
47+
$taskStatus = $this->model->getAllTaskStatus();
48+
49+
return $this->render('create', ['task' => $task, 'taskStatus' => $taskStatus]);
50+
}
51+
52+
public function update($id) {
53+
$task = $this->model->update([
54+
'title' => $_POST['title'],
55+
'description' => $_POST['description'],
56+
'status' => $_POST['status'],
57+
'id' => $id,
58+
]);
59+
60+
$_SESSION['success'] = 'Sửa task thành công';
61+
62+
return header('Location: /task/index');
63+
}
64+
65+
public function delete($id) {
66+
$task = $this->model->deleteTaskById($id);
67+
68+
$_SESSION['success'] = 'Xoá task thành công';
69+
70+
return header('Location: /task/index');
71+
}
4472
}

Models/Task.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class Task extends Connection
44
{
5-
private $perPage = 1;
5+
private $perPage = 3;
66

77
public function getLastPage() {
88
$query = $this->getBdd()->query('SELECT COUNT(*) AS total FROM tasks');
@@ -15,9 +15,8 @@ public function getLastPage() {
1515
public function getAllTask($page)
1616
{
1717
$offset = ($page - 1) * $this->perPage;
18-
var_dump($this->perPage);
1918
$sql = '
20-
SELECT tasks.*,
19+
SELECT tasks.*, ROW_NUMBER() OVER (ORDER BY id) AS row_id,
2120
CASE
2221
WHEN ts.id = 1 THEN CONCAT(\'<span class="btn btn-outline-primary disabled">\', ts.name, \'</span>\')
2322
WHEN ts.id = 2 THEN CONCAT(\'<span class="btn btn-outline-primary disabled">\', ts.name, \'</span>\')
@@ -55,4 +54,36 @@ public function create($params = []) {
5554

5655
return $query->fetch();
5756
}
57+
58+
public function getTaskById($id) {
59+
$query = $this->getBdd()->prepare('SELECT * from tasks where id = :id');
60+
61+
$query->execute([
62+
'id' => $id
63+
]);
64+
65+
return $query->fetch();
66+
}
67+
68+
public function update($params = []) {
69+
$query = $this->getBdd()->prepare('UPDATE tasks SET title=:title, description=:description, status=:status where id =:id');
70+
$query->execute([
71+
'title' => $params['title'],
72+
'description' => $params['description'],
73+
'status' => $params['status'],
74+
'id' => $params['id'],
75+
]);
76+
77+
return $query->fetch();
78+
}
79+
80+
public function deleteTaskById($id) {
81+
$query = $this->getBdd()->prepare('DELETE FROM tasks WHERE id = :id');
82+
83+
$query->execute([
84+
'id' => $id,
85+
]);
86+
87+
return true;
88+
}
5889
}

view/layout/layout.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<meta name="viewport" content="width=device-width, initial-scale=1.0">
67
<link rel="stylesheet" href="/public/assets/lib/bootstrap/css/bootstrap.css">
78
<link rel="stylesheet" href="/public/assets/lib/fontawesome/css/all.css">
9+
<script src="https://code.jquery.com/jquery-3.7.0.js" integrity="sha256-JlqSTELeR4TLqP0OG9dxM7yDPqX1ox/HfgiSLBj8+kM=" crossorigin="anonymous"></script>
810
<script src="/public/assets/lib/bootstrap/js/bootstrap.bundle.js"></script>
911
<script src="/public/assets/lib/fontawesome/js/all.js"></script>
1012
<title>Layout MVC</title>
1113
</head>
14+
1215
<body>
1316
<div class="container">
1417
<div class="row">
1518
<?php echo $screen ?>
1619
</div>
1720
</div>
1821
</body>
22+
1923
</html>

view/task/create.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<div class="col-12">
2-
<form action="/task/store" method="post">
2+
<form action="<?= isset($task) ? '/task/update/' . $task->id : '/task/store' ?>" method="post">
33
<div class="card">
44
<div class="card-header">
5-
<h2 class="card-title float-start">Create new Task</h2>
5+
<h2 class="card-title float-start"><?= isset($task) ? 'Edit Task' : 'Create new Task' ?></h2>
66
</div>
77
<div class="card-body">
88
<div class="mb-3">
99
<label for="title" class="form-label">Task Title</label>
10-
<input type="text" name="title" class="form-control" id="title">
10+
<input type="text" name="title" value="<?= $task->title ?? '' ?>" class="form-control" id="title">
1111
</div>
1212
<div class="mb-3">
1313
<label for="description" class="form-label">Task Description</label>
14-
<textarea class="form-control" name="description" id="description" rows="3"></textarea>
14+
<textarea class="form-control" name="description" id="description" rows="3"><?= $task->description ?? '' ?></textarea>
1515
</div>
1616
<label for="status" class="form-label">Status</label>
1717
<select class="form-select" name="status" id="status">
1818
<?php foreach ($taskStatus as $status): ?>
19-
<option class="text-<?php echo $this->statusColor[$status->id] ?>" value="<?php echo $status->id ?>"><?php echo $status->name ?></option>
19+
<option class="text-<?php echo $this->statusColor[$status->id] ?>" value="<?php echo $status->id ?>" <?= $task->status == $status->id ? 'selected' : '' ?>><?php echo $status->name ?></option>
2020
<?php endforeach; ?>
2121
</select>
2222
</div>

view/task/index.php

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@
2626
<?php $i = 0;
2727
foreach ($tasks as $task) : ?>
2828
<tr>
29-
<th scope="row"><?php echo ++$i ?></th>
29+
<th scope="row"><?php echo $task->row_id ?></th>
3030
<td><?php echo $task->title; ?></td>
3131
<td><?php echo $task->description ?></td>
3232
<td><?php echo $task->statusName ?></td>
3333
<td>
3434
<a href="#" class="btn btn-outline-success">
3535
<i class="fa-solid fa-eye"></i>
3636
</a>
37-
<a href="#" class="btn btn-outline-primary">
37+
<a href="<?= '/task/edit/' . $task->id ?>" class="btn btn-outline-primary">
3838
<i class="fa-solid fa-pen"></i>
3939
</a>
40-
<a href="#" class="btn btn-outline-danger">
40+
<a href="<?= '/task/delete/' . $task->id ?>" class="btn btn-outline-danger btn-delete" data-bs-toggle="modal" data-bs-target="#exampleModal">
4141
<i class="fa-solid fa-trash"></i>
4242
</a>
4343
</td>
@@ -53,25 +53,54 @@
5353
<nav aria-label="Page navigation">
5454
<ul class="pagination float-end">
5555
<?php if ($page != 1 || !isset($page)) : ?>
56-
<li class="page-item">
57-
<a class="page-link" href="<?= '/task/index/' . $page - 1 ?>" aria-label="Previous">
58-
<span aria-hidden="true">&laquo;</span>
59-
</a>
60-
</li>
56+
<li class="page-item">
57+
<a class="page-link" href="<?= '/task/index/' . $page - 1 ?>" aria-label="Previous">
58+
<span aria-hidden="true">&laquo;</span>
59+
</a>
60+
</li>
6161
<?php endif; ?>
62-
<?php for ($i = 1; $i <= $lastPage; $i++): ?>
63-
<li class="page-item <?= $page == $i ? 'active' : '' ?>"><a class="page-link" href="<?= '/task/index/' . $i ?>"><?= $i ?></a></li>
62+
<?php for ($i = 1; $i <= $lastPage; $i++) : ?>
63+
<li class="page-item <?= $page == $i ? 'active' : '' ?>"><a class="page-link" href="<?= '/task/index/' . $i ?>"><?= $i ?></a></li>
6464
<?php endfor; ?>
6565
<?php if ($page != $lastPage) : ?>
66-
<li class="page-item">
67-
<a class="page-link" href="<?= '/task/index/' . $page + 1 ?>" aria-label="Next">
68-
<span aria-hidden="true">&raquo;</span>
69-
</a>
70-
</li>
66+
<li class="page-item">
67+
<button type="button" class="page-link" href="<?= '/task/index/' . $page + 1 ?>" aria-label="Next">
68+
<span aria-hidden="true">&raquo;</span>
69+
</button>
70+
</li>
7171
<?php endif; ?>
72-
<?= $page ?>
7372
</ul>
7473
</nav>
7574
</div>
7675
</div>
77-
</div>
76+
</div>
77+
78+
<!-- Modal -->
79+
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
80+
<div class="modal-dialog">
81+
<div class="modal-content">
82+
<div class="modal-header">
83+
<h1 class="modal-title fs-5" id="exampleModalLabel">Xoá</h1>
84+
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
85+
</div>
86+
<div class="modal-body">
87+
Bạn có chắc chắn muốn xoá không?
88+
</div>
89+
<div class="modal-footer">
90+
<form action="" method="post" id="delete-form">
91+
<button type="submit" class="btn btn-primary">Có</button>
92+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">!Có</button>
93+
</form>
94+
</div>
95+
</div>
96+
</div>
97+
</div>
98+
99+
<script>
100+
$(document).ready(function () {
101+
$('.btn-delete').click(function() {
102+
const href = $(this).attr('href');
103+
$('#delete-form').attr('action', href);
104+
})
105+
});
106+
</script>

0 commit comments

Comments
 (0)
0