Files
2026-06-11 18:47:38 +09:00

132 lines
4.2 KiB
PHP

<?php
include_once('../../_common.php');
// 설문 관리 라이브러리 로드
include_once(G5_ADMIN_PATH.'/survey_manage/lib/survey.lib.php');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
alert('잘못된 접근입니다.', G5_URL);
}
$sv_id = isset($_POST['sv_id']) ? (int)$_POST['sv_id'] : 0;
if (!$sv_id) {
alert('잘못된 접근입니다.', G5_URL);
}
// 설문 유효성 검사
$validation = validate_survey_access($sv_id, $member['mb_id'], $_SERVER['REMOTE_ADDR']);
if (!$validation['success']) {
alert($validation['message'], G5_URL);
}
$survey = $validation['survey'];
$questions = get_survey_questions($sv_id);
if (empty($questions)) {
alert('설문 질문이 없습니다.', G5_URL);
}
// 응답 시작
$sr_id = start_survey_response(
$sv_id,
$member['mb_id'] ?: null,
$_SERVER['REMOTE_ADDR'],
$_SERVER['HTTP_USER_AGENT'] ?: '',
session_id()
);
if (!$sr_id) {
alert('응답 저장 중 오류가 발생했습니다.', G5_URL);
}
// 답변 저장
$errors = array();
foreach ($questions as $question) {
$field_name = 'answer_' . $question['sq_id'];
$value = isset($_POST[$field_name]) ? $_POST[$field_name] : '';
// 필수 질문 검증
if ($question['sq_required']) {
if (is_array($value)) {
if (empty($value) || (count($value) == 1 && empty($value[0]))) {
$errors[] = $question['sq_title'] . ' 항목은 필수입니다.';
continue;
}
} else {
if (empty(trim($value))) {
$errors[] = $question['sq_title'] . ' 항목은 필수입니다.';
continue;
}
}
}
// 유효성 검사
if ($question['sq_validation'] && !empty($value)) {
$validation_rules = $question['sq_validation'];
if (isset($validation_rules['minLength']) && strlen($value) < $validation_rules['minLength']) {
$errors[] = $question['sq_title'] . ' 항목은 최소 ' . $validation_rules['minLength'] . '자 이상 입력해주세요.';
continue;
}
if (isset($validation_rules['maxLength']) && strlen($value) > $validation_rules['maxLength']) {
$errors[] = $question['sq_title'] . ' 항목은 최대 ' . $validation_rules['maxLength'] . '자까지 입력 가능합니다.';
continue;
}
if (isset($validation_rules['pattern'])) {
$pattern = $validation_rules['pattern'];
if ($pattern === 'email' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
$errors[] = $question['sq_title'] . ' 항목에 올바른 이메일 주소를 입력해주세요.';
continue;
}
if ($pattern === 'phone' && !preg_match('/^[0-9-+\s()]+$/', $value)) {
$errors[] = $question['sq_title'] . ' 항목에 올바른 전화번호를 입력해주세요.';
continue;
}
}
}
// 답변 저장
if (!empty($value) || $question['sq_required']) {
save_survey_answer($sr_id, $question['sq_id'], $value);
}
}
// 오류가 있으면 응답 삭제하고 돌아가기
if (!empty($errors)) {
sql_query("DELETE FROM survey_responses WHERE sr_id = '$sr_id'");
// JSON 응답인지 확인
if (isset($_POST['ajax']) && $_POST['ajax'] == '1') {
header('Content-Type: application/json');
echo json_encode(array(
'success' => false,
'errors' => $errors
));
exit;
} else {
alert(implode('\n', $errors), 'survey_page.php?sv_id=' . $sv_id);
}
}
// 응답 완료 처리
complete_survey_response($sr_id);
// 통계 업데이트
update_survey_statistics($sv_id);
// JSON 응답인지 확인
if (isset($_POST['ajax']) && $_POST['ajax'] == '1') {
header('Content-Type: application/json');
echo json_encode(array(
'success' => true,
'redirect_url' => G5_THEME_PATH.'/rb.custom/survey_form/survey_complete_page.php?sv_id=' . $sv_id . '&sr_id=' . $sr_id
));
exit;
} else {
// 완료 페이지로 이동
goto_url(G5_THEME_PATH.'/rb.custom/survey_form/survey_complete_page.php?sv_id=' . $sv_id . '&sr_id=' . $sr_id);
}
?>