Files
2026-09-09 19:30:18 +08:00

55 lines
2.3 KiB
PHP

<?php
require_once __DIR__ . "/../includes/functions.php";
require_once __DIR__ . "/../includes/auth.php";
$adminPage = "categories";
$pageTitle = "分类管理";
require __DIR__ . "/_layout.php";
$msg = "";
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["name"])) {
$name = trim($_POST["name"]);
$slug = slugify($_POST["slug"] ?? "");
if ($name === "") { $msg = '<div class="msg">分类名不能为空</div>'; }
else {
$s = db()->prepare("INSERT INTO categories (name, slug) VALUES (?,?)");
$s->execute([$name, $slug]);
header("Location: category.php?ok=1"); exit;
}
}
if (isset($_GET["del"])) {
$cid = (int)$_GET["del"];
db()->prepare("UPDATE articles SET category_id=NULL WHERE category_id=?")->execute([$cid]);
db()->prepare("DELETE FROM categories WHERE id=?")->execute([$cid]);
header("Location: category.php?deleted=1"); exit;
}
$list = db()->query("SELECT c.*, (SELECT COUNT(*) FROM articles a WHERE a.category_id=c.id) AS cnt FROM categories c ORDER BY c.id DESC")->fetchAll();
?>
<div class="admin-head"><h1>分类管理</h1></div>
<?php echo $msg; ?><?php if (isset($_GET["deleted"])) { ?><div class="msg ok">删除成功</div><?php } ?>
<div class="card glass form-card" style="margin-bottom:20px">
<h3>新增分类</h3>
<form method="post">
<div class="form-row">
<div class="field"><label>名称</label><input class="input" type="text" name="name" required></div>
<div class="field"><label>别名(slug,可选)</label><input class="input" type="text" name="slug" placeholder="english-slug"></div>
</div>
<button class="glass-btn primary" type="submit">添加</button>
</form>
</div>
<div class="card glass">
<table class="admin-table">
<thead><tr><th>ID</th><th>名称</th><th>别名</th><th>文章数</th><th>操作</th></tr></thead>
<tbody>
<?php foreach($list as $c): ?>
<tr>
<td><?php echo $c["id"]; ?></td>
<td><?php echo e($c["name"]); ?></td>
<td class="text-muted"><?php echo e($c["slug"]); ?></td>
<td><?php echo $c["cnt"]; ?></td>
<td><div class="actions"><a href="?del=<?php echo $c["id"]; ?>" data-confirm="删除分类不会删除其下文章,仅取消关联,继续?">删除</a></div></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php require __DIR__ . "/_tail.php"; ?>