2013-09-14 19:10:59 +03:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* @namespace BackendSettings
|
|
|
|
*/
|
|
|
|
var BackendSettings = {
|
|
|
|
SETTINGS_SYSTEM: 'SETTINGS_SYSTEM',
|
|
|
|
SETTINGS_USER: 'SETTINGS_USER',
|
|
|
|
|
2013-09-18 19:36:29 +03:00
|
|
|
/**
|
2013-09-25 18:43:17 +03:00
|
|
|
* Use this WorkingPlan class instance to perform actions on the page's working plan
|
|
|
|
* tables.
|
2013-09-18 19:36:29 +03:00
|
|
|
*/
|
2013-09-25 18:43:17 +03:00
|
|
|
wp: {},
|
2013-09-18 19:36:29 +03:00
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
/**
|
|
|
|
* Tab settings object.
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
settings: {},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Page
|
|
|
|
*
|
|
|
|
* @param {bool} bindEventHandlers (OPTIONAL)Determines whether to bind the default event
|
|
|
|
* handlers (default = true).
|
|
|
|
* @returns {undefined}
|
|
|
|
*/
|
|
|
|
initialize: function(bindEventHandlers) {
|
|
|
|
if (bindEventHandlers == undefined) bindEventHandlers = true;
|
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
// Apply setting values from database.
|
2013-09-14 19:10:59 +03:00
|
|
|
$.each(GlobalVariables.settings.system, function(index, setting) {
|
2013-09-18 19:36:29 +03:00
|
|
|
$('input[data-field="' + setting.name + '"]').val(setting.value);
|
|
|
|
});
|
|
|
|
|
|
|
|
var workingPlan = {};
|
|
|
|
$.each(GlobalVariables.settings.system, function(index, setting) {
|
|
|
|
if (setting.name == 'company_working_plan') {
|
|
|
|
workingPlan = $.parseJSON(setting.value);
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-14 19:10:59 +03:00
|
|
|
});
|
|
|
|
|
2013-09-25 18:43:17 +03:00
|
|
|
BackendSettings.wp = new WorkingPlan();
|
|
|
|
BackendSettings.wp.setup(workingPlan);
|
|
|
|
BackendSettings.wp.timepickers(false);
|
2013-09-18 19:36:29 +03:00
|
|
|
|
|
|
|
// Book Advance Timeout Spinner
|
|
|
|
$('#book-advance-timeout').spinner({
|
2013-09-20 16:58:11 +03:00
|
|
|
'min': 0
|
2013-09-18 19:36:29 +03:00
|
|
|
});
|
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
$('#username').val(GlobalVariables.settings.user.settings.username);
|
|
|
|
$('#password, #retype-password').val('');
|
|
|
|
|
|
|
|
if (GlobalVariables.settings.user.settings.notifications == true) {
|
|
|
|
$('#user-notifications').addClass('active');
|
|
|
|
} else {
|
|
|
|
$('#user-notifications').removeClass('active');
|
|
|
|
}
|
|
|
|
|
2013-09-18 19:36:29 +03:00
|
|
|
// Set default settings helper.
|
|
|
|
BackendSettings.settings = new SystemSettings();
|
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
if (bindEventHandlers) {
|
|
|
|
BackendSettings.bindEventHandlers();
|
2013-09-26 19:06:57 +03:00
|
|
|
$('#settings-page .nav li').first().addClass('active');
|
|
|
|
$('#settings-page .nav li').first().find('a').trigger('click');
|
2013-09-14 19:10:59 +03:00
|
|
|
}
|
2013-09-26 19:06:57 +03:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2013-10-17 18:31:43 +03:00
|
|
|
Backend.placeFooterToBottom();
|
2013-09-14 19:10:59 +03:00
|
|
|
},
|
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-09-14 19:10:59 +03:00
|
|
|
bindEventHandlers: function() {
|
2013-09-25 18:43:17 +03:00
|
|
|
BackendSettings.wp.bindEventHandlers();
|
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
/**
|
|
|
|
* Event: Tab "Click"
|
|
|
|
*
|
|
|
|
* Change the visible tab contents.
|
|
|
|
*/
|
|
|
|
$('.tab').click(function() {
|
2013-09-20 16:58:11 +03:00
|
|
|
// Bootstrap has a bug with toggle buttons. Their toggle state is lost when the
|
|
|
|
// button is hidden or shown. Show before anything else we need to store the toggle
|
|
|
|
// and apply it whenever the user tab is clicked..
|
|
|
|
var areNotificationsActive = $('#user-notifications').hasClass('active');
|
|
|
|
|
2013-10-11 18:58:46 +03:00
|
|
|
$(this).parent().find('.active').removeClass('active');
|
2013-09-14 19:10:59 +03:00
|
|
|
$(this).addClass('active');
|
|
|
|
$('.tab-content').hide();
|
|
|
|
|
|
|
|
if ($(this).hasClass('general-tab')) {
|
|
|
|
$('#general').show();
|
|
|
|
BackendSettings.settings = new SystemSettings();
|
|
|
|
} else if ($(this).hasClass('business-logic-tab')) {
|
|
|
|
$('#business-logic').show();
|
|
|
|
BackendSettings.settings = new SystemSettings();
|
|
|
|
} else if ($(this).hasClass('user-tab')) {
|
|
|
|
$('#user').show();
|
|
|
|
BackendSettings.settings = new UserSettings();
|
2013-09-20 16:58:11 +03:00
|
|
|
|
|
|
|
// Apply toggle state to user notifications button.
|
|
|
|
if (areNotificationsActive) {
|
|
|
|
$('#user-notifications').addClass('active');
|
|
|
|
} else {
|
|
|
|
$('#user-notifications').removeClass('active');
|
|
|
|
}
|
2013-11-22 18:19:52 +02:00
|
|
|
} else if ($(this).hasClass('about-tab')) {
|
|
|
|
$('#about').show();
|
2013-09-14 19:10:59 +03:00
|
|
|
}
|
2013-10-17 18:31:43 +03:00
|
|
|
|
|
|
|
Backend.placeFooterToBottom();
|
2013-09-14 19:10:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Save Settings Button "Click"
|
|
|
|
*
|
|
|
|
* Store the setting changes into the database.
|
|
|
|
*/
|
|
|
|
$('.save-settings').click(function() {
|
|
|
|
var settings = BackendSettings.settings.get();
|
2013-09-18 19:36:29 +03:00
|
|
|
BackendSettings.settings.save(settings);
|
2013-09-20 16:58:11 +03:00
|
|
|
//////////////////////////////////////////////
|
2013-10-18 17:56:12 +03:00
|
|
|
//console.log('Settings To Save: ', settings);
|
2013-09-20 16:58:11 +03:00
|
|
|
//////////////////////////////////////////////
|
2013-09-18 19:36:29 +03:00
|
|
|
});
|
2013-10-18 17:56:12 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2015-01-29 00:53:13 +02:00
|
|
|
var postUrl = GlobalVariables.baseUrl + '/backend_api/ajax_validate_username';
|
2013-10-18 17:56:12 +03:00
|
|
|
var postData = {
|
|
|
|
'username': $input.val(),
|
2013-11-24 17:45:44 +02:00
|
|
|
'user_id': $input.parents().eq(2).find('#user-id').val()
|
2013-10-18 17:56:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
//console.log('Validate Username Response:', response);
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
|
|
|
if (response == false) {
|
|
|
|
$input.css('border', '2px solid red');
|
2013-12-20 19:44:44 +02:00
|
|
|
Backend.displayNotification(EALang['username_already_exists']);
|
2013-11-24 17:45:44 +02:00
|
|
|
$input.attr('already-exists', 'true');
|
2013-10-18 17:56:12 +03:00
|
|
|
} else {
|
|
|
|
$input.css('border', '');
|
2013-11-24 17:45:44 +02:00
|
|
|
$input.attr('already-exists', 'false');
|
2013-10-18 17:56:12 +03:00
|
|
|
}
|
|
|
|
}, 'json');
|
|
|
|
});
|
2013-09-14 19:10:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* "System Settings" Tab Helper
|
|
|
|
* @class SystemSettings
|
|
|
|
*/
|
|
|
|
var SystemSettings = function() {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the system settings. This method is run after changes are detected on the
|
|
|
|
* tab input fields.
|
|
|
|
*
|
|
|
|
* @param {array} settings Contains the system settings data.
|
|
|
|
*/
|
|
|
|
SystemSettings.prototype.save = function(settings) {
|
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_save_settings';
|
|
|
|
var postData = {
|
|
|
|
'settings': JSON.stringify(settings),
|
|
|
|
'type': BackendSettings.SETTINGS_SYSTEM
|
|
|
|
};
|
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
console.log('Save General Settings Response:', response);
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
2013-09-14 19:10:59 +03:00
|
|
|
|
2013-12-20 19:44:44 +02:00
|
|
|
Backend.displayNotification(EALang['settings_saved']);
|
2013-10-18 17:56:12 +03:00
|
|
|
|
|
|
|
// Update the logo title on the header.
|
|
|
|
$('#header-logo span').text($('#company-name').val());
|
|
|
|
|
|
|
|
// We need to refresh the working plan.
|
|
|
|
var workingPlan = BackendSettings.wp.get();
|
|
|
|
$('.breaks').empty();
|
|
|
|
BackendSettings.wp.setup(workingPlan);
|
|
|
|
BackendSettings.wp.timepickers(false);
|
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
}, 'json');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare the system settings array. This method uses the DOM elements of the
|
|
|
|
* backend/settings page, so it can't be used in another page.
|
|
|
|
*
|
|
|
|
* @returns {array} Returns the system settings array.
|
|
|
|
*/
|
|
|
|
SystemSettings.prototype.get = function() {
|
2013-09-18 19:36:29 +03:00
|
|
|
var settings = [];
|
2013-09-14 19:10:59 +03:00
|
|
|
|
|
|
|
// General Settings Tab
|
|
|
|
$('#general input').each(function() {
|
2013-09-18 19:36:29 +03:00
|
|
|
settings.push({
|
|
|
|
'name': $(this).attr('data-field'),
|
|
|
|
'value': $(this).val()
|
|
|
|
});
|
2013-09-14 19:10:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Business Logic Tab
|
2013-09-18 19:36:29 +03:00
|
|
|
settings.push({
|
|
|
|
'name': 'company_working_plan',
|
2013-09-25 18:43:17 +03:00
|
|
|
'value': JSON.stringify(BackendSettings.wp.get())
|
2013-09-18 19:36:29 +03:00
|
|
|
});
|
2013-09-14 19:10:59 +03:00
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
settings.push({
|
|
|
|
'name': 'book_advance_timeout',
|
|
|
|
'value': $('#book-advance-timeout').val()
|
|
|
|
});
|
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
return settings;
|
|
|
|
};
|
|
|
|
|
2013-09-18 19:36:29 +03:00
|
|
|
/**
|
|
|
|
* Validate the settings data. If the validation fails then display a
|
|
|
|
* message to the user.
|
|
|
|
*
|
|
|
|
* @returns {bool} Returns the validation result.
|
|
|
|
*/
|
|
|
|
SystemSettings.prototype.validate = function() {
|
2013-09-20 16:58:11 +03:00
|
|
|
$('#general .required').css('border', '');
|
2013-09-18 19:36:29 +03:00
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
try {
|
|
|
|
// Validate required fields.
|
|
|
|
var missingRequired = false;
|
|
|
|
$('#general .required').each(function() {
|
|
|
|
if ($(this).val() == '' || $(this).val() == undefined) {
|
|
|
|
$(this).css('border', '2px solid red');
|
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (missingRequired) {
|
2013-12-20 19:44:44 +02:00
|
|
|
throw EALang['fields_are_required'];
|
2013-09-20 16:58:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate company email address.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#company-email').val())) {
|
|
|
|
$('#company-email').css('border', '2px solid red');
|
2013-12-20 19:44:44 +02:00
|
|
|
throw EALang['invalid_email'];
|
2013-09-20 16:58:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch(exc) {
|
|
|
|
Backend.displayNotification(exc);
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-18 19:36:29 +03:00
|
|
|
};
|
|
|
|
|
2013-09-14 19:10:59 +03:00
|
|
|
/**
|
|
|
|
* "User Settings" Tab Helper
|
|
|
|
* @class UserSettings
|
|
|
|
*/
|
|
|
|
var UserSettings = function() {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the settings data for the user settings.
|
|
|
|
*
|
2013-09-20 16:58:11 +03:00
|
|
|
* @returns {object} Returns the user settings array.
|
2013-09-14 19:10:59 +03:00
|
|
|
*/
|
|
|
|
UserSettings.prototype.get = function() {
|
2013-09-20 16:58:11 +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(),
|
|
|
|
'settings': {
|
|
|
|
'username': $('#username').val(),
|
|
|
|
'notifications': $('#user-notifications').hasClass('active')
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($('#password').val() != '') {
|
|
|
|
user.settings.password = $('#password').val();
|
|
|
|
}
|
2013-09-14 19:10:59 +03:00
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
return user;
|
2013-09-18 19:36:29 +03:00
|
|
|
};
|
2013-09-14 19:10:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the user settings into the database.
|
|
|
|
*
|
|
|
|
* @param {array} settings Contains the user settings.
|
|
|
|
*/
|
|
|
|
UserSettings.prototype.save = function(settings) {
|
2013-09-20 16:58:11 +03:00
|
|
|
if (!BackendSettings.settings.validate(settings)) {
|
2013-12-20 19:44:44 +02:00
|
|
|
Backend.displayNotification(EALang['user_settings_are_invalid']);
|
2013-09-20 16:58:11 +03:00
|
|
|
return; // Validation failed, do not procceed.
|
|
|
|
}
|
2013-09-14 19:10:59 +03:00
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_save_settings';
|
|
|
|
var postData = {
|
|
|
|
'type': BackendSettings.SETTINGS_USER,
|
|
|
|
'settings': JSON.stringify(settings)
|
|
|
|
};
|
|
|
|
|
|
|
|
$.post(postUrl, postData, function(response) {
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
console.log('Save User Settings Response: ', response);
|
|
|
|
//////////////////////////////////////////////////////////
|
|
|
|
|
2013-09-23 18:42:36 +03:00
|
|
|
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
2013-12-20 19:44:44 +02:00
|
|
|
Backend.displayNotification(EALang['settings_saved']);
|
2013-09-20 16:58:11 +03:00
|
|
|
|
2013-10-18 17:56:12 +03:00
|
|
|
// Update footer greetings.
|
2013-10-21 15:36:40 +03:00
|
|
|
$('#footer-user-display-name').text('Hello, ' + $('#first-name').val() + ' ' + $('#last-name').val() + '!');
|
2013-10-18 17:56:12 +03:00
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
}, 'json');
|
2013-09-18 19:36:29 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the settings data. If the validation fails then display a
|
|
|
|
* message to the user.
|
|
|
|
*
|
|
|
|
* @returns {bool} Returns the validation result.
|
|
|
|
*/
|
|
|
|
UserSettings.prototype.validate = function() {
|
2013-09-20 16:58:11 +03:00
|
|
|
$('#user .required').css('border', '');
|
|
|
|
$('#user').find('#password, #retype-password').css('border', '');
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Validate required fields.
|
|
|
|
var missingRequired = false;
|
|
|
|
$('#user .required').each(function() {
|
|
|
|
if ($(this).val() == '' || $(this).val() == undefined) {
|
|
|
|
$(this).css('border', '2px solid red');
|
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (missingRequired) {
|
2013-12-20 19:44:44 +02:00
|
|
|
throw EALang['fields_are_required'];
|
2013-09-20 16:58:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate passwords (if provided).
|
|
|
|
if ($('#password').val() != $('#retype-password').val()) {
|
|
|
|
$('#password, #retype-password').css('border', '2px solid red');
|
2013-12-20 19:44:44 +02:00
|
|
|
throw EALang['passwords_mismatch'];
|
2013-09-20 16:58:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate user email.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#email').val())) {
|
|
|
|
$('#email').css('border', '2px solid red');
|
2013-12-20 19:44:44 +02:00
|
|
|
throw EALang['invalid_email'];
|
2013-09-20 16:58:11 +03:00
|
|
|
}
|
|
|
|
|
2013-11-24 17:45:44 +02:00
|
|
|
if ($('#username').attr('already-exists') === 'true') {
|
|
|
|
$('#username').css('border', '2px solid red');
|
2013-12-20 19:44:44 +02:00
|
|
|
throw EALang['username_already_exists'];
|
2013-11-24 17:45:44 +02:00
|
|
|
}
|
|
|
|
|
2013-09-20 16:58:11 +03:00
|
|
|
return true;
|
|
|
|
} catch(exc) {
|
|
|
|
Backend.displayNotification(exc);
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-18 19:36:29 +03:00
|
|
|
};
|