Initial commit: 교육 프로젝트 배포
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
// DB 접속 설정을 포함합니다.
|
||||
require __DIR__ . '/../../bbs/db_conn.php';
|
||||
|
||||
// PDO 연결 생성
|
||||
$pdo = db_conn();
|
||||
|
||||
// POST 요청이 아닐 경우 비정상 접근으로 간주하고 업로드 페이지로 돌려보냅니다.
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
header('Location: ../skin/content_upload.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 신규 학습목표 코드를 생성하는 함수입니다.
|
||||
* 예) 2026년에 첫 등록이면 2026-001, 이후 2026-002 등으로 자동 번호를 부여합니다.
|
||||
*/
|
||||
function generate_goal_code(PDO $pdo, string $year): string
|
||||
{
|
||||
$prefix = $year . '-'; // 연도- 문자열 접두사 생성 (예: 2026-)
|
||||
// 해당 연도의 접두사로 시작하는 코드 중 가장 큰 값(가장 최근 번호)을 찾습니다.
|
||||
$stmt = $pdo->prepare("SELECT MAX(goal_code) AS max_code FROM edu_learning_goals WHERE goal_code LIKE :pfx");
|
||||
$stmt->execute([':pfx' => $prefix . '%']);
|
||||
$max = $stmt->fetchColumn();
|
||||
|
||||
$next = 1; // 기본 순번은 1로 설정합니다.
|
||||
// 이전에 등록된 코드가 있다면, 마지막 세 자리 숫자를 추출하여 1을 더해줍니다.
|
||||
if ($max) {
|
||||
$seq = (int) substr($max, -3);
|
||||
$next = $seq + 1;
|
||||
}
|
||||
// 생성된 번호를 3자리 형식(001, 002 등)으로 포맷팅한 후 반환합니다.
|
||||
return $prefix . str_pad((string) $next, 3, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
// POST로 전달된 값들을 변수에 대입합니다. 빈 값 처리와 trim을 수행합니다.
|
||||
$goal_code = isset($_POST['goal_code']) ? trim($_POST['goal_code']) : '';
|
||||
$title = isset($_POST['title']) ? trim($_POST['title']) : '';
|
||||
$base_year = isset($_POST['base_year']) ? trim($_POST['base_year']) : date('Y');
|
||||
$quarter = isset($_POST['quarter']) ? trim($_POST['quarter']) : null;
|
||||
$is_active = isset($_POST['is_active']) ? '1' : '0';
|
||||
$sort_order = isset($_POST['sort_order']) && $_POST['sort_order'] !== '' ? (int) $_POST['sort_order'] : null;
|
||||
$remarks = isset($_POST['remarks']) ? trim($_POST['remarks']) : null;
|
||||
|
||||
// 필수 입력 항목인 제목과 기준년도가 누락되었다면 에러 파라미터와 함께 되돌아갑니다.
|
||||
if ($title === '' || $base_year === '') {
|
||||
header('Location: ../skin/content_upload.php?goal_error=required');
|
||||
exit;
|
||||
}
|
||||
|
||||
$goal_no = isset($_POST['goal_no']) ? trim($_POST['goal_no']) : null;
|
||||
$qParam = $quarter !== '' ? $quarter : null;
|
||||
$gnParam = $goal_no !== '' ? $goal_no : null;
|
||||
|
||||
// Validate uniqueness of year, quarter and goal_no
|
||||
if ($gnParam !== null && $qParam !== null) {
|
||||
$dupSql = "SELECT COUNT(*) FROM edu_learning_goals WHERE base_year = :by AND quarter = :q AND goal_no = :gn";
|
||||
$dupParams = [':by' => $base_year, ':q' => $qParam, ':gn' => $gnParam];
|
||||
if ($goal_code !== '') {
|
||||
$dupSql .= " AND goal_code != :gc";
|
||||
$dupParams[':gc'] = $goal_code;
|
||||
}
|
||||
$chk = $pdo->prepare($dupSql);
|
||||
$chk->execute($dupParams);
|
||||
if ($chk->fetchColumn() > 0) {
|
||||
echo json_encode(['success' => false, 'message' => '해당 년도/분기에 이미 같은 책장번호가 사용되었습니다.']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// 기존에 존재하는 학습목표 코드(goal_code)가 넘겨져왔다면, 수정(UPDATE) 동작을 수행합니다.
|
||||
if ($goal_code !== '') {
|
||||
// 업데이트할 필드들을 지정하는 쿼리입니다.
|
||||
$sql = "UPDATE edu_learning_goals SET title=:title, quarter=:quarter, base_year=:base_year, is_active=:is_active, sort_order=:sort_order, remarks=:remarks, goal_no=:goal_no WHERE goal_code=:goal_code";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
// 파라미터를 바인딩합니다.
|
||||
$stmt->bindValue(':title', $title);
|
||||
$stmt->bindValue(':quarter', $quarter !== '' ? $quarter : null, $quarter !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
|
||||
$stmt->bindValue(':base_year', $base_year);
|
||||
$stmt->bindValue(':is_active', $is_active);
|
||||
// 정렬 순서 파라미터는 null 허용이므로 분기 처리하여 바인딩합니다.
|
||||
if ($sort_order === null) {
|
||||
$stmt->bindValue(':sort_order', null, PDO::PARAM_NULL);
|
||||
} else {
|
||||
$stmt->bindValue(':sort_order', $sort_order, PDO::PARAM_INT);
|
||||
}
|
||||
$stmt->bindValue(':remarks', $remarks !== '' ? $remarks : null, $remarks !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
|
||||
$stmt->bindValue(':goal_no', $goal_no !== '' ? $goal_no : null, $goal_no !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
|
||||
$stmt->bindValue(':goal_code', $goal_code);
|
||||
|
||||
// 수정 쿼리를 실행합니다.
|
||||
$stmt->execute();
|
||||
}
|
||||
// 전송된 코드가 없다면, 신규 등록(INSERT) 동작을 수행합니다.
|
||||
else {
|
||||
// 위의 생성 함수를 통해 새로운 코드를 채번합니다.
|
||||
$goal_code = generate_goal_code($pdo, $base_year);
|
||||
$sql = "INSERT INTO edu_learning_goals (goal_code, title, quarter, base_year, is_active, sort_order, remarks, goal_no, created_at) VALUES (:goal_code, :title, :quarter, :base_year, :is_active, :sort_order, :remarks, :goal_no, NOW())";
|
||||
$stmt = $pdo->prepare($sql);
|
||||
|
||||
$stmt->bindValue(':goal_code', $goal_code);
|
||||
$stmt->bindValue(':title', $title);
|
||||
$stmt->bindValue(':quarter', $quarter !== '' ? $quarter : null, $quarter !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
|
||||
$stmt->bindValue(':base_year', $base_year);
|
||||
$stmt->bindValue(':is_active', $is_active);
|
||||
if ($sort_order === null) {
|
||||
$stmt->bindValue(':sort_order', null, PDO::PARAM_NULL);
|
||||
} else {
|
||||
$stmt->bindValue(':sort_order', $sort_order, PDO::PARAM_INT);
|
||||
}
|
||||
$stmt->bindValue(':remarks', $remarks !== '' ? $remarks : null, $remarks !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
|
||||
$stmt->bindValue(':goal_no', $goal_no !== '' ? $goal_no : null, $goal_no !== '' ? PDO::PARAM_STR : PDO::PARAM_NULL);
|
||||
|
||||
// 등록 쿼리를 실행합니다.
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
// AJAX JSON response
|
||||
echo json_encode(['success' => true]);
|
||||
exit;
|
||||
Reference in New Issue
Block a user