-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin_category.php
executable file
·126 lines (109 loc) · 4.1 KB
/
admin_category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
//Gọi cấu hình, thư viện được sử dụng
include 'libs/config.php';
include 'libs/functions.php';
//Khai báo các class sử dụng
use libs\classes\DBAccess;
use libs\classes\FlashMessage;
use libs\classes\DBPagination;
use libs\classes\HttpException;
//Kiểm tra đăng nhập, chưa đăng nhập thì chuyển đến trang đăng nhập
if(!checkAuthentication()) {
header("Location: admin_login.php");
exit;
}
//Tạo các đối tượng cần dùng
$oFlashMessage = new FlashMessage();
$oDBAccess = new DBAccess();
//Khai báo tiêu đề và module cho page
$pageAliasName = 'category';
$pageTitle = 'Quản lý Danh mục';
//Kiểm tra xem biến keyword có trên đường link hay không
$keyword = '';
if(isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
}
//Tạo ra câu điều kiện theo keyword
$where = '';
if(!empty($keyword)) {
$where = "WHERE title LIKE '%$keyword%' OR slug LIKE '%$keyword%'";
}
//Thực hiện xử lý xóa bản ghi
if(isset($_GET['action']) && isset($_GET['id']) && $_GET['action']=='delete'){
try {
$id = $_GET['id'];
$oDBAccess->deleteById('category', $id);
$oFlashMessage->setFlashMessage('success', 'Đã xóa bản ghi có id là '.$id);
} catch (HttpException $e) {
$oFlashMessage->setFlashMessage('error', "Không thể xóa bản ghi có id $id được.");
}
header("Location: admin_$pageAliasName.php");
exit;
}
//Thực hiện thay đổi trạng thái bản ghi
if(isset($_GET['action']) && isset($_GET['id']) && $_GET['action']=='active'){
try {
$id = $_GET['id'];
$record = $oDBAccess->findOneById('category', $id);
$attributes['id'] = $id;
$attributes['is_active'] = ($record->is_active == 0)?1:0;
$attributes['updated_at'] = date('Y-m-d H:i:s');
$record = $oDBAccess->save('category', $attributes, 'id');
$oFlashMessage->setFlashMessage('success', 'Cập nhật bản ghi có id là '.$id);
} catch (HttpException $e) {
$oFlashMessage->setFlashMessage('error', "Không thể cập nhật bản ghi có id $id được.");
}
header("Location: admin_$pageAliasName.php");
exit;
}
//Lấy ra tổng số bản ghi
$totalRecord = intval($oDBAccess->scalarBySQL("SELECT COUNT(*) FROM category ".$where));
//Tạo ra đối tượng phân trang
$oDBPagination = new DBPagination($totalRecord, 10);
//Lấy ra danh sách các bản ghi
$list = $oDBAccess->findAllBySql("SELECT * FROM category $where ORDER BY created_at DESC {$oDBPagination->getLimit()}");
?>
<?php include 'libs/includes/admin/header.inc.php'; ?>
<h2 id="pageTitle"><?= $pageTitle ?></h2>
<p><a href="admin_<?= $pageAliasName ?>_form.php">Thêm mới</a></p>
<?php include "libs/includes/admin/flash_message.inc.php"; ?>
<?php if(!empty($list)): ?>
<div class="search-box m-b-10">
<form action="" method="GET">
<a href="admin_<?= $pageAliasName ?>.php"><img src="img/admin/refresh.png" class="v-middle"/></a>
<input type="image" src="img/admin/search.png" class="v-middle"/>
<input type="text" name="keyword" value="<?= $keyword ?>" placeholder="Từ khóa..." required aria-required="true" minlength="3" maxlength="255"/>
</form>
</div><!-- /.search-box -->
<table>
<thead>
<tr>
<th>#</th>
<th>Tiêu đề</th>
<th>Slug</th>
<th>Trạng thái</th>
<th>Thao tác</th>
</tr>
</thead>
<tbody>
<?php foreach ($list as $item): ?>
<tr>
<td><?= $item->id ?></td>
<td><a href="admin_<?= $pageAliasName ?>_form.php?id=<?= $item->id ?>"><?= $item->title ?></a></td>
<td><?= $item->slug ?></td>
<td class="text-center"><?= renderActive($item, 'admin_'.$pageAliasName.'.php') ?></td>
<td class="text-center">
<a href="admin_<?= $pageAliasName ?>_form.php?id=<?= $item->id ?>"><img src="img/admin/edit.png"/></a>
<a href="admin_<?= $pageAliasName ?>.php?action=delete&id=<?= $item->id ?>" class="btn-delete"><img src="img/admin/trash.png"/></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php if($oDBPagination->getMaxPage() > 1): ?>
<div class="pagination-link">
<?= $oDBPagination->renderPagination('admin_'.$pageAliasName.'.php') ?>
</div>
<?php endif; ?>
<?php include 'libs/includes/admin/footer.inc.php'; ?>