2021-12-18 23:36:25 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web 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
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
2022-01-14 11:26:44 +03:00
|
|
|
* Business settings page.
|
2021-12-18 23:36:25 +03:00
|
|
|
*
|
2022-01-14 11:26:44 +03:00
|
|
|
* This module implements the functionality of the business settings page.
|
2021-12-18 23:36:25 +03:00
|
|
|
*/
|
|
|
|
App.Pages.BusinessSettings = (function () {
|
|
|
|
const $saveSettings = $('#save-settings');
|
|
|
|
const $applyGlobalWorkingPlan = $('#apply-global-working-plan');
|
|
|
|
let workingPlanManager = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the form has invalid values.
|
|
|
|
*
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
function isInvalid() {
|
|
|
|
try {
|
|
|
|
$('#business-settings .is-invalid').removeClass('is-invalid');
|
|
|
|
|
|
|
|
// Validate required fields.
|
|
|
|
|
|
|
|
let missingRequiredFields = false;
|
|
|
|
|
|
|
|
$('#business-settings .required').each((index, requiredField) => {
|
|
|
|
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 23:36:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (error) {
|
2022-01-17 07:26:57 +03:00
|
|
|
App.Layouts.Backend.displayNotification(error.message);
|
2021-12-18 23:36:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
function deserialize(businessSettings) {
|
2021-12-18 23:36:25 +03:00
|
|
|
businessSettings.forEach((businessSetting) => {
|
|
|
|
$('[data-field="' + businessSetting.name + '"]').val(businessSetting.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
function serialize() {
|
2021-12-18 23:36:25 +03:00
|
|
|
const businessSettings = [];
|
|
|
|
|
|
|
|
$('[data-field]').each((index, field) => {
|
|
|
|
const $field = $(field);
|
|
|
|
|
|
|
|
businessSettings.push({
|
|
|
|
name: $field.data('field'),
|
|
|
|
value: $field.val()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const workingPlan = workingPlanManager.get();
|
|
|
|
|
|
|
|
businessSettings.push({
|
|
|
|
name: 'company_working_plan',
|
|
|
|
value: JSON.stringify(workingPlan)
|
|
|
|
});
|
|
|
|
|
|
|
|
return businessSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 23:36:25 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
const businessSettings = serialize();
|
2021-12-18 23:36:25 +03:00
|
|
|
|
|
|
|
App.Http.BusinessSettings.save(businessSettings).done(() => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('settings_saved'));
|
2021-12-18 23:36:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the global working plan information.
|
|
|
|
*/
|
|
|
|
function onApplyGlobalWorkingPlan() {
|
2022-01-17 23:20:29 +03:00
|
|
|
const buttons = [
|
2021-12-18 23:36:25 +03:00
|
|
|
{
|
2022-01-18 10:22:25 +03:00
|
|
|
text: lang('cancel'),
|
2022-01-17 23:20:29 +03:00
|
|
|
click: () => {
|
2021-12-18 23:36:25 +03:00
|
|
|
$('#message-box').dialog('close');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'OK',
|
2022-01-17 23:20:29 +03:00
|
|
|
click: () => {
|
2021-12-18 23:36:25 +03:00
|
|
|
const workingPlan = workingPlanManager.get();
|
|
|
|
|
|
|
|
App.Http.BusinessSettings.applyGlobalWorkingPlan(workingPlan)
|
|
|
|
.done(() => {
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Layouts.Backend.displayNotification(lang('working_plans_got_updated'));
|
2021-12-18 23:36:25 +03:00
|
|
|
})
|
2022-01-18 13:14:45 +03:00
|
|
|
.always(() => {
|
2021-12-18 23:36:25 +03:00
|
|
|
$('#message-box').dialog('close');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2022-01-18 10:22:25 +03:00
|
|
|
App.Utils.Message.show(lang('working_plan'), lang('overwrite_existing_working_plans'), buttons);
|
2021-12-18 23:36:25 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 11:26:44 +03:00
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
2022-01-14 10:31:12 +03:00
|
|
|
function initialize() {
|
2022-01-18 10:18:22 +03:00
|
|
|
const businessSettings = vars('business_settings');
|
2021-12-18 23:36:25 +03:00
|
|
|
|
2021-12-20 10:47:16 +03:00
|
|
|
deserialize(businessSettings);
|
2021-12-18 23:36:25 +03:00
|
|
|
|
2022-01-11 12:53:32 +03:00
|
|
|
let companyWorkingPlan = {};
|
2021-12-18 23:36:25 +03:00
|
|
|
|
2022-01-18 10:18:22 +03:00
|
|
|
vars('business_settings').forEach((businessSetting) => {
|
2022-01-17 23:20:29 +03:00
|
|
|
if (businessSetting.name === 'company_working_plan') {
|
|
|
|
companyWorkingPlan = JSON.parse(businessSetting.value);
|
2021-12-18 23:36:25 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-01-11 12:53:32 +03:00
|
|
|
workingPlanManager = new App.Utils.WorkingPlan();
|
|
|
|
workingPlanManager.setup(companyWorkingPlan);
|
2021-12-18 23:36:25 +03:00
|
|
|
workingPlanManager.timepickers(false);
|
2022-01-18 10:47:16 +03:00
|
|
|
workingPlanManager.addEventListeners();
|
2021-12-18 23:36:25 +03:00
|
|
|
|
|
|
|
$saveSettings.on('click', onSaveSettingsClick);
|
|
|
|
|
|
|
|
$applyGlobalWorkingPlan.on('click', onApplyGlobalWorkingPlan);
|
|
|
|
|
2022-01-17 07:26:57 +03:00
|
|
|
App.Layouts.Backend.placeFooterToBottom();
|
2021-12-18 23:36:25 +03:00
|
|
|
}
|
|
|
|
|
2022-01-14 10:31:12 +03:00
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
2021-12-18 23:36:25 +03:00
|
|
|
|
|
|
|
return {};
|
|
|
|
})();
|