From 42ff670928c4a910a8dac3ac09367893f912f71a Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 18 Dec 2021 20:53:59 +0100 Subject: [PATCH] Refactored the general settings page functionality and structure --- application/controllers/General_settings.php | 9 +- application/views/pages/general_settings.php | 266 +++++++++--------- .../js/http/general_settings_http_client.js | 34 +++ assets/js/pages/backend_settings_general.js | 86 ------ assets/js/pages/general_settings.js | 133 +++++++++ 5 files changed, 299 insertions(+), 229 deletions(-) create mode 100644 assets/js/http/general_settings_http_client.js delete mode 100644 assets/js/pages/backend_settings_general.js create mode 100644 assets/js/pages/general_settings.js diff --git a/application/controllers/General_settings.php b/application/controllers/General_settings.php index 3feb1191..007a407f 100644 --- a/application/controllers/General_settings.php +++ b/application/controllers/General_settings.php @@ -54,15 +54,14 @@ class General_settings extends EA_Controller { $user_id = session('user_id'); - $role_slug = session('role_slug'); + script_vars([ + 'general_settings' => $this->settings_model->get(), + ]); html_vars([ 'page_title' => lang('settings'), 'active_menu' => PRIV_SYSTEM_SETTINGS, 'user_display_name' => $this->accounts->get_user_display_name($user_id), - 'timezones' => $this->timezones->to_array(), - 'privileges' => $this->roles_model->get_permissions_by_slug($role_slug), - 'system_settings' => $this->settings_model->get(), ]); $this->load->view('pages/general_settings', html_vars()); @@ -80,7 +79,7 @@ class General_settings extends EA_Controller { throw new Exception('You do not have the required permissions for this task.'); } - $settings = json_decode(request('settings', FALSE), TRUE); + $settings = request('general_settings', FALSE); // Check if phone number settings are valid. diff --git a/application/views/pages/general_settings.php b/application/views/pages/general_settings.php index 7e8aaeeb..e0c73a48 100755 --- a/application/views/pages/general_settings.php +++ b/application/views/pages/general_settings.php @@ -1,162 +1,152 @@ - - -
-
-
-
- - - - - - +
+
+
+
+ +
+ + + + + + + -
-
-
- - -
- +
+
+
+ + +
+ + + +
-
-
- - -
- +
+ + +
+ + + +
-
-
- - -
- +
+ + +
+ + + +
+
+
+ + +
+ + + +
+
+
+ + +
+ + + +
+
+
+ + +
+ + + +
-
- - -
- +
+
+ + +
+ + + +
-
-
- - -
- -
-
-
- - -
- +
+ + +
+ + + +
-
-
- - -
- -
-
-
- - -
- -
-
- - -
-
-
- +
+ +
+ + + - - - + + + + diff --git a/assets/js/http/general_settings_http_client.js b/assets/js/http/general_settings_http_client.js new file mode 100644 index 00000000..aa08ae11 --- /dev/null +++ b/assets/js/http/general_settings_http_client.js @@ -0,0 +1,34 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Open Source Web Scheduler + * + * @package EasyAppointments + * @author A.Tselegidis + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +App.Http.GeneralSettings = (function () { + /** + * Save general settings. + * + * @param {Object} generalSettings + * + * @return {Object} + */ + function save(generalSettings) { + const url = App.Utils.Url.siteUrl('general_settings/save'); + + const data = { + csrf_token: App.Vars.csrf_token, + general_settings: generalSettings + }; + + return $.post(url, data); + } + + return { + save + }; +})(); diff --git a/assets/js/pages/backend_settings_general.js b/assets/js/pages/backend_settings_general.js deleted file mode 100644 index 80c6a232..00000000 --- a/assets/js/pages/backend_settings_general.js +++ /dev/null @@ -1,86 +0,0 @@ -/* ---------------------------------------------------------------------------- - * Easy!Appointments - Open Source Web Scheduler - * - * @package EasyAppointments - * @author A.Tselegidis - * @copyright Copyright (c) Alex Tselegidis - * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 - * @link https://easyappointments.org - * @since v1.0.0 - * ---------------------------------------------------------------------------- */ - -window.BackendSettingsGeneral = window.BackendSettingsGeneral || {}; - -/** - * Backend Settings - * - * Contains the functionality of the backend settings page. Can either work for system or user settings, - * but the actions allowed to the user are restricted to his role (only admin has full privileges). - * - * @module BackendSettingsGeneral - */ -(function (exports) { - 'use strict'; - - // Constants - exports.SETTINGS_SYSTEM = 'SETTINGS_SYSTEM'; - - /** - * Tab settings object. - * - * @type {Object} - */ - var settings = {}; - - /** - * Initialize Page - * - * @param {bool} defaultEventHandlers Optional (true), determines whether to bind the default event handlers. - */ - exports.initialize = function (defaultEventHandlers) { - defaultEventHandlers = defaultEventHandlers || true; - - // Apply setting values from database. - GlobalVariables.settings.system.forEach(function (setting) { - $('input[data-field="' + setting.name + '"]').val(setting.value); - $('select[data-field="' + setting.name + '"]').val(setting.value); - }); - - // Set default settings helper. - settings = new SystemSettingsGeneralHelper(); - - if (defaultEventHandlers) { - bindEventHandlers(); - } - - Backend.placeFooterToBottom(); - }; - - /** - * Bind the backend/settings default event handlers. - * - * This method depends on the backend/settings html, so do not use this method on a different page. - */ - function bindEventHandlers() { - /** - * Event: Save Settings Button "Click" - * - * Store the setting changes into the database. - */ - $('.save-settings').on('click', function () { - var data = settings.get(); - settings.save(data); - }); - - /** - * Event: require phone number switch "Click" - * - * make sure that our phone number is visible when it is required. - */ - $('#require-phone-number').on('click', function () { - if ($(this).prop('checked')) { - setShowToggleValue($('#show-phone-number'), true); - } - }); - } -})(window.BackendSettingsGeneral); diff --git a/assets/js/pages/general_settings.js b/assets/js/pages/general_settings.js new file mode 100644 index 00000000..777ae482 --- /dev/null +++ b/assets/js/pages/general_settings.js @@ -0,0 +1,133 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Open Source Web Scheduler + * + * @package EasyAppointments + * @author A.Tselegidis + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.0.0 + * ---------------------------------------------------------------------------- */ + +/** + * Account + * + * Contains the functionality of the general settings page. + */ +App.Pages.GeneralSettings = (function () { + const $saveSettings = $('#save-settings'); + + /** + * Bind the backend/settings default event handlers. + * + * This method depends on the backend/settings html, so do not use this method on a different page. + */ + function bindEventHandlers() { + /** + * Event: Save Settings Button "Click" + * + * Store the setting changes into the database. + */ + $('.save-settings').on('click', function () { + var data = settings.get(); + settings.save(data); + }); + + /** + * Event: require phone number switch "Click" + * + * make sure that our phone number is visible when it is required. + */ + $('#require-phone-number').on('click', function () { + if ($(this).prop('checked')) { + setShowToggleValue($('#show-phone-number'), true); + } + }); + } + + /** + * Check if the form has invalid values. + * + * @return {Boolean} + */ + function isInvalid() { + try { + $('#general-settings .is-invalid').removeClass('is-invalid'); + + // Validate required fields. + + let missingRequiredFields = false; + + $('#general-settings .required').each(function (index, requiredField) { + const $requiredField = $(requiredField); + + if (!$requiredField.val()) { + $requiredField.addClass('is-invalid'); + missingRequiredFields = true; + } + }); + + if (missingRequiredFields) { + throw new Error(App.Lang.fields_are_required); + } + + return false; + } catch (error) { + Backend.displayNotification(error.message); + return true; + } + } + + function serialize(generalSettings) { + generalSettings.forEach(function (generalSetting) { + $('input[data-field="' + generalSetting.name + '"]').val(generalSetting.value); + $('select[data-field="' + generalSetting.name + '"]').val(generalSetting.value); + }); + } + + function deserialize() { + const generalSettings = []; + + $('[data-field]').each(function (index, field) { + const $field = $(field); + + generalSettings.push({ + name: $field.data('field'), + value: $field.val() + }); + }); + + return generalSettings; + } + + /** + * Save the account information. + */ + function onSaveSettingsClick() { + if (isInvalid()) { + Backend.displayNotification(App.Lang.settings_are_invalid); + + return; + } + + const generalSettings = deserialize(); + + App.Http.GeneralSettings.save(generalSettings).done(function () { + Backend.displayNotification(App.Lang.settings_saved); + }); + } + + function init() { + const generalSettings = App.Vars.general_settings; + + serialize(generalSettings); + + $saveSettings.on('click', onSaveSettingsClick); + + Backend.placeFooterToBottom(); + } + + document.addEventListener('DOMContentLoaded', init); + + return {}; +})();