2016-04-13 20:54:56 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2020-03-11 12:10:59 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2020, Alex Tselegidis
|
2016-04-13 20:54:56 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.0.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
(function () {
|
2016-04-13 20:54:56 +03:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
2016-05-14 13:52:08 +03:00
|
|
|
* "User Settings" Tab Helper Class
|
|
|
|
*
|
2016-04-13 20:54:56 +03:00
|
|
|
* @class UserSettings
|
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
var UserSettings = function () {
|
|
|
|
};
|
2016-04-13 20:54:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the settings data for the user settings.
|
|
|
|
*
|
2016-05-14 13:52:08 +03:00
|
|
|
* @returns {Object} Returns the user settings array.
|
2016-04-13 20:54:56 +03:00
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
UserSettings.prototype.get = function () {
|
2016-04-13 20:54:56 +03:00
|
|
|
var user = {
|
|
|
|
id: $('#user-id').val(),
|
|
|
|
first_name: $('#first-name').val(),
|
|
|
|
last_name: $('#last-name').val(),
|
|
|
|
email: $('#email').val(),
|
|
|
|
mobile_number: $('#mobile-number').val(),
|
|
|
|
phone_number: $('#phone-number').val(),
|
|
|
|
address: $('#address').val(),
|
|
|
|
city: $('#city').val(),
|
|
|
|
state: $('#state').val(),
|
|
|
|
zip_code: $('#zip-code').val(),
|
|
|
|
notes: $('#notes').val(),
|
2020-03-29 16:12:44 +03:00
|
|
|
timezone: $('#timezone').val(),
|
2016-04-13 20:54:56 +03:00
|
|
|
settings: {
|
|
|
|
username: $('#username').val(),
|
2016-07-19 00:46:15 +03:00
|
|
|
notifications: $('#user-notifications').hasClass('active'),
|
|
|
|
calendar_view: $('#calendar-view').val()
|
2016-04-13 20:54:56 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#password').val() != '') {
|
|
|
|
user.settings.password = $('#password').val();
|
|
|
|
}
|
|
|
|
|
|
|
|
return user;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the user settings into the database.
|
|
|
|
*
|
2016-05-14 13:52:08 +03:00
|
|
|
* @param {Array} settings Contains the user settings.
|
2016-04-13 20:54:56 +03:00
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
UserSettings.prototype.save = function (settings) {
|
2016-04-13 20:54:56 +03:00
|
|
|
if (!this.validate(settings)) {
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.user_settings_are_invalid);
|
2016-10-10 19:29:48 +03:00
|
|
|
return; // Validation failed, do not proceed.
|
2016-04-13 20:54:56 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 21:52:21 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_settings';
|
|
|
|
var postData = {
|
|
|
|
csrfToken: GlobalVariables.csrfToken,
|
|
|
|
type: BackendSettings.SETTINGS_USER,
|
|
|
|
settings: JSON.stringify(settings)
|
|
|
|
};
|
2016-04-13 20:54:56 +03:00
|
|
|
|
2018-01-23 12:08:37 +03:00
|
|
|
$.post(postUrl, postData, function (response) {
|
2016-04-13 20:54:56 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-11 17:09:15 +03:00
|
|
|
Backend.displayNotification(EALang.settings_saved);
|
2016-04-13 20:54:56 +03:00
|
|
|
|
|
|
|
// Update footer greetings.
|
|
|
|
$('#footer-user-display-name').text('Hello, ' + $('#first-name').val() + ' ' + $('#last-name').val() + '!');
|
|
|
|
|
|
|
|
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-05-14 13:52:08 +03:00
|
|
|
* Validate the settings data.
|
|
|
|
*
|
|
|
|
* If the validation fails then display a message to the user.
|
2016-04-13 20:54:56 +03:00
|
|
|
*
|
2016-05-14 13:52:08 +03:00
|
|
|
* @return {Boolean} Returns the validation result.
|
2016-04-13 20:54:56 +03:00
|
|
|
*/
|
2018-01-23 12:08:37 +03:00
|
|
|
UserSettings.prototype.validate = function () {
|
2017-11-14 15:52:59 +03:00
|
|
|
$('#user .has-error').removeClass('has-error');
|
2016-04-13 20:54:56 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Validate required fields.
|
|
|
|
var missingRequired = false;
|
2018-01-23 12:08:37 +03:00
|
|
|
$('#user .required').each(function () {
|
2016-05-14 13:52:08 +03:00
|
|
|
if ($(this).val() === '' || $(this).val() === undefined) {
|
2017-11-14 15:52:59 +03:00
|
|
|
$(this).closest('.form-group').addClass('has-error');
|
2016-04-13 20:54:56 +03:00
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
2017-09-11 17:09:15 +03:00
|
|
|
throw EALang.fields_are_required;
|
2016-04-13 20:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate passwords (if provided).
|
|
|
|
if ($('#password').val() != $('#retype-password').val()) {
|
2017-11-14 15:52:59 +03:00
|
|
|
$('#password, #retype-password').closest('.form-group').addClass('has-error');
|
2017-09-11 17:09:15 +03:00
|
|
|
throw EALang.passwords_mismatch;
|
2016-04-13 20:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate user email.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#email').val())) {
|
2017-11-14 15:52:59 +03:00
|
|
|
$('#email').closest('.form-group').addClass('has-error');
|
2017-09-11 17:09:15 +03:00
|
|
|
throw EALang.invalid_email;
|
2016-04-13 20:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($('#username').attr('already-exists') === 'true') {
|
2017-11-14 15:52:59 +03:00
|
|
|
$('#username').closest('.form-group').addClass('has-error');
|
2017-09-11 17:09:15 +03:00
|
|
|
throw EALang.username_already_exists;
|
2016-04-13 20:54:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-01-23 12:08:37 +03:00
|
|
|
} catch (exc) {
|
2016-04-13 20:54:56 +03:00
|
|
|
Backend.displayNotification(exc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.UserSettings = UserSettings;
|
|
|
|
|
|
|
|
})();
|