133 lines
5.0 KiB
PHP
133 lines
5.0 KiB
PHP
<?php
|
|
if (!defined('_GNUBOARD_')) exit;
|
|
|
|
/**
|
|
* UI 리소스 관리 클래스
|
|
* Singleton 패턴을 사용하여 인스턴스를 한 번만 생성하고,
|
|
* 불러온 데이터를 캐시하여 DB 조회를 최소화합니다.
|
|
*/
|
|
class UiManager
|
|
{
|
|
private static $instance = null;
|
|
private $resources = []; // 데이터를 캐시할 배열
|
|
|
|
// 외부에서 new 키워드로 인스턴스 생성을 막음
|
|
private function __construct() {}
|
|
|
|
/**
|
|
* 클래스의 유일한 인스턴스를 반환합니다.
|
|
* @return UiManager
|
|
*/
|
|
public static function getInstance()
|
|
{
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* 'LABEL' 타입의 UI 텍스트를 가져옵니다.
|
|
* @param string $resource_code 리소스 코드
|
|
* @param string $lang 언어 코드 (기본값: 'ko')
|
|
* @return string 라벨 텍스트 (없으면 resource_code 반환)
|
|
*/
|
|
public function get_label($resource_code, $lang = 'ko')
|
|
{
|
|
// 캐시 확인
|
|
if (isset($this->resources['labels'][$lang][$resource_code])) {
|
|
return $this->resources['labels'][$lang][$resource_code];
|
|
}
|
|
|
|
global $g5;
|
|
$resource_code_escaped = sql_real_escape_string($resource_code);
|
|
$lang_escaped = sql_real_escape_string($lang);
|
|
|
|
$sql = "SELECT B.cl_name
|
|
FROM {$g5['ui_manager_table']} AS A
|
|
LEFT JOIN {$g5['common_lang_table']} AS B
|
|
ON (A.um_id = B.target_id AND B.target_table = '{$g5['ui_manager_table']}' AND B.lang_code = '{$lang_escaped}')
|
|
WHERE A.resource_code = '{$resource_code_escaped}' AND A.resource_type = 'LABEL'";
|
|
$row = sql_fetch($sql);
|
|
|
|
$label_text = $row['cl_name'] ?? $resource_code;
|
|
|
|
// 결과 캐시
|
|
$this->resources['labels'][$lang][$resource_code] = $label_text;
|
|
|
|
return $label_text;
|
|
}
|
|
|
|
/**
|
|
* 'DATA' 타입의 옵션 목록을 배열로 가져옵니다.
|
|
* @param string $resource_code 리소스 코드
|
|
* @param string $lang 언어 코드 (기본값: 'ko')
|
|
* @return array 옵션 목록 배열
|
|
*/
|
|
public function get_data($resource_code, $lang = 'ko')
|
|
{
|
|
// 캐시 확인
|
|
if (isset($this->resources['data'][$lang][$resource_code])) {
|
|
return $this->resources['data'][$lang][$resource_code];
|
|
}
|
|
|
|
global $g5;
|
|
$resource_code_escaped = sql_real_escape_string($resource_code);
|
|
$lang_escaped = sql_real_escape_string($lang);
|
|
|
|
$sql_um = "SELECT um_id FROM {$g5['ui_manager_table']} WHERE resource_code = '{$resource_code_escaped}' AND resource_type = 'DATA'";
|
|
$um_row = sql_fetch($sql_um);
|
|
|
|
if (!isset($um_row['um_id'])) {
|
|
$this->resources['data'][$lang][$resource_code] = []; // 빈 결과도 캐시
|
|
return [];
|
|
}
|
|
$um_id = $um_row['um_id'];
|
|
|
|
$sql = "SELECT A.fc_id, A.parent_id, A.fc_key, A.fc_order, B.cl_name
|
|
FROM {$g5['form_category_table']} AS A
|
|
LEFT JOIN {$g5['common_lang_table']} AS B
|
|
ON (A.fc_id = B.target_id AND B.target_table = '{$g5['form_category_table']}' AND B.lang_code = '{$lang_escaped}')
|
|
WHERE A.um_id = '{$um_id}' AND A.is_used = 1 AND A.is_deleted = 0
|
|
ORDER BY A.fc_order, A.fc_id";
|
|
|
|
$result = sql_query($sql);
|
|
$options = [];
|
|
while ($row = sql_fetch_array($result)) {
|
|
$options[] = $row;
|
|
}
|
|
|
|
// 결과 캐시
|
|
$this->resources['data'][$lang][$resource_code] = $options;
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* 'DATA' 타입 리소스를 사용하여 HTML <select> 태그를 생성합니다.
|
|
* @param string $resource_code 리소스 코드
|
|
* @param string $select_name <select> 태그의 name 속성
|
|
* @param string $selected_value 미리 선택될 옵션의 값(fc_key)
|
|
* @param string $attributes <select> 태그에 추가할 HTML 속성 (e.g., 'id="my-id" class="my-class"')
|
|
* @param string $lang 언어 코드 (기본값: 'ko')
|
|
* @return string 생성된 HTML <select> 태그
|
|
*/
|
|
public function render_select($resource_code, $select_name, $selected_value = '', $attributes = '', $lang = 'ko')
|
|
{
|
|
$options = $this->get_data($resource_code, $lang);
|
|
|
|
if (empty($options)) {
|
|
return "<select name=\"{$select_name}\" {$attributes}><option value=\"\">옵션 없음</option></select>";
|
|
}
|
|
|
|
$html = "<select name=\"{$select_name}\" {$attributes}>";
|
|
$html .= "<option value=\"\">선택</option>";
|
|
foreach ($options as $option) {
|
|
$selected = ($option['fc_key'] == $selected_value) ? ' selected' : '';
|
|
$html .= "<option value=\"" . htmlspecialchars($option['fc_key']) . "\"{$selected}>" . htmlspecialchars($option['cl_name']) . "</option>";
|
|
}
|
|
$html .= "</select>";
|
|
|
|
return $html;
|
|
}
|
|
} |