main
xgkblog/admin/pages.php
pages.php9.6 KB
<?php
require 'config.php';
$page_title = "独立页管理";

// ================= 1. 处理删除操作 =================
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $stmt = $pdo->prepare("DELETE FROM pages WHERE id = ?");
    $stmt->execute([$id]);
    header('Location: pages.php?msg=deleted'); exit;
}

// ================= 2. 处理表单提交 (新增 / 编辑) =================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 判断是编辑还是新增:如果 POST 中带有 id 且大于0,则为编辑
    $id = intval($_POST['id'] ?? 0); 
    
    if ($id > 0) {
        // 【编辑模式】更新数据库
        $stmt = $pdo->prepare("UPDATE pages SET title = :title, slug = :slug, content = :content WHERE id = :id");
        $stmt->execute([
            ':title'   => $_POST['title'],
            ':slug'    => $_POST['slug'],
            ':content' => $_POST['content'],
            ':id'      => $id
        ]);
        header('Location: pages.php?msg=updated'); exit;
    } else {
        // 【新增模式】插入数据库
        $stmt = $pdo->prepare("INSERT INTO pages (title, slug, content) VALUES (:title, :slug, :content)");
        $stmt->execute([
            ':title'   => $_POST['title'],
            ':slug'    => $_POST['slug'],
            ':content' => $_POST['content']
        ]);
        header('Location: pages.php?msg=added'); exit;
    }
}

// ================= 3. 获取数据用于渲染 =================
// 3.1 获取所有页面列表
$stmt = $pdo->query("SELECT * FROM pages ORDER BY id DESC");
$pages = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 3.2 检查是否在编辑状态(URL 带有 ?edit=ID)
$editing_page = null;
if (isset($_GET['edit'])) {
    $edit_id = intval($_GET['edit']);
    $stmt = $pdo->prepare("SELECT * FROM pages WHERE id = ?");
    $stmt->execute([$edit_id]);
    $editing_page = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($editing_page) {
        $page_title = "编辑独立页"; // 动态修改页面标题
    }
}

ob_start(); 
?>



<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title><?php echo $page_title; ?></title>
    <style>
      
    </style>
</head>
<body>

    <div class="overlay" id="overlay"></div>

    <!-- 侧边栏 -->
    <?php require_once __DIR__ . '/views/sidebar.php'; ?>

    <!-- 主内容区 -->
    <div class="main-wrapper">
      <?php require_once __DIR__ . '/views/header.php'; ?>
        <main class="content-area">
            <div class="page-header">
                <h1 class="page-title"><?php echo $page_title; ?></h1>
                <button class="btn-primary"><a href="post_edit.php">+ 撰写新文章</a></button>
            </div>
            
          

<style>
    /* ========== 页面表单卡片 ========== */
    .page-form-card {
        background: #ffffff;
        border-radius: 12px;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
        padding: 30px;
        margin-bottom: 30px;
        border: 1px solid #eef2f6;
    }
    .page-form-card h3 {
        margin: 0 0 25px 0;
        font-size: 20px;
        color: #2c3e50;
        padding-bottom: 15px;
        border-bottom: 2px solid #f0f4f8;
    }

    /* ========== 表单元素通用样式 ========== */
    .page-form-card .form-group { margin-bottom: 20px; }
    .page-form-card .form-group label {
        display: block;
        margin-bottom: 8px;
        font-weight: 600;
        color: #4a5568;
        font-size: 14px;
    }
    .page-form-card .form-control {
        width: 100%;
        padding: 12px 15px;
        border: 1px solid #d1d9e6;
        border-radius: 8px;
        font-size: 15px;
        color: #333;
        background-color: #fafbfc;
        transition: all 0.3s ease;
        box-sizing: border-box;
    }
    .page-form-card .form-control:focus {
        outline: none;
        border-color: #4a90e2;
        background-color: #fff;
        box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.1);
    }
    /* Textarea 专属样式 */
    .page-form-card textarea.form-control {
        resize: vertical; /* 只允许垂直拖拽 */
        min-height: 150px;
        line-height: 1.6;
    }

    /* ========== 按钮组 ========== */
    .form-actions {
        margin-top: 25px;
        display: flex;
        align-items: center;
        gap: 12px;
    }
    .btn {
        padding: 12px 24px;
        border: none;
        border-radius: 8px;
        font-size: 15px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        text-decoration: none;
        display: inline-block;
        text-align: center;
    }
    .btn-primary {
        background: linear-gradient(135deg, #4a90e2, #357abd);
        color: #fff;
    }
    .btn-primary:hover { background: linear-gradient(135deg, #357abd, #2a629e); transform: translateY(-1px); }
    
    .btn-secondary {
        background: #f0f4f8;
        color: #5a6b7d;
        border: 1px solid #d1d9e6;
    }
    .btn-secondary:hover { background: #e4e9f0; }

    /* ========== 提示信息 ========== */
    .alert-success {
        background-color: #e6f9ee;
        color: #1e7e34;
        border: 1px solid #b2e6c4;
        padding: 12px 18px;
        border-radius: 8px;
        margin-bottom: 20px;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 8px;
    }
</style>

<!-- 提示信息 -->
<?php if(isset($_GET['msg'])): ?>
    <div class="alert-success">
        ✅ <?php echo $_GET['msg'] == 'updated' ? '页面已更新!' : ($_GET['msg'] == 'deleted' ? '页面已删除!' : '新页面已添加!'); ?>
    </div>
<?php endif; ?>

<!-- ================= 4. 统一表单 (新增 & 编辑共用) ================= -->
<div class="page-form-card">
    <h3>
        <?php echo $editing_page ? '✏️ 编辑页面' : '➕ 添加新页面'; ?>
    </h3>
    
    <form method="POST">
        <!-- 隐藏字段:用于在编辑时传递当前页面的 ID -->
        <input type="hidden" name="id" value="<?php echo htmlspecialchars($editing_page['id'] ?? 0); ?>">
        
        <div class="form-group">
            <label for="page-title">页面标题 <span style="color:red;">*</span></label>
            <input type="text" id="page-title" name="title" class="form-control" placeholder="例如:关于我们" required 
                   value="<?php echo htmlspecialchars($editing_page['title'] ?? ''); ?>">
        </div>
        
        <div class="form-group">
            <label for="page-slug">URL别名 (Slug) <span style="color:red;">*</span></label>
            <input type="text" id="page-slug" name="slug" class="form-control" placeholder="例如:about-us" required 
                   value="<?php echo htmlspecialchars($editing_page['slug'] ?? ''); ?>">
        </div>
        
        <div class="form-group">
            <label for="page-content">页面内容 <span style="color:red;">*</span></label>
            <textarea id="page-content" name="content" class="form-control" rows="8" placeholder="支持 HTML 标签..." required><?php echo htmlspecialchars($editing_page['content'] ?? ''); ?></textarea>
        </div>
        
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">
                <?php echo $editing_page ? '💾 保存修改' : '➕ 添加新页面'; ?>
            </button>
            
            <!-- 如果在编辑状态,提供一个取消按钮 -->
            <?php if ($editing_page): ?>
                <a href="pages.php" class="btn btn-secondary">取消编辑</a>
            <?php endif; ?>
        </div>
    </form>
</div>
<!-- ================= 5. 页面列表表格 ================= -->
<div class="card" style="padding: 0; overflow: hidden;">
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th style="width: 40px;"><input type="checkbox" class="table-checkbox"></th>
                    <th style="width: 60px;">ID</th>
                    <th>标题</th>
                    <th>Slug</th>
                    <th style="width: 120px;">操作</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($pages)): ?>
                    <?php foreach ($pages as $page): ?>
                    <tr>
                        <td><input type="checkbox" class="table-checkbox"></td>
                        <td><?php echo $page['id']; ?></td>
                        <td>
                            <div class="article-title"><?php echo htmlspecialchars($page['title']); ?></div>
                        </td>
                        <td>
                            <div class="article-cat"><code>/<?php echo htmlspecialchars($page['slug']); ?></code></div>
                        </td>
                        <td>
                            <!-- 编辑按钮 -->
                            <a href="?edit=<?php echo $page['id']; ?>" class="action-btn" title="编辑">✏️</a>
                            <!-- 删除按钮 -->
                            <a href="?delete=<?php echo $page['id']; ?>" class="action-btn" title="删除" onclick="return confirm('确定要删除这个页面吗?')">🗑️</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" style="text-align:center; color:#999; padding:30px 0;">暂无独立页,请在上方添加。</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
          
        </main>
    </div>

    <?php require_once __DIR__ . '/views/footer.php'; ?>
</body>
</html>