You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wastesortingserver/admin/WasteSorting.php

297 lines
12 KiB

5 years ago
<?php
5 years ago
require_once $_SERVER["DOCUMENT_ROOT"] . "/config.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/head.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/Query.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/Log.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/admin/Alert.php";
5 years ago
getMenu("题目管理");
if (empty($_REQUEST)) {
5 years ago
echo <<<EOF
<!DOCTYPE html>
<html lang="zh">
<script>
$(function() {
$('[data-toggle="tooltip"]').tooltip();
Array.prototype.filter.call($("form.needs-validation"), function(form) {
form.addEventListener('submit', function(event) {
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
});
</script>
<body>
<ul class="nav nav-pills nav-justified">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#list" role="tab">垃圾列表</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#add" role="tab">添加垃圾</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#" onclick="location.href='/'" role="tab">返回首页</a>
</li>
</ul>
<div class="container py-5">
<div class="tab-content" id="v-pills-tabContent">
<div class="tab-pane fade show active" id="list" role="tabpanel" aria-labelledby="v-pills-home-tab">
<div class="list-group d-flex justify-content-center align-items-center w-100">
EOF;
$count_query = new CountGrabage();
// 查询垃圾分类数据量
try {
DbUtil::query("select category,count(*) from garbage group by category order by category", $count_query);
} catch (Exception $e) {
error_res("系统出现严重异常,请联系管理员", "/");
return;
}
5 years ago
if (empty($count_query->getResult())) {
warn_res_with_click("暂无垃圾分类数据,请添加", <<<EOF
$('a[href="#add"]').click()
EOF
);
} else {
foreach ($count_query->getResult() as $item) {
$category = $item["category"];
$category_name = $item["category_name"];
$count = $item["count"];
echo <<<EOF
<a class="list-group-item list-group-item-action d-flex align-items-center col-6 m-auto justify-content-between" href="/admin/WasteSorting.php?action=list&category=$category"
data-toggle="tooltip" data-placement="right" title="查看垃圾数据">
5 years ago
$category_name
<span class="badge badge-info badge-pill" >$count 条</span>
</a>
EOF;
}
}
echo <<<EOF
</div>
</div>
<div id="add" class="tab-pane fade">
<div class="d-flex justify-content-center align-items-center w-100">
<form class="col-6 needs-validation" novalidate method="post" action="WasteSorting.php">
<input type="hidden" name="action" value="add">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">垃圾名</span>
</div>
<input type="text" class="form-control" placeholder="垃圾名" name="name" required>
<div class="invalid-feedback">
垃圾名不为空
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">垃圾分类</span>
</div>
<select class="custom-select custom-select" name="category" required>
<option value="1">可回收垃圾</option>
<option value="2">有害垃圾</option>
<option value="4">湿垃圾</option>
<option value="8">干垃圾</option>
<option value="16">大件垃圾</option>
</select>
<div class="invalid-feedback">
请选择垃圾分类
</div>
</div>
<button class="btn btn-block btn-info">提交</button>
</form>
</div>
</div>
</body>
</html>
EOF;
} else if (isset($_POST["action"]) && $_POST["action"] == "add") {
if (empty($_POST["name"])) {
error_res("垃圾名不能为空!", $_SERVER["PHP_SELF"]);
5 years ago
} else if (empty($_POST["category"])) {
error_res("垃圾分类不能为空!", $_SERVER["PHP_SELF"]);
5 years ago
} else if (array_search((int)$_POST["category"], array(1, 2, 4, 8, 16)) === false) {
error_res("垃圾分类参数不合法", $_SERVER["PHP_SELF"]);
5 years ago
} else {
try {
DbUtil::insert("insert into garbage (name, category, create_at, update_at) values (?,?,?,?)", null, "siss", $_POST["name"], $_POST["category"], date(default_format), date(default_format));
info_res("保存成功", $_SERVER["PHP_SELF"]);
5 years ago
} catch (Exception $e) {
error($e);
error_res("保存操作异常,请联系管理员", $_SERVER["PHP_SELF"]);
5 years ago
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "list") {
if (empty($_GET["category"])) {
error_res("垃圾分类不能为空!", $_SERVER["PHP_SELF"]);
5 years ago
} else if (array_search((int)$_GET["category"], array(1, 2, 4, 8, 16)) === false) {
error_res("垃圾分类参数不合法", $_SERVER["PHP_SELF"]);
5 years ago
} else {
// 分页查询
$page = (int)($_GET["page"] ?? 1);
$category_param = (int)$_GET["category"];
if (isset($_GET["name"])) {
$name_param = $_GET["name"];
} else {
$name_param = "";
}
5 years ago
$count_query = new QueryGarbageCount();
// 获取记录数
try {
if (empty($name_param)) {
DbUtil::query("select count(*) from garbage where category=?", $count_query, array($category_param));
} else {
DbUtil::query("select count(*) from garbage where category=? and name like ?", $count_query, array($category_param, "%" . $name_param . "%"));
}
} catch (Exception $e) {
error_res("系统出现严重异常,请联系管理员", "/");
return;
}
5 years ago
$count_result = $count_query->getCount();
$garbage_query = new QueryGarbage();
5 years ago
// 分页大小
$page_size = 10;
// 计算分页数
if ($count_result < $page_size) {
$max_page = 1;
} else if ($count_result % $page_size == 0) {
$max_page = $count_result / $page_size;
} else {
$max_page = (int)floor($count_result / $page_size) + 1;
}
try {
if (empty($name_param)) {
DbUtil::query("select * from garbage where category=? limit ?,?", $garbage_query, array($category_param, ($page - 1) * $page_size, $page_size));
} else {
DbUtil::query("select * from garbage where category=? and name like ? limit ?,?", $garbage_query, array($category_param, "%" . $name_param . "%", ($page - 1) * $page_size, $page_size));
}
} catch (Exception $e) {
error_res("系统出现严重异常,请联系管理员", "/");
return;
}
if (empty($garbage_query->getGarbageObjArray())) {
5 years ago
error("无法查询分类" . get_category($category_param) . "的第" . $page . "数据");
warn_res("查询结果为空", $_SERVER["PHP_SELF"] . "?action=list&category=" . $category_param);
return;
5 years ago
} else {
echo <<<EOF
<script>
<!--删除垃圾-->
function doDelete(name){
if(confirm("确认删除"+name+"?")){
EOF;
echo 'location.href="' . $_SERVER["PHP_SELF"] . '?action=delete&name="+name';
echo <<<EOF
}
}
</script>
5 years ago
<div class="container py-5">
<div class="d-flex justify-content-center align-items-center w-100">
EOF;
echo '<form class="col-6 needs-validation" novalidate method="get" action="' . htmlspecialchars($_SERVER["PHP_SELF"]) . '">';
echo <<<EOF
<input type="hidden" name="action" value="list">
<input type="hidden" name="category" value="$category_param">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">垃圾名</span>
</div>
<input type="text" class="form-control" placeholder="垃圾名" name="name" required>
<div class="invalid-feedback">
垃圾名不为空
</div>
<button class="btn btn-info" type="submit">搜索</button>
</div>
</form>
</div>
5 years ago
<table class="table col-8 m-auto">
<thead>
<tr>
<th scope="col">垃圾名</th>
<th scope="col">垃圾分类</th>
<th scope="col">创建时间</th>
5 years ago
<!-- <th scope="col">修改时间</th>-->
<th scope="col">操作</th>
5 years ago
</tr>
</thead>
<tbody>
EOF;
foreach ($garbage_query->getGarbageObjArray() as $garbage) {
5 years ago
if ($garbage instanceof Garbage) {
$name = $garbage->getName();
$category = get_category($garbage->getCategory());
$create_at = $garbage->getCreateAt();
$update_at = $garbage->getUpdateAt();
echo <<<EOF
<tr>
<th>$name</th>
<td>$category</td>
<td>$create_at</td>
5 years ago
<!-- <td>$update_at</td>-->
5 years ago
EOF;
echo '<td><button class="btn btn-info" onclick="doDelete(\'' . $name . '\')">删除</button></td></tr>';
5 years ago
}
}
echo <<<EOF
</tbody>
</table>
<nav aria-label="..." >
<ul class="pagination justify-content-center">
EOF;
if ($page === 1) {
echo <<<EOF
<li class="page-item disabled">
<span class="page-link">上一页</span>
</li>
EOF;
} else {
$prev = $page - 1;
echo '<li class="page-item"><a class="page-link" href="' . $_SERVER["PHP_SELF"] . '?action=list&category=' . $category_param . '&name=' . $name_param . '&page=' . $prev . '">上一页</a></li>';
5 years ago
}
for ($start_page = $page; $start_page <= $max_page and $start_page < $page + 3; $start_page++) {
echo '<li class="page-item ' . ($start_page == $page ? "active" : "") . '"><a class="page-link" href="' . $_SERVER["PHP_SELF"] . '?action=list&category=' . $category_param . '&name=' . $name_param . '&page=' . $start_page . '">' . $start_page . '</a></li>';
5 years ago
}
if ($page != $max_page) {
$next = $page + 1;
echo '<li class="page-item"><a class="page-link" href="' . $_SERVER["PHP_SELF"] . '?action=list&category=' . $category_param . '&name=' . $name_param . '&page=' . $next . '">下一页</a></li>';
5 years ago
} else {
echo <<<EOF
<li class="page-item disabled">
<span class="page-link">下一页</span>
</li>
EOF;
}
echo '</ul></nav><div class="row justify-content-center"><a class="btn btn-info col-4" href="' . $_SERVER["PHP_SELF"] . '">返回</a></div></div>';
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "delete") {
if (empty($_GET["name"])) {
error_res("缺少垃圾名,无法删除!", $_SERVER["PHP_SELF"]);
} else {
try {
DbUtil::delete("delete from garbage where name=?", null, "s", $_GET["name"]);
info_res("删除成功", $_SERVER["PHP_SELF"]);
} catch (Exception $e) {
error($e);
error_res("删除失败,请联系管理员", $_SERVER["PHP_SELF"]);
5 years ago
}
}
} else {
error("非法操作");
error_res("非法操作", "/");
}