diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 81d0f3d4..464c5a13 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -11,6 +11,8 @@ var timerRefreshData = '' var modalCallbackFunction = ''; var emptyArr = ['undefined', "", undefined, null, 'null']; +UI_LANG = "English"; + // urlParams = new Proxy(new URLSearchParams(window.location.search), { // get: (searchParams, prop) => searchParams.get(prop.toString()), @@ -100,38 +102,92 @@ function deleteAllCookies() { } + + +// ----------------------------------------------------------------------------- +// Get settings from the .json file generated by the python backend +// ----------------------------------------------------------------------------- +function cacheSettings() +{ + + $.get('api/table_settings.json', function(res) { + + data = res["data"]; + + data.forEach((set) => { + setCache(`pia_set_${set.Code_Name}`, set.Value) + }); + }) +} + +function getSetting (key) { + + result = getCache(`pia_set_${key}`, true); + + if (result == "") + { + console.log(`Setting with key "${key}" not found`) + } + + return result; +} + // ----------------------------------------------------------------------------- // Get language string // ----------------------------------------------------------------------------- function cacheStrings() { + + // handle core strings and translations + var allLanguages = ["en_us","es_es","de_de"]; // needs to be same as in lang.php + + allLanguages.forEach(function (language_code) { + + $.get(`php/templates/language/${language_code}.json`, function(res) { + + Object.entries(res).forEach(([language, translations]) => { + + Object.entries(translations).forEach(([key, value]) => { + + setCache(`pia_lang_${key}_${language}`, value) + }); + }); + + }) + + }); + + // handle strings and translations from plugins $.get('api/table_language_strings.json', function(res) { data = res["data"]; - data.forEach((langString) => { - // console.log(langString) - setCache(`pia_lang_${langString.String_Key}`, langString.String_Value) - }); - + data.forEach((langString) => { + setCache(`pia_lang_${langString.String_Key}_${langString.Language_Code}`, langString.String_Value) + }); }) + } function getString (key) { - // todo - // php/server/utilDB.php?key=Gen_Okay - // need to initilaize session storage - - result = getCache(`pia_lang_${key}`, true); + + UI_LANG = getSetting("UI_LANG"); - if (result == "") + lang_code = 'en_us'; + + switch(UI_LANG) { - getCache(`pia_lang_${key}`, true) - } - else - { - // this is async can't do that + case 'English': + lang_code = 'en_us'; + break; + case 'Spanish': + lang_code = 'es_es'; + break; + case 'German': + lang_code = 'de_de'; + break; } + result = getCache(`pia_lang_${key}_${lang_code}`, true); return result; } @@ -410,8 +466,10 @@ function navigateToDeviceWithIp (ip) { } // initialize +cacheSettings() cacheStrings() + console.log("init pialert_common.js") diff --git a/front/php/server/utilDB.php b/front/php/server/utilDB.php index 4d88c4b8..54f7018f 100644 --- a/front/php/server/utilDB.php +++ b/front/php/server/utilDB.php @@ -1,4 +1,4 @@ - + fetchArray (SQLITE3_ASSOC)) {
- ) '; - $isPlugin = TRUE; - } - - $html = $html.'
- -
-

'.lang($group.'_icon')." ".lang($group.'_display_name').$settingGroupTypeHtml.'

-
-
-
-
'; - $isIn = ' '; // open the first panel only by default on page load - - if($isPlugin) - { - $html = $html. - ''; - } - - // populate settings for each group - foreach ($settings as $set) - { - if($set["Group"] == $group) - { - $html = $html. - '
-
'; - - $html = $html.'
'.$set['Code_Name'].'
'; - - $html = $html. - '
-
'; - - $html = $html.getString ($set['Code_Name'].'_description', $set['Description']); - - $html = $html. - '
-
'; - - // render different input types based on the settings type - $input = ""; - - // text - textbox - if($set['Type'] == 'text' || $set['Type'] == 'string' || $set['Type'] == 'date-time' ) - { - $input = ''; - } - // password - hidden text - elseif ($set['Type'] == 'password') - { - $input = ''; - } - // readonly - elseif ($set['Type'] == 'readonly') - { - $input = ''; - } - // boolean - checkbox - elseif ($set['Type'] == 'boolean' || $set['Type'] == 'integer.checkbox') - { - $checked = ""; - if ($set['Value'] == "True" || $set['Value'] == "1") { $checked = "checked";}; - $input = ''; - - } - // integer - number input - elseif ($set['Type'] == 'integer') - { - $input = ''; - } - // text.select - dropdown - elseif ($set['Type'] == 'text.select') - { - $input = ''; - } - // integer.select - dropdown - elseif ($set['Type'] == 'integer.select') - { - $input = ''; - } - // text.multiselect - elseif ($set['Type'] == 'text.multiselect') - { - $input = ''; - } - // subnets - elseif ($set['Type'] == 'subnets') - { - $input = $input. - '
-
- -
'; - // Add interface button - $input = $input. - '
- -
-
-
'; - - // list all interfaces as options - $input = $input.'
-
'; - // Remove all interfaces button - $input = $input.'
'; - - } - // list - elseif ($set['Type'] == 'list') - { - - $settingKeyOfLists[] = $set['Code_Name']; - - $input = $input. - '
-
- -
'; - // Add interface button - $input = $input. - '
-
'; - - // list all interfaces as options - $input = $input.'
-
'; - // Remove all interfaces button - $input = $input.'
'; - - } - // json - elseif ($set['Type'] == 'json') - { - $input = ''; - } - - $html = $html.$input; - - // render any buttons or additional actions if specified - $eventsHtml = ""; - // if available get all the events associated with this setting - $eventsList = createArray($set['Events']); - - // icon map for the events - // $iconMap = [ - // "test" => [lang("settings_event_tooltip"),""] - // ]; - - if(count($eventsList) > 0 && $set['Type'] != 'json') - { - foreach ($eventsList as $event) { - $eventsHtml = $eventsHtml.' - - - '; - } - } - - $html = $html.$eventsHtml.'
-
'; - } - } - - $html = $html.'
'; - } - - // echo $html; - ?> +
@@ -356,10 +96,6 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {