|
|
@@ -33,7 +33,11 @@ class multilingual{
|
|
|
|
|
|
/*
|
|
|
* multilingual_load
|
|
|
- */
|
|
|
+ */
|
|
|
+ if ($operate == "multilingual_init") {
|
|
|
+ $this->_multilingual_init();
|
|
|
+ }
|
|
|
+
|
|
|
if ($operate == "multilingual_search") {
|
|
|
$this->_multilingual_search();
|
|
|
}
|
|
|
@@ -47,11 +51,149 @@ class multilingual{
|
|
|
$unverifiedNumber = $multilingual['unverifiedNumber'];
|
|
|
$page = $multilingual['page'];
|
|
|
$data = $multilingual['data'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function _multilingual_init() {
|
|
|
+ // 1. 获取并解析 JSON 数据
|
|
|
+ $rawData = $_POST['josn_input'];
|
|
|
+ $mode = $_POST['mode'];
|
|
|
+ $json = json_decode($rawData, true);
|
|
|
|
|
|
+ if (!$json) {
|
|
|
+ common::echo_json_encode(500, "Invalid JSON format");
|
|
|
+ exit();
|
|
|
+ }
|
|
|
|
|
|
+ // 获取控制模式,默认为 'update'
|
|
|
+ $mode = empty($mode) ? 'update' : 'replace';
|
|
|
|
|
|
+ $pageKey = isset($json['page']) ? $json['page'] : '';
|
|
|
+ $unverifiedNumber = isset($json['unverifiedNumber']) ? intval($json['unverifiedNumber']) : 0;
|
|
|
+ $dataList = isset($json['data']) ? $json['data'] : [];
|
|
|
+
|
|
|
+ if (empty($pageKey)) {
|
|
|
+ common::echo_json_encode(500, "Missing page key");
|
|
|
+ exit();
|
|
|
}
|
|
|
+
|
|
|
+ // 2. 定义语言映射配置
|
|
|
+ $langMap = [
|
|
|
+ 'traditionalChinese' => ['name' => '繁体中文', 'code' => 'zh-TW'],
|
|
|
+ 'simplifiedChinese' => ['name' => '简体中文', 'code' => 'zh-CN'],
|
|
|
+ 'english' => ['name' => '英语', 'code' => 'en-US'],
|
|
|
+ 'french' => ['name' => '法语', 'code' => 'fr-FR'],
|
|
|
+ 'spanish' => ['name' => '西班牙语', 'code' => 'es-ES'],
|
|
|
+ 'portuguese' => ['name' => '葡萄牙语', 'code' => 'pt-BR']
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 开启事务
|
|
|
+ global $db;
|
|
|
+ $db->StartTrans();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // ==========================================
|
|
|
+ // 第一步:处理页面表 (kln_i18n_pages)
|
|
|
+ // ==========================================
|
|
|
+ $pageId = null;
|
|
|
+
|
|
|
+ // 如果是覆盖模式,先删除旧页面及关联数据
|
|
|
+ if ($mode === 'replace') {
|
|
|
+ // 1. 先删子表 (kln_i18n_keys),防止外键报错
|
|
|
+ $kikSqlDelete = "DELETE FROM kln_i18n_keys WHERE page_id IN (SELECT id FROM kln_i18n_pages WHERE page_key = '$pageKey')";
|
|
|
+ $db->GetRow($kikSqlDelete) or ( (!$db->ErrorMsg()) or error_log(common::dbLog($db, $kikSqlDelete), 0));
|
|
|
+ // 2. 再删主表
|
|
|
+ $kipSqlDelete = "DELETE FROM kln_i18n_pages WHERE page_key = '$pageKey'";
|
|
|
+ $db->GetRow($kipSqlDelete) or ( (!$db->ErrorMsg()) or error_log(common::dbLog($db, $kipSqlDelete), 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询页面是否存在
|
|
|
+ $checkPageSql = "SELECT id FROM kln_i18n_pages WHERE page_key = '$pageKey'";
|
|
|
+ $checkPageRes = common::excuteObjectSql($checkPageSql);
|
|
|
+
|
|
|
+ if (!empty($checkPageRes)) {
|
|
|
+ // 存在 -> 更新
|
|
|
+ $pageId = $checkPageRes[0]['id'];
|
|
|
+ $updatePageSql = "UPDATE kln_i18n_pages SET unverified_number = $unverifiedNumber WHERE id = $pageId";
|
|
|
+ common::excuteObjectSql($updatePageSql);
|
|
|
+ } else {
|
|
|
+ // 不存在 -> 插入
|
|
|
+ $insertPageSql = "INSERT INTO kln_i18n_pages (page_key, unverified_number, description) VALUES ('$pageKey', $unverifiedNumber, '')";
|
|
|
+ common::excuteObjectSql($insertPageSql);
|
|
|
+ // 获取新生成的 ID (PostgreSQL 语法)
|
|
|
+ $pageIdRow = common::excuteObjectSql("SELECT currval('kln_i18n_pages_id_seq')");
|
|
|
+ $pageId = $pageIdRow[0]['currval'];
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==========================================
|
|
|
+ // 第二步:循环处理数据
|
|
|
+ // ==========================================
|
|
|
+ foreach ($dataList as $item) {
|
|
|
+ $transKey = $item['key'];
|
|
|
+ $originValue = isset($item['orginEnglish']) ? $item['orginEnglish'] : '';
|
|
|
+
|
|
|
+ foreach ($langMap as $jsonKey => $langInfo) {
|
|
|
+ // 只处理 JSON 中存在的数据
|
|
|
+ if (isset($item[$jsonKey])) {
|
|
|
+ $transValue = $item[$jsonKey];
|
|
|
+ $statusKey = $jsonKey . 'Status';
|
|
|
+ $status = isset($item[$statusKey]) ? intval($item[$statusKey]) : 0;
|
|
|
+
|
|
|
+ // --- 1. 处理语言表 (kln_i18n_languages) ---
|
|
|
+ $langKey = $jsonKey;
|
|
|
+ $langName = $langInfo['name'];
|
|
|
+ $localeCode = $langInfo['code'];
|
|
|
+
|
|
|
+ // 查询语言是否存在
|
|
|
+ $checkLangSql = "SELECT id FROM kln_i18n_languages WHERE lang_key = '$langKey'";
|
|
|
+ $checkLangRes = common::excuteObjectSql($checkLangSql);
|
|
|
+
|
|
|
+ if (!empty($checkLangRes)) {
|
|
|
+ // 存在 -> 更新
|
|
|
+ $langId = $checkLangRes[0]['id'];
|
|
|
+ $updateLangSql = "UPDATE kln_i18n_languages SET lang_name = '$langName', locale_code = '$localeCode' WHERE id = $langId";
|
|
|
+ common::excuteObjectSql($updateLangSql);
|
|
|
+ } else {
|
|
|
+ // 不存在 -> 插入
|
|
|
+ $insertLangSql = "INSERT INTO kln_i18n_languages (lang_key, lang_name, locale_code, is_active) VALUES ('$langKey', '$langName', '$localeCode', 1)";
|
|
|
+ common::excuteObjectSql($insertLangSql);
|
|
|
+ // 获取 ID
|
|
|
+ $langIdRow = common::excuteObjectSql("SELECT currval('kln_i18n_languages_id_seq')");
|
|
|
+ $langId = $langIdRow[0]['currval'];
|
|
|
+ }
|
|
|
+
|
|
|
+ // --- 2. 处理词条表 (kln_i18n_keys) ---
|
|
|
+ // 查询词条是否存在 (根据 page_id, lang_id, trans_key 唯一性)
|
|
|
+ $checkKeySql = "SELECT id FROM kln_i18n_keys WHERE page_id = $pageId AND lang_id = $langId AND trans_key = '$transKey'";
|
|
|
+ $checkKeyRes = common::excuteObjectSql($checkKeySql);
|
|
|
+
|
|
|
+ if (!empty($checkKeyRes)) {
|
|
|
+ // 存在 -> 更新
|
|
|
+ $updateKeySql = "UPDATE kln_i18n_keys SET trans_value = '$transValue', orgin_value = '$originValue', status = $status WHERE id = " . $checkKeyRes[0]['id'];
|
|
|
+ common::excuteObjectSql($updateKeySql);
|
|
|
+ } else {
|
|
|
+ // 不存在 -> 插入
|
|
|
+ $insertKeySql = "INSERT INTO kln_i18n_keys (page_id, lang_id, trans_key, trans_value, orgin_value, status) VALUES ($pageId, $langId, '$transKey', '$transValue', '$originValue', $status)";
|
|
|
+ common::excuteObjectSql($insertKeySql);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提交事务
|
|
|
+ common::excuteObjectSql("COMMIT");
|
|
|
+ common::echo_json_encode(200, "Initialization successful (Mode: $mode)");
|
|
|
+
|
|
|
+ } catch (Exception $e) {
|
|
|
+ // 发生错误回滚
|
|
|
+ common::excuteObjectSql("ROLLBACK");
|
|
|
+ common::echo_json_encode(500, "Database Error: " . $e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ exit();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
private function _multilingual_search() {
|
|
|
$page = common::check_input($_POST ['page']);
|