138 lines
3.1 KiB
PHP
138 lines
3.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../bbs/db_conn.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
ini_set('display_errors', '1');
|
|
error_reporting(E_ALL);
|
|
|
|
// TODO: 실제 로그인 세션 연동 후 교체
|
|
$memberId = 'U001';
|
|
$sysCompCode = 'COMP01';
|
|
|
|
/**
|
|
* JSON 응답 출력 함수
|
|
*/
|
|
function response_json(array $data): void
|
|
{
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo = db_conn();
|
|
$pdo->exec("SET NAMES 'utf8mb4'");
|
|
|
|
$typeCode = trim((string)($_POST['type_code'] ?? ''));
|
|
$statusCode = trim((string)($_POST['status_code'] ?? ''));
|
|
$referenceUrl = trim((string)($_POST['reference_url'] ?? ''));
|
|
$reason = trim((string)($_POST['reason'] ?? ''));
|
|
|
|
|
|
//제안자는 URL(reference_url)과 제안이유(reason)만 입력한다.
|
|
//나머지 입력이 필요한 컬럼은 관리자가 입력하게 된다.(type_code,title)
|
|
|
|
// ★ 기본값 보정
|
|
if ($typeCode === '') {
|
|
$typeCode = '';
|
|
}
|
|
|
|
if ($statusCode === '') {
|
|
$statusCode = 'OF10002';
|
|
}
|
|
|
|
// ★ 필수값 검증
|
|
if ($referenceUrl === '') {
|
|
response_json([
|
|
'success' => false,
|
|
'message' => 'URL을 입력해주세요.'
|
|
]);
|
|
}
|
|
|
|
if ($reason === '') {
|
|
response_json([
|
|
'success' => false,
|
|
'message' => '추천이유를 입력해주세요.'
|
|
]);
|
|
}
|
|
|
|
// ★ URL 형식 검증
|
|
if (!filter_var($referenceUrl, FILTER_VALIDATE_URL)) {
|
|
response_json([
|
|
'success' => false,
|
|
'message' => '올바른 URL 형식이 아닙니다.'
|
|
]);
|
|
}
|
|
|
|
// ★ 해당 년월(YYYYMM) 기준 offer_id 생성
|
|
// 예: 202603-001, 202603-002
|
|
$offerYm = date('Ym');
|
|
|
|
// ★ 같은 년월 prefix를 가진 마지막 offer_id 조회
|
|
$stmtSeq = $pdo->prepare("
|
|
SELECT offer_id
|
|
FROM edu_content_offer
|
|
WHERE offer_id LIKE :offer_prefix
|
|
ORDER BY offer_id DESC
|
|
LIMIT 1
|
|
");
|
|
$stmtSeq->execute([
|
|
':offer_prefix' => $offerYm . '-%',
|
|
]);
|
|
|
|
$lastOfferId = (string)$stmtSeq->fetchColumn();
|
|
$nextSeq = 1;
|
|
|
|
// ★ 마지막 번호가 있으면 뒤 3자리 숫자를 추출해서 +1
|
|
if ($lastOfferId !== '') {
|
|
$lastSeq = (int)substr($lastOfferId, -3);
|
|
$nextSeq = $lastSeq + 1;
|
|
}
|
|
|
|
$offerId = $offerYm . '-' . str_pad((string)$nextSeq, 3, '0', STR_PAD_LEFT);
|
|
|
|
// ★ 저장
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO edu_content_offer (
|
|
offer_id,
|
|
member_id,
|
|
sys_comp_code,
|
|
reference_url,
|
|
reason,
|
|
status_code,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
:offer_id,
|
|
:member_id,
|
|
:sys_comp_code,
|
|
:reference_url,
|
|
:reason,
|
|
:status_code,
|
|
NOW(),
|
|
NOW()
|
|
)
|
|
");
|
|
|
|
$stmt->execute([
|
|
':offer_id' => $offerId,
|
|
':member_id' => $memberId,
|
|
':sys_comp_code' => $sysCompCode,
|
|
':reference_url' => $referenceUrl,
|
|
':reason' => $reason,
|
|
':status_code' => $statusCode,
|
|
]);
|
|
|
|
response_json([
|
|
'success' => true,
|
|
'message' => '제안이 정상적으로 등록되었습니다.',
|
|
'offer_id' => $offerId,
|
|
]);
|
|
|
|
} catch (Throwable $e) {
|
|
response_json([
|
|
'success' => false,
|
|
'message' => '제안 등록 중 오류가 발생했습니다.',
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
} |