Files
edu/admin/bbs/content_save.php

222 lines
10 KiB
PHP

<?php
require __DIR__ . '/../../bbs/db_conn.php';
require_once __DIR__ . '/../../bbs/auth.php';
edu_start_session();
// 로그인 사용자 member_id 추출
$login_member_id = $_SESSION['member_id'] ?? null;
// PDO 연결 생성
$pdo = db_conn();
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ../skin/content_upload.php');
exit;
}
function generate_content_id(PDO $pdo): string
{
$prefix = date('Ym') . '-';
$stmt = $pdo->prepare("SELECT MAX(content_id) AS max_id FROM edu_contents WHERE content_id LIKE :pfx");
$stmt->execute([':pfx' => $prefix . '%']);
$max = $stmt->fetchColumn();
$next = 1;
if ($max) {
$seq = (int) substr($max, -3);
$next = $seq + 1;
}
return $prefix . str_pad((string) $next, 3, '0', STR_PAD_LEFT);
}
$content_id = isset($_POST['content_id']) ? trim($_POST['content_id']) : '';
$category_code = isset($_POST['category_code']) ? trim($_POST['category_code']) : '';
if ($category_code === '' && isset($_POST['category_code_hidden'])) {
$category_code = trim($_POST['category_code_hidden']);
}
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
$description = isset($_POST['description']) ? trim($_POST['description']) : '';
$description1 = isset($_POST['description1']) ? trim($_POST['description1']) : '';
$description2 = isset($_POST['description2']) ? trim($_POST['description2']) : '';
$description3 = isset($_POST['description3']) ? trim($_POST['description3']) : '';
$content_url = isset($_POST['content_url']) ? trim($_POST['content_url']) : '';
$thumbnail_url = isset($_POST['thumbnail_url']) ? trim($_POST['thumbnail_url']) : '';
$sort_order = isset($_POST['sort_order']) && $_POST['sort_order'] !== '' ? (int) $_POST['sort_order'] : null;
$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';
$is_active = isset($_POST['is_active']) ? '1' : '0';
$content_tm = isset($_POST['content_tm']) && $_POST['content_tm'] !== '' ? (int) $_POST['content_tm'] : null;
// 카테고리별 추가 필드
$base_year = null;
$goal_code = null;
$category_group = null;
$issue_type_code = null;
$start_date = null;
$end_date = null;
$is_offer = '0';
$offer_id = null;
$book_yn = '0'; // 사내도서여부 기본값
// 카테고리 이름 조회
$catName = null;
if ($category_code !== '') {
$nstmt = $pdo->prepare("SELECT code_name FROM edu_codes WHERE group_code='CA100' AND base_code=:c LIMIT 1");
$nstmt->execute([':c' => $category_code]);
$catName = $nstmt->fetchColumn();
}
if ($catName === '마이클래스') {
$base_year = isset($_POST['base_year']) ? trim($_POST['base_year']) : null;
$goal_code = isset($_POST['goal_code']) ? trim($_POST['goal_code']) : null;
}
// 공통 카테고리구분
$category_group = isset($_POST['category_group']) ? trim($_POST['category_group']) : null;
if ($catName === '법정교육') {
$base_year = isset($_POST['base_year_law']) ? trim($_POST['base_year_law']) : null;
$start_date = isset($_POST['start_date']) ? $_POST['start_date'] : null;
$end_date = isset($_POST['end_date']) ? $_POST['end_date'] : null;
}
if ($catName === '인사이트' || $catName === '리더십') {
$issue_type_code = isset($_POST['issue_type_code']) ? trim($_POST['issue_type_code']) : null;
$offer_id = isset($_POST['offer_id']) ? trim($_POST['offer_id']) : null;
$is_offer = isset($_POST['is_offer']) ? '1' : '0';
if ($is_offer === '1') {
// 기존 추천콘텐츠 초기화 인사이트/리더십카테고리 , 카테고리 구분별 (단일 적용)
$stmt = $pdo->prepare("UPDATE edu_contents SET is_offer='0' WHERE is_offer='1' AND category_code = :category_code and issue_type_code = :issue_type_code ");
$stmt->execute([':category_code' => $category_code, ':issue_type_code' => $issue_type_code]);
}
}
if ($catName === '비즈트렌드') {
$start_date = isset($_POST['start_date_bzt']) ? $_POST['start_date_bzt'] : null;
// 사내도서여부: 체크 시 '1', 미체크 시 '0'
$book_yn = isset($_POST['book_yn']) && $_POST['book_yn'] === '1' ? '1' : '0';
}
if ($category_code === '' || $title === '') {
echo json_encode(['success' => false, 'message' => '카테고리와 콘텐츠명은 필수입니다.']);
exit;
}
$image_name = null;
if ($content_id !== '') {
$stmtImg = $pdo->prepare("SELECT image_name FROM edu_contents WHERE content_id = :id");
$stmtImg->execute([':id' => $content_id]);
$image_name = $stmtImg->fetchColumn();
}
if (isset($_FILES['image_name']) && $_FILES['image_name']['error'] === UPLOAD_ERR_OK) {
if ($_FILES['image_name']['size'] > 4 * 1024 * 1024) {
echo json_encode(['success' => false, 'message' => '이미지 파일은 4MB 이하만 등록 가능합니다.']);
exit;
}
$finfo = @finfo_open(FILEINFO_MIME_TYPE);
if ($finfo) {
$mime = @finfo_file($finfo, $_FILES['image_name']['tmp_name']);
@finfo_close($finfo);
if ($mime && strpos($mime, 'image/') !== 0) {
echo json_encode(['success' => false, 'message' => '이미지 파일만 등록 가능합니다.']);
exit;
}
}
$upload_dir = __DIR__ . '/../../uploads/biztrend/';
if (!is_dir($upload_dir)) {
@mkdir($upload_dir, 0777, true);
}
$ext = strtolower(pathinfo($_FILES['image_name']['name'], PATHINFO_EXTENSION));
if (!$ext)
$ext = 'jpg';
$new_filename = 'img_' . time() . '_' . mt_rand(1000, 9999) . '.' . $ext;
$target_file = $upload_dir . $new_filename;
if (move_uploaded_file($_FILES['image_name']['tmp_name'], $target_file)) {
if ($image_name && file_exists($upload_dir . $image_name)) {
@unlink($upload_dir . $image_name);
}
$image_name = $new_filename;
} else {
echo json_encode(['success' => false, 'message' => '이미지 업로드에 실패했습니다.']);
exit;
}
}
if ($content_id !== '') {
$sql = "UPDATE edu_contents SET
category_code = :category_code,
category_group = :category_group,
title = :title,
description = :description,
description1 = :description1,
description2 = :description2,
description3 = :description3,
content_url = :content_url,
thumbnail_url = :thumbnail_url,
content_tm = :content_tm,
base_year = :base_year,
start_date = :start_date,
end_date = :end_date,
sort_order = :sort_order,
goal_code = :goal_code,
issue_type_code = :issue_type_code,
is_offer = :is_offer,
offer_id = :offer_id,
is_active = :is_active,
book_yn = :book_yn,
image_name = :image_name,
updated_by = :updated_by,
updated_at = NOW()
WHERE content_id = :content_id";
} else {
$content_id = generate_content_id($pdo);
$sql = "INSERT INTO edu_contents (
content_id, category_code, category_group, title, description,description1, description2, description3,
content_url, thumbnail_url, content_tm, base_year, start_date, end_date, sort_order,
goal_code, issue_type_code, is_offer, offer_id, is_active, book_yn, image_name, created_by , created_at , updated_by , updated_at
) VALUES (
:content_id, :category_code, :category_group, :title, :description, :description1, :description2, :description3,
:content_url, :thumbnail_url, :content_tm, :base_year, :start_date, :end_date, :sort_order,
:goal_code, :issue_type_code, :is_offer, :offer_id, :is_active, :book_yn, :image_name, :created_by, NOW(), :updated_by, NOW()
)";
}
try {
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':image_name', $image_name !== '' && $image_name !== null ? $image_name : null, $image_name !== '' && $image_name !== null ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':content_id', $content_id);
$stmt->bindValue(':category_code', $category_code !== '' ? $category_code : null, $category_code !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':category_group', $category_group !== '' ? $category_group : null, $category_group !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':title', $title);
$stmt->bindValue(':description', $description !== '' ? $description : null, $description !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':description1', $description1 !== '' ? $description1 : null, $description1 !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':description2', $description2 !== '' ? $description2 : null, $description2 !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':description3', $description3 !== '' ? $description3 : null, $description3 !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':content_url', $content_url !== '' ? $content_url : null, $content_url !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':thumbnail_url', $thumbnail_url !== '' ? $thumbnail_url : null, $thumbnail_url !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
if ($content_tm === null) {
$stmt->bindValue(':content_tm', null, PDO::PARAM_NULL);
} else {
$stmt->bindValue(':content_tm', $content_tm, PDO::PARAM_INT);
}
$stmt->bindValue(':base_year', $base_year !== '' ? $base_year : null, $base_year !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':start_date', $start_date !== '' ? $start_date : null, $start_date !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':end_date', $end_date !== '' ? $end_date : null, $end_date !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
if ($sort_order === null || $sort_order === '') {
$stmt->bindValue(':sort_order', null, PDO::PARAM_NULL);
} else {
$stmt->bindValue(':sort_order', (int) $sort_order, PDO::PARAM_INT);
}
$stmt->bindValue(':goal_code', $goal_code !== '' ? $goal_code : null, $goal_code !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':issue_type_code', $issue_type_code !== '' ? $issue_type_code : null, $issue_type_code !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':is_offer', $is_offer);
$stmt->bindValue(':offer_id', $offer_id !== '' ? $offer_id : null, $offer_id !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
$stmt->bindValue(':is_active', $is_active);
$stmt->bindValue(':book_yn', $book_yn);
// created_by는 INSERT 모드에서만 필요함
if (!isset($_POST['content_id']) || $_POST['content_id'] === '') {
$stmt->bindValue(':created_by', $login_member_id);
}
$stmt->bindValue(':updated_by', $login_member_id);
$stmt->execute();
echo json_encode(['success' => true]);
} catch (Exception $e) {
// 에러 내용을 JSON 으로 반환하여 화면에서 확인할 수 있게 합니다.
echo json_encode(['success' => false, 'message' => '에러 발생: ' . $e->getMessage()]);
}
exit;