From bcc205522e80b6b7c492415d5d01a06bb6c14fb0 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Tue, 26 Apr 2016 21:33:30 +0200 Subject: [PATCH] Refactored the backend/users js files. --- src/assets/js/backend_users.js | 107 ++- src/assets/js/backend_users_admins.js | 727 +++++++------- src/assets/js/backend_users_providers.js | 1009 ++++++++++---------- src/assets/js/backend_users_secretaries.js | 810 ++++++++-------- 4 files changed, 1361 insertions(+), 1292 deletions(-) diff --git a/src/assets/js/backend_users.js b/src/assets/js/backend_users.js index 6314b875..55e7c3b7 100644 --- a/src/assets/js/backend_users.js +++ b/src/assets/js/backend_users.js @@ -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 = '
'; @@ -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); diff --git a/src/assets/js/backend_users_admins.js b/src/assets/js/backend_users_admins.js index 71d2e6b8..a579871f 100644 --- a/src/assets/js/backend_users_admins.js +++ b/src/assets/js/backend_users_admins.js @@ -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('' + EALang['no_records_found'] + '') - } + 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 = - '
' + - '' + name + '
' + - info + '
' + - '

'; + // 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('' + EALang['no_records_found'] + '') + } + + 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 = + '
' + + '' + name + '
' + + info + '
' + + '

'; + + 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; + +})(); diff --git a/src/assets/js/backend_users_providers.js b/src/assets/js/backend_users_providers.js index 92318cfc..1a440270 100644 --- a/src/assets/js/backend_users_providers.js +++ b/src/assets/js/backend_users_providers.js @@ -9,538 +9,561 @@ * @since v1.0.0 * ---------------------------------------------------------------------------- */ -/** - * This class contains the Providers helper class declaration, along with the "Providers" tab - * event handlers. By deviding the backend/users tab functionality into separate files - * it is easier to maintain the code. - * - * @class ProvidersHelper - */ -var ProvidersHelper = function() { - this.filterResults = {}; // Store the results for later use. -}; +(function() { + + 'use strict'; -/** - * Bind the event handlers for the backend/users "Providers" tab. - */ -ProvidersHelper.prototype.bindEventHandlers = function() { /** - * Event: Filter Providers Form "Submit" + * Providers Helper * - * Filter the provider records with the given key string. - */ - $('#filter-providers form').submit(function(event) { - var key = $('#filter-providers .key').val(); - $('.selected-row').removeClass('selected-row'); - BackendUsers.helper.resetForm(); - BackendUsers.helper.filter(key); - return false; - }); - - /** - * Event: Clear Filter Button "Click" - */ - $('#filter-providers .clear').click(function() { - BackendUsers.helper.filter(''); - $('#filter-providers .key').val(''); - BackendUsers.helper.resetForm(); - }); - - /** - * Event: Filter Provider Row "Click" + * This class contains the Providers helper class declaration, along with the "Providers" tab + * event handlers. By deviding the backend/users tab functionality into separate files + * it is easier to maintain the code. * - * Display the selected provider data to the user. + * @class ProvidersHelper */ - $(document).on('click', '.provider-row', function() { - if ($('#filter-providers .filter').prop('disabled')) { + var ProvidersHelper = function() { + this.filterResults = {}; // Store the results for later use. + }; + + /** + * Bind the event handlers for the backend/users "Providers" tab. + */ + ProvidersHelper.prototype.bindEventHandlers = function() { + /** + * Event: Filter Providers Form "Submit" + * + * Filter the provider records with the given key string. + */ + $('#providers').on('submit', '#filter-providers form', function(event) { + var key = $('#filter-providers .key').val(); + $('.selected-row').removeClass('selected-row'); + this.resetForm(); + this.filter(key); + return false; + }.bind(this)); + + /** + * Event: Clear Filter Button "Click" + */ + $('#providers').on('click', '#filter-providers .clear', function() { + this.filter(''); + $('#filter-providers .key').val(''); + this.resetForm(); + }.bind(this)); + + /** + * Event: Filter Provider Row "Click" + * + * Display the selected provider data to the user. + */ + $('#providers').on('click', '.provider-row', function(e) { + if ($('#filter-providers .filter').prop('disabled')) { + $('#filter-providers .results').css('color', '#AAA'); + return; // Exit because we are currently on edit mode. + } + + var providerId = $(e.currentTarget).attr('data-id'), + provider = {}; + $.each(this.filterResults, function(index, item) { + if (item.id === providerId) { + provider = item; + return false; + } + }); + + this.display(provider); + $('#filter-providers .selected-row').removeClass('selected-row'); + $(e.currentTarget).addClass('selected-row'); + $('#edit-provider, #delete-provider').prop('disabled', false); + }.bind(this)); + + /** + * Event: Add New Provider Button "Click" + */ + $('#providers').on('click', '#add-provider', function() { + this.resetForm(); + $('#filter-providers button').prop('disabled', true); $('#filter-providers .results').css('color', '#AAA'); - return; // Exit because we are currently on edit mode. - } + $('#providers .add-edit-delete-group').hide(); + $('#providers .save-cancel-group').show(); + $('#providers .details').find('input, textarea').prop('readonly', false); + $('#provider-password, #provider-password-confirm').addClass('required'); + $('#provider-notifications').prop('disabled', false); + $('#providers').find('.add-break, .edit-break, .delete-break, #reset-working-plan').prop('disabled', false); + $('#provider-services input[type="checkbox"]').prop('disabled', false); + $('#providers input[type="checkbox"]').prop('disabled', false); - var providerId = $(this).attr('data-id'); - var provider = {}; - $.each(BackendUsers.helper.filterResults, function(index, item) { - if (item.id === providerId) { - provider = item; - return false; - } + // Apply default working plan + BackendUsers.wp.setup(GlobalVariables.workingPlan); + BackendUsers.wp.timepickers(false); + }.bind(this)); + + /** + * Event: Edit Provider Button "Click" + */ + $('#providers').on('click', '#edit-provider', function() { + $('#providers .add-edit-delete-group').hide(); + $('#providers .save-cancel-group').show(); + $('#filter-providers button').prop('disabled', true); + $('#filter-providers .results').css('color', '#AAA'); + $('#providers .details').find('input, textarea').prop('readonly', false); + $('#provider-password, #provider-password-confirm').removeClass('required'); + $('#provider-notifications').prop('disabled', false); + $('#provider-services input[type="checkbox"]').prop('disabled', false); + $('#providers').find('.add-break, .edit-break, .delete-break, #reset-working-plan').prop('disabled', false); + $('#providers input[type="checkbox"]').prop('disabled', false); + BackendUsers.wp.timepickers(false); }); - BackendUsers.helper.display(provider); - $('#filter-providers .selected-row').removeClass('selected-row'); - $(this).addClass('selected-row'); - $('#edit-provider, #delete-provider').prop('disabled', false); - }); + /** + * Event: Delete Provider Button "Click" + */ + $('#providers').on('click', '#delete-provider', function() { + var providerId = $('#provider-id').val(); - /** - * Event: Add New Provider Button "Click" - */ - $('#add-provider').click(function() { - BackendUsers.helper.resetForm(); - $('#filter-providers button').prop('disabled', true); - $('#filter-providers .results').css('color', '#AAA'); - $('#providers .add-edit-delete-group').hide(); - $('#providers .save-cancel-group').show(); - $('#providers .details').find('input, textarea').prop('readonly', false); - $('#provider-password, #provider-password-confirm').addClass('required'); - $('#provider-notifications').prop('disabled', false); - $('#providers').find('.add-break, .edit-break, .delete-break, #reset-working-plan').prop('disabled', false); - $('#provider-services input[type="checkbox"]').prop('disabled', false); - $('#providers input[type="checkbox"]').prop('disabled', false); + var messageBtns = {}; + messageBtns[EALang['delete']] = function() { + this.delete(providerId); + $('#message_box').dialog('close'); + }.bind(this); + messageBtns[EALang['cancel']] = function() { + $('#message_box').dialog('close'); + }; - // Apply default working plan - BackendUsers.wp.setup(GlobalVariables.workingPlan); - BackendUsers.wp.timepickers(false); - }); + GeneralFunctions.displayMessageBox(EALang['delete_provider'], + EALang['delete_record_prompt'], messageBtns); + }.bind(this)); - /** - * Event: Edit Provider Button "Click" - */ - $('#edit-provider').click(function() { - $('#providers .add-edit-delete-group').hide(); - $('#providers .save-cancel-group').show(); - $('#filter-providers button').prop('disabled', true); - $('#filter-providers .results').css('color', '#AAA'); - $('#providers .details').find('input, textarea').prop('readonly', false); - $('#provider-password, #provider-password-confirm').removeClass('required'); - $('#provider-notifications').prop('disabled', false); - $('#provider-services input[type="checkbox"]').prop('disabled', false); - $('#providers').find('.add-break, .edit-break, .delete-break, #reset-working-plan').prop('disabled', false); - $('#providers input[type="checkbox"]').prop('disabled', false); - BackendUsers.wp.timepickers(false); - }); + /** + * Event: Save Provider Button "Click" + */ + $('#providers').on('click', '#save-provider', function() { + var provider = { + first_name: $('#provider-first-name').val(), + last_name: $('#provider-last-name').val(), + email: $('#provider-email').val(), + mobile_number: $('#provider-mobile-number').val(), + phone_number: $('#provider-phone-number').val(), + address: $('#provider-address').val(), + city: $('#provider-city').val(), + state: $('#provider-state').val(), + zip_code: $('#provider-zip-code').val(), + notes: $('#provider-notes').val(), + settings: { + username: $('#provider-username').val(), + working_plan: JSON.stringify(BackendUsers.wp.get()), + notifications: $('#provider-notifications').hasClass('active') + } + }; - /** - * Event: Delete Provider Button "Click" - */ - $('#delete-provider').click(function() { - var providerId = $('#provider-id').val(); + // Include provider services. + provider.services = []; + $('#provider-services input[type="checkbox"]').each(function() { + if ($(this).prop('checked')) { + provider.services.push($(this).attr('data-id')); + } + }); - var messageBtns = {}; - messageBtns[EALang['delete']] = function() { - BackendUsers.helper.delete(providerId); - $('#message_box').dialog('close'); - }; - messageBtns[EALang['cancel']] = function() { - $('#message_box').dialog('close'); - }; - - GeneralFunctions.displayMessageBox(EALang['delete_provider'], - EALang['delete_record_prompt'], messageBtns); - }); - - /** - * Event: Save Provider Button "Click" - */ - $('#save-provider').click(function() { - var provider = { - 'first_name': $('#provider-first-name').val(), - 'last_name': $('#provider-last-name').val(), - 'email': $('#provider-email').val(), - 'mobile_number': $('#provider-mobile-number').val(), - 'phone_number': $('#provider-phone-number').val(), - 'address': $('#provider-address').val(), - 'city': $('#provider-city').val(), - 'state': $('#provider-state').val(), - 'zip_code': $('#provider-zip-code').val(), - 'notes': $('#provider-notes').val(), - 'settings': { - 'username': $('#provider-username').val(), - 'working_plan': JSON.stringify(BackendUsers.wp.get()), - 'notifications': $('#provider-notifications').hasClass('active') + // Include password if changed. + if ($('#provider-password').val() !== '') { + provider.settings.password = $('#provider-password').val(); } - }; - // Include provider services. - provider.services = []; - $('#provider-services input[type="checkbox"]').each(function() { - if ($(this).prop('checked')) { - provider.services.push($(this).attr('data-id')); + // Include id if changed. + if ($('#provider-id').val() !== '') { + provider.id = $('#provider-id').val(); } + + if (!this.validate(provider)) { + return; + } + + this.save(provider); + }.bind(this)); + + /** + * Event: Cancel Provider Button "Click" + * + * Cancel add or edit of an provider record. + */ + $('#providers').on('click', '#cancel-provider', function() { + var id = $('#filter-providers .selected-row').attr('data-id'); + this.resetForm(); + if (id != '') { + this.select(id, true); + } + }.bind(this)); + + /** + * Event: Display Provider Details "Click" + */ + $('#providers').on('click', '.display-details', function() { + $('#providers .switch-view .current').removeClass('current'); + $(this).addClass('current'); + $('.working-plan-view').hide('fade', function() { + $('.details-view').show('fade'); + }); }); - // Include password if changed. - if ($('#provider-password').val() !== '') { - provider.settings.password = $('#provider-password').val(); - } + /** + * Event: Display Provider Working Plan "Click" + */ + $('#providers').on('click', '.display-working-plan', function() { + $('#providers .switch-view .current').removeClass('current'); + $(this).addClass('current'); + $('.details-view').hide('fade', function() { + $('.working-plan-view').show('fade'); + }); + }); - // Include id if changed. - if ($('#provider-id').val() !== '') { - provider.id = $('#provider-id').val(); - } - - if (!BackendUsers.helper.validate(provider)) return; - - BackendUsers.helper.save(provider); - }); + /** + * Event: Reset Working Plan Button "Click". + */ + $('#providers').on('click', '#reset-working-plan', function() { + $('.breaks').empty(); + $('.work-start, .work-end').val(''); + BackendUsers.wp.setup(GlobalVariables.workingPlan); + BackendUsers.wp.timepickers(false); + }); + }; /** - * Event: Cancel Provider Button "Click" + * Save provider record to database. * - * Cancel add or edit of an provider record. + * @param {object} provider Contains the admin record data. If an 'id' value is provided + * then the update operation is going to be executed. */ - $('#cancel-provider').click(function() { - var id = $('#filter-providers .selected-row').attr('data-id'); - BackendUsers.helper.resetForm(); - if (id != '') { - BackendUsers.helper.select(id, true); - } - }); + ProvidersHelper.prototype.save = function(provider) { + var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_provider', + postData = { + csrfToken: GlobalVariables.csrfToken, + provider: JSON.stringify(provider) + }; - /** - * Event: Display Provider Details "Click" - */ - $('#providers .display-details').click(function() { - $('#providers .switch-view .current').removeClass('current'); - $(this).addClass('current'); - $('.working-plan-view').hide('fade', function() { - $('.details-view').show('fade'); - }); - }); - - /** - * Event: Display Provider Working Plan "Click" - */ - $('#providers .display-working-plan').click(function() { - $('#providers .switch-view .current').removeClass('current'); - $(this).addClass('current'); - $('.details-view').hide('fade', function() { - $('.working-plan-view').show('fade'); - }); - }); - - /** - * Event: Reset Working Plan Button "Click". - */ - $('#reset-working-plan').click(function() { - $('.breaks').empty(); - $('.work-start, .work-end').val(''); - BackendUsers.wp.setup(GlobalVariables.workingPlan); - BackendUsers.wp.timepickers(false); - }); -}; - -/** - * Save provider record to database. - * - * @param {object} provider Contains the admin record data. If an 'id' value is provided - * then the update operation is going to be executed. - */ -ProvidersHelper.prototype.save = function(provider) { - var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_provider'; - var postData = { - 'csrfToken': GlobalVariables.csrfToken, - 'provider': JSON.stringify(provider) - }; - - $.post(postUrl, postData, function(response) { - if (!GeneralFunctions.handleAjaxExceptions(response)) return; - Backend.displayNotification(EALang['provider_saved']); - BackendUsers.helper.resetForm(); - $('#filter-providers .key').val(''); - BackendUsers.helper.filter('', response.id, true); - }, 'json').fail(GeneralFunctions.ajaxFailureHandler); -}; - -/** - * Delete a provider record from database. - * - * @param {numeric} id Record id to be deleted. - */ -ProvidersHelper.prototype.delete = function(id) { - var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_provider'; - var postData = { - 'csrfToken': GlobalVariables.csrfToken, - 'provider_id': id - }; - - $.post(postUrl, postData, function(response) { - if (!GeneralFunctions.handleAjaxExceptions(response)) return; - Backend.displayNotification(EALang['provider_deleted']); - BackendUsers.helper.resetForm(); - BackendUsers.helper.filter($('#filter-providers .key').val()); - }, 'json').fail(GeneralFunctions.ajaxFailureHandler); -}; - -/** - * Validates a provider record. - * - * @param {object} provider Contains the admin data to be validated. - * @returns {bool} Returns the validation result. - */ -ProvidersHelper.prototype.validate = function(provider) { - $('#providers .required').css('border', ''); - $('#provider-password, #provider-password-confirm').css('border', ''); - - try { - // Validate required fields. - var missingRequired = false; - $('#providers .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 EALang['fields_are_required']; - } - - // Validate passwords. - if ($('#provider-password').val() != $('#provider-password-confirm').val()) { - $('#provider-password, #provider-password-confirm').css('border', '2px solid red'); - throw EALang['passwords_mismatch']; - } - - if ($('#provider-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH - && $('#provider-password').val() != '') { - $('#provider-password, #provider-password-confirm').css('border', '2px solid red'); - throw EALang['password_length_notice'].replace('$number', BackendUsers.MIN_PASSWORD_LENGTH); - } - - // Validate user email. - if (!GeneralFunctions.validateEmail($('#provider-email').val())) { - $('#provider-email').css('border', '2px solid red'); - throw EALang['invalid_email']; - } - - // Check if username exists - if ($('#provider-username').attr('already-exists') == 'true') { - $('#provider-username').css('border', '2px solid red'); - throw EALang['username_already_exists']; - } - - return true; - } catch(exc) { - $('#providers .form-message').text(exc); - $('#providers .form-message').show(); - return false; - } -}; - -/** - * Resets the admin tab form back to its initial state. - */ -ProvidersHelper.prototype.resetForm = function() { - $('#filter-providers .selected-row').removeClass('selected-row'); - $('#filter-providers button').prop('disabled', false); - $('#filter-providers .results').css('color', ''); - - $('#providers .add-edit-delete-group').show(); - $('#providers .save-cancel-group').hide(); - $('#providers .details').find('input, textarea').prop('readonly', true); - $('#providers .form-message').hide(); - $('#provider-notifications').removeClass('active'); - $('#provider-notifications').prop('disabled', true); - $('#provider-services input[type="checkbox"]').prop('disabled', true); - $('#providers .required').css('border', ''); - $('#provider-password, #provider-password-confirm').css('border', ''); - $('#providers .add-break, #reset-working-plan').prop('disabled', true); - BackendUsers.wp.timepickers(true); - $('#providers .working-plan input[type="text"]').timepicker('destroy'); - $('#providers .working-plan input[type="checkbox"]').prop('disabled', true); - $('.breaks').find('.edit-break, .delete-break').prop('disabled', true); - - $('#edit-provider, #delete-provider').prop('disabled', true); - $('#providers .details').find('input, textarea').val(''); - $('#providers input[type="checkbox"]').prop('checked', false); - $('#provider-services input[type="checkbox"]').prop('checked', false); - $('#providers .breaks tbody').empty(); - -}; - -/** - * Display a provider record into the admin form. - * - * @param {object} provider Contains the provider record data. - */ -ProvidersHelper.prototype.display = function(provider) { - $('#provider-id').val(provider.id); - $('#provider-first-name').val(provider.first_name); - $('#provider-last-name').val(provider.last_name); - $('#provider-email').val(provider.email); - $('#provider-mobile-number').val(provider.mobile_number); - $('#provider-phone-number').val(provider.phone_number); - $('#provider-address').val(provider.address); - $('#provider-city').val(provider.city); - $('#provider-state').val(provider.state); - $('#provider-zip-code').val(provider.zip_code); - $('#provider-notes').val(provider.notes); - - $('#provider-username').val(provider.settings.username); - if (provider.settings.notifications == true) { - $('#provider-notifications').addClass('active'); - } else { - $('#provider-notifications').removeClass('active'); - } - - $('#provider-services input[type="checkbox"]').prop('checked', false); - $.each(provider.services, function(index, serviceId) { - $('#provider-services input[type="checkbox"]').each(function() { - if ($(this).attr('data-id') == serviceId) { - $(this).prop('checked', true); - } - }); - }); - - // Display working plan - $('#providers .breaks tbody').empty(); - var workingPlan = $.parseJSON(provider.settings.working_plan); - BackendUsers.wp.setup(workingPlan); - $('.breaks').find('.edit-break, .delete-break').prop('disabled', true); -}; - -/** - * Filters provider records depending a string key. - * - * @param {string} key This is used to filter the provider records of the database. - * @param {numeric} selectId (OPTIONAL = undefined) If set, when the function is complete - * a result row can be set as selected. - * @param {bool} display (OPTIONAL = false) If true then the selected record will be also - * displayed. - */ -ProvidersHelper.prototype.filter = function(key, selectId, display) { - if (display == undefined) display = false; - - var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers'; - var postData = { - 'csrfToken': GlobalVariables.csrfToken, - 'key': key + Backend.displayNotification(EALang['provider_saved']); + this.resetForm(); + $('#filter-providers .key').val(''); + this.filter('', response.id, true); + }.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler); }; - $.post(postUrl, postData, function(response) { - if (!GeneralFunctions.handleAjaxExceptions(response)) return; + /** + * Delete a provider record from database. + * + * @param {numeric} id Record id to be deleted. + */ + ProvidersHelper.prototype.delete = function(id) { + var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_provider', + postData = { + csrfToken: GlobalVariables.csrfToken, + provider_id: id + }; - BackendUsers.helper.filterResults = response; + $.post(postUrl, postData, function(response) { + if (!GeneralFunctions.handleAjaxExceptions(response)) { + return; + } + Backend.displayNotification(EALang['provider_deleted']); + this.resetForm(); + this.filter($('#filter-providers .key').val()); + }.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler); + }; + /** + * Validates a provider record. + * + * @param {object} provider Contains the admin data to be validated. + * @returns {bool} Returns the validation result. + */ + ProvidersHelper.prototype.validate = function(provider) { + $('#providers .required').css('border', ''); + $('#provider-password, #provider-password-confirm').css('border', ''); - $('#filter-providers .results').data('jsp').destroy; - $('#filter-providers .results').html(''); - $.each(response, function(index, provider) { - var html = ProvidersHelper.prototype.getFilterHtml(provider); - $('#filter-providers .results').append(html); - }); - $('#filter-providers .results').jScrollPane({ mouseWheelSpeed: 70 }); + try { + // Validate required fields. + var missingRequired = false; + $('#providers .required').each(function() { + if ($(this).val() == '' || $(this).val() == undefined) { + $(this).css('border', '2px solid red'); + missingRequired = true; + } + }); + if (missingRequired) { + throw EALang['fields_are_required']; + } - if (response.length == 0) { - $('#filter-providers .results').html('' + EALang['no_records_found'] + '') - } + // Validate passwords. + if ($('#provider-password').val() != $('#provider-password-confirm').val()) { + $('#provider-password, #provider-password-confirm').css('border', '2px solid red'); + throw EALang['passwords_mismatch']; + } - if (selectId != undefined) { - BackendUsers.helper.select(selectId, display); - } - }, 'json').fail(GeneralFunctions.ajaxFailureHandler); -}; + if ($('#provider-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH + && $('#provider-password').val() != '') { + $('#provider-password, #provider-password-confirm').css('border', '2px solid red'); + throw EALang['password_length_notice'].replace('$number', BackendUsers.MIN_PASSWORD_LENGTH); + } -/** - * Get an provider row html code that is going to be displayed on the filter results list. - * - * @param {object} provider Contains the provider record data. - * @returns {string} The html code that represents the record on the filter results list. - */ -ProvidersHelper.prototype.getFilterHtml = function(provider) { - var name = provider.first_name + ' ' + provider.last_name; - var info = provider.email; - info = (provider.mobile_number != '' && provider.mobile_number != null) - ? info + ', ' + provider.mobile_number : info; - info = (provider.phone_number != '' && provider.phone_number != null) - ? info + ', ' + provider.phone_number : info; + // Validate user email. + if (!GeneralFunctions.validateEmail($('#provider-email').val())) { + $('#provider-email').css('border', '2px solid red'); + throw EALang['invalid_email']; + } - var html = - '
' + - '' + name + '
' + - info + '
' + - '

'; + // Check if username exists + if ($('#provider-username').attr('already-exists') == 'true') { + $('#provider-username').css('border', '2px solid red'); + throw EALang['username_already_exists']; + } - return html; -}; - -/** - * Initialize the editable functionality to the break day table cells. - * - * @param {object} $selector The cells to be initialized. - */ -ProvidersHelper.prototype.editableBreakDay = function($selector) { - var weekDays = {}; - weekDays[EALang['monday']] = 'Monday'; - weekDays[EALang['tuesday']] = 'Tuesday'; - weekDays[EALang['wednesday']] = 'Wednesday'; - weekDays[EALang['thursday']] = 'Thursday'; - weekDays[EALang['friday']] = 'Friday'; - weekDays[EALang['saturday']] = 'Saturday'; - weekDays[EALang['sunday']] = 'Sunday'; - - - $selector.editable(function(value, settings) { - return value; - }, { - 'type': 'select', - // 'data': '{ "Monday": "Monday", "Tuesday": "Tuesday", "Wednesday": "Wednesday", ' - // + '"Thursday": "Thursday", "Friday": "Friday", "Saturday": "Saturday", ' - // + '"Sunday": "Sunday", "selected": "Monday"}', - 'data': weekDays, - 'event': 'edit', - 'height': '30px', - 'submit': '', - 'cancel': '', - 'onblur': 'ignore', - 'onreset': function(settings, td) { - if (!BackendUsers.enableCancel) return false; // disable ESC button - }, - 'onsubmit': function(settings, td) { - if (!BackendUsers.enableSubmit) return false; // disable Enter button - } - }); -}; - -/** - * Initialize the editable functionality to the break time table cells. - * - * @param {object} $selector The cells to be initialized. - */ -ProvidersHelper.prototype.editableBreakTime = function($selector) { - $selector.editable(function(value, settings) { - // Do not return the value because the user needs to press the "Save" button. - return value; - }, { - 'event': 'edit', - 'height': '25px', - 'submit': '', - 'cancel': '', - 'onblur': 'ignore', - 'onreset': function(settings, td) { - if (!BackendUsers.enableCancel) return false; // disable ESC button - }, - 'onsubmit': function(settings, td) { - if (!BackendUsers.enableSubmit) return false; // disable Enter button - } - }); -}; - -/** - * Select and display a providers filter result on the form. - * - * @param {numeric} id Record id to be selected. - * @param {bool} display (OPTIONAL = false) If true the record will be displayed on the form. - */ -ProvidersHelper.prototype.select = function(id, display) { - if (display == undefined) display = false; - - // Select record in filter results. - $('#filter-providers .provider-row').each(function() { - if ($(this).attr('data-id') == id) { - $(this).addClass('selected-row'); + return true; + } catch(exc) { + $('#providers .form-message').text(exc); + $('#providers .form-message').show(); return false; } - }); + }; - // Display record in form (if display = true). - if (display) { - $.each(BackendUsers.helper.filterResults, function(index, provider) { - if (provider.id == id) { - BackendUsers.helper.display(provider); - $('#edit-provider, #delete-provider').prop('disabled', false); + /** + * Resets the admin tab form back to its initial state. + */ + ProvidersHelper.prototype.resetForm = function() { + $('#filter-providers .selected-row').removeClass('selected-row'); + $('#filter-providers button').prop('disabled', false); + $('#filter-providers .results').css('color', ''); + + $('#providers .add-edit-delete-group').show(); + $('#providers .save-cancel-group').hide(); + $('#providers .details').find('input, textarea').prop('readonly', true); + $('#providers .form-message').hide(); + $('#provider-notifications').removeClass('active'); + $('#provider-notifications').prop('disabled', true); + $('#provider-services input[type="checkbox"]').prop('disabled', true); + $('#providers .required').css('border', ''); + $('#provider-password, #provider-password-confirm').css('border', ''); + $('#providers .add-break, #reset-working-plan').prop('disabled', true); + BackendUsers.wp.timepickers(true); + $('#providers .working-plan input[type="text"]').timepicker('destroy'); + $('#providers .working-plan input[type="checkbox"]').prop('disabled', true); + $('.breaks').find('.edit-break, .delete-break').prop('disabled', true); + + $('#edit-provider, #delete-provider').prop('disabled', true); + $('#providers .details').find('input, textarea').val(''); + $('#providers input[type="checkbox"]').prop('checked', false); + $('#provider-services input[type="checkbox"]').prop('checked', false); + $('#providers .breaks tbody').empty(); + }; + + /** + * Display a provider record into the admin form. + * + * @param {object} provider Contains the provider record data. + */ + ProvidersHelper.prototype.display = function(provider) { + $('#provider-id').val(provider.id); + $('#provider-first-name').val(provider.first_name); + $('#provider-last-name').val(provider.last_name); + $('#provider-email').val(provider.email); + $('#provider-mobile-number').val(provider.mobile_number); + $('#provider-phone-number').val(provider.phone_number); + $('#provider-address').val(provider.address); + $('#provider-city').val(provider.city); + $('#provider-state').val(provider.state); + $('#provider-zip-code').val(provider.zip_code); + $('#provider-notes').val(provider.notes); + + $('#provider-username').val(provider.settings.username); + if (provider.settings.notifications == true) { + $('#provider-notifications').addClass('active'); + } else { + $('#provider-notifications').removeClass('active'); + } + + $('#provider-services input[type="checkbox"]').prop('checked', false); + $.each(provider.services, function(index, serviceId) { + $('#provider-services input[type="checkbox"]').each(function() { + if ($(this).attr('data-id') == serviceId) { + $(this).prop('checked', true); + } + }); + }); + + // Display working plan + $('#providers .breaks tbody').empty(); + var workingPlan = $.parseJSON(provider.settings.working_plan); + BackendUsers.wp.setup(workingPlan); + $('.breaks').find('.edit-break, .delete-break').prop('disabled', true); + }; + + /** + * Filters provider records depending a string key. + * + * @param {string} key This is used to filter the provider records of the database. + * @param {numeric} selectId (OPTIONAL = undefined) If set, when the function is complete + * a result row can be set as selected. + * @param {bool} display (OPTIONAL = false) If true then the selected record will be also + * displayed. + */ + ProvidersHelper.prototype.filter = function(key, selectId, display) { + display = display || false; + + var postUrl = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers', + postData = { + csrfToken: GlobalVariables.csrfToken, + key: key + }; + + $.post(postUrl, postData, function(response) { + if (!GeneralFunctions.handleAjaxExceptions(response)) { + return; + } + + this.filterResults = response; + + $('#filter-providers .results').data('jsp').destroy; + $('#filter-providers .results').html(''); + $.each(response, function(index, provider) { + var html = this.getFilterHtml(provider); + $('#filter-providers .results').append(html); + }.bind(this)); + $('#filter-providers .results').jScrollPane({ mouseWheelSpeed: 70 }); + + if (response.length == 0) { + $('#filter-providers .results').html('' + EALang['no_records_found'] + '') + } + + if (selectId != undefined) { + this.select(selectId, display); + } + }.bind(this), 'json').fail(GeneralFunctions.ajaxFailureHandler); + }; + + /** + * Get an provider row html code that is going to be displayed on the filter results list. + * + * @param {object} provider Contains the provider record data. + * @returns {string} The html code that represents the record on the filter results list. + */ + ProvidersHelper.prototype.getFilterHtml = function(provider) { + var name = provider.first_name + ' ' + provider.last_name, + info = provider.email; + + info = (provider.mobile_number != '' && provider.mobile_number != null) + ? info + ', ' + provider.mobile_number : info; + + info = (provider.phone_number != '' && provider.phone_number != null) + ? info + ', ' + provider.phone_number : info; + + var html = + '
' + + '' + name + '
' + + info + '
' + + '

'; + + return html; + }; + + /** + * Initialize the editable functionality to the break day table cells. + * + * @param {object} $selector The cells to be initialized. + */ + ProvidersHelper.prototype.editableBreakDay = function($selector) { + var weekDays = {}; + weekDays[EALang['monday']] = 'Monday'; + weekDays[EALang['tuesday']] = 'Tuesday'; + weekDays[EALang['wednesday']] = 'Wednesday'; + weekDays[EALang['thursday']] = 'Thursday'; + weekDays[EALang['friday']] = 'Friday'; + weekDays[EALang['saturday']] = 'Saturday'; + weekDays[EALang['sunday']] = 'Sunday'; + + + $selector.editable(function(value, settings) { + return value; + }, { + type: 'select', + data: weekDays, + event: 'edit', + height: '30px', + submit: '', + cancel: '', + onblur: 'ignore', + onreset: function(settings, td) { + if (!BackendUsers.enableCancel) { + return false; // disable ESC button + } + }, + onsubmit: function(settings, td) { + if (!BackendUsers.enableSubmit) { + return false; // disable Enter button + } + } + }); + }; + + /** + * Initialize the editable functionality to the break time table cells. + * + * @param {object} $selector The cells to be initialized. + */ + ProvidersHelper.prototype.editableBreakTime = function($selector) { + $selector.editable(function(value, settings) { + // Do not return the value because the user needs to press the "Save" button. + return value; + }, { + event: 'edit', + height: '25px', + submit: '', + cancel: '', + onblur: 'ignore', + onreset: function(settings, td) { + if (!BackendUsers.enableCancel) { + return false; // disable ESC button + } + }, + onsubmit: function(settings, td) { + if (!BackendUsers.enableSubmit) { + return false; // disable Enter button + } + } + }); + }; + + /** + * Select and display a providers filter result on the form. + * + * @param {numeric} id Record id to be selected. + * @param {bool} display (OPTIONAL = false) If true the record will be displayed on the form. + */ + ProvidersHelper.prototype.select = function(id, display) { + display = display || false; + + // Select record in filter results. + $('#filter-providers .provider-row').each(function() { + if ($(this).attr('data-id') == id) { + $(this).addClass('selected-row'); return false; } }); - } -}; + + // Display record in form (if display = true). + if (display) { + $.each(this.filterResults, function(index, provider) { + if (provider.id == id) { + this.display(provider); + $('#edit-provider, #delete-provider').prop('disabled', false); + return false; + } + }.bind(this)); + } + }; + + window.ProvidersHelper = ProvidersHelper; + +})(); diff --git a/src/assets/js/backend_users_secretaries.js b/src/assets/js/backend_users_secretaries.js index 076af921..6f9fccee 100644 --- a/src/assets/js/backend_users_secretaries.js +++ b/src/assets/js/backend_users_secretaries.js @@ -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('' + EALang['no_records_found'] + '') - } + /** + * 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 = - '
' + - '' + name + '
' + - info + '
' + - '

'; + /** + * 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('' + EALang['no_records_found'] + '') + } + + 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 = + '
' + + '' + name + '
' + + info + '
' + + '

'; + + 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; + +})();