159 lines
6.5 KiB
PHP
159 lines
6.5 KiB
PHP
<?php
|
|
if (!defined('_GNUBOARD_')) exit;
|
|
|
|
class TemplateManager
|
|
{
|
|
protected $table = 'g5_mail_template';
|
|
private $log_table = 'g5_mail_template_change_log';
|
|
// [추가] 템플릿 변수 테이블명
|
|
private $vars_table = 'g5_mail_template_vars';
|
|
|
|
public function getAll()
|
|
{
|
|
// [수정] is_use -> is_deleted 로 조건 변경
|
|
$result = sql_query("SELECT * FROM {$this->table} WHERE is_deleted = 0 ORDER BY id DESC");
|
|
$list = [];
|
|
while ($row = sql_fetch_array($result)) {
|
|
$list[] = $row;
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
public function getById($id)
|
|
{
|
|
$id = (int)$id;
|
|
return sql_fetch("SELECT * FROM {$this->table} WHERE id = '{$id}' AND is_deleted = 0 AND is_use = 1");
|
|
}
|
|
|
|
public function getByCode($code)
|
|
{
|
|
$code = sql_real_escape_string($code);
|
|
return sql_fetch("SELECT * FROM {$this->table} WHERE code = '{$code}' AND is_deleted = 0");
|
|
}
|
|
|
|
/**
|
|
* [추가] 특정 템플릿에 속한 모든 변수들을 가져옵니다.
|
|
* @param int $template_id
|
|
* @return array
|
|
*/
|
|
public function getVarsByTemplateId($template_id)
|
|
{
|
|
$template_id = (int)$template_id;
|
|
$sql = "SELECT var_name, default_value FROM {$this->vars_table} WHERE template_id = '{$template_id}'";
|
|
$result = sql_query($sql);
|
|
$vars = [];
|
|
while ($row = sql_fetch_array($result)) {
|
|
$vars[$row['var_name']] = $row['default_value'];
|
|
}
|
|
return $vars;
|
|
}
|
|
|
|
public function save($data)
|
|
{
|
|
global $member;
|
|
|
|
$id = isset($data['id']) ? (int)$data['id'] : 0;
|
|
$code = sql_real_escape_string($data['code']);
|
|
$title = sql_real_escape_string($data['title']);
|
|
$content = $data['content']; // [주의] SQL Escape는 syncTemplateVars에서 처리
|
|
// [추가] 헤더와 푸터 데이터를 받습니다.
|
|
$header_html = $data['header_html'] ?? '';
|
|
$footer_html = $data['footer_html'] ?? '';
|
|
$is_use = isset($data['is_use']) ? 1 : 0;
|
|
$now = G5_TIME_YMDHIS;
|
|
$mb_id = sql_real_escape_string($member['mb_id']);
|
|
|
|
if ($id > 0) {
|
|
// [수정] 업데이트 쿼리 수정
|
|
$sql = "UPDATE {$this->table} SET
|
|
code = '{$code}',
|
|
title = '{$title}',
|
|
content = '" . sql_real_escape_string($content) . "',
|
|
header_html = '" . sql_real_escape_string($header_html) . "',
|
|
footer_html = '" . sql_real_escape_string($footer_html) . "',
|
|
is_use = '{$is_use}',
|
|
updated_by = '{$mb_id}',
|
|
updated_at = '{$now}'
|
|
WHERE id = '{$id}'";
|
|
sql_query($sql);
|
|
|
|
// [추가] 업데이트 로그 기록
|
|
$details = "템플릿 수정 (ID: {$id}, 코드: {$code})";
|
|
$this->insertTemplateLog($id, 'update', $mb_id, $details);
|
|
} else {
|
|
// [수정] 생성 쿼리 수정
|
|
$sql = "INSERT INTO {$this->table}
|
|
(code, title, content, header_html, footer_html, is_use, is_deleted, created_by, updated_by, created_at, updated_at)
|
|
VALUES
|
|
('{$code}', '{$title}', '" . sql_real_escape_string($content) . "', '" . sql_real_escape_string($header_html) . "', '" . sql_real_escape_string($footer_html) . "', '{$is_use}', 0, '{$mb_id}', '{$mb_id}', '{$now}', '{$now}')";
|
|
sql_query($sql);
|
|
$id = sql_insert_id(); // 새로 생성된 ID를 가져옴
|
|
|
|
// [추가] 생성 로그 기록
|
|
$details = "새 템플릿 생성: " . $title;
|
|
$this->insertTemplateLog($id, 'insert', $mb_id, $details);
|
|
}
|
|
|
|
// [핵심 추가] 템플릿 변수 동기화
|
|
$this->syncTemplateVars($id, $content, $data['variables'] ?? []);
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
global $member;
|
|
$id = (int)$id;
|
|
$mb_id = sql_real_escape_string($member['mb_id']);
|
|
$now = G5_TIME_YMDHIS;
|
|
|
|
// [참고] is_deleted 필드는 사용하지 않으므로 실제 삭제 처리합니다.
|
|
// g5_mail_template_vars 테이블은 FOREIGN KEY (ON DELETE CASCADE) 설정으로 자동 삭제됩니다.
|
|
sql_query("DELETE FROM {$this->table} WHERE id = '{$id}'");
|
|
|
|
// [수정] 삭제 로그 기록
|
|
$details = "템플릿 삭제 (ID: {$id})";
|
|
$this->insertTemplateLog($id, 'delete', $mb_id, $details);
|
|
}
|
|
|
|
private function insertTemplateLog($template_id, $action, $changed_by, $details)
|
|
{
|
|
$template_id = (int)$template_id;
|
|
$action = sql_real_escape_string($action);
|
|
$changed_by = sql_real_escape_string($changed_by);
|
|
$details = sql_real_escape_string($details);
|
|
$change_date = G5_TIME_YMDHIS;
|
|
|
|
$sql = "INSERT INTO {$this->log_table}
|
|
(template_id, `action`, changed_by, change_date, change_details)
|
|
VALUES
|
|
('{$template_id}', '{$action}', '{$changed_by}', '{$change_date}', '{$details}')";
|
|
sql_query($sql);
|
|
}
|
|
|
|
/**
|
|
* [핵심 추가] 템플릿 내용의 변수와 DB를 동기화하는 메소드
|
|
*/
|
|
private function syncTemplateVars($template_id, $content, $submitted_vars)
|
|
{
|
|
// 1. 템플릿 내용에서 현재 사용중인 모든 변수 추출 (예: {name}, {order_id})
|
|
preg_match_all('/{(\w+)}/u', $content, $matches);
|
|
$vars_in_content = array_unique($matches[1]);
|
|
|
|
// 2. 이 템플릿과 관련된 기존 변수들을 모두 삭제 (가장 간단하고 확실한 동기화 방법)
|
|
sql_query("DELETE FROM {$this->vars_table} WHERE template_id = '{$template_id}'");
|
|
|
|
// 3. 내용에 존재하는 변수들만 다시 INSERT
|
|
if (!empty($vars_in_content)) {
|
|
$now = G5_TIME_YMDHIS;
|
|
foreach ($vars_in_content as $var_name) {
|
|
// 폼에서 전송된 기본값이 있으면 사용하고, 없으면 빈 문자열로 설정
|
|
$default_value = $submitted_vars[$var_name] ?? '';
|
|
|
|
$sql = "INSERT INTO {$this->vars_table}
|
|
(template_id, var_name, default_value, created_at, updated_at)
|
|
VALUES
|
|
('{$template_id}', '" . sql_real_escape_string($var_name) . "', '" . sql_real_escape_string($default_value) . "', '{$now}', '{$now}')";
|
|
sql_query($sql);
|
|
}
|
|
}
|
|
}
|
|
} |