From f1c78659be9bca80e0866be3ede17274faa047a1 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sun, 30 Sep 2018 17:14:04 +0200 Subject: [PATCH] Optimize user settings --- config-dist.php | 15 ++++++ helpers/extensions.php | 11 ++++ public/js/extensions.js | 13 +++++ public/js/grocy.js | 34 +++++++++++- public/js/grocy_dbchangedhandling.js | 24 +-------- public/js/grocy_nightmode.js | 78 ++-------------------------- public/viewjs/recipeposform.js | 1 - services/UsersService.php | 4 +- views/layout/default.blade.php | 33 +++--------- 9 files changed, 87 insertions(+), 126 deletions(-) diff --git a/config-dist.php b/config-dist.php index 24c4007a..3718c1c7 100644 --- a/config-dist.php +++ b/config-dist.php @@ -25,3 +25,18 @@ Setting('STOCK_BARCODE_LOOKUP_PLUGIN', 'DemoBarcodeLookupPlugin'); # If, however, your webserver does not support URL rewriting, # set this to true Setting('DISABLE_URL_REWRITING', false); + + +# Default user settings +# These settings can be changed per user, here the defaults +# are defined which are used when the user has not changed the setting so far + +# Night mode related +DefaultUserSetting('night_mode_enabled', false); // If night mode is enabled always +DefaultUserSetting('auto_night_mode_enabled', false); // If night mode is enabled automatically when inside a given time range (see the two settings below) +DefaultUserSetting('auto_night_mode_time_range_from', "20:00"); // Format HH:mm +DefaultUserSetting('auto_night_mode_time_range_to', "07:00"); // Format HH:mm + +# If the page should be automatically reloaded when there was +# an external change +DefaultUserSetting('auto_reload_on_db_change', true); diff --git a/helpers/extensions.php b/helpers/extensions.php index 416e2809..3c17414f 100644 --- a/helpers/extensions.php +++ b/helpers/extensions.php @@ -145,6 +145,17 @@ function Setting(string $name, $value) } } +global $GROCY_DEFAULT_USER_SETTINGS; +$GROCY_DEFAULT_USER_SETTINGS = array(); +function DefaultUserSetting(string $name, $value) +{ + global $GROCY_DEFAULT_USER_SETTINGS; + if (!array_key_exists($name, $GROCY_DEFAULT_USER_SETTINGS)) + { + $GROCY_DEFAULT_USER_SETTINGS[$name] = $value; + } +} + function GetUserDisplayName($user) { $displayName = ''; diff --git a/public/js/extensions.js b/public/js/extensions.js index 8e3340fe..eade9f9a 100644 --- a/public/js/extensions.js +++ b/public/js/extensions.js @@ -41,3 +41,16 @@ IsTouchInputDevice = function() return false; } + +BoolVal = function(test) +{ + var anything = test.toString().toLowerCase(); + if (anything === true || anything === "true" || anything === "1" || anything === "on") + { + return true + } + else + { + return false; + } +} diff --git a/public/js/grocy.js b/public/js/grocy.js index be47eb16..4aa2acc7 100644 --- a/public/js/grocy.js +++ b/public/js/grocy.js @@ -14,7 +14,7 @@ }, function(xhr) { - console.log(xhr) + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) } ); } @@ -216,3 +216,35 @@ $("form").on("click", "select", function() { $(this).closest("form").addClass("is-dirty"); }); + +// Auto saving user setting controls +$(".user-setting-control").on("change", function() +{ + var element = $(this); + var inputType = element.attr("type").toLowerCase(); + var settingKey = element.attr("data-setting-key"); + + if (inputType === "checkbox") + { + value = element.is(":checked"); + } + else + { + var value = element.val(); + } + + Grocy.UserSettings[settingKey] = value; + + jsonData = { }; + jsonData.value = value; + Grocy.Api.Post('user/settings/' + settingKey, jsonData, + function(result) + { + // Nothing to do... + }, + function(xhr) + { + Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) + } + ); +}); diff --git a/public/js/grocy_dbchangedhandling.js b/public/js/grocy_dbchangedhandling.js index 055696a2..04f5cdaa 100644 --- a/public/js/grocy_dbchangedhandling.js +++ b/public/js/grocy_dbchangedhandling.js @@ -22,7 +22,7 @@ setInterval(function() { if (Grocy.IdleTime >= 50) { - if (Grocy.AutoReloadOnDatabaseChangeEnabled && $("form.is-dirty").length === 0) + if (BoolVal(Grocy.UserSettings.auto_reload_on_db_change) && $("form.is-dirty").length === 0) { window.location.reload(); } @@ -56,27 +56,7 @@ setInterval(function() Grocy.IdleTime += 1; }, 1000); -$("#auto-reload-enabled").on("change", function() -{ - var value = $(this).is(":checked"); - - Grocy.AutoReloadOnDatabaseChangeEnabled = value; - - jsonData = { }; - jsonData.value = value; - Grocy.Api.Post('user/settings/auto_reload_on_db_change', jsonData, - function(result) - { - // Nothing to do... - }, - function(xhr) - { - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); -}); - -if (Grocy.AutoReloadOnDatabaseChangeEnabled) +if (BoolVal(Grocy.UserSettings.auto_reload_on_db_change)) { $("#auto-reload-enabled").prop("checked", true); } diff --git a/public/js/grocy_nightmode.js b/public/js/grocy_nightmode.js index 5ff42087..05ccba79 100644 --- a/public/js/grocy_nightmode.js +++ b/public/js/grocy_nightmode.js @@ -1,20 +1,6 @@ $("#night-mode-enabled").on("change", function() { var value = $(this).is(":checked"); - - jsonData = { }; - jsonData.value = value; - Grocy.Api.Post('user/settings/night_mode_enabled', jsonData, - function(result) - { - // Nothing to do... - }, - function(xhr) - { - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); - if (value) { $("body").addClass("night-mode"); @@ -28,67 +14,9 @@ $("#auto-night-mode-enabled").on("change", function() { var value = $(this).is(":checked"); - - jsonData = { }; - jsonData.value = value; - Grocy.Api.Post('user/settings/auto_night_mode_enabled', jsonData, - function(result) - { - // Nothing to do... - }, - function(xhr) - { - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); - - if (value) - { - $("#auto-night-mode-time-range-from").prop("readonly", false); - $("#auto-night-mode-time-range-to").prop("readonly", false); - } - else - { - $("#auto-night-mode-time-range-from").prop("readonly", true); - $("#auto-night-mode-time-range-to").prop("readonly", true); - } -}); - -$("#auto-night-mode-time-range-from").on("blur", function() -{ - var value = $(this).val(); - - jsonData = { }; - jsonData.value = value; - Grocy.Api.Post('user/settings/auto_night_mode_time_range_from', jsonData, - function(result) - { - // Nothing to do... - }, - function(xhr) - { - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); -}); - -$("#auto-night-mode-time-range-to").on("blur", function() -{ - var value = $(this).val(); - - jsonData = { }; - jsonData.value = value; - Grocy.Api.Post('user/settings/auto_night_mode_time_range_to', jsonData, - function(result) - { - // Nothing to do... - }, - function(xhr) - { - Grocy.FrontendHelpers.ShowGenericError('Error while saving, probably this item already exists', xhr.response) - } - ); + $("#auto-night-mode-time-range-from").prop("readonly", !value); + $("#auto-night-mode-time-range-to").prop("readonly", !value); }); $("#night-mode-enabled").prop("checked", Grocy.NightModeEnabled); -$("#auto-night-mode-enabled").prop("checked", Grocy.AutoNightModeEnabled); +$("#auto-night-mode-enabled").prop("checked", Grocy.UserSettings.auto_night_mode_enabled); diff --git a/public/viewjs/recipeposform.js b/public/viewjs/recipeposform.js index a3a6312b..71a43db7 100644 --- a/public/viewjs/recipeposform.js +++ b/public/viewjs/recipeposform.js @@ -4,7 +4,6 @@ var jsonData = $('#recipe-pos-form').serializeJSON({ checkboxUncheckedValue: "0" }); jsonData.recipe_id = Grocy.EditObjectParentId; - console.log(jsonData); if (Grocy.EditMode === 'create') { Grocy.Api.Post('add-object/recipes_pos', jsonData, diff --git a/services/UsersService.php b/services/UsersService.php index cf5b4965..2b431cbe 100644 --- a/services/UsersService.php +++ b/services/UsersService.php @@ -73,7 +73,9 @@ class UsersService extends BaseService $settings[$settingRow->key] = $settingRow->value; } - return $settings; + // Use the configured default values for all missing settings + global $GROCY_DEFAULT_USER_SETTINGS; + return array_merge($GROCY_DEFAULT_USER_SETTINGS, $settings); } public function SetUserSetting($userId, $settingKey, $settingValue) diff --git a/views/layout/default.blade.php b/views/layout/default.blade.php index 4831d4d2..34f2b45e 100644 --- a/views/layout/default.blade.php +++ b/views/layout/default.blade.php @@ -42,30 +42,11 @@ Grocy.ActiveNav = '@yield('activeNav', '')'; Grocy.Culture = '{{ GROCY_CULTURE }}'; Grocy.Currency = '{{ GROCY_CURRENCY }}'; - - @if(array_key_exists('auto_reload_on_db_change', $userSettings)) - Grocy.AutoReloadOnDatabaseChangeEnabled = {{ BoolToString($userSettings['auto_reload_on_db_change']) }}; - @else - Grocy.AutoReloadOnDatabaseChangeEnabled = true; - @endif - - @if(array_key_exists('night_mode_enabled', $userSettings)) - @php $nightModeEnabled = boolval($userSettings['night_mode_enabled']); @endphp - Grocy.NightModeEnabled = {{ BoolToString($userSettings['night_mode_enabled']) }}; - @else - @php $nightModeEnabled = false; @endphp - Grocy.NightModeEnabled = false; - @endif - - @if(array_key_exists('auto_night_mode_enabled', $userSettings)) - Grocy.AutoNightModeEnabled = {{ BoolToString($userSettings['auto_night_mode_enabled']) }}; - @else - Grocy.AutoNightModeEnabled = false; - @endif + Grocy.UserSettings = {!! json_encode($userSettings) !!}; - +