init: 我的博客项目

This commit is contained in:
Your Name
2026-09-09 19:30:18 +08:00
commit 76c241f032
34 changed files with 3420 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
<?php
require_once __DIR__ . "/../includes/functions.php";
require_once __DIR__ . "/../includes/auth.php";
$adminPage = "articles";
$pageTitle = "编辑文章";
require __DIR__ . "/_layout.php";
$id = isset($_GET["id"]) ? (int)$_GET["id"] : 0;
$article = null;
if ($id) {
$stmt = db()->prepare("SELECT * FROM articles WHERE id=?");
$stmt->execute([$id]);
$article = $stmt->fetch();
if (!$article) { echo '<div class="msg">文章不存在或已删除</div>'; echo '<p><a href="article_list.php">返回列表</a></p>'; require __DIR__ . "/_tail.php"; exit; }
}
$cats = get_categories();
$allTags = get_all_tags();
$curTags = $article ? array_column(get_article_tags($article["id"]), "id") : [];
$msg = "";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = trim($_POST["title"] ?? "");
$slug = trim($_POST["slug"] ?? "");
$category_id = (int)($_POST["category_id"] ?? 0);
$excerpt = trim($_POST["excerpt"] ?? "");
$content = $_POST["content"] ?? "";
$status = ($_POST["status"] ?? "published") === "draft" ? "draft" : "published";
$tags = $_POST["tags"] ?? []; if (!is_array($tags)) { $tags = []; }
if ($title === "" || $content === "") {
$msg = '<div class="msg">标题和内容不能为空</div>';
} else {
if ($slug === "") { $slug = slugify($title); }
if ($msg === "") {
$user_id = current_user()["id"];
if ($id) {
$s = db()->prepare("UPDATE articles SET title=?, slug=?, category_id=?, excerpt=?, content=?, status=? WHERE id=?");
$s->execute([$title, $slug, $category_id ?: null, $excerpt, $content, $status, $id]);
} else {
$s = db()->prepare("INSERT INTO articles (user_id, title, slug, category_id, excerpt, content, status, views) VALUES (?,?,?,?,?,?,?,0)");
$s->execute([$user_id, $title, $slug, $category_id ?: null, $excerpt, $content, $status]);
$id = (int)db()->lastInsertId();
}
db()->prepare("DELETE FROM article_tags WHERE article_id=?")->execute([$id]);
if ($tags) {
$si = db()->prepare("INSERT IGNORE INTO article_tags (article_id, tag_id) VALUES (?,?)");
foreach ($tags as $tid) { $si->execute([$id, (int)$tid]); }
}
header("Location: article_list.php?ok=1"); exit;
}
}
}
?>
<div class="admin-head"><h1><?php echo $id ? "编辑文章" : "写文章"; ?></h1></div>
<?php echo $msg; ?>
<div class="card glass form-card">
<form method="post">
<div class="field">
<label>标题</label>
<input class="input" type="text" name="title" value="<?php echo e($article["title"] ?? ""); ?>" required>
</div>
<div class="field">
<label>别名(slug,留空自动生成)</label>
<input class="input" type="text" name="slug" value="<?php echo e($article["slug"] ?? ""); ?>" placeholder="english-slug">
</div>
<div class="form-row">
<div class="field">
<label>分类</label>
<select class="input" name="category_id">
<option value="0">未分类</option>
<?php foreach($cats as $c): ?><option value="<?php echo $c["id"]; ?>" <?php echo ($article["category_id"] ?? 0)==$c["id"]?"selected":""; ?>><?php echo e($c["name"]); ?></option><?php endforeach; ?>
</select>
</div>
<div class="field">
<label>状态</label>
<select class="input" name="status">
<option value="published" <?php echo ($article["status"] ?? "published")==="published"?"selected":""; ?>>发布</option>
<option value="draft" <?php echo ($article["status"] ?? "")==="draft"?"selected":""; ?>>草稿</option>
</select>
</div>
</div>
<div class="field">
<label>摘要</label>
<textarea class="input" name="excerpt" rows="2"><?php echo e($article["excerpt"] ?? ""); ?></textarea>
</div>
<div class="field">
<label>标签</label>
<div>
<?php foreach($allTags as $t): ?>
<label class="tag-check"><input type="checkbox" name="tags[]" value="<?php echo $t["id"]; ?>" <?php echo in_array($t["id"],$curTags)?"checked":""; ?>> <?php echo e($t["name"]); ?></label>
<?php endforeach; ?>
<?php if(!$allTags): ?><span class="text-muted">暂无标签,可先在标签管理中创建</span><?php endif; ?>
</div>
</div>
<div class="field">
<label>内容(支持 Markdown)</label>
<textarea class="input code" name="content" rows="16" required><?php echo e($article["content"] ?? ""); ?></textarea>
</div>
<button class="glass-btn primary" type="submit">保存</button>
<a class="glass-btn" href="article_list.php">返回</a>
</form>
</div>
<?php require __DIR__ . "/_tail.php"; ?>