mirror of
https://github.com/alextselegidis/easyappointments.git
synced 2024-11-21 23:42:21 +03:00
Refactored the backend/users js files.
This commit is contained in:
parent
967021acf7
commit
bcc205522e
4 changed files with 1361 additions and 1292 deletions
|
@ -9,28 +9,40 @@
|
|||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
window.BackendUsers = window.BackendUsers || {};
|
||||
|
||||
/**
|
||||
* This namespace handles the js functionality of the users backend page. It uses three other
|
||||
* Backend Users
|
||||
*
|
||||
* This module handles the js functionality of the users backend page. It uses three other
|
||||
* classes (defined below) in order to handle the admin, provider and secretary record types.
|
||||
*
|
||||
* @namespace BackendUsers
|
||||
* @module BackendUsers
|
||||
*/
|
||||
var BackendUsers = {
|
||||
MIN_PASSWORD_LENGTH: 7,
|
||||
(function(exports){
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Minimum Password Length
|
||||
*
|
||||
* @type {int}
|
||||
*/
|
||||
exports.MIN_PASSWORD_LENGTH = 7;
|
||||
|
||||
/**
|
||||
* Contains the current tab record methods for the page.
|
||||
*
|
||||
* @type AdminsHelper|ProvidersHelper|SecretariesHelper
|
||||
*/
|
||||
helper: {},
|
||||
var helper = {};
|
||||
|
||||
/**
|
||||
* Use this class instance for performing actions on the working plan.
|
||||
*
|
||||
* @type {object}
|
||||
*/
|
||||
wp: {},
|
||||
exports.wp = {};
|
||||
|
||||
/**
|
||||
* Initialize the backend users page.
|
||||
|
@ -38,8 +50,8 @@ var BackendUsers = {
|
|||
* @param {bool} defaultEventHandlers (OPTIONAL) Whether to bind the default event handlers
|
||||
* (default: true).
|
||||
*/
|
||||
initialize: function(defaultEventHandlers) {
|
||||
if (defaultEventHandlers == undefined) defaultEventHandlers = true;
|
||||
exports.initialize = function(defaultEventHandlers) {
|
||||
defaultEventHandlers = defaultEventHandlers || true;
|
||||
|
||||
// Initialize jScrollPane Scrollbars
|
||||
$('#filter-admins .results').jScrollPane();
|
||||
|
@ -47,12 +59,13 @@ var BackendUsers = {
|
|||
$('#filter-secretaries .results').jScrollPane();
|
||||
|
||||
// Instanciate default helper object (admin).
|
||||
BackendUsers.helper = new AdminsHelper();
|
||||
BackendUsers.helper.resetForm();
|
||||
BackendUsers.helper.filter('');
|
||||
helper = new AdminsHelper();
|
||||
helper.resetForm();
|
||||
helper.filter('');
|
||||
helper.bindEventHandlers();
|
||||
|
||||
BackendUsers.wp = new WorkingPlan();
|
||||
BackendUsers.wp.bindEventHandlers();
|
||||
exports.wp = new WorkingPlan();
|
||||
exports.wp.bindEventHandlers();
|
||||
|
||||
// Fill the services and providers list boxes.
|
||||
var html = '<div class="col-md-12">';
|
||||
|
@ -96,14 +109,16 @@ var BackendUsers = {
|
|||
});
|
||||
|
||||
// Bind event handlers.
|
||||
if (defaultEventHandlers) BackendUsers.bindEventHandlers();
|
||||
},
|
||||
if (defaultEventHandlers) {
|
||||
_bindEventHandlers();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Binds the defauly backend users event handlers. Do not use this method on a different
|
||||
* page because it needs the backend users page DOM.
|
||||
*/
|
||||
bindEventHandlers: function() {
|
||||
function _bindEventHandlers() {
|
||||
/**
|
||||
* Event: Page Tab Button "Click"
|
||||
*
|
||||
|
@ -113,27 +128,30 @@ var BackendUsers = {
|
|||
$(this).parent().find('.active').removeClass('active');
|
||||
$(this).addClass('active');
|
||||
$('.tab-content').hide();
|
||||
$('#admins, #providers, #secretaries').off();
|
||||
|
||||
if ($(this).hasClass('admins-tab')) { // display admins tab
|
||||
$('#admins').show();
|
||||
BackendUsers.helper = new AdminsHelper();
|
||||
helper = new AdminsHelper();
|
||||
} else if ($(this).hasClass('providers-tab')) { // display providers tab
|
||||
$('#providers').show();
|
||||
$('#provider-services').data('jsp').destroy();
|
||||
$('#provider-services').jScrollPane({ mouseWheelSpeed: 70 });
|
||||
BackendUsers.helper = new ProvidersHelper();
|
||||
helper = new ProvidersHelper();
|
||||
} else if ($(this).hasClass('secretaries-tab')) { // display secretaries tab
|
||||
$('#secretaries').show();
|
||||
BackendUsers.helper = new SecretariesHelper();
|
||||
helper = new SecretariesHelper();
|
||||
|
||||
// Update the list with the all the available providers.
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers';
|
||||
var postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'key': ''
|
||||
};
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
key: ''
|
||||
};
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
GlobalVariables.providers = response;
|
||||
|
||||
|
@ -158,8 +176,9 @@ var BackendUsers = {
|
|||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
}
|
||||
|
||||
BackendUsers.helper.resetForm();
|
||||
BackendUsers.helper.filter('');
|
||||
helper.resetForm();
|
||||
helper.filter('');
|
||||
helper.bindEventHandlers();
|
||||
$('.filter-key').val('');
|
||||
});
|
||||
|
||||
|
@ -182,15 +201,18 @@ var BackendUsers = {
|
|||
return;
|
||||
}
|
||||
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username';
|
||||
var postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'username': $input.val(),
|
||||
'user_id': userId
|
||||
};
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_validate_username',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
username: $input.val(),
|
||||
user_id: userId
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (response == false) {
|
||||
$input.css('border', '2px solid red');
|
||||
$input.attr('already-exists', 'true');
|
||||
|
@ -205,19 +227,6 @@ var BackendUsers = {
|
|||
}
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
});
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
AdminsHelper.prototype.bindEventHandlers();
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
ProvidersHelper.prototype.bindEventHandlers();
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
SecretariesHelper.prototype.bindEventHandlers();
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
}
|
||||
};
|
||||
})(window.BackendUsers);
|
||||
|
|
|
@ -9,401 +9,418 @@
|
|||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* This class contains the Admins helper class declaration, along with the "Admins" tab
|
||||
* event handlers. By deviding the backend/users tab functionality into separate files
|
||||
* it is easier to maintain the code.
|
||||
*
|
||||
* @class AdminsHelper
|
||||
*/
|
||||
var AdminsHelper = function() {
|
||||
this.filterResults = {}; // Store the results for later use.
|
||||
};
|
||||
(function() {
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Bind the event handlers for the backend/users "Admins" tab.
|
||||
*/
|
||||
AdminsHelper.prototype.bindEventHandlers = function() {
|
||||
/**
|
||||
* Event: Filter Admins Form "Sumbit"
|
||||
* This class contains the Admins helper class declaration, along with the "Admins" tab
|
||||
* event handlers. By deviding the backend/users tab functionality into separate files
|
||||
* it is easier to maintain the code.
|
||||
*
|
||||
* Filter the admin records with the given key string.
|
||||
* @class AdminsHelper
|
||||
*/
|
||||
$('#filter-admins form').submit(function(event) {
|
||||
var key = $('#filter-admins .key').val();
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
BackendUsers.helper.resetForm();
|
||||
BackendUsers.helper.filter(key);
|
||||
return false;
|
||||
});
|
||||
var AdminsHelper = function() {
|
||||
this.filterResults = []; // Store the results for later use.
|
||||
};
|
||||
|
||||
/**
|
||||
* Event: Clear Filter Results Button "Click"
|
||||
* Bind the event handlers for the backend/users "Admins" tab.
|
||||
*/
|
||||
$('#filter-admins .clear').click(function() {
|
||||
BackendUsers.helper.filter('');
|
||||
$('#filter-admins .key').val('');
|
||||
BackendUsers.helper.resetForm();
|
||||
});
|
||||
AdminsHelper.prototype.bindEventHandlers = function() {
|
||||
/**
|
||||
* Event: Filter Admins Form "Sumbit"
|
||||
*
|
||||
* Filter the admin records with the given key string.
|
||||
*/
|
||||
$('#admins').on('submit', '#filter-admins form', function(event) {
|
||||
var key = $('#filter-admins .key').val();
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
this.resetForm();
|
||||
this.filter(key);
|
||||
return false;
|
||||
}.bind(this));
|
||||
|
||||
/**
|
||||
* Event: Filter Admin Row "Click"
|
||||
*
|
||||
* Display the selected admin data to the user.
|
||||
*/
|
||||
$(document).on('click', '.admin-row', function() {
|
||||
if ($('#filter-admins .filter').prop('disabled')) {
|
||||
/**
|
||||
* Event: Clear Filter Results Button "Click"
|
||||
*/
|
||||
$('#admins').on('click', '#filter-admins .clear', function() {
|
||||
this.filter('');
|
||||
$('#filter-admins .key').val('');
|
||||
this.resetForm();
|
||||
}.bind(this));
|
||||
|
||||
/**
|
||||
* Event: Filter Admin Row "Click"
|
||||
*
|
||||
* Display the selected admin data to the user.
|
||||
*/
|
||||
$('#admins').on('click', '.admin-row', function(e) {
|
||||
if ($('#filter-admins .filter').prop('disabled')) {
|
||||
$('#filter-admins .results').css('color', '#AAA');
|
||||
return; // exit because we are currently on edit mode
|
||||
}
|
||||
|
||||
var adminId = $(e.currentTarget).attr('data-id'),
|
||||
admin = {};
|
||||
$.each(this.filterResults, function(index, item) {
|
||||
if (item.id === adminId) {
|
||||
admin = item;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
this.display(admin);
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
$(e.currentTarget).addClass('selected-row');
|
||||
$('#edit-admin, #delete-admin').prop('disabled', false);
|
||||
}.bind(this));
|
||||
|
||||
/**
|
||||
* Event: Add New Admin Button "Click"
|
||||
*/
|
||||
$('#admins').on('click', '#add-admin', function() {
|
||||
this.resetForm();
|
||||
$('#admins .add-edit-delete-group').hide();
|
||||
$('#admins .save-cancel-group').show();
|
||||
$('#admins .details').find('input, textarea').prop('readonly', false);
|
||||
$('#admin-password, #admin-password-confirm').addClass('required');
|
||||
$('#admin-notifications').prop('disabled', false);
|
||||
$('#filter-admins button').prop('disabled', true);
|
||||
$('#filter-admins .results').css('color', '#AAA');
|
||||
return; // exit because we are currently on edit mode
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
var adminId = $(this).attr('data-id');
|
||||
var admin = {};
|
||||
$.each(BackendUsers.helper.filterResults, function(index, item) {
|
||||
if (item.id === adminId) {
|
||||
admin = item;
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Event: Edit Admin Button "Click"
|
||||
*/
|
||||
$('#admins').on('click', '#edit-admin', function() {
|
||||
$('#admins .add-edit-delete-group').hide();
|
||||
$('#admins .save-cancel-group').show();
|
||||
$('#admins .details').find('input, textarea').prop('readonly', false);
|
||||
$('#admin-password, #admin-password-confirm').removeClass('required');
|
||||
$('#admin-notifications').prop('disabled', false);
|
||||
|
||||
$('#filter-admins .filter').prop('disabled', true);
|
||||
$('#filter-admins .results').css('color', '#AAA');
|
||||
});
|
||||
|
||||
BackendUsers.helper.display(admin);
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
$(this).addClass('selected-row');
|
||||
$('#edit-admin, #delete-admin').prop('disabled', false);
|
||||
});
|
||||
/**
|
||||
* Event: Delete Admin Button "Click"
|
||||
*/
|
||||
$('#admins').on('click', '#delete-admin', function() {
|
||||
var adminId = $('#admin-id').val();
|
||||
|
||||
/**
|
||||
* Event: Add New Admin Button "Click"
|
||||
*/
|
||||
$('#add-admin').click(function() {
|
||||
BackendUsers.helper.resetForm();
|
||||
$('#admins .add-edit-delete-group').hide();
|
||||
$('#admins .save-cancel-group').show();
|
||||
$('#admins .details').find('input, textarea').prop('readonly', false);
|
||||
$('#admin-password, #admin-password-confirm').addClass('required');
|
||||
$('#admin-notifications').prop('disabled', false);
|
||||
$('#filter-admins button').prop('disabled', true);
|
||||
$('#filter-admins .results').css('color', '#AAA');
|
||||
});
|
||||
var messageBtns = {};
|
||||
messageBtns[EALang['delete']] = function() {
|
||||
this.delete(adminId);
|
||||
$('#message_box').dialog('close');
|
||||
}.bind(this);
|
||||
messageBtns[EALang['cancel']] = function() {
|
||||
$('#message_box').dialog('close');
|
||||
};
|
||||
|
||||
/**
|
||||
* Event: Edit Admin Button "Click"
|
||||
*/
|
||||
$('#edit-admin').click(function() {
|
||||
$('#admins .add-edit-delete-group').hide();
|
||||
$('#admins .save-cancel-group').show();
|
||||
$('#admins .details').find('input, textarea').prop('readonly', false);
|
||||
$('#admin-password, #admin-password-confirm').removeClass('required');
|
||||
$('#admin-notifications').prop('disabled', false);
|
||||
GeneralFunctions.displayMessageBox(EALang['delete_admin'],
|
||||
EALang['delete_record_prompt'], messageBtns);
|
||||
}.bind(this));
|
||||
|
||||
$('#filter-admins .filter').prop('disabled', true);
|
||||
$('#filter-admins .results').css('color', '#AAA');
|
||||
});
|
||||
/**
|
||||
* Event: Save Admin Button "Click"
|
||||
*/
|
||||
$('#admins').on('click', '#save-admin', function() {
|
||||
var admin = {
|
||||
first_name: $('#admin-first-name').val(),
|
||||
last_name: $('#admin-last-name').val(),
|
||||
email: $('#admin-email').val(),
|
||||
mobile_number: $('#admin-mobile-number').val(),
|
||||
phone_number: $('#admin-phone-number').val(),
|
||||
address: $('#admin-address').val(),
|
||||
city: $('#admin-city').val(),
|
||||
state: $('#admin-state').val(),
|
||||
zip_code: $('#admin-zip-code').val(),
|
||||
notes: $('#admin-notes').val(),
|
||||
settings: {
|
||||
username: $('#admin-username').val(),
|
||||
notifications: $('#admin-notifications').hasClass('active')
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Event: Delete Admin Button "Click"
|
||||
*/
|
||||
$('#delete-admin').click(function() {
|
||||
var adminId = $('#admin-id').val();
|
||||
|
||||
var messageBtns = {};
|
||||
messageBtns[EALang['delete']] = function() {
|
||||
BackendUsers.helper.delete(adminId);
|
||||
$('#message_box').dialog('close');
|
||||
};
|
||||
messageBtns[EALang['cancel']] = function() {
|
||||
$('#message_box').dialog('close');
|
||||
};
|
||||
|
||||
GeneralFunctions.displayMessageBox(EALang['delete_admin'],
|
||||
EALang['delete_record_prompt'], messageBtns);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Save Admin Button "Click"
|
||||
*/
|
||||
$('#save-admin').click(function() {
|
||||
var admin = {
|
||||
'first_name': $('#admin-first-name').val(),
|
||||
'last_name': $('#admin-last-name').val(),
|
||||
'email': $('#admin-email').val(),
|
||||
'mobile_number': $('#admin-mobile-number').val(),
|
||||
'phone_number': $('#admin-phone-number').val(),
|
||||
'address': $('#admin-address').val(),
|
||||
'city': $('#admin-city').val(),
|
||||
'state': $('#admin-state').val(),
|
||||
'zip_code': $('#admin-zip-code').val(),
|
||||
'notes': $('#admin-notes').val(),
|
||||
'settings': {
|
||||
'username': $('#admin-username').val(),
|
||||
'notifications': $('#admin-notifications').hasClass('active')
|
||||
// Include password if changed.
|
||||
if ($('#admin-password').val() !== '') {
|
||||
admin.settings.password = $('#admin-password').val();
|
||||
}
|
||||
};
|
||||
|
||||
// Include password if changed.
|
||||
if ($('#admin-password').val() !== '') {
|
||||
admin.settings.password = $('#admin-password').val();
|
||||
}
|
||||
// Include id if changed.
|
||||
if ($('#admin-id').val() !== '') {
|
||||
admin.id = $('#admin-id').val();
|
||||
}
|
||||
|
||||
// Include id if changed.
|
||||
if ($('#admin-id').val() !== '') {
|
||||
admin.id = $('#admin-id').val();
|
||||
}
|
||||
if (!this.validate(admin)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!BackendUsers.helper.validate(admin)) return;
|
||||
this.save(admin);
|
||||
}.bind(this));
|
||||
|
||||
BackendUsers.helper.save(admin);
|
||||
});
|
||||
/**
|
||||
* Event: Cancel Admin Button "Click"
|
||||
*
|
||||
* Cancel add or edit of an admin record.
|
||||
*/
|
||||
$('#admins').on('click', '#cancel-admin', function() {
|
||||
var id = $('#admin-id').val();
|
||||
this.resetForm();
|
||||
if (id != '') {
|
||||
this.select(id, true);
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
* Event: Cancel Admin Button "Click"
|
||||
* Save admin record to database.
|
||||
*
|
||||
* Cancel add or edit of an admin record.
|
||||
* @param {object} admin Contains the admin record data. If an 'id' value is provided
|
||||
* then the update operation is going to be executed.
|
||||
*/
|
||||
$('#cancel-admin').click(function() {
|
||||
var id = $('#admin-id').val();
|
||||
BackendUsers.helper.resetForm();
|
||||
if (id != '') {
|
||||
BackendUsers.helper.select(id, true);
|
||||
}
|
||||
});
|
||||
};
|
||||
AdminsHelper.prototype.save = function(admin) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_admin',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
admin: JSON.stringify(admin)
|
||||
};
|
||||
|
||||
/**
|
||||
* Save admin record to database.
|
||||
*
|
||||
* @param {object} admin Contains the admin record data. If an 'id' value is provided
|
||||
* then the update operation is going to be executed.
|
||||
*/
|
||||
AdminsHelper.prototype.save = function(admin) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_admin';
|
||||
var postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'admin': JSON.stringify(admin)
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
Backend.displayNotification(EALang['admin_saved']);
|
||||
BackendUsers.helper.resetForm();
|
||||
$('#filter-admins .key').val('');
|
||||
BackendUsers.helper.filter('', response.id, true);
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete an admin record from database.
|
||||
*
|
||||
* @param {int} id Record id to be deleted.
|
||||
*/
|
||||
AdminsHelper.prototype.delete = function(id) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_admin';
|
||||
var postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'admin_id': id
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
Backend.displayNotification(EALang['admin_deleted']);
|
||||
BackendUsers.helper.resetForm();
|
||||
BackendUsers.helper.filter($('#filter-admins .key').val());
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates an admin record.
|
||||
*
|
||||
* @param {object} admin Contains the admin data to be validated.
|
||||
* @returns {bool} Returns the validation result.
|
||||
*/
|
||||
AdminsHelper.prototype.validate = function(admin) {
|
||||
$('#admins .required').css('border', '');
|
||||
$('#admin-password, #admin-password-confirm').css('border', '');
|
||||
|
||||
try {
|
||||
// Validate required fields.
|
||||
var missingRequired = false;
|
||||
$('#admins .required').each(function() {
|
||||
if ($(this).val() == '' || $(this).val() == undefined) {
|
||||
$(this).css('border', '2px solid red');
|
||||
missingRequired = true;
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
if (missingRequired) {
|
||||
throw 'Fields with * are required.';
|
||||
}
|
||||
|
||||
// Validate passwords.
|
||||
if ($('#admin-password').val() != $('#admin-password-confirm').val()) {
|
||||
$('#admin-password, #admin-password-confirm').css('border', '2px solid red');
|
||||
throw EALang['passwords_mismatch'];
|
||||
}
|
||||
|
||||
if ($('#admin-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH
|
||||
&& $('#admin-password').val() != '') {
|
||||
$('#admin-password, #admin-password-confirm').css('border', '2px solid red');
|
||||
|
||||
throw EALang['password_length_notice'].replace('$number', BackendUsers.MIN_PASSWORD_LENGTH);
|
||||
}
|
||||
|
||||
// Validate user email.
|
||||
if (!GeneralFunctions.validateEmail($('#admin-email').val())) {
|
||||
$('#admin-email').css('border', '2px solid red');
|
||||
throw EALang['invalid_email'];
|
||||
}
|
||||
|
||||
// Check if username exists
|
||||
if ($('#admin-username').attr('already-exists') == 'true') {
|
||||
$('#admin-username').css('border', '2px solid red');
|
||||
throw EALang['username_already_exists'];
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch(exc) {
|
||||
$('#admins .form-message').text(exc);
|
||||
$('#admins .form-message').show();
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets the admin form back to its initial state.
|
||||
*/
|
||||
AdminsHelper.prototype.resetForm = function() {
|
||||
$('#admins .add-edit-delete-group').show();
|
||||
$('#admins .save-cancel-group').hide();
|
||||
$('#admins .details').find('input, textarea').prop('readonly', true);
|
||||
$('#admins .form-message').hide();
|
||||
$('#admin-notifications').prop('disabled', true);
|
||||
$('#admins .required').css('border', '');
|
||||
$('#admin-password, #admin-password-confirm').css('border', '');
|
||||
$('#admins .details').find('input, textarea').val('');
|
||||
$('#admin-notifications').removeClass('active');
|
||||
$('#edit-admin, #delete-admin').prop('disabled', true);
|
||||
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
$('#filter-admins button').prop('disabled', false);
|
||||
$('#filter-admins .results').css('color', '');
|
||||
};
|
||||
|
||||
/**
|
||||
* Display a admin record into the admin form.
|
||||
*
|
||||
* @param {object} admin Contains the admin record data.
|
||||
*/
|
||||
AdminsHelper.prototype.display = function(admin) {
|
||||
$('#admin-id').val(admin.id);
|
||||
$('#admin-first-name').val(admin.first_name);
|
||||
$('#admin-last-name').val(admin.last_name);
|
||||
$('#admin-email').val(admin.email);
|
||||
$('#admin-mobile-number').val(admin.mobile_number);
|
||||
$('#admin-phone-number').val(admin.phone_number);
|
||||
$('#admin-address').val(admin.address);
|
||||
$('#admin-city').val(admin.city);
|
||||
$('#admin-state').val(admin.state);
|
||||
$('#admin-zip-code').val(admin.zip_code);
|
||||
$('#admin-notes').val(admin.notes);
|
||||
|
||||
$('#admin-username').val(admin.settings.username);
|
||||
if (admin.settings.notifications == true) {
|
||||
$('#admin-notifications').addClass('active');
|
||||
} else {
|
||||
$('#admin-notifications').removeClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Filters admin records depending a key string.
|
||||
*
|
||||
* @param {string} key This string is used to filter the admin records of the database.
|
||||
* @param {numeric} selectId (OPTIONAL = undefined) This record id will be selected when
|
||||
* the filter operation is finished.
|
||||
* @param {bool} display (OPTIONAL = false) If true the selected record data are going
|
||||
* to be displayed on the details column (requires a selected record though).
|
||||
*/
|
||||
AdminsHelper.prototype.filter = function(key, selectId, display) {
|
||||
if (display == undefined) display = false;
|
||||
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_admins';
|
||||
var postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'key': key
|
||||
Backend.displayNotification(EALang['admin_saved']);
|
||||
this.resetForm();
|
||||
$('#filter-admins .key').val('');
|
||||
this.filter('', response.id, true);
|
||||
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
/**
|
||||
* Delete an admin record from database.
|
||||
*
|
||||
* @param {int} id Record id to be deleted.
|
||||
*/
|
||||
AdminsHelper.prototype.delete = function(id) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_admin',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
admin_id: id
|
||||
};
|
||||
|
||||
BackendUsers.helper.filterResults = response;
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
Backend.displayNotification(EALang['admin_deleted']);
|
||||
this.resetForm();
|
||||
this.filter($('#filter-admins .key').val());
|
||||
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
$('#filter-admins .results').data('jsp').destroy();
|
||||
$('#filter-admins .results').html('');
|
||||
$.each(response, function(index, admin) {
|
||||
var html = AdminsHelper.prototype.getFilterHtml(admin);
|
||||
$('#filter-admins .results').append(html);
|
||||
});
|
||||
$('#filter-admins .results').jScrollPane({ mouseWheelSpeed: 70 });
|
||||
/**
|
||||
* Validates an admin record.
|
||||
*
|
||||
* @param {object} admin Contains the admin data to be validated.
|
||||
* @returns {bool} Returns the validation result.
|
||||
*/
|
||||
AdminsHelper.prototype.validate = function(admin) {
|
||||
$('#admins .required').css('border', '');
|
||||
$('#admin-password, #admin-password-confirm').css('border', '');
|
||||
|
||||
if (response.length == 0) {
|
||||
$('#filter-admins .results').html('<em>' + EALang['no_records_found'] + '</em>')
|
||||
}
|
||||
try {
|
||||
// Validate required fields.
|
||||
var missingRequired = false;
|
||||
$('#admins .required').each(function() {
|
||||
if ($(this).val() == '' || $(this).val() == undefined) {
|
||||
$(this).css('border', '2px solid red');
|
||||
missingRequired = true;
|
||||
}
|
||||
});
|
||||
if (missingRequired) {
|
||||
throw 'Fields with * are required.';
|
||||
}
|
||||
|
||||
if (selectId != undefined) {
|
||||
BackendUsers.helper.select(selectId, display);
|
||||
}
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
// Validate passwords.
|
||||
if ($('#admin-password').val() != $('#admin-password-confirm').val()) {
|
||||
$('#admin-password, #admin-password-confirm').css('border', '2px solid red');
|
||||
throw EALang['passwords_mismatch'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an admin row html code that is going to be displayed on the filter results list.
|
||||
*
|
||||
* @param {object} admin Contains the admin record data.
|
||||
* @returns {string} The html code that represents the record on the filter results list.
|
||||
*/
|
||||
AdminsHelper.prototype.getFilterHtml = function(admin) {
|
||||
var name = admin.first_name + ' ' + admin.last_name;
|
||||
var info = admin.email;
|
||||
info = (admin.mobile_number != '' && admin.mobile_number != null)
|
||||
? info + ', ' + admin.mobile_number : info;
|
||||
info = (admin.phone_number != '' && admin.phone_number != null)
|
||||
? info + ', ' + admin.phone_number : info;
|
||||
if ($('#admin-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH
|
||||
&& $('#admin-password').val() != '') {
|
||||
$('#admin-password, #admin-password-confirm').css('border', '2px solid red');
|
||||
throw EALang['password_length_notice'].replace('$number', BackendUsers.MIN_PASSWORD_LENGTH);
|
||||
}
|
||||
|
||||
var html =
|
||||
'<div class="admin-row" data-id="' + admin.id + '">' +
|
||||
'<strong>' + name + '</strong><br>' +
|
||||
info + '<br>' +
|
||||
'</div><hr>';
|
||||
// Validate user email.
|
||||
if (!GeneralFunctions.validateEmail($('#admin-email').val())) {
|
||||
$('#admin-email').css('border', '2px solid red');
|
||||
throw EALang['invalid_email'];
|
||||
}
|
||||
|
||||
return html;
|
||||
};
|
||||
// Check if username exists
|
||||
if ($('#admin-username').attr('already-exists') == 'true') {
|
||||
$('#admin-username').css('border', '2px solid red');
|
||||
throw EALang['username_already_exists'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a specific record from the current filter results. If the admin id does not exist
|
||||
* in the list then no record will be selected.
|
||||
*
|
||||
* @param {numeric} id The record id to be selected from the filter results.
|
||||
* @param {bool} display (OPTIONAL = false) If true then the method will display the record
|
||||
* on the form.
|
||||
*/
|
||||
AdminsHelper.prototype.select = function(id, display) {
|
||||
if (display == undefined) display = false;
|
||||
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
|
||||
$('.admin-row').each(function() {
|
||||
if ($(this).attr('data-id') == id) {
|
||||
$(this).addClass('selected-row');
|
||||
return true;
|
||||
} catch(exc) {
|
||||
$('#admins .form-message').text(exc);
|
||||
$('#admins .form-message').show();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (display) {
|
||||
$.each(BackendUsers.helper.filterResults, function(index, admin) {
|
||||
if (admin.id == id) {
|
||||
BackendUsers.helper.display(admin);
|
||||
$('#edit-admin, #delete-admin').prop('disabled', false);
|
||||
/**
|
||||
* Resets the admin form back to its initial state.
|
||||
*/
|
||||
AdminsHelper.prototype.resetForm = function() {
|
||||
$('#admins .add-edit-delete-group').show();
|
||||
$('#admins .save-cancel-group').hide();
|
||||
$('#admins .details').find('input, textarea').prop('readonly', true);
|
||||
$('#admins .form-message').hide();
|
||||
$('#admin-notifications').prop('disabled', true);
|
||||
$('#admins .required').css('border', '');
|
||||
$('#admin-password, #admin-password-confirm').css('border', '');
|
||||
$('#admins .details').find('input, textarea').val('');
|
||||
$('#admin-notifications').removeClass('active');
|
||||
$('#edit-admin, #delete-admin').prop('disabled', true);
|
||||
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
$('#filter-admins button').prop('disabled', false);
|
||||
$('#filter-admins .results').css('color', '');
|
||||
};
|
||||
|
||||
/**
|
||||
* Display a admin record into the admin form.
|
||||
*
|
||||
* @param {object} admin Contains the admin record data.
|
||||
*/
|
||||
AdminsHelper.prototype.display = function(admin) {
|
||||
$('#admin-id').val(admin.id);
|
||||
$('#admin-first-name').val(admin.first_name);
|
||||
$('#admin-last-name').val(admin.last_name);
|
||||
$('#admin-email').val(admin.email);
|
||||
$('#admin-mobile-number').val(admin.mobile_number);
|
||||
$('#admin-phone-number').val(admin.phone_number);
|
||||
$('#admin-address').val(admin.address);
|
||||
$('#admin-city').val(admin.city);
|
||||
$('#admin-state').val(admin.state);
|
||||
$('#admin-zip-code').val(admin.zip_code);
|
||||
$('#admin-notes').val(admin.notes);
|
||||
|
||||
$('#admin-username').val(admin.settings.username);
|
||||
if (admin.settings.notifications == true) {
|
||||
$('#admin-notifications').addClass('active');
|
||||
} else {
|
||||
$('#admin-notifications').removeClass('active');
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Filters admin records depending a key string.
|
||||
*
|
||||
* @param {string} key This string is used to filter the admin records of the database.
|
||||
* @param {numeric} selectId (OPTIONAL = undefined) This record id will be selected when
|
||||
* the filter operation is finished.
|
||||
* @param {bool} display (OPTIONAL = false) If true the selected record data are going
|
||||
* to be displayed on the details column (requires a selected record though).
|
||||
*/
|
||||
AdminsHelper.prototype.filter = function(key, selectId, display) {
|
||||
display = display || false;
|
||||
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_admins',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
key: key
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.filterResults = response;
|
||||
|
||||
$('#filter-admins .results').data('jsp').destroy();
|
||||
$('#filter-admins .results').html('');
|
||||
$.each(response, function(index, admin) {
|
||||
var html = this.getFilterHtml(admin);
|
||||
$('#filter-admins .results').append(html);
|
||||
}.bind(this));
|
||||
$('#filter-admins .results').jScrollPane({ mouseWheelSpeed: 70 });
|
||||
|
||||
if (response.length == 0) {
|
||||
$('#filter-admins .results').html('<em>' + EALang['no_records_found'] + '</em>')
|
||||
}
|
||||
|
||||
if (selectId != undefined) {
|
||||
this.select(selectId, display);
|
||||
}
|
||||
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get an admin row html code that is going to be displayed on the filter results list.
|
||||
*
|
||||
* @param {object} admin Contains the admin record data.
|
||||
* @returns {string} The html code that represents the record on the filter results list.
|
||||
*/
|
||||
AdminsHelper.prototype.getFilterHtml = function(admin) {
|
||||
var name = admin.first_name + ' ' + admin.last_name,
|
||||
info = admin.email;
|
||||
|
||||
info = (admin.mobile_number != '' && admin.mobile_number != null)
|
||||
? info + ', ' + admin.mobile_number : info;
|
||||
|
||||
info = (admin.phone_number != '' && admin.phone_number != null)
|
||||
? info + ', ' + admin.phone_number : info;
|
||||
|
||||
var html =
|
||||
'<div class="admin-row" data-id="' + admin.id + '">' +
|
||||
'<strong>' + name + '</strong><br>' +
|
||||
info + '<br>' +
|
||||
'</div><hr>';
|
||||
|
||||
return html;
|
||||
};
|
||||
|
||||
/**
|
||||
* Select a specific record from the current filter results. If the admin id does not exist
|
||||
* in the list then no record will be selected.
|
||||
*
|
||||
* @param {numeric} id The record id to be selected from the filter results.
|
||||
* @param {bool} display (OPTIONAL = false) If true then the method will display the record
|
||||
* on the form.
|
||||
*/
|
||||
AdminsHelper.prototype.select = function(id, display) {
|
||||
display = display || false;
|
||||
|
||||
$('#filter-admins .selected-row').removeClass('selected-row');
|
||||
|
||||
$('.admin-row').each(function() {
|
||||
if ($(this).attr('data-id') == id) {
|
||||
$(this).addClass('selected-row');
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (display) {
|
||||
$.each(this.filterResults, function(index, admin) {
|
||||
if (admin.id == id) {
|
||||
this.display(admin);
|
||||
$('#edit-admin, #delete-admin').prop('disabled', false);
|
||||
return false;
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
||||
|
||||
window.AdminsHelper = AdminsHelper;
|
||||
|
||||
})();
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -9,422 +9,442 @@
|
|||
* @since v1.0.0
|
||||
* ---------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* This class contains the Secretaries helper class declaration, along with the "Secretaries"
|
||||
* tab event handlers. By deviding the backend/users tab functionality into separate files
|
||||
* it is easier to maintain the code.
|
||||
*
|
||||
* @class SecretariesHelper
|
||||
*/
|
||||
var SecretariesHelper = function() {
|
||||
this.filterResults = {}; // Store the results for later use.
|
||||
};
|
||||
(function() {
|
||||
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Bind the event handlers for the backend/users "Secretaries" tab.
|
||||
*/
|
||||
SecretariesHelper.prototype.bindEventHandlers = function() {
|
||||
/**
|
||||
* Event: Filter Secretaries Form "Submit"
|
||||
* Secretaries Helper
|
||||
*
|
||||
* Filter the secretary records with the given key string.
|
||||
*/
|
||||
$('#filter-secretaries form').submit(function(event) {
|
||||
var key = $('#filter-secretaries .key').val();
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
BackendUsers.helper.resetForm();
|
||||
BackendUsers.helper.filter(key);
|
||||
return false;
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Clear Filter Results Button "Click"
|
||||
*/
|
||||
$('#filter-secretaries .clear').click(function() {
|
||||
BackendUsers.helper.filter('');
|
||||
$('#filter-secretaries .key').val('');
|
||||
BackendUsers.helper.resetForm();
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Filter Secretary Row "Click"
|
||||
* This class contains the Secretaries helper class declaration, along with the "Secretaries"
|
||||
* tab event handlers. By deviding the backend/users tab functionality into separate files
|
||||
* it is easier to maintain the code.
|
||||
*
|
||||
* Display the selected secretary data to the user.
|
||||
* @class SecretariesHelper
|
||||
*/
|
||||
$(document).on('click', '.secretary-row', function() {
|
||||
if ($('#filter-secretaries .filter').prop('disabled')) {
|
||||
$('#filter-secretaries .results').css('color', '#AAA');
|
||||
return; // exit because we are currently on edit mode
|
||||
}
|
||||
|
||||
var secretaryId = $(this).attr('data-id');
|
||||
var secretary = {};
|
||||
$.each(BackendUsers.helper.filterResults, function(index, item) {
|
||||
if (item.id === secretaryId) {
|
||||
secretary = item;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
BackendUsers.helper.display(secretary);
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
$(this).addClass('selected-row');
|
||||
$('#edit-secretary, #delete-secretary').prop('disabled', false);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Add New Secretary Button "Click"
|
||||
*/
|
||||
$('#add-secretary').click(function() {
|
||||
BackendUsers.helper.resetForm();
|
||||
$('#filter-secretaries button').prop('disabled', true);
|
||||
$('#filter-secretaries .results').css('color', '#AAA');
|
||||
|
||||
$('#secretaries .add-edit-delete-group').hide();
|
||||
$('#secretaries .save-cancel-group').show();
|
||||
$('#secretaries .details').find('input, textarea').prop('readonly', false);
|
||||
$('#secretary-password, #secretary-password-confirm').addClass('required');
|
||||
$('#secretary-notifications').prop('disabled', false);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('disabled', false);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Edit Secretary Button "Click"
|
||||
*/
|
||||
$('#edit-secretary').click(function() {
|
||||
$('#filter-secretaries button').prop('disabled', true);
|
||||
$('#filter-secretaries .results').css('color', '#AAA');
|
||||
|
||||
$('#secretaries .add-edit-delete-group').hide();
|
||||
$('#secretaries .save-cancel-group').show();
|
||||
$('#secretaries .details').find('input, textarea').prop('readonly', false);
|
||||
$('#secretary-password, #secretary-password-confirm').removeClass('required');
|
||||
$('#secretary-notifications').prop('disabled', false);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('disabled', false);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Delete Secretary Button "Click"
|
||||
*/
|
||||
$('#delete-secretary').click(function() {
|
||||
var secretaryId = $('#secretary-id').val();
|
||||
|
||||
var messageBtns = {};
|
||||
messageBtns[EALang['delete']] = function() {
|
||||
BackendUsers.helper.delete(secretaryId);
|
||||
$('#message_box').dialog('close');
|
||||
};
|
||||
messageBtns[EALang['cancel']] = function() {
|
||||
$('#message_box').dialog('close');
|
||||
};
|
||||
|
||||
GeneralFunctions.displayMessageBox(EALang['delete_secretary'],
|
||||
EALang['delete_record_prompt'], messageBtns);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Save Secretary Button "Click"
|
||||
*/
|
||||
$('#save-secretary').click(function() {
|
||||
var secretary = {
|
||||
'first_name': $('#secretary-first-name').val(),
|
||||
'last_name': $('#secretary-last-name').val(),
|
||||
'email': $('#secretary-email').val(),
|
||||
'mobile_number': $('#secretary-mobile-number').val(),
|
||||
'phone_number': $('#secretary-phone-number').val(),
|
||||
'address': $('#secretary-address').val(),
|
||||
'city': $('#secretary-city').val(),
|
||||
'state': $('#secretary-state').val(),
|
||||
'zip_code': $('#secretary-zip-code').val(),
|
||||
'notes': $('#secretary-notes').val(),
|
||||
'settings': {
|
||||
'username': $('#secretary-username').val(),
|
||||
'notifications': $('#secretary-notifications').hasClass('active')
|
||||
}
|
||||
};
|
||||
|
||||
// Include secretary services.
|
||||
secretary.providers = [];
|
||||
$('#secretary-providers input[type="checkbox"]').each(function() {
|
||||
if ($(this).prop('checked')) {
|
||||
secretary.providers.push($(this).attr('data-id'));
|
||||
}
|
||||
});
|
||||
|
||||
// Include password if changed.
|
||||
if ($('#secretary-password').val() !== '') {
|
||||
secretary.settings.password = $('#secretary-password').val();
|
||||
}
|
||||
|
||||
// Include id if changed.
|
||||
if ($('#secretary-id').val() !== '') {
|
||||
secretary.id = $('#secretary-id').val();
|
||||
}
|
||||
|
||||
if (!BackendUsers.helper.validate(secretary)) return;
|
||||
|
||||
BackendUsers.helper.save(secretary);
|
||||
});
|
||||
|
||||
/**
|
||||
* Event: Cancel Secretary Button "Click"
|
||||
*
|
||||
* Cancel add or edit of an secretary record.
|
||||
*/
|
||||
$('#cancel-secretary').click(function() {
|
||||
var id = $('#secretary-id').val();
|
||||
BackendUsers.helper.resetForm();
|
||||
if (id != '') {
|
||||
BackendUsers.helper.select(id, true);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Save secretary record to database.
|
||||
*
|
||||
* @param {object} secretary Contains the admin record data. If an 'id' value is provided
|
||||
* then the update operation is going to be executed.
|
||||
*/
|
||||
SecretariesHelper.prototype.save = function(secretary) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_secretary',
|
||||
postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'secretary': JSON.stringify(secretary)
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
Backend.displayNotification(EALang['secretary_saved']);
|
||||
BackendUsers.helper.resetForm();
|
||||
$('#filter-secretaries .key').val('');
|
||||
BackendUsers.helper.filter('', response.id, true);
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a secretary record from database.
|
||||
*
|
||||
* @param {int} id Record id to be deleted.
|
||||
*/
|
||||
SecretariesHelper.prototype.delete = function(id) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_secretary',
|
||||
postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'secretary_id': id
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
Backend.displayNotification(EALang['secretary_deleted']);
|
||||
BackendUsers.helper.resetForm();
|
||||
BackendUsers.helper.filter($('#filter-secretaries .key').val());
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a secretary record.
|
||||
*
|
||||
* @param {object} secretary Contains the admin data to be validated.
|
||||
* @returns {bool} Returns the validation result.
|
||||
*/
|
||||
SecretariesHelper.prototype.validate = function(secretary) {
|
||||
$('#secretaries .required').css('border', '');
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '');
|
||||
|
||||
try {
|
||||
// Validate required fields.
|
||||
var missingRequired = false;
|
||||
$('#secretaries .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 ($('#secretary-password').val() != $('#secretary-password-confirm').val()) {
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '2px solid red');
|
||||
throw 'Passwords mismatch!';
|
||||
}
|
||||
|
||||
if ($('#secretary-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH
|
||||
&& $('#secretary-password').val() != '') {
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '2px solid red');
|
||||
throw 'Password must be at least ' + BackendUsers.MIN_PASSWORD_LENGTH
|
||||
+ ' characters long.';
|
||||
}
|
||||
|
||||
// Validate user email.
|
||||
if (!GeneralFunctions.validateEmail($('#secretary-email').val())) {
|
||||
$('#secretary-email').css('border', '2px solid red');
|
||||
throw 'Invalid email address!';
|
||||
}
|
||||
|
||||
// Check if username exists
|
||||
if ($('#secretary-username').attr('already-exists') == 'true') {
|
||||
$('#secretary-username').css('border', '2px solid red');
|
||||
throw 'Username already exists.';
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch(exc) {
|
||||
$('#secretaries .form-message').text(exc);
|
||||
$('#secretaries .form-message').show();
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets the admin tab form back to its initial state.
|
||||
*/
|
||||
SecretariesHelper.prototype.resetForm = function() {
|
||||
$('#secretaries .details').find('input, textarea').val('');
|
||||
$('#secretaries .add-edit-delete-group').show();
|
||||
$('#secretaries .save-cancel-group').hide();
|
||||
$('#edit-secretary, #delete-secretary').prop('disabled', true);
|
||||
$('#secretaries .details').find('input, textarea').prop('readonly', true);
|
||||
$('#secretaries .form-message').hide();
|
||||
$('#secretary-notifications').removeClass('active');
|
||||
$('#secretary-notifications').prop('disabled', true);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('checked', false);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('disabled', true);
|
||||
$('#secretaries .required').css('border', '');
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '');
|
||||
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
$('#filter-secretaries button').prop('disabled', false);
|
||||
$('#filter-secretaries .results').css('color', '');
|
||||
};
|
||||
|
||||
/**
|
||||
* Display a secretary record into the admin form.
|
||||
*
|
||||
* @param {object} secretary Contains the secretary record data.
|
||||
*/
|
||||
SecretariesHelper.prototype.display = function(secretary) {
|
||||
$('#secretary-id').val(secretary.id);
|
||||
$('#secretary-first-name').val(secretary.first_name);
|
||||
$('#secretary-last-name').val(secretary.last_name);
|
||||
$('#secretary-email').val(secretary.email);
|
||||
$('#secretary-mobile-number').val(secretary.mobile_number);
|
||||
$('#secretary-phone-number').val(secretary.phone_number);
|
||||
$('#secretary-address').val(secretary.address);
|
||||
$('#secretary-city').val(secretary.city);
|
||||
$('#secretary-state').val(secretary.state);
|
||||
$('#secretary-zip-code').val(secretary.zip_code);
|
||||
$('#secretary-notes').val(secretary.notes);
|
||||
|
||||
$('#secretary-username').val(secretary.settings.username);
|
||||
if (secretary.settings.notifications == true) {
|
||||
$('#secretary-notifications').addClass('active');
|
||||
} else {
|
||||
$('#secretary-notifications').removeClass('active');
|
||||
}
|
||||
|
||||
$('#secretary-providers input[type="checkbox"]').prop('checked', false);
|
||||
$.each(secretary.providers, function(index, providerId) {
|
||||
$('#secretary-providers input[type="checkbox"]').each(function() {
|
||||
if ($(this).attr('data-id') == providerId) {
|
||||
$(this).prop('checked', true);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Filters secretary records depending a string key.
|
||||
*
|
||||
* @param {string} key This is used to filter the secretary records of the database.
|
||||
* @param {numeric} selectId (OPTIONAL = undefined) If provided then the given id will be
|
||||
* selected in the filter results (only selected, not displayed).
|
||||
* @param {bool} display (OPTIONAL = false)
|
||||
*/
|
||||
SecretariesHelper.prototype.filter = function(key, selectId, display) {
|
||||
if (display == undefined) display = false;
|
||||
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_secretaries';
|
||||
var postData = {
|
||||
'csrfToken': GlobalVariables.csrfToken,
|
||||
'key': key
|
||||
var SecretariesHelper = function() {
|
||||
this.filterResults = {}; // Store the results for later use.
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) return;
|
||||
/**
|
||||
* Bind the event handlers for the backend/users "Secretaries" tab.
|
||||
*/
|
||||
SecretariesHelper.prototype.bindEventHandlers = function() {
|
||||
/**
|
||||
* Event: Filter Secretaries Form "Submit"
|
||||
*
|
||||
* Filter the secretary records with the given key string.
|
||||
*/
|
||||
$('#secretaries').on('submit', '#filter-secretaries form', function(event) {
|
||||
var key = $('#filter-secretaries .key').val();
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
this.resetForm();
|
||||
this.filter(key);
|
||||
return false;
|
||||
}.bind(this));
|
||||
|
||||
BackendUsers.helper.filterResults = response;
|
||||
/**
|
||||
* Event: Clear Filter Results Button "Click"
|
||||
*/
|
||||
$('#secretaries').on('click', '#filter-secretaries .clear', function() {
|
||||
this.filter('');
|
||||
$('#filter-secretaries .key').val('');
|
||||
this.resetForm();
|
||||
}.bind(this));
|
||||
|
||||
$('#filter-secretaries .results').data('jsp').destroy();
|
||||
$('#filter-secretaries .results').html('');
|
||||
$.each(response, function(index, secretary) {
|
||||
var html = SecretariesHelper.prototype.getFilterHtml(secretary);
|
||||
$('#filter-secretaries .results').append(html);
|
||||
/**
|
||||
* Event: Filter Secretary Row "Click"
|
||||
*
|
||||
* Display the selected secretary data to the user.
|
||||
*/
|
||||
$('#secretaries').on('click', '.secretary-row', function(e) {
|
||||
if ($('#filter-secretaries .filter').prop('disabled')) {
|
||||
$('#filter-secretaries .results').css('color', '#AAA');
|
||||
return; // exit because we are currently on edit mode
|
||||
}
|
||||
|
||||
var secretaryId = $(e.currentTarget).attr('data-id'),
|
||||
secretary = {};
|
||||
$.each(this.filterResults, function(index, item) {
|
||||
if (item.id === secretaryId) {
|
||||
secretary = item;
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
this.display(secretary);
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
$(e.currentTarget).addClass('selected-row');
|
||||
$('#edit-secretary, #delete-secretary').prop('disabled', false);
|
||||
}.bind(this));
|
||||
|
||||
/**
|
||||
* Event: Add New Secretary Button "Click"
|
||||
*/
|
||||
$('#secretaries').on('click', '#add-secretary', function() {
|
||||
this.resetForm();
|
||||
$('#filter-secretaries button').prop('disabled', true);
|
||||
$('#filter-secretaries .results').css('color', '#AAA');
|
||||
|
||||
$('#secretaries .add-edit-delete-group').hide();
|
||||
$('#secretaries .save-cancel-group').show();
|
||||
$('#secretaries .details').find('input, textarea').prop('readonly', false);
|
||||
$('#secretary-password, #secretary-password-confirm').addClass('required');
|
||||
$('#secretary-notifications').prop('disabled', false);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('disabled', false);
|
||||
}.bind(this));
|
||||
|
||||
/**
|
||||
* Event: Edit Secretary Button "Click"
|
||||
*/
|
||||
$('#secretaries').on('click', '#edit-secretary', function() {
|
||||
$('#filter-secretaries button').prop('disabled', true);
|
||||
$('#filter-secretaries .results').css('color', '#AAA');
|
||||
|
||||
$('#secretaries .add-edit-delete-group').hide();
|
||||
$('#secretaries .save-cancel-group').show();
|
||||
$('#secretaries .details').find('input, textarea').prop('readonly', false);
|
||||
$('#secretary-password, #secretary-password-confirm').removeClass('required');
|
||||
$('#secretary-notifications').prop('disabled', false);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('disabled', false);
|
||||
});
|
||||
$('#filter-secretaries .results').jScrollPane({ mouseWheelSpeed: 70 });
|
||||
|
||||
if (response.length == 0) {
|
||||
$('#filter-secretaries .results').html('<em>' + EALang['no_records_found'] + '</em>')
|
||||
}
|
||||
/**
|
||||
* Event: Delete Secretary Button "Click"
|
||||
*/
|
||||
$('#secretaries').on('click', '#delete-secretary', function() {
|
||||
var secretaryId = $('#secretary-id').val();
|
||||
|
||||
if (selectId != undefined) {
|
||||
BackendUsers.helper.select(selectId, display);
|
||||
}
|
||||
}, 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
var messageBtns = {};
|
||||
messageBtns[EALang['delete']] = function() {
|
||||
this.delete(secretaryId);
|
||||
$('#message_box').dialog('close');
|
||||
}.bind(this);
|
||||
messageBtns[EALang['cancel']] = function() {
|
||||
$('#message_box').dialog('close');
|
||||
};
|
||||
|
||||
/**
|
||||
* Get an secretary row html code that is going to be displayed on the filter results list.
|
||||
*
|
||||
* @param {object} secretary Contains the secretary record data.
|
||||
* @returns {string} The html code that represents the record on the filter results list.
|
||||
*/
|
||||
SecretariesHelper.prototype.getFilterHtml = function(secretary) {
|
||||
var name = secretary.first_name + ' ' + secretary.last_name;
|
||||
var info = secretary.email;
|
||||
info = (secretary.mobile_number != '' && secretary.mobile_number != null)
|
||||
? info + ', ' + secretary.mobile_number : info;
|
||||
info = (secretary.phone_number != '' && secretary.phone_number != null)
|
||||
? info + ', ' + secretary.phone_number : info;
|
||||
GeneralFunctions.displayMessageBox(EALang['delete_secretary'],
|
||||
EALang['delete_record_prompt'], messageBtns);
|
||||
});
|
||||
|
||||
var html =
|
||||
'<div class="secretary-row" data-id="' + secretary.id + '">' +
|
||||
'<strong>' + name + '</strong><br>' +
|
||||
info + '<br>' +
|
||||
'</div><hr>';
|
||||
/**
|
||||
* Event: Save Secretary Button "Click"
|
||||
*/
|
||||
$('#secretaries').on('click', '#save-secretary', function() {
|
||||
var secretary = {
|
||||
first_name: $('#secretary-first-name').val(),
|
||||
last_name: $('#secretary-last-name').val(),
|
||||
email: $('#secretary-email').val(),
|
||||
mobile_number: $('#secretary-mobile-number').val(),
|
||||
phone_number: $('#secretary-phone-number').val(),
|
||||
address: $('#secretary-address').val(),
|
||||
city: $('#secretary-city').val(),
|
||||
state: $('#secretary-state').val(),
|
||||
zip_code: $('#secretary-zip-code').val(),
|
||||
notes: $('#secretary-notes').val(),
|
||||
settings: {
|
||||
username: $('#secretary-username').val(),
|
||||
notifications: $('#secretary-notifications').hasClass('active')
|
||||
}
|
||||
};
|
||||
|
||||
return html;
|
||||
};
|
||||
// Include secretary services.
|
||||
secretary.providers = [];
|
||||
$('#secretary-providers input[type="checkbox"]').each(function() {
|
||||
if ($(this).prop('checked')) {
|
||||
secretary.providers.push($(this).attr('data-id'));
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Select a specific record from the current filter results. If the secretary id does not exist
|
||||
* in the list then no record will be selected.
|
||||
*
|
||||
* @param {numeric} id The record id to be selected from the filter results.
|
||||
* @param {bool} display (OPTIONAL = false) If true then the method will display the record
|
||||
* on the form.
|
||||
*/
|
||||
SecretariesHelper.prototype.select = function(id, display) {
|
||||
if (display == undefined) display = false;
|
||||
// Include password if changed.
|
||||
if ($('#secretary-password').val() !== '') {
|
||||
secretary.settings.password = $('#secretary-password').val();
|
||||
}
|
||||
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
// Include id if changed.
|
||||
if ($('#secretary-id').val() !== '') {
|
||||
secretary.id = $('#secretary-id').val();
|
||||
}
|
||||
|
||||
$('#filter-secretaries .secretary-row').each(function() {
|
||||
if ($(this).attr('data-id') == id) {
|
||||
$(this).addClass('selected-row');
|
||||
if (!this.validate(secretary)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.save(secretary);
|
||||
}.bind(this));
|
||||
|
||||
/**
|
||||
* Event: Cancel Secretary Button "Click"
|
||||
*
|
||||
* Cancel add or edit of an secretary record.
|
||||
*/
|
||||
$('#secretaries').on('Click', '#cancel-secretary', function() {
|
||||
var id = $('#secretary-id').val();
|
||||
this.resetForm();
|
||||
if (id != '') {
|
||||
this.select(id, true);
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
|
||||
/**
|
||||
* Save secretary record to database.
|
||||
*
|
||||
* @param {object} secretary Contains the admin record data. If an 'id' value is provided
|
||||
* then the update operation is going to be executed.
|
||||
*/
|
||||
SecretariesHelper.prototype.save = function(secretary) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_secretary',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
secretary: JSON.stringify(secretary)
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
Backend.displayNotification(EALang['secretary_saved']);
|
||||
this.resetForm();
|
||||
$('#filter-secretaries .key').val('');
|
||||
this.filter('', response.id, true);
|
||||
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Delete a secretary record from database.
|
||||
*
|
||||
* @param {int} id Record id to be deleted.
|
||||
*/
|
||||
SecretariesHelper.prototype.delete = function(id) {
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_secretary',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
secretary_id: id
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
Backend.displayNotification(EALang['secretary_deleted']);
|
||||
this.resetForm();
|
||||
this.filter($('#filter-secretaries .key').val());
|
||||
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Validates a secretary record.
|
||||
*
|
||||
* @param {object} secretary Contains the admin data to be validated.
|
||||
* @returns {bool} Returns the validation result.
|
||||
*/
|
||||
SecretariesHelper.prototype.validate = function(secretary) {
|
||||
$('#secretaries .required').css('border', '');
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '');
|
||||
|
||||
try {
|
||||
// Validate required fields.
|
||||
var missingRequired = false;
|
||||
$('#secretaries .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 ($('#secretary-password').val() != $('#secretary-password-confirm').val()) {
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '2px solid red');
|
||||
throw 'Passwords mismatch!';
|
||||
}
|
||||
|
||||
if ($('#secretary-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH
|
||||
&& $('#secretary-password').val() != '') {
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '2px solid red');
|
||||
throw 'Password must be at least ' + BackendUsers.MIN_PASSWORD_LENGTH
|
||||
+ ' characters long.';
|
||||
}
|
||||
|
||||
// Validate user email.
|
||||
if (!GeneralFunctions.validateEmail($('#secretary-email').val())) {
|
||||
$('#secretary-email').css('border', '2px solid red');
|
||||
throw 'Invalid email address!';
|
||||
}
|
||||
|
||||
// Check if username exists
|
||||
if ($('#secretary-username').attr('already-exists') == 'true') {
|
||||
$('#secretary-username').css('border', '2px solid red');
|
||||
throw 'Username already exists.';
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch(exc) {
|
||||
$('#secretaries .form-message').text(exc);
|
||||
$('#secretaries .form-message').show();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (display) {
|
||||
$.each(BackendUsers.helper.filterResults, function(index, admin) {
|
||||
if (admin.id == id) {
|
||||
BackendUsers.helper.display(admin);
|
||||
$('#edit-secretary, #delete-secretary').prop('disabled', false);
|
||||
/**
|
||||
* Resets the admin tab form back to its initial state.
|
||||
*/
|
||||
SecretariesHelper.prototype.resetForm = function() {
|
||||
$('#secretaries .details').find('input, textarea').val('');
|
||||
$('#secretaries .add-edit-delete-group').show();
|
||||
$('#secretaries .save-cancel-group').hide();
|
||||
$('#edit-secretary, #delete-secretary').prop('disabled', true);
|
||||
$('#secretaries .details').find('input, textarea').prop('readonly', true);
|
||||
$('#secretaries .form-message').hide();
|
||||
$('#secretary-notifications').removeClass('active');
|
||||
$('#secretary-notifications').prop('disabled', true);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('checked', false);
|
||||
$('#secretary-providers input[type="checkbox"]').prop('disabled', true);
|
||||
$('#secretaries .required').css('border', '');
|
||||
$('#secretary-password, #secretary-password-confirm').css('border', '');
|
||||
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
$('#filter-secretaries button').prop('disabled', false);
|
||||
$('#filter-secretaries .results').css('color', '');
|
||||
};
|
||||
|
||||
/**
|
||||
* Display a secretary record into the admin form.
|
||||
*
|
||||
* @param {object} secretary Contains the secretary record data.
|
||||
*/
|
||||
SecretariesHelper.prototype.display = function(secretary) {
|
||||
$('#secretary-id').val(secretary.id);
|
||||
$('#secretary-first-name').val(secretary.first_name);
|
||||
$('#secretary-last-name').val(secretary.last_name);
|
||||
$('#secretary-email').val(secretary.email);
|
||||
$('#secretary-mobile-number').val(secretary.mobile_number);
|
||||
$('#secretary-phone-number').val(secretary.phone_number);
|
||||
$('#secretary-address').val(secretary.address);
|
||||
$('#secretary-city').val(secretary.city);
|
||||
$('#secretary-state').val(secretary.state);
|
||||
$('#secretary-zip-code').val(secretary.zip_code);
|
||||
$('#secretary-notes').val(secretary.notes);
|
||||
|
||||
$('#secretary-username').val(secretary.settings.username);
|
||||
if (secretary.settings.notifications == true) {
|
||||
$('#secretary-notifications').addClass('active');
|
||||
} else {
|
||||
$('#secretary-notifications').removeClass('active');
|
||||
}
|
||||
|
||||
$('#secretary-providers input[type="checkbox"]').prop('checked', false);
|
||||
$.each(secretary.providers, function(index, providerId) {
|
||||
$('#secretary-providers input[type="checkbox"]').each(function() {
|
||||
if ($(this).attr('data-id') == providerId) {
|
||||
$(this).prop('checked', true);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Filters secretary records depending a string key.
|
||||
*
|
||||
* @param {string} key This is used to filter the secretary records of the database.
|
||||
* @param {numeric} selectId (OPTIONAL = undefined) If provided then the given id will be
|
||||
* selected in the filter results (only selected, not displayed).
|
||||
* @param {bool} display (OPTIONAL = false)
|
||||
*/
|
||||
SecretariesHelper.prototype.filter = function(key, selectId, display) {
|
||||
display = display || false;
|
||||
|
||||
var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_secretaries',
|
||||
postData = {
|
||||
csrfToken: GlobalVariables.csrfToken,
|
||||
key: key
|
||||
};
|
||||
|
||||
$.post(postUrl, postData, function(response) {
|
||||
if (!GeneralFunctions.handleAjaxExceptions(response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.filterResults = response;
|
||||
|
||||
$('#filter-secretaries .results').data('jsp').destroy();
|
||||
$('#filter-secretaries .results').html('');
|
||||
$.each(response, function(index, secretary) {
|
||||
var html = this.getFilterHtml(secretary);
|
||||
$('#filter-secretaries .results').append(html);
|
||||
}.bind(this));
|
||||
$('#filter-secretaries .results').jScrollPane({ mouseWheelSpeed: 70 });
|
||||
|
||||
if (response.length == 0) {
|
||||
$('#filter-secretaries .results').html('<em>' + EALang['no_records_found'] + '</em>')
|
||||
}
|
||||
|
||||
if (selectId != undefined) {
|
||||
this.select(selectId, display);
|
||||
}
|
||||
}.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler);
|
||||
};
|
||||
|
||||
/**
|
||||
* Get an secretary row html code that is going to be displayed on the filter results list.
|
||||
*
|
||||
* @param {object} secretary Contains the secretary record data.
|
||||
* @returns {string} The html code that represents the record on the filter results list.
|
||||
*/
|
||||
SecretariesHelper.prototype.getFilterHtml = function(secretary) {
|
||||
var name = secretary.first_name + ' ' + secretary.last_name,
|
||||
info = secretary.email;
|
||||
|
||||
info = (secretary.mobile_number != '' && secretary.mobile_number != null)
|
||||
? info + ', ' + secretary.mobile_number : info;
|
||||
|
||||
info = (secretary.phone_number != '' && secretary.phone_number != null)
|
||||
? info + ', ' + secretary.phone_number : info;
|
||||
|
||||
var html =
|
||||
'<div class="secretary-row" data-id="' + secretary.id + '">' +
|
||||
'<strong>' + name + '</strong><br>' +
|
||||
info + '<br>' +
|
||||
'</div><hr>';
|
||||
|
||||
return html;
|
||||
};
|
||||
|
||||
/**
|
||||
* Select a specific record from the current filter results. If the secretary id does not exist
|
||||
* in the list then no record will be selected.
|
||||
*
|
||||
* @param {numeric} id The record id to be selected from the filter results.
|
||||
* @param {bool} display (OPTIONAL = false) If true then the method will display the record
|
||||
* on the form.
|
||||
*/
|
||||
SecretariesHelper.prototype.select = function(id, display) {
|
||||
display = display || false;
|
||||
|
||||
$('#filter-secretaries .selected-row').removeClass('selected-row');
|
||||
|
||||
$('#filter-secretaries .secretary-row').each(function() {
|
||||
if ($(this).attr('data-id') == id) {
|
||||
$(this).addClass('selected-row');
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
if (display) {
|
||||
$.each(this.filterResults, function(index, admin) {
|
||||
if (admin.id == id) {
|
||||
this.display(admin);
|
||||
$('#edit-secretary, #delete-secretary').prop('disabled', false);
|
||||
return false;
|
||||
}
|
||||
}.bind(this));
|
||||
}
|
||||
};
|
||||
|
||||
window.SecretariesHelper = SecretariesHelper;
|
||||
|
||||
})();
|
||||
|
|
Loading…
Reference in a new issue