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-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-09-14 19:10:59 +03:00
|
|
|
$('.active').removeClass('active');
|
|
|
|
$(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-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
|
|
|
//////////////////////////////////////////////
|
|
|
|
console.log('Settings To Save: ', settings);
|
|
|
|
//////////////////////////////////////////////
|
2013-09-18 19:36:29 +03:00
|
|
|
});
|
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
|
|
|
|
|
|
|
Backend.displayNotification('Settings saved successfully!');
|
|
|
|
}, '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) {
|
|
|
|
throw 'Fields with * are required.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate company email address.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#company-email').val())) {
|
|
|
|
$('#company-email').css('border', '2px solid red');
|
|
|
|
throw 'Invalid email address!';
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
Backend.displayNotification('User settings are invalid! Please review your settings '
|
|
|
|
+ 'and try again.');
|
|
|
|
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-09-20 16:58:11 +03:00
|
|
|
Backend.displayNotification('Settings saved successfully!');
|
|
|
|
|
|
|
|
}, '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) {
|
|
|
|
throw 'Fields with * are required.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate passwords (if provided).
|
|
|
|
if ($('#password').val() != $('#retype-password').val()) {
|
|
|
|
$('#password, #retype-password').css('border', '2px solid red');
|
|
|
|
throw 'Passwords mismatch!';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate user email.
|
|
|
|
if (!GeneralFunctions.validateEmail($('#email').val())) {
|
|
|
|
$('#email').css('border', '2px solid red');
|
|
|
|
throw 'Invalid email address!';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch(exc) {
|
|
|
|
Backend.displayNotification(exc);
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-18 19:36:29 +03:00
|
|
|
};
|