2021-12-18 22:53:59 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
2022-01-18 15:05:42 +03:00
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
2021-12-18 22:53:59 +03:00
|
|
|
*
|
|
|
|
* @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
|
2021-12-18 23:04:15 +03:00
|
|
|
* @since v1.5.0
|
2021-12-18 22:53:59 +03:00
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2022-01-14 11:26:44 +03:00
|
|
|
* General settings page.
|
2021-12-18 22:53:59 +03:00
|
|
|
*
|
2022-01-14 11:26:44 +03:00
|
|
|
* This module implements the functionality of the general settings page.
|
2021-12-18 22:53:59 +03:00
|
|
|
*/
|
|
|
|
App.Pages.GeneralSettings = (function () {
|
|
|
|
const $saveSettings = $('#save-settings');
|
2022-01-24 20:36:12 +03:00
|
|
|
const $companyLogo = $('#company-logo');
|
|
|
|
const $companyLogoPreview = $('#company-logo-preview');
|
|
|
|
const $removeCompanyLogo = $('#remove-company-logo');
|
2022-01-24 22:58:15 +03:00
|
|
|
const $companyColor = $('#company-color');
|
|
|
|
const $resetCompanyColor = $('#reset-company-color');
|
2022-01-24 20:36:12 +03:00
|
|
|
let companyLogoBase64 = '';
|
2021-12-18 22:53:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2021-12-18 23:14:10 +03:00
|
|
|
$('#general-settings .required').each((index, requiredField) => {
|
2021-12-18 22:53:59 +03:00
|
|
|
const $requiredField = $(requiredField);
|
|
|
|
|
|
|
|
if (!$requiredField.val()) {
|
|
|
|
$requiredField.addClass('is-invalid');
|
|
|
|
missingRequiredFields = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequiredFields) {
|
2022-01-18 10:22:25 +03:00
|
|
|
throw new Error(lang('fields_are_required'));
|
2021-12-18 22:53:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (error) {
|
2022-01-17 07:26:57 +03:00
|
|
|
App.Layouts.Backend.displayNotification(error.message);
|
2021-12-18 22:53:59 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
function deserialize(generalSettings) {
|
2021-12-18 23:14:10 +03:00
|
|
|
generalSettings.forEach((generalSetting) => {
|
2022-01-24 20:36:12 +03:00
|
|
|
if (generalSetting.name === 'company_logo' && generalSetting.value) {
|
|
|
|
companyLogoBase64 = generalSetting.value;
|
|
|
|
$companyLogoPreview.attr('src', generalSetting.value);
|
|
|
|
$companyLogoPreview.prop('hidden', false);
|
|
|
|
$removeCompanyLogo.prop('hidden', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 22:58:15 +03:00
|
|
|
if (generalSetting.name === 'company_color' && generalSetting.value !== '#ffffff') {
|
|
|
|
$resetCompanyColor.prop('hidden', false);
|
|
|
|
}
|
|
|
|
|
2021-12-18 23:04:15 +03:00
|
|
|
$('[data-field="' + generalSetting.name + '"]').val(generalSetting.value);
|
2021-12-18 22:53:59 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
function serialize() {
|
2021-12-18 22:53:59 +03:00
|
|
|
const generalSettings = [];
|
|
|
|
|
2021-12-18 23:14:10 +03:00
|
|
|
$('[data-field]').each((index, field) => {
|
2021-12-18 22:53:59 +03:00
|
|
|
const $field = $(field);
|
|
|
|
|
|
|
|
generalSettings.push({
|
|
|
|
name: $field.data('field'),
|
|
|
|
value: $field.val()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-01-24 20:36:12 +03:00
|
|
|
generalSettings.push({
|
|
|
|
name: 'company_logo',
|
|
|
|
value: companyLogoBase64
|
|
|
|
});
|
|
|
|
|
2021-12-18 22:53:59 +03:00
|
|
|
return generalSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the account information.
|
|
|
|
*/
|
|
|
|
function onSaveSettingsClick() {
|
|
|
|
if (isInvalid()) {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('settings_are_invalid'));
|
2021-12-18 22:53:59 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
const generalSettings = serialize();
|
2021-12-18 22:53:59 +03:00
|
|
|
|
2021-12-18 23:14:10 +03:00
|
|
|
App.Http.GeneralSettings.save(generalSettings).done(() => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('settings_saved'));
|
2021-12-18 22:53:59 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:36:12 +03:00
|
|
|
/**
|
|
|
|
* Convert the selected image to a base64 encoded string.
|
|
|
|
*/
|
|
|
|
function onCompanyLogoChange() {
|
|
|
|
const file = $companyLogo[0].files[0];
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
$removeCompanyLogo.trigger('click');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
App.Utils.File.toBase64(file).then((base64) => {
|
|
|
|
companyLogoBase64 = base64;
|
|
|
|
$companyLogoPreview.attr('src', base64);
|
|
|
|
$companyLogoPreview.prop('hidden', false);
|
|
|
|
$removeCompanyLogo.prop('hidden', false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the company logo data.
|
|
|
|
*/
|
|
|
|
function onRemoveCompanyLogoClick() {
|
|
|
|
companyLogoBase64 = '';
|
|
|
|
$companyLogo.val('');
|
|
|
|
$companyLogoPreview.attr('src', '#');
|
|
|
|
$companyLogoPreview.prop('hidden', true);
|
|
|
|
$removeCompanyLogo.prop('hidden', true);
|
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
2022-01-24 22:58:15 +03:00
|
|
|
* Toggle the reset company color button.
|
2022-01-14 11:26:44 +03:00
|
|
|
*/
|
2022-01-24 22:58:15 +03:00
|
|
|
function onCompanyColorChange() {
|
2022-01-25 00:38:45 +03:00
|
|
|
$resetCompanyColor.prop('hidden', $companyColor.val() === '#ffffff');
|
2022-01-24 22:58:15 +03:00
|
|
|
}
|
2021-12-18 22:53:59 +03:00
|
|
|
|
2022-01-24 22:58:15 +03:00
|
|
|
/**
|
|
|
|
* Set the company color value to "#ffffff" which is the default one.
|
|
|
|
*/
|
|
|
|
function onResetCompanyColorClick() {
|
|
|
|
$companyColor.val('#ffffff');
|
|
|
|
}
|
2021-12-18 22:53:59 +03:00
|
|
|
|
2022-01-24 22:58:15 +03:00
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
|
|
|
function initialize() {
|
2021-12-18 22:53:59 +03:00
|
|
|
$saveSettings.on('click', onSaveSettingsClick);
|
|
|
|
|
2022-01-24 20:36:12 +03:00
|
|
|
$companyLogo.on('change', onCompanyLogoChange);
|
|
|
|
|
|
|
|
$removeCompanyLogo.on('click', onRemoveCompanyLogoClick);
|
|
|
|
|
2022-01-24 22:58:15 +03:00
|
|
|
$companyColor.on('change', onCompanyColorChange);
|
|
|
|
|
|
|
|
$resetCompanyColor.on('click', onResetCompanyColorClick);
|
|
|
|
|
|
|
|
const generalSettings = vars('general_settings');
|
|
|
|
|
|
|
|
deserialize(generalSettings);
|
|
|
|
|
2022-01-17 07:26:57 +03:00
|
|
|
App.Layouts.Backend.placeFooterToBottom();
|
2021-12-18 22:53:59 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 10:31:12 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
2021-12-18 22:53:59 +03:00
|
|
|
|
|
|
|
return {};
|
|
|
|
})();
|