From 8eddb768d439139555ec2415a5c086df68aeaa75 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Tue, 26 Jul 2022 16:39:15 +0300 Subject: [PATCH] Add google analytics settings page --- .../controllers/Google_analytics_settings.php | 105 +++++++++++++++++ .../views/pages/google_analytics_settings.php | 53 +++++++++ .../google_analytics_settings_http_client.js | 39 +++++++ assets/js/pages/google_analytics_settings.js | 107 ++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 application/controllers/Google_analytics_settings.php create mode 100644 application/views/pages/google_analytics_settings.php create mode 100644 assets/js/http/google_analytics_settings_http_client.js create mode 100644 assets/js/pages/google_analytics_settings.js diff --git a/application/controllers/Google_analytics_settings.php b/application/controllers/Google_analytics_settings.php new file mode 100644 index 00000000..25ee0151 --- /dev/null +++ b/application/controllers/Google_analytics_settings.php @@ -0,0 +1,105 @@ + + * @copyright Copyright (c) Alex Tselegidis + * @license https://opensource.org/licenses/GPL-3.0 - GPLv3 + * @link https://easyappointments.org + * @since v1.5.0 + * ---------------------------------------------------------------------------- */ + +/** + * Google Analytics settings controller. + * + * Handles Google Analytics settings related operations. + * + * @package Controllers + */ +class Google_analytics_settings extends EA_Controller { + /** + * Calendar constructor. + */ + public function __construct() + { + parent::__construct(); + + $this->load->model('settings_model'); + + $this->load->library('accounts'); + } + + /** + * Render the settings page. + */ + public function index() + { + session(['dest_url' => site_url('google_analytics_settings')]); + + $user_id = session('user_id'); + + if (cannot('view', PRIV_SYSTEM_SETTINGS)) + { + if ($user_id) + { + abort(403, 'Forbidden'); + } + + redirect('login'); + + return; + } + + $role_slug = session('role_slug'); + + script_vars([ + 'user_id' => $user_id, + 'role_slug' => $role_slug, + 'google_analytics_settings' => $this->settings_model->get('name like "google_analytics_%"'), + ]); + + html_vars([ + 'page_title' => lang('google_analytics'), + 'active_menu' => PRIV_SYSTEM_SETTINGS, + 'user_display_name' => $this->accounts->get_user_display_name($user_id), + ]); + + $this->load->view('pages/google_analytics_settings'); + } + + /** + * Save general settings. + */ + public function save() + { + try + { + if (cannot('edit', PRIV_SYSTEM_SETTINGS)) + { + throw new RuntimeException('You do not have the required permissions for this task.'); + } + + $settings = request('google_analytics_settings', []); + + foreach ($settings as $setting) + { + $existing_setting = $this->settings_model->query()->where('name', $setting['name'])->get()->row_array(); + + if ( ! empty($existing_setting)) + { + $setting['id'] = $existing_setting['id']; + } + + $this->settings_model->save($setting); + } + + response(); + } + catch (Throwable $e) + { + json_exception($e); + } + } +} diff --git a/application/views/pages/google_analytics_settings.php b/application/views/pages/google_analytics_settings.php new file mode 100644 index 00000000..c96c7aa3 --- /dev/null +++ b/application/views/pages/google_analytics_settings.php @@ -0,0 +1,53 @@ + + + + +
+
+
+
+
+
+ + + + + + + + +
+
+
+ + +
+ + + +
+
+
+
+
+
+
+
+
+
+ + + + + + + + + + diff --git a/assets/js/http/google_analytics_settings_http_client.js b/assets/js/http/google_analytics_settings_http_client.js new file mode 100644 index 00000000..de24aabd --- /dev/null +++ b/assets/js/http/google_analytics_settings_http_client.js @@ -0,0 +1,39 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Online Appointment 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 + * ---------------------------------------------------------------------------- */ + +/** + * Google Analytics Settings HTTP client. + * + * This module implements the Google Analytics settings related HTTP requests. + */ +App.Http.GoogleAnalyticsSettings = (function () { + /** + * Save Google Analytics settings. + * + * @param {Object} googleAnalyticsSettings + * + * @return {Object} + */ + function save(googleAnalyticsSettings) { + const url = App.Utils.Url.siteUrl('google_analytics_settings/save'); + + const data = { + csrf_token: vars('csrf_token'), + google_analytics_settings: googleAnalyticsSettings + }; + + return $.post(url, data); + } + + return { + save + }; +})(); diff --git a/assets/js/pages/google_analytics_settings.js b/assets/js/pages/google_analytics_settings.js new file mode 100644 index 00000000..063c49d6 --- /dev/null +++ b/assets/js/pages/google_analytics_settings.js @@ -0,0 +1,107 @@ +/* ---------------------------------------------------------------------------- + * Easy!Appointments - Online Appointment 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 + * ---------------------------------------------------------------------------- */ + +/** + * Google Analytics settings page. + * + * This module implements the functionality of the Google Analytics settings page. + */ +App.Pages.GoogleAnalyticsSettings = (function () { + const $saveSettings = $('#save-settings'); + + /** + * Check if the form has invalid values. + * + * @return {Boolean} + */ + function isInvalid() { + try { + $('#google-analytics-settings .is-invalid').removeClass('is-invalid'); + + // Validate required fields. + + let missingRequiredFields = false; + + $('#google-analytics-settings .required').each((index, requiredField) => { + const $requiredField = $(requiredField); + + if (!$requiredField.val()) { + $requiredField.addClass('is-invalid'); + missingRequiredFields = true; + } + }); + + if (missingRequiredFields) { + throw new Error(lang('fields_are_required')); + } + + return false; + } catch (error) { + App.Layouts.Backend.displayNotification(error.message); + return true; + } + } + + function deserialize(googleAnalyticsSettings) { + googleAnalyticsSettings.forEach((googleAnalyticsSetting) => { + $('[data-field="' + googleAnalyticsSetting.name + '"]').val(googleAnalyticsSetting.value); + }); + } + + function serialize() { + const googleAnalyticsSettings = []; + + $('[data-field]').each((index, field) => { + const $field = $(field); + + googleAnalyticsSettings.push({ + name: $field.data('field'), + value: $field.val() + }); + }); + + return googleAnalyticsSettings; + } + + /** + * Save the account information. + */ + function onSaveSettingsClick() { + if (isInvalid()) { + App.Layouts.Backend.displayNotification(lang('settings_are_invalid')); + + return; + } + + const googleAnalyticsSettings = serialize(); + + App.Http.GoogleAnalyticsSettings.save(googleAnalyticsSettings).done(() => { + App.Layouts.Backend.displayNotification(lang('settings_saved')); + }); + } + + /** + * Initialize the module. + */ + function initialize() { + $saveSettings.on('click', onSaveSettingsClick); + + const googleAnalyticsSettings = vars('google_analytics_settings'); + + deserialize(googleAnalyticsSettings); + + App.Layouts.Backend.placeFooterToBottom(); + } + + document.addEventListener('DOMContentLoaded', initialize); + + return {}; +})();