MaketRandevu/assets/js/backend_users_providers.js

675 lines
25 KiB
JavaScript
Raw Normal View History

2015-07-20 22:41:24 +03:00
/* ----------------------------------------------------------------------------
* Easy!Appointments - Open Source Web Scheduler
*
2015-07-20 22:41:24 +03:00
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
2015-07-20 22:41:24 +03:00
* @since v1.0.0
* ---------------------------------------------------------------------------- */
2018-01-23 12:08:37 +03:00
(function () {
2016-04-26 22:33:30 +03:00
'use strict';
/**
2016-04-26 22:33:30 +03:00
* Providers Helper
*
* This class contains the Providers helper class declaration, along with the "Providers" tab
2016-10-06 22:10:56 +03:00
* event handlers. By dividing the backend/users tab functionality into separate files
2016-04-26 22:33:30 +03:00
* it is easier to maintain the code.
*
2016-04-26 22:33:30 +03:00
* @class ProvidersHelper
*/
2018-01-23 12:08:37 +03:00
var ProvidersHelper = function () {
2016-04-26 22:33:30 +03:00
this.filterResults = {}; // Store the results for later use.
this.filterLimit = 20;
2016-04-26 22:33:30 +03:00
};
/**
2016-04-26 22:33:30 +03:00
* Bind the event handlers for the backend/users "Providers" tab.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.bindEventHandlers = function () {
2016-04-26 22:33:30 +03:00
/**
* Event: Filter Providers Form "Submit"
*
* Filter the provider records with the given key string.
*
* @param {jQuery.Event} event
2016-04-26 22:33:30 +03:00
*/
$('#providers').on(
'submit',
'#filter-providers form',
function (event) {
event.preventDefault();
var key = $('#filter-providers .key').val();
$('.selected').removeClass('selected');
this.resetForm();
this.filter(key);
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* Event: Clear Filter Button "Click"
*/
$('#providers').on(
'click',
'#filter-providers .clear',
function () {
this.filter('');
$('#filter-providers .key').val('');
this.resetForm();
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* Event: Filter Provider Row "Click"
*
* Display the selected provider data to the user.
*/
$('#providers').on(
'click',
'.provider-row',
function (event) {
if ($('#filter-providers .filter').prop('disabled')) {
$('#filter-providers .results').css('color', '#AAA');
return; // Exit because we are currently on edit mode.
}
var providerId = $(event.currentTarget).attr('data-id');
var provider = this.filterResults.find(function (filterResult) {
return Number(filterResult.id) === Number(providerId);
});
2016-04-26 22:33:30 +03:00
this.display(provider);
$('#filter-providers .selected').removeClass('selected');
$(event.currentTarget).addClass('selected');
$('#edit-provider, #delete-provider').prop('disabled', false);
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* 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');
$('#providers .add-edit-delete-group').hide();
$('#providers .save-cancel-group').show();
$('#providers .record-details').find('input, select, textarea').prop('disabled', false);
$('#provider-password, #provider-password-confirm').addClass('required');
$('#providers')
.find(
'.add-break, .edit-break, .delete-break, .add-working-plan-exception, .edit-working-plan-exception, .delete-working-plan-exception, #reset-working-plan'
)
.prop('disabled', false);
$('#provider-services input:checkbox').prop('disabled', false);
// Apply default working plan
BackendUsers.wp.setup(GlobalVariables.workingPlan);
BackendUsers.wp.timepickers(false);
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* Event: Edit Provider Button "Click"
*/
2018-01-23 12:08:37 +03:00
$('#providers').on('click', '#edit-provider', function () {
2016-04-26 22:33:30 +03:00
$('#providers .add-edit-delete-group').hide();
$('#providers .save-cancel-group').show();
$('#filter-providers button').prop('disabled', true);
$('#filter-providers .results').css('color', '#AAA');
$('#providers .record-details').find('input, select, textarea').prop('disabled', false);
2016-04-26 22:33:30 +03:00
$('#provider-password, #provider-password-confirm').removeClass('required');
$('#provider-services input:checkbox').prop('disabled', false);
$('#providers')
.find(
'.add-break, .edit-break, .delete-break, .add-working-plan-exception, .edit-working-plan-exception, .delete-working-plan-exception, #reset-working-plan'
)
.prop('disabled', false);
$('#providers input:checkbox').prop('disabled', false);
2016-04-26 22:33:30 +03:00
BackendUsers.wp.timepickers(false);
});
2016-04-26 22:33:30 +03:00
/**
* Event: Delete Provider Button "Click"
*/
$('#providers').on(
'click',
'#delete-provider',
function () {
var providerId = $('#provider-id').val();
var buttons = [
{
text: App.Lang.cancel,
click: function () {
$('#message-box').dialog('close');
}
},
{
text: App.Lang.delete,
click: function () {
this.delete(providerId);
$('#message-box').dialog('close');
}.bind(this)
}
];
2016-04-26 22:33:30 +03:00
GeneralFunctions.displayMessageBox(App.Lang.delete_provider, App.Lang.delete_record_prompt, buttons);
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* 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(),
timezone: $('#provider-timezone').val(),
settings: {
username: $('#provider-username').val(),
working_plan: JSON.stringify(BackendUsers.wp.get()),
working_plan_exceptions: JSON.stringify(BackendUsers.wp.getWorkingPlanExceptions()),
notifications: $('#provider-notifications').prop('checked'),
calendar_view: $('#provider-calendar-view').val()
}
};
2016-04-26 22:33:30 +03:00
// Include provider services.
provider.services = [];
$('#provider-services input:checkbox').each(function (index, checkbox) {
if ($(checkbox).prop('checked')) {
provider.services.push($(checkbox).attr('data-id'));
}
});
2016-04-26 22:33:30 +03:00
// Include password if changed.
if ($('#provider-password').val() !== '') {
provider.settings.password = $('#provider-password').val();
}
2016-04-26 22:33:30 +03:00
// Include id if changed.
if ($('#provider-id').val() !== '') {
provider.id = $('#provider-id').val();
}
2016-04-26 22:33:30 +03:00
if (!this.validate()) {
return;
}
2016-04-26 22:33:30 +03:00
this.save(provider);
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* Event: Cancel Provider Button "Click"
*
* Cancel add or edit of an provider record.
*/
$('#providers').on(
'click',
'#cancel-provider',
function () {
var id = $('#filter-providers .selected').attr('data-id');
this.resetForm();
if (id) {
this.select(id, true);
}
}.bind(this)
);
2016-04-26 22:33:30 +03:00
/**
* Event: Display Provider Details "Click"
*/
$('#providers').on('shown.bs.tab', 'a[data-toggle="tab"]', function () {
Backend.placeFooterToBottom();
2016-04-26 22:33:30 +03:00
});
2016-04-26 22:33:30 +03:00
/**
* Event: Reset Working Plan Button "Click".
*/
2018-01-23 12:08:37 +03:00
$('#providers').on('click', '#reset-working-plan', function () {
$('.breaks tbody').empty();
$('.working-plan-exceptions tbody').empty();
2016-04-26 22:33:30 +03:00
$('.work-start, .work-end').val('');
BackendUsers.wp.setup(GlobalVariables.workingPlan);
BackendUsers.wp.timepickers(false);
});
};
/**
* Remove the previously registered event handlers.
*/
2020-12-09 15:17:45 +03:00
ProvidersHelper.prototype.unbindEventHandlers = function () {
$('#providers')
.off('submit', '#filter-providers form')
.off('click', '#filter-providers .clear')
.off('click', '.provider-row')
.off('click', '#add-provider')
.off('click', '#edit-provider')
.off('click', '#delete-provider')
.off('click', '#save-provider')
.off('click', '#cancel-provider')
.off('shown.bs.tab', 'a[data-toggle="tab"]')
.off('click', '#reset-working-plan');
};
/**
2016-04-26 22:33:30 +03:00
* Save provider record to database.
*
* @param {Object} provider Contains the admin record data. If an 'id' value is provided
2016-04-26 22:33:30 +03:00
* then the update operation is going to be executed.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.save = function (provider) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_save_provider';
var data = {
csrf_token: GlobalVariables.csrfToken,
2016-07-15 21:52:21 +03:00
provider: JSON.stringify(provider)
};
2016-04-26 22:33:30 +03:00
$.post(url, data).done(
function (response) {
Backend.displayNotification(App.Lang.provider_saved);
this.resetForm();
$('#filter-providers .key').val('');
this.filter('', response.id, true);
}.bind(this)
);
2016-04-26 22:33:30 +03:00
};
/**
2016-04-26 22:33:30 +03:00
* Delete a provider record from database.
*
* @param {Number} id Record id to be deleted.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.delete = function (id) {
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_provider';
var data = {
csrf_token: GlobalVariables.csrfToken,
2016-07-15 21:52:21 +03:00
provider_id: id
};
2016-04-26 22:33:30 +03:00
$.post(url, data).done(
function () {
Backend.displayNotification(App.Lang.provider_deleted);
this.resetForm();
this.filter($('#filter-providers .key').val());
}.bind(this)
);
2016-04-26 22:33:30 +03:00
};
/**
2016-04-26 22:33:30 +03:00
* Validates a provider record.
*
* @return {Boolean} Returns the validation result.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.validate = function () {
$('#providers .is-invalid').removeClass('is-invalid');
$('#providers .form-message').removeClass('alert-danger').hide();
2016-04-26 22:33:30 +03:00
try {
// Validate required fields.
var missingRequired = false;
$('#providers .required').each(function (index, requiredField) {
if (!$(requiredField).val()) {
$(requiredField).addClass('is-invalid');
2016-04-26 22:33:30 +03:00
missingRequired = true;
}
});
if (missingRequired) {
throw new Error(App.Lang.fields_are_required);
}
2016-04-26 22:33:30 +03:00
// Validate passwords.
if ($('#provider-password').val() !== $('#provider-password-confirm').val()) {
$('#provider-password, #provider-password-confirm').addClass('is-invalid');
throw new Error(App.Lang.passwords_mismatch);
}
if (
$('#provider-password').val().length < BackendUsers.MIN_PASSWORD_LENGTH &&
$('#provider-password').val() !== ''
) {
$('#provider-password, #provider-password-confirm').addClass('is-invalid');
throw new Error(App.Lang.password_length_notice.replace('$number', BackendUsers.MIN_PASSWORD_LENGTH));
2016-04-26 22:33:30 +03:00
}
// Validate user email.
if (!GeneralFunctions.validateEmail($('#provider-email').val())) {
$('#provider-email').addClass('is-invalid');
throw new Error(App.Lang.invalid_email);
2016-04-26 22:33:30 +03:00
}
2016-04-26 22:33:30 +03:00
// Check if username exists
if ($('#provider-username').attr('already-exists') === 'true') {
$('#provider-username').addClass('is-invalid');
throw new Error(App.Lang.username_already_exists);
2016-04-26 22:33:30 +03:00
}
return true;
} catch (error) {
$('#providers .form-message').addClass('alert-danger').text(error.message).show();
2016-04-26 22:33:30 +03:00
return false;
}
2016-04-26 22:33:30 +03:00
};
2016-04-26 22:33:30 +03:00
/**
* Resets the admin tab form back to its initial state.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.resetForm = function () {
$('#filter-providers .selected').removeClass('selected');
2016-04-26 22:33:30 +03:00
$('#filter-providers button').prop('disabled', false);
$('#filter-providers .results').css('color', '');
2016-04-26 22:33:30 +03:00
$('#providers .add-edit-delete-group').show();
$('#providers .save-cancel-group').hide();
$('#providers .record-details h3 a').remove();
$('#providers .record-details').find('input, select, textarea').val('').prop('disabled', true);
$('#providers .record-details #provider-calendar-view').val('default');
$('#providers .record-details #provider-timezone').val('UTC');
$('#providers .add-break, .add-working-plan-exception, #reset-working-plan').prop('disabled', true);
2016-04-26 22:33:30 +03:00
BackendUsers.wp.timepickers(true);
$('#providers .working-plan input:text').timepicker('destroy');
$('#providers .working-plan input:checkbox').prop('disabled', true);
2016-04-26 22:33:30 +03:00
$('.breaks').find('.edit-break, .delete-break').prop('disabled', true);
$('.working-plan-exceptions')
.find('.edit-working-plan-exception, .delete-working-plan-exception')
.prop('disabled', true);
2016-04-26 22:33:30 +03:00
$('#providers .record-details .is-invalid').removeClass('is-invalid');
$('#providers .record-details .form-message').hide();
2016-04-26 22:33:30 +03:00
$('#edit-provider, #delete-provider').prop('disabled', true);
$('#provider-services input:checkbox').prop('disabled', true).prop('checked', false);
$('#provider-services a').remove();
$('#providers .working-plan tbody').empty();
2016-04-26 22:33:30 +03:00
$('#providers .breaks tbody').empty();
$('#providers .working-plan-exceptions tbody').empty();
2016-04-26 22:33:30 +03:00
};
/**
2016-04-26 22:33:30 +03:00
* Display a provider record into the admin form.
*
* @param {Object} provider Contains the provider record data.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.display = function (provider) {
2016-04-26 22:33:30 +03:00
$('#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-timezone').val(provider.timezone);
2016-04-26 22:33:30 +03:00
$('#provider-username').val(provider.settings.username);
$('#provider-calendar-view').val(provider.settings.calendar_view);
$('#provider-notifications').prop('checked', Boolean(Number(provider.settings.notifications)));
// Add dedicated provider link.
2018-01-23 12:08:37 +03:00
var dedicatedUrl = GlobalVariables.baseUrl + '/index.php?provider=' + encodeURIComponent(provider.id);
var $link = $('<a/>', {
'href': dedicatedUrl,
'html': [
$('<span/>', {
'class': 'fas fa-link'
})
]
});
$('#providers .details-view h3').find('a').remove().end().append($link);
$('#provider-services a').remove();
$('#provider-services input:checkbox').prop('checked', false);
provider.services.forEach(function (providerServiceId) {
var $checkbox = $('#provider-services input[data-id="' + providerServiceId + '"]');
if (!$checkbox.length) {
return;
}
$checkbox.prop('checked', true);
// Add dedicated service-provider link.
dedicatedUrl =
GlobalVariables.baseUrl +
'/index.php?provider=' +
encodeURIComponent(provider.id) +
'&service=' +
encodeURIComponent(providerServiceId);
$link = $('<a/>', {
'href': dedicatedUrl,
'html': [
$('<span/>', {
'class': 'fas fa-link'
})
]
2016-04-26 22:33:30 +03:00
});
$checkbox.parent().append($link);
});
2016-04-26 22:33:30 +03:00
// Display working plan
var workingPlan = $.parseJSON(provider.settings.working_plan);
BackendUsers.wp.setup(workingPlan);
$('.working-plan').find('input').prop('disabled', true);
2016-04-26 22:33:30 +03:00
$('.breaks').find('.edit-break, .delete-break').prop('disabled', true);
$('#providers .working-plan-exceptions tbody').empty();
var workingPlanExceptions = $.parseJSON(provider.settings.working_plan_exceptions);
BackendUsers.wp.setupWorkingPlanExceptions(workingPlanExceptions);
$('.working-plan-exceptions')
.find('.edit-working-plan-exception, .delete-working-plan-exception')
.prop('disabled', true);
$('#providers .working-plan input:checkbox').prop('disabled', true);
Backend.placeFooterToBottom();
2016-04-26 22:33:30 +03:00
};
/**
2016-04-26 22:33:30 +03:00
* 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, if set, when the function is complete a result row can be set as selected.
* @param {bool} display Optional (false), if true the selected record will be also displayed.
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.filter = function (key, selectId, display) {
2016-04-26 22:33:30 +03:00
display = display || false;
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_providers';
var data = {
csrf_token: GlobalVariables.csrfToken,
key: key,
limit: this.filterLimit
2016-07-15 21:52:21 +03:00
};
2016-04-26 22:33:30 +03:00
$.post(url, data).done(
function (response) {
this.filterResults = response;
$('#filter-providers .results').empty();
response.forEach(
function (provider) {
$('#filter-providers .results').append(this.getFilterHtml(provider)).append($('<hr/>'));
}.bind(this)
);
if (!response.length) {
$('#filter-providers .results').append(
$('<em/>', {
'text': App.Lang.no_records_found
})
);
} else if (response.length === this.filterLimit) {
$('<button/>', {
'type': 'button',
'class': 'btn btn-outline-secondary w-100 load-more text-center',
'text': App.Lang.load_more,
'click': function () {
this.filterLimit += 20;
this.filter(key, selectId, display);
}.bind(this)
}).appendTo('#filter-providers .results');
}
if (selectId) {
this.select(selectId, display);
}
}.bind(this)
);
2016-04-26 22:33:30 +03:00
};
2016-04-26 22:33:30 +03:00
/**
* 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.
*
* @return {String} The html code that represents the record on the filter results list.
2016-04-26 22:33:30 +03:00
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.getFilterHtml = function (provider) {
var name = provider.first_name + ' ' + provider.last_name;
var info = provider.email;
info = provider.mobile_number ? info + ', ' + provider.mobile_number : info;
info = provider.phone_number ? info + ', ' + provider.phone_number : info;
return $('<div/>', {
'class': 'provider-row entry',
'data-id': provider.id,
'html': [
$('<strong/>', {
'text': name
}),
$('<br/>'),
$('<span/>', {
'text': info
}),
$('<br/>')
]
});
2016-04-26 22:33:30 +03:00
};
2016-04-26 22:33:30 +03:00
/**
* Initialize the editable functionality to the break day table cells.
*
* @param {Object} $selector The cells to be initialized.
2016-04-26 22:33:30 +03:00
*/
ProvidersHelper.prototype.editableDayCell = function ($selector) {
2016-04-26 22:33:30 +03:00
var weekDays = {};
weekDays[App.Lang.monday] = 'Monday';
weekDays[App.Lang.tuesday] = 'Tuesday';
weekDays[App.Lang.wednesday] = 'Wednesday';
weekDays[App.Lang.thursday] = 'Thursday';
weekDays[App.Lang.friday] = 'Friday';
weekDays[App.Lang.saturday] = 'Saturday';
weekDays[App.Lang.sunday] = 'Sunday';
2016-04-26 22:33:30 +03:00
$selector.editable(
function (value, settings) {
return value;
2016-04-26 22:33:30 +03:00
},
{
type: 'select',
data: weekDays,
event: 'edit',
height: '30px',
submit: '<button type="button" class="d-none submit-editable">Submit</button>',
cancel: '<button type="button" class="d-none cancel-editable">Cancel</button>',
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
}
2016-04-26 22:33:30 +03:00
}
}
);
2015-05-28 00:42:40 +03:00
};
2016-04-26 22:33:30 +03:00
/**
* Initialize the editable functionality to the break time table cells.
*
* @param {jQuery} $selector The cells to be initialized.
2016-04-26 22:33:30 +03:00
*/
ProvidersHelper.prototype.editableTimeCell = function ($selector) {
$selector.editable(
function (value, settings) {
// Do not return the value because the user needs to press the "Save" button.
return value;
2016-04-26 22:33:30 +03:00
},
{
event: 'edit',
height: '25px',
submit: '<button type="button" class="d-none submit-editable">Submit</button>',
cancel: '<button type="button" class="d-none cancel-editable">Cancel</button>',
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
}
2016-04-26 22:33:30 +03:00
}
}
);
2016-04-26 22:33:30 +03:00
};
2016-04-26 22:33:30 +03:00
/**
* Select and display a providers filter result on the form.
*
* @param {Number} id Record id to be selected.
* @param {Boolean} display Optional (false), if true the record will be displayed on the form.
2016-04-26 22:33:30 +03:00
*/
2018-01-23 12:08:37 +03:00
ProvidersHelper.prototype.select = function (id, display) {
2016-04-26 22:33:30 +03:00
display = display || false;
2016-04-26 22:33:30 +03:00
// Select record in filter results.
$('#filter-providers .provider-row[data-id="' + id + '"]').addClass('selected');
2016-04-26 22:33:30 +03:00
// Display record in form (if display = true).
if (display) {
var provider = this.filterResults.find(
function (filterResult) {
return Number(filterResult.id) === Number(id);
}.bind(this)
);
this.display(provider);
$('#edit-provider, #delete-provider').prop('disabled', false);
}
2016-04-26 22:33:30 +03:00
};
2016-04-26 22:33:30 +03:00
window.ProvidersHelper = ProvidersHelper;
})();