Add matomo analytics settings page

This commit is contained in:
Alex Tselegidis 2022-07-26 16:39:31 +03:00
parent 8eddb768d4
commit ec0924fb7e
4 changed files with 304 additions and 0 deletions

View file

@ -0,0 +1,105 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Matomo Analytics settings controller.
*
* Handles Matomo Analytics settings related operations.
*
* @package Controllers
*/
class Matomo_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('matomo_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,
'matomo_analytics_settings' => $this->settings_model->get('name like "matomo_analytics_%"'),
]);
html_vars([
'page_title' => lang('matomo_analytics'),
'active_menu' => PRIV_SYSTEM_SETTINGS,
'user_display_name' => $this->accounts->get_user_display_name($user_id),
]);
$this->load->view('pages/matomo_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('matomo_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);
}
}
}

View file

@ -0,0 +1,53 @@
<?php extend('layouts/backend_layout') ?>
<?php section('content') ?>
<div id="matomo-analytics-settings-page" class="container backend-page">
<div id="matomo-analytics-settings">
<div class="row">
<div class="col-lg-8 offset-lg-2">
<form>
<fieldset>
<legend class="d-flex justify-content-between align-items-center border-bottom mb-4 py-2">
<?= lang('matomo_analytics') ?>
<?php if (can('edit', PRIV_SYSTEM_SETTINGS)): ?>
<button type="button" id="save-settings" class="btn btn-primary">
<i class="fas fa-check-square me-2"></i>
<?= lang('save') ?>
</button>
<?php endif ?>
</legend>
<div class="row">
<div class="col-12">
<div class="mb-3">
<label class="form-label" for="google-matomo_analytics_urlanalytics-code">
<?= lang('matomo_analytics_url') ?>
</label>
<input id="matomo-analytics-url" placeholder="//example.org/url/to/matomo/"
data-field="matomo_analytics_url" class="form-control">
<div class="form-text text-muted">
<small>
<?= lang('matomo_analytics_url_hint') ?>
</small>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
<?php section('content') ?>
<?php section('scripts') ?>
<script src="<?= asset_url('assets/js/utils/url.js') ?>"></script>
<script src="<?= asset_url('assets/js/http/matomo_analytics_settings_http_client.js') ?>"></script>
<script src="<?= asset_url('assets/js/pages/matomo_analytics_settings.js') ?>"></script>
<?php section('scripts') ?>

View file

@ -0,0 +1,39 @@
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Matomo Analytics Settings HTTP client.
*
* This module implements the Matomo Analytics settings related HTTP requests.
*/
App.Http.MatomoAnalyticsSettings = (function () {
/**
* Save Matomo Analytics settings.
*
* @param {Object} matomoAnalyticsSettings
*
* @return {Object}
*/
function save(matomoAnalyticsSettings) {
const url = App.Utils.Url.siteUrl('matomo_analytics_settings/save');
const data = {
csrf_token: vars('csrf_token'),
matomo_analytics_settings: matomoAnalyticsSettings
};
return $.post(url, data);
}
return {
save
};
})();

View file

@ -0,0 +1,107 @@
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/**
* Matomo Analytics settings page.
*
* This module implements the functionality of the Matomo Analytics settings page.
*/
App.Pages.MatomoAnalyticsSettings = (function () {
const $saveSettings = $('#save-settings');
/**
* Check if the form has invalid values.
*
* @return {Boolean}
*/
function isInvalid() {
try {
$('#matomo-analytics-settings .is-invalid').removeClass('is-invalid');
// Validate required fields.
let missingRequiredFields = false;
$('#matomo-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(matomoAnalyticsSettings) {
matomoAnalyticsSettings.forEach((matomoAnalyticsSetting) => {
$('[data-field="' + matomoAnalyticsSetting.name + '"]').val(matomoAnalyticsSetting.value);
});
}
function serialize() {
const matomoAnalyticsSettings = [];
$('[data-field]').each((index, field) => {
const $field = $(field);
matomoAnalyticsSettings.push({
name: $field.data('field'),
value: $field.val()
});
});
return matomoAnalyticsSettings;
}
/**
* Save the account information.
*/
function onSaveSettingsClick() {
if (isInvalid()) {
App.Layouts.Backend.displayNotification(lang('settings_are_invalid'));
return;
}
const matomoAnalyticsSettings = serialize();
App.Http.MatomoAnalyticsSettings.save(matomoAnalyticsSettings).done(() => {
App.Layouts.Backend.displayNotification(lang('settings_saved'));
});
}
/**
* Initialize the module.
*/
function initialize() {
$saveSettings.on('click', onSaveSettingsClick);
const matomoAnalyticsSettings = vars('matomo_analytics_settings');
deserialize(matomoAnalyticsSettings);
App.Layouts.Backend.placeFooterToBottom();
}
document.addEventListener('DOMContentLoaded', initialize);
return {};
})();