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

243 lines
9.2 KiB

<?php
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";
getMenu("题目管理");
if (empty($_REQUEST)) {
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();
// 查询垃圾分类数据量
DbUtil::query("select category,count(*) from garbage group by category order by category", $count_query);
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="查看垃圾数据" onclick="doList($category;)">
$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("垃圾名不能为空!", "/admin/WasteSorting.php");
} else if (empty($_POST["category"])) {
error_res("垃圾分类不能为空!", "/admin/WasteSorting.php");
} else if (array_search((int)$_POST["category"], array(1, 2, 4, 8, 16)) === false) {
error_res("垃圾分类参数不合法", "/admin/WasteSorting.php");
} else {
try {
DbUtil::insert("insert into garbage (name, category, create_at, update_at) values (?,?,?,?)", "siss", $_POST["name"], $_POST["category"], date(default_format), date(default_format));
info_res("保存成功", "/admin/WasteSorting.php");
} catch (Exception $e) {
error($e);
error_res("保存操作异常,请联系管理员", "/admin/WasteSorting.php");
}
}
} else if (isset($_GET["action"]) && $_GET["action"] == "list") {
if (empty($_GET["category"])) {
error_res("垃圾分类不能为空!", "/admin/WasteSorting.php");
} else if (array_search((int)$_GET["category"], array(1, 2, 4, 8, 16)) === false) {
error_res("垃圾分类参数不合法", "/admin/WasteSorting.php");
} else {
// 分页查询
$page = (int)($_GET["page"] ?? 1);
$category_param = (int)$_GET["category"];
$count_query = new QueryGarbageCount();
// 获取记录数
DbUtil::query("select count(*) from garbage where category=?", $count_query, array($category_param));
$count_result = $count_query->getCount();
$garbage_query = new QueryGarbageWithCategory();
// 分页大小
$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;
}
DbUtil::query("select * from garbage where category=? limit ?,?", $garbage_query, array($category_param, ($page - 1) * $page_size, $page_size));
if (empty($garbage_query->getResultList())) {
error("无法查询分类" . get_category($category_param) . "的第" . $page . "数据");
error_res("非法查询,请联系管理员", "/admin/WasteSorting.php");
} else {
echo <<<EOF
<div class="container py-5">
<table class="table col-8 m-auto">
<thead>
<tr>
<th scope="col">垃圾名</th>
<th scope="col">垃圾分类</th>
<th scope="col">创建时间</th>
<!-- <th scope="col">修改时间</th>-->
</tr>
</thead>
<tbody>
EOF;
foreach ($garbage_query->getResultList() as $garbage) {
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>
<!-- <td>$update_at</td>-->
</tr>
EOF;
}
}
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 <<<EOF
<li class="page-item">
<a class="page-link" href="/admin/WasteSorting.php?action=list&category=$category_param&page=$prev">上一页</a>
</li>
EOF;
}
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="/admin/WasteSorting.php?action=list&category=' . $category_param . '&page=' . $start_page . '">' . $start_page . '</a></li>';
}
if ($page != $max_page) {
$next = $page + 1;
echo <<<EOF
<li class="page-item">
<a class="page-link" href="/admin/WasteSorting.php?action=list&category=$category_param&page=$next">下一页</a>
</li>
EOF;
} else {
echo <<<EOF
<li class="page-item disabled">
<span class="page-link">下一页</span>
</li>
EOF;
}
echo <<<EOF
</ul>
</nav>
<div class="row justify-content-center">
<a class="btn btn-info col-4" href="/admin/WasteSorting.php">返回</a>
</div>
</div>
EOF;
}
}
} else {
error("非法操作");
error_res("非法操作", "/");
}