Files
kngil_home/kngil/bbs/qa_list.php

153 lines
3.4 KiB
PHP

<?php
/**
* Q&A 리스트 컨트롤러
* - 스킨: /kngil/skin/qa_list.skin.php
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once $_SERVER['DOCUMENT_ROOT'].'/kngil/bbs/db_conn.php';
/* =========================
1. 입력값 정리
========================= */
$page = max(1, (int)($_GET['page'] ?? 1));
$pageSize = 10;
$offset = ($page - 1) * $pageSize;
$search = trim($_GET['q'] ?? '');
$cats = $_GET['cat'] ?? []; // 배열
$writer = $_GET['writer'] ?? ''; // me
$status = $_GET['status'] ?? 'all'; // 상태
$isLogin = !empty($_SESSION['login']);
$loginUserId = $_SESSION['login']['user_id'] ?? null;
/* =========================
2. WHERE 조건 구성
========================= */
$where = [];
$params = [];
/* 카테고리 */
if (!empty($cats) && !in_array('all', $cats, true)) {
$catKeys = [];
foreach ($cats as $i => $cat) {
$key = ":cat{$i}";
$catKeys[] = $key;
$params[$key] = $cat;
}
$where[] = "p.category IN (" . implode(',', $catKeys) . ")";
}
/* 내가 작성한 글 */
if ($writer === 'me' && $loginUserId) {
$where[] = "p.user_id = :me";
$params[':me'] = $loginUserId;
}
/* 상태 */
if ($status !== 'all') {
$where[] = "p.stat_bc = :status";
$params[':status'] = $status;
}
/* 검색 (제목 + 내용) */
if ($search !== '') {
$where[] = "(p.title ILIKE :q OR p.content ILIKE :q)";
$params[':q'] = "%{$search}%";
}
$whereSql = $where ? 'WHERE ' . implode(' AND ', $where) : '';
/* =========================
3. 전체 건수
========================= */
$countSql = "
SELECT COUNT(*)
FROM kngil.qa_posts p
{$whereSql}
";
$stmt = $pdo->prepare($countSql);
$stmt->execute($params);
$totalCount = (int)$stmt->fetchColumn();
/* =========================
4. 리스트 조회
========================= */
$listSql = "
SELECT
p.post_id,
p.category,
p.title,
p.user_nm,
p.co_nm,
p.dept_nm,
p.stat_bc AS status,
p.is_secret,
p.cdt_dt AS created_at,
-- 댓글 수
(
SELECT COUNT(*)
FROM kngil.qa_comments c
WHERE c.post_id = p.post_id
) AS comment_count,
-- 첨부파일 수
(
SELECT COUNT(*)
FROM kngil.qa_attachments a
WHERE a.post_id = p.post_id
) AS file_count
FROM kngil.qa_posts p
{$whereSql}
ORDER BY
(p.category = 'notice') DESC,
p.post_id DESC
LIMIT :limit OFFSET :offset
";
$stmt = $pdo->prepare($listSql);
/* 바인딩 */
foreach ($params as $k => $v) {
$stmt->bindValue($k, $v);
}
$stmt->bindValue(':limit', $pageSize, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
/* =========================
5. 표시용 가공
========================= */
foreach ($posts as &$row) {
// 회사 표시
$row['display_company'] = $row['co_nm'] ?? '';
// 작성자 표시
$row['display_name'] = $row['user_nm'] ?? $row['user_id'];
// 날짜 포맷
$row['created_at'] = substr($row['created_at'], 0, 10);
}
unset($row);
/* =========================
6. 페이징 계산
========================= */
$totalPages = (int)ceil($totalCount / $pageSize);
/* =========================
7. 스킨 렌더링
========================= */
include $_SERVER['DOCUMENT_ROOT'].'/kngil/skin/qa_list.skin.php';