main
xgkblog/admin/comments.php
comments.php6.3 KB
<?php
require 'config.php';

$msg = ''; // 操作提示信息

// 1. 处理状态变更请求 (通过 / 拒绝)
if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = $_GET['id'];
    $action = $_GET['action'];
    
    if ($action === 'approve') {
        $stmt = $pdo->prepare("UPDATE comments SET status = 'approved' WHERE id = :id");
        $stmt->execute([':id' => $id]);
        header('Location: comments.php?msg=approved'); exit;
    } elseif ($action === 'reject') {
        $stmt = $pdo->prepare("UPDATE comments SET status = 'rejected' WHERE id = :id");
        $stmt->execute([':id' => $id]);
        header('Location: comments.php?msg=rejected'); exit;
    }
}

// 2. 处理删除请求
if (isset($_GET['delete_id'])) {
    $stmt = $pdo->prepare("DELETE FROM comments WHERE id = :id");
    $stmt->execute([':id' => $_GET['delete_id']]);
    header('Location: comments.php?msg=deleted'); exit;
}

// 3. 获取所有评论(关联文章标题和用户昵称)
$sql = "SELECT 
            c.*, 
            p.title AS post_title,
            u.username AS user_name
        FROM comments c
        LEFT JOIN posts p ON c.post_id = p.id
        LEFT JOIN users u ON c.user_id = u.id
        ORDER BY c.created_at DESC";
$comments = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>



<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <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">数据概览</h1>
                <button class="btn-primary"><a href="post_edit.php">+ 撰写新文章</a></button>
            </div>
            
            <!-- 统计卡片 -->
           <?php require_once __DIR__ . '/views/tj.php'; ?>

            <!-- 数据表格 -->
           <?php if (!empty($_GET['msg'])): ?>
    <p style="color:green;font-weight:bold;margin-bottom:15px;">操作成功!</p>
<?php endif; ?>

<!-- 数据表格 -->
<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>评论者</th>
                    <th>评论内容</th>
                    <th>状态</th>
                    <th>时间</th>
                    <th style="width: 180px;">操作</th> <!-- 操作列加宽,以容纳多个按钮 -->
                </tr>
            </thead>
            <tbody>
                <?php foreach ($comments as $comment): ?>
                <tr>
                    <td><input type="checkbox" class="table-checkbox"></td>
                    <td><?php echo $comment['id']; ?></td>
                    <td>
                        <!-- 所属文章标题样式 -->
                        <div class="article-title"><?php echo htmlspecialchars($comment['post_title'] ?? '[文章已删除]'); ?></div>
                    </td>
                    <td>
                        <!-- 评论者:优先显示登录用户名,否则显示游客昵称 -->
                        <div class="article-cat">
                            <?php 
                            echo !empty($comment['user_name']) 
                                ? '<b>'.htmlspecialchars($comment['user_name']).'</b>' 
                                : htmlspecialchars($comment['guest_name']); 
                            ?>
                        </div>
                    </td>
                    <td>
                        <!-- 评论内容截断显示,防止撑开表格 -->
                        <div style="max-width: 250px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;" title="<?php echo htmlspecialchars($comment['content']); ?>">
                            <?php echo htmlspecialchars(mb_substr($comment['content'], 0, 40)) . (mb_strlen($comment['content']) > 40 ? '...' : ''); ?>
                        </div>
                    </td>
                    <td>
                        <!-- 状态标签:使用新的 status-badge 样式 -->
                        <?php 
                        $status_map = [
                            'pending' => ['待审核', 'status-draft'], 
                            'approved' => ['已通过', 'status-pub'], 
                            'rejected' => ['已拒绝', 'status-rej'] // 如果你的CSS中有 status-rej 的话
                        ];
                        $st = $status_map[$comment['status']] ?? ['未知', 'status-draft'];
                        echo "<span class='status-badge {$st[1]}'>{$st[0]}</span>";
                        ?>
                    </td>
                    <td>
                        <!-- 时间显示 -->
                        <div class="article-cat"><?php echo date('m-d H:i', strtotime($comment['created_at'])); ?></div>
                    </td>
                    <td>
                        <!-- 操作按钮区域 -->
                        <?php if ($comment['status'] !== 'approved'): ?>
                            <a href="?action=approve&id=<?php echo $comment['id']; ?>" class="action-btn" title="通过">通过</a>
                        <?php endif; ?>
                        
                        <?php if ($comment['status'] !== 'rejected'): ?>
                            <a href="?action=reject&id=<?php echo $comment['id']; ?>" class="action-btn" title="拒绝">拒绝 </a>
                        <?php endif; ?>
                        
                        <a href="?delete_id=<?php echo $comment['id']; ?>" class="action-btn" title="删除" onclick="return confirm('确定要永久删除这条评论吗?');">删除️ </a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
          
        </main>
    </div>

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