From 88301c97eb9307edf2906b2874f09a339c73553a Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sat, 5 Aug 2023 15:05:10 +1000 Subject: [PATCH] docs + template setting override work --- docs/SETTINGS_SYSTEM.md | 4 +- front/css/pialert.css | 24 +++++++ front/php/server/util.php | 2 +- front/php/templates/language/en_us.json | 4 +- front/plugins/README.md | 2 +- front/plugins/known_template/config.json | 33 +++++++--- front/settings.php | 84 ++++++++++++++++++------ 7 files changed, 119 insertions(+), 34 deletions(-) diff --git a/docs/SETTINGS_SYSTEM.md b/docs/SETTINGS_SYSTEM.md index 661705ec..8dd44c80 100644 --- a/docs/SETTINGS_SYSTEM.md +++ b/docs/SETTINGS_SYSTEM.md @@ -22,14 +22,14 @@ The json file is also cached on the client-side local storage of the browser. #### pialert.conf -> [!INFO] +> [!NOTE] > This is the source of truth for settings. User-defined values in this files always override default values specified in the Plugin definition. The App generates two `pialert.conf` entries for every setting (Since version 23.8+). One entry is the setting value, the second is the `__metadata` associated with the setting. This `__metadata` entry contains the full setting definition in JSON format. This should helps the future extensibility of the Settings system. #### Plugin settings -> [!INFO] +> [!NOTE] > This is the preferred way adding settings going forward. I'll be likely migrating all app settings into plugin-based settings. Plugin settings are loaded dynamically from the `config.json` of individual plugins. If a setting isn't defined in the `pialert.conf` file, it is initialized via the `default_value` property of a setting from the `config.json` file. Check the [Plugins documentation](/front/plugins/README.md), section `⚙ Setting object structure` for details on the structure of the setting. diff --git a/front/css/pialert.css b/front/css/pialert.css index 24d017db..45dc9dc6 100755 --- a/front/css/pialert.css +++ b/front/css/pialert.css @@ -698,6 +698,8 @@ height: 50px; margin-top: 50px; } +/* Settings */ + .settings_content { padding: 10px; /* background-color: #272c30; */ @@ -720,6 +722,28 @@ height: 50px; width: auto } +.override{ + padding: 5px; +} + +.override .override-text +{ + float:left; + width: 80%; +} + +.override .override-check +{ + float:left; + width: 20%; +} + +#settingsPage input[readonly] { + /* Apply styles to the readonly input */ + background-color: #646566; + color: #000; + cursor: not-allowed; + } /* Devices */ .drp-edit diff --git a/front/php/server/util.php b/front/php/server/util.php index 8d20e99f..b4ee1e3e 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -312,7 +312,7 @@ function saveSettings() $temp = '['.$temp.']'; // wrap brackets $txt .= $settingKey . "=" . $temp . "\n"; - } elseif ($settingType == 'json' || substr($settingType, -9) === ".template") { + } elseif ($settingType == 'json') { $txt .= $settingKey . "=" . $settingValue . "\n"; } } diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index 6ab01bc5..45c8b541 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -421,8 +421,8 @@ "settings_old" : "The settings in the DB (shown on this page) are outdated. This is probably caused by a running scan. The settings were saved in the pialert.conf file, but the background process didn not have time to import it yet to the DB. You can wait until the settings get refreshed so you do not overwrite your old values. Feel free to save your settings either way if you don not mind losing the settings between the last save and now. There are also backup files created if you need to compare your settings later.", "settings_imported" : "Last time settings were imported from the pialert.conf file:", "settings_expand_all" : "Expand all", - "Setting_Override" : "Override default", - "Setting_Override_Description" : "TBD", + "Setting_Override" : "Override value", + "Setting_Override_Description" : "Enabling this option will override an App supplied default value with the value specified above.", "General_display_name" : "General", "General_icon" : "", "ENABLE_ARPSCAN_name" : "Enable ARP scan", diff --git a/front/plugins/README.md b/front/plugins/README.md index e74f317f..7ff75a88 100755 --- a/front/plugins/README.md +++ b/front/plugins/README.md @@ -384,7 +384,7 @@ Below are some general additional notes, when defining `params`: #### ⚙ Setting object structure -> [!INFO] +> [!NOTE] > The settings flow and when Plugin specific settings are applied is described under the [Settings system](/docs/SETTINGS_SYSTEM.md). Required attributes are: diff --git a/front/plugins/known_template/config.json b/front/plugins/known_template/config.json index f83ad0f6..85190a54 100755 --- a/front/plugins/known_template/config.json +++ b/front/plugins/known_template/config.json @@ -168,13 +168,9 @@ }, { "function": "dev_Name", - "type": "text.template", + "type": "readonly", "maxLength": 50, "default_value": "(unknown)", - "override_value": { - "value":"(unknown)", - "override": true - }, "options": [], "localized": ["name", "description"], "name": [ @@ -195,6 +191,9 @@ "type": "string", "maxLength": 30, "default_value": "House", + "override_value": { + "override": false + }, "options": [], "localized": ["name", "description"], "name": [ @@ -254,6 +253,9 @@ "function": "dev_Favorite", "type": "integer.checkbox", "default_value": 0, + "override_value": { + "override": false + }, "options": [], "localized": ["name", "description"], "name": [ @@ -274,6 +276,9 @@ "type": "string", "maxLength": 10, "default_value": "", + "override_value": { + "override": false + }, "options": [], "localized": ["name", "description"], "name": [ @@ -371,7 +376,10 @@ { "function": "dev_StaticIP", "type": "integer.checkbox", - "default_value": 0, + "default_value": 1, + "override_value": { + "override": true + }, "options": [], "localized": ["name", "description"], "name": [ @@ -391,6 +399,9 @@ "function": "dev_ScanCycle", "type": "integer.checkbox", "default_value": 1, + "override_value": { + "override": true + }, "options": [], "localized": ["name", "description"], "name": [ @@ -409,7 +420,10 @@ { "function": "dev_LogEvents", "type": "integer.checkbox", - "default_value": 1, + "default_value": 0, + "override_value": { + "override": false + }, "options": [], "localized": ["name", "description"], "name": [ @@ -427,7 +441,10 @@ { "function": "dev_AlertEvents", "type": "integer.checkbox", - "default_value": 1, + "default_value": 0, + "override_value": { + "override": true + }, "options": [], "localized": ["name", "description"], "name": [ diff --git a/front/settings.php b/front/settings.php index 1cca1579..03873853 100755 --- a/front/settings.php +++ b/front/settings.php @@ -217,34 +217,36 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { //pre-check if this is a json object that needs value extraction - let overridable = false; - let override = false; - let overrideValue = val; + let overridable = false; // indicates if the setting is overridable + let override = false; // If the setting is set to be overriden by the user or by default + let readonly = ""; // helper variable to make text input readonly + let disabled = ""; // helper variable to make checkbox input readonly // TODO finish if ('override_value' in setObj) { overridable = true; - const overrideObj = setObj["override_value"] - const override = overrideObj["override"]; - overrideValue = overrideObj["value"]; + overrideObj = setObj["override_value"] + override = overrideObj["override"]; - console.log(isJsonObject(val)) console.log(setObj) console.log(group) } + // prepare override checkbox and HTML if(overridable) - { - let checked = override; - overrideHtml = `
- + { + let checked = override ? 'checked' : ''; + + overrideHtml = `
+
+ +
+
${getString("Setting_Override")} - - - - -
`; - } +
+
`; + + } // INPUT @@ -261,7 +263,14 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { { inputHtml = generateInputOptions(set, inputHtml, isMultiSelect = true) } else{ - inputHtml = ``; + + // if it's overridable set readonly accordingly + if(overridable) + { + override ? readonly = "" : readonly = " readonly" ; + } + + inputHtml = ``; } } else if (setType === 'integer') { inputHtml = ``; @@ -270,8 +279,15 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { } else if (setType === 'readonly') { inputHtml = ``; } else if (setType === 'boolean' || setType === 'integer.checkbox') { - let checked = val === 'True' || val === '1' ? 'checked' : ''; - inputHtml = ``; + let checked = val === 'True' || val === '1' ? 'checked' : ''; + + // if it's overridable set readonly accordingly + if(overridable) + { + override ? disabled = "" : disabled = " disabled" ; + } + + inputHtml = ``; } else if (setType === 'integer.select') { inputHtml = generateInputOptions(set, inputHtml) @@ -388,6 +404,34 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { return input; } + + + // --------------------------------------------------------- + // Helper methods + // --------------------------------------------------------- + // Toggle readonly mode of teh target element specified by the id in the "my-input-toggle-readonly" attribute + function overrideToggle(element) { + settingsChanged(); + + targetId = $(element).attr("my-input-toggle-readonly"); + + inputElement = $(`#${targetId}`)[0]; + + if (!inputElement) { + console.error("Input element not found!"); + return; + } + + if (inputElement.type === "text" || inputElement.type === "password") { + inputElement.readOnly = !inputElement.readOnly; + } else if (inputElement.type === "checkbox") { + inputElement.disabled = !inputElement.disabled; + } else { + console.warn("Unsupported input type. Only text, password, and checkbox inputs are supported."); + } + + } + // --------------------------------------------------------- // Generate an array object from a string representation of an array function createArray(input) {