easyappointments/assets/js/backend_settings.js
Tthecreator 5cff20f4bf
Fix warnings given by npm start by incorrect var/let.
Apply suggestions from @vitormattos.

Co-authored-by: Vitor Mattos <vitor@php.rio>
2021-06-13 16:45:25 +00:00

337 lines
12 KiB
JavaScript

/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
* @link http://easyappointments.org
* @since v1.0.0
* ---------------------------------------------------------------------------- */
window.BackendSettings = window.BackendSettings || {};
/**
* 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 BackendSettings
*/
(function (exports) {
'use strict';
// Constants
exports.SETTINGS_SYSTEM = 'SETTINGS_SYSTEM';
exports.SETTINGS_USER = 'SETTINGS_USER';
/**
* Use this WorkingPlan class instance to perform actions on the page's working plan tables.
*
* @type {WorkingPlan}
*/
exports.wp = {};
/**
* 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;
$('#cookie-notice-content, #terms-and-conditions-content, #privacy-policy-content').trumbowyg();
// Apply setting values from database.
var workingPlan = {};
GlobalVariables.settings.system.forEach(function (setting) {
$('input[data-field="' + setting.name + '"]').val(setting.value);
$('select[data-field="' + setting.name + '"]').val(setting.value);
if (setting.name === 'company_working_plan') {
workingPlan = $.parseJSON(setting.value);
}
if (setting.name === 'customer_notifications') {
$('#customer-notifications').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'require_captcha') {
$('#require-captcha').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'require_phone_number') {
$('#require-phone-number').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'display_any_provider') {
$('#display-any-provider').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'display_cookie_notice') {
$('#display-cookie-notice').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'cookie_notice_content') {
$('#cookie-notice-content').trumbowyg('html', setting.value);
}
if (setting.name === 'display_terms_and_conditions') {
$('#display-terms-and-conditions').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'terms_and_conditions_content') {
$('#terms-and-conditions-content').trumbowyg('html', setting.value);
}
if (setting.name === 'display_privacy_policy') {
$('#display-privacy-policy').prop('checked', Boolean(Number(setting.value)));
}
if (setting.name === 'privacy_policy_content') {
$('#privacy-policy-content').trumbowyg('html', setting.value);
}
if (setting.name === 'show_phone_number' && setting.value === "1") {
$('#show-phone-number').find("div").toggleClass("hidden");
}
if (setting.name === 'show_address' && setting.value === "1") {
$('#show-address').find("div").toggleClass("hidden");
}
if (setting.name === 'show_city' && setting.value === "1") {
$('#show-city').find("div").toggleClass("hidden");
}
if (setting.name === 'show_zip_code' && setting.value === "1") {
$('#show-zip-code').find("div").toggleClass("hidden");
}
if (setting.name === 'show_notes' && setting.value === "1") {
$('#show-notes').find("div").toggleClass("hidden");
}
});
exports.wp = new WorkingPlan();
exports.wp.setup(workingPlan);
exports.wp.timepickers(false);
// Load user settings into form
$('#user-id').val(GlobalVariables.settings.user.id);
$('#first-name').val(GlobalVariables.settings.user.first_name);
$('#last-name').val(GlobalVariables.settings.user.last_name);
$('#email').val(GlobalVariables.settings.user.email);
$('#mobile-number').val(GlobalVariables.settings.user.mobile_number);
$('#phone-number').val(GlobalVariables.settings.user.phone_number);
$('#address').val(GlobalVariables.settings.user.address);
$('#city').val(GlobalVariables.settings.user.city);
$('#state').val(GlobalVariables.settings.user.state);
$('#zip-code').val(GlobalVariables.settings.user.zip_code);
$('#notes').val(GlobalVariables.settings.user.notes);
$('#timezone').val(GlobalVariables.settings.user.timezone);
$('#username').val(GlobalVariables.settings.user.settings.username);
$('#password, #retype-password').val('');
$('#calendar-view').val(GlobalVariables.settings.user.settings.calendar_view);
$('#user-notifications').prop('checked', Boolean(Number(GlobalVariables.settings.user.settings.notifications)));
// Set default settings helper.
settings = new SystemSettings();
if (defaultEventHandlers) {
bindEventHandlers();
var $link = $('#settings-page .nav li').not('.d-none').first().find('a');
$link.tab('show');
}
// Apply Privileges
if (GlobalVariables.user.privileges.system_settings.edit === false) {
$('#general, #business-logic').find('select, input, textarea').prop('readonly', true);
$('#general, #business-logic').find('button').prop('disabled', true);
}
if (GlobalVariables.user.privileges.user_settings.edit === false) {
$('#user').find('select, input, textarea').prop('readonly', true);
$('#user').find('button').prop('disabled', true);
}
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() {
exports.wp.bindEventHandlers();
/**
* Event: Tab "Click"
*
* Change the visible tab contents.
*/
$('a[data-toggle="tab"]').on('shown.bs.tab', function () {
var href = $(this).attr('href');
if (href === '#general') {
settings = new SystemSettings();
} else if (href === '#business-logic') {
settings = new SystemSettings();
} else if (href === '#legal-contents') {
settings = new SystemSettings();
} else if (href === '#current-user') {
settings = new UserSettings();
}
Backend.placeFooterToBottom();
});
/**
* 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: Visible/Hidden button "Click"
*
* Change the state of the Visible/Hidden button
*/
$('.hide-toggle').on('click', function () {
var $input = $(this);
$input.find("div").toggleClass("hidden");
});
/**
* set a Visible/Hidden toggle button to a certain state
*
* @argument $element for which jquery element to set the state for
* @argument isVisible a boolean which is true if the button should display 'visible' and false when the button should display 'hidden'
*
*/
function setShowToggleValue($element, isVisible){
if (getShowToggleValue($element) !== isVisible){
$element.find("div").toggleClass("hidden");
}
}
/**
* get the Visible/Hidden toggle button
*
* @argument $element for which jquery element to set the state for
*
* @return the state of the button. True for visible, false for hidden.
*/
function getShowToggleValue($element){
var visiblePartArray = $element.find(".hide-toggle-visible");
return !visiblePartArray.hasClass("hidden");
}
/**
* Event: require phone number switch "Click"
*
* make sure that our phone number is visible when it is required.
*/
$('#show-phone-number').on('click', function () {
if (!getShowToggleValue($(this))){//if button is set to hidden
$('#require-phone-number').prop('checked', false);
}
});
/**
* 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);
}
});
/**
* Event: Username "Focusout"
*
* When the user leaves the username input field we will need to check if the username
* is not taken by another record in the system. Usernames must be unique.
*/
$('#username').focusout(function () {
var $input = $(this);
if ($input.prop('readonly') === true || $input.val() === '') {
return;
}
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username';
var data = {
csrfToken: GlobalVariables.csrfToken,
username: $input.val(),
user_id: $input.parents().eq(2).find('#user-id').val()
};
$.post(url, data)
.done(function (response) {
if (response === 'false') {
$input.closest('.form-group').addClass('has-error');
Backend.displayNotification(EALang.username_already_exists);
$input.attr('already-exists', 'true');
} else {
$input.closest('.form-group').removeClass('has-error');
$input.attr('already-exists', 'false');
}
});
});
/**
* Event: Apply Global Working Plan
*/
$('#apply-global-working-plan').on('click', function () {
var buttons = [
{
text: EALang.cancel,
click: function () {
$('#message-box').dialog('close');
}
},
{
text: 'OK',
click: function () {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_apply_global_working_plan';
var data = {
csrfToken: GlobalVariables.csrfToken,
working_plan: JSON.stringify(exports.wp.get())
};
$.post(url, data)
.done(function () {
Backend.displayNotification(EALang.working_plans_got_updated);
})
.always(function () {
$('#message-box').dialog('close');
});
}
}
];
GeneralFunctions.displayMessageBox(EALang.working_plan, EALang.overwrite_existing_working_plans, buttons);
});
}
})(window.BackendSettings);