2023-11-03 20:25:23 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Online Appointment Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
|
|
|
* @copyright Copyright (c) Alex Tselegidis
|
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link https://easyappointments.org
|
|
|
|
* @since v1.5.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocked-periods page.
|
|
|
|
*
|
|
|
|
* This module implements the functionality of the blocked-periods page.
|
|
|
|
*/
|
|
|
|
App.Pages.BlockedPeriods = (function () {
|
|
|
|
const $blockedPeriods = $('#blocked-periods');
|
|
|
|
const $filterBlockedPeriods = $('#filter-blocked-periods');
|
|
|
|
const $id = $('#id');
|
|
|
|
const $name = $('#name');
|
2023-11-17 10:04:27 +03:00
|
|
|
const $startDateTime = $('#start-date-time');
|
|
|
|
const $endDateTime = $('#end-date-time');
|
2023-11-17 11:37:38 +03:00
|
|
|
const $notes = $('#notes');
|
2023-11-17 10:04:27 +03:00
|
|
|
const moment = window.moment;
|
|
|
|
|
2023-11-03 20:25:23 +03:00
|
|
|
let filterResults = {};
|
|
|
|
let filterLimit = 20;
|
2024-04-15 15:40:03 +03:00
|
|
|
let backupStartDateTimeObject = undefined;
|
2023-11-03 20:25:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the page event listeners.
|
|
|
|
*/
|
|
|
|
function addEventListeners() {
|
|
|
|
/**
|
|
|
|
* Event: Filter Blocked Periods Form "Submit"
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('submit', '#filter-blocked-periods form', (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
const key = $('#filter-blocked-periods .key').val();
|
|
|
|
$('.selected').removeClass('selected');
|
|
|
|
resetForm();
|
|
|
|
filter(key);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Filter Blocked-Periods Row "Click"
|
|
|
|
*
|
|
|
|
* Displays the selected row data on the right side of the page.
|
|
|
|
*
|
|
|
|
* @param {jQuery.Event} event
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('click', '.blocked-period-row', (event) => {
|
|
|
|
if ($('#filter-blocked-periods .filter').prop('disabled')) {
|
|
|
|
$('#filter-blocked-periods .results').css('color', '#AAA');
|
|
|
|
return; // exit because we are on edit mode
|
|
|
|
}
|
|
|
|
|
|
|
|
const blockedPeriodId = $(event.currentTarget).attr('data-id');
|
|
|
|
|
2023-12-22 13:35:41 +03:00
|
|
|
const blockedPeriod = filterResults.find(
|
|
|
|
(filterResult) => Number(filterResult.id) === Number(blockedPeriodId),
|
|
|
|
);
|
2023-11-03 20:25:23 +03:00
|
|
|
|
|
|
|
display(blockedPeriod);
|
|
|
|
$('#filter-blocked-periods .selected').removeClass('selected');
|
|
|
|
$(event.currentTarget).addClass('selected');
|
|
|
|
$('#edit-blocked-period, #delete-blocked-period').prop('disabled', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Add Blocked-Period Button "Click"
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('click', '#add-blocked-period', () => {
|
|
|
|
resetForm();
|
|
|
|
$blockedPeriods.find('.add-edit-delete-group').hide();
|
|
|
|
$blockedPeriods.find('.save-cancel-group').show();
|
|
|
|
$blockedPeriods.find('.record-details').find('input, select, textarea').prop('disabled', false);
|
|
|
|
$blockedPeriods.find('.record-details .form-label span').prop('hidden', false);
|
|
|
|
$filterBlockedPeriods.find('button').prop('disabled', true);
|
|
|
|
$filterBlockedPeriods.find('.results').css('color', '#AAA');
|
2024-04-15 15:09:55 +03:00
|
|
|
|
|
|
|
App.Utils.UI.setDateTimePickerValue($startDateTime, moment('00:00', 'HH:mm').toDate());
|
|
|
|
App.Utils.UI.setDateTimePickerValue($endDateTime, moment('00:00', 'HH:mm').add(1, 'day').toDate());
|
2023-11-03 20:25:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Edit Blocked-Period Button "Click"
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('click', '#edit-blocked-period', () => {
|
|
|
|
$blockedPeriods.find('.add-edit-delete-group').hide();
|
|
|
|
$blockedPeriods.find('.save-cancel-group').show();
|
|
|
|
$blockedPeriods.find('.record-details').find('input, select, textarea').prop('disabled', false);
|
|
|
|
$blockedPeriods.find('.record-details .form-label span').prop('hidden', false);
|
|
|
|
$filterBlockedPeriods.find('button').prop('disabled', true);
|
|
|
|
$filterBlockedPeriods.find('.results').css('color', '#AAA');
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Delete Blocked-Period Button "Click"
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('click', '#delete-blocked-period', () => {
|
|
|
|
const blockedPeriodId = $id.val();
|
|
|
|
|
|
|
|
const buttons = [
|
|
|
|
{
|
|
|
|
text: lang('cancel'),
|
|
|
|
click: (event, messageModal) => {
|
|
|
|
messageModal.dispose();
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
2023-11-03 20:25:23 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text: lang('delete'),
|
|
|
|
click: (event, messageModal) => {
|
|
|
|
remove(blockedPeriodId);
|
|
|
|
messageModal.dispose();
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
|
|
|
},
|
2023-11-03 20:25:23 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
App.Utils.Message.show(lang('delete_blocked_period'), lang('delete_record_prompt'), buttons);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Blocked period Save Button "Click"
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('click', '#save-blocked-period', () => {
|
2024-01-06 12:46:57 +03:00
|
|
|
const startDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDateTime);
|
2023-11-17 10:04:27 +03:00
|
|
|
const startDateTimeMoment = moment(startDateTimeObject);
|
2024-01-06 12:46:57 +03:00
|
|
|
const endDateTimeObject = App.Utils.UI.getDateTimePickerValue($endDateTime);
|
2023-11-17 10:04:27 +03:00
|
|
|
const endDateTimeMoment = moment(endDateTimeObject);
|
|
|
|
|
2023-11-03 20:25:23 +03:00
|
|
|
const blockedPeriod = {
|
|
|
|
name: $name.val(),
|
2023-11-17 10:04:27 +03:00
|
|
|
start_datetime: startDateTimeMoment.format('YYYY-MM-DD HH:mm:ss'),
|
|
|
|
end_datetime: endDateTimeMoment.format('YYYY-MM-DD HH:mm:ss'),
|
2023-12-22 13:35:41 +03:00
|
|
|
notes: $notes.val(),
|
2023-11-03 20:25:23 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($id.val() !== '') {
|
|
|
|
blockedPeriod.id = $id.val();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validate()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
save(blockedPeriod);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Cancel Blocked-Period Button "Click"
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('click', '#cancel-blocked-period', () => {
|
|
|
|
const id = $id.val();
|
|
|
|
resetForm();
|
|
|
|
if (id !== '') {
|
|
|
|
select(id, true);
|
|
|
|
}
|
|
|
|
});
|
2024-04-15 15:40:03 +03:00
|
|
|
|
|
|
|
$blockedPeriods.on('focus', '#start-date-time', () => {
|
|
|
|
backupStartDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDateTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event: Start Date Time Input "Change"
|
|
|
|
*/
|
|
|
|
$blockedPeriods.on('change', '#start-date-time', (event) => {
|
|
|
|
const endDateTimeObject = App.Utils.UI.getDateTimePickerValue($endDateTime);
|
|
|
|
|
|
|
|
if (!backupStartDateTimeObject || !endDateTimeObject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const endDateTimeMoment = moment(endDateTimeObject);
|
|
|
|
const backupStartDateTimeMoment = moment(backupStartDateTimeObject);
|
|
|
|
const diff = endDateTimeMoment.diff(backupStartDateTimeMoment);
|
|
|
|
const newEndDateTimeMoment = endDateTimeMoment.clone().add(diff, 'milliseconds');
|
|
|
|
App.Utils.UI.setDateTimePickerValue($endDateTime, newEndDateTimeMoment.toDate());
|
|
|
|
});
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter blocked periods records.
|
|
|
|
*
|
|
|
|
* @param {String} keyword This key string is used to filter the blocked-period records.
|
2023-11-17 10:04:27 +03:00
|
|
|
* @param {Number} selectId Optional, if set then after the filter operation the record with the given ID will be
|
2023-11-03 20:25:23 +03:00
|
|
|
* selected (but not displayed).
|
|
|
|
* @param {Boolean} show Optional (false), if true then the selected record will be displayed on the form.
|
|
|
|
*/
|
|
|
|
function filter(keyword, selectId = null, show = false) {
|
|
|
|
App.Http.BlockedPeriods.search(keyword, filterLimit).then((response) => {
|
|
|
|
filterResults = response;
|
|
|
|
|
|
|
|
$('#filter-blocked-periods .results').empty();
|
|
|
|
|
|
|
|
response.forEach((blockedPeriod) => {
|
|
|
|
$('#filter-blocked-periods .results').append(getFilterHtml(blockedPeriod)).append($('<hr/>'));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (response.length === 0) {
|
|
|
|
$('#filter-blocked-periods .results').append(
|
|
|
|
$('<em/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': lang('no_records_found'),
|
|
|
|
}),
|
2023-11-03 20:25:23 +03:00
|
|
|
);
|
|
|
|
} else if (response.length === filterLimit) {
|
|
|
|
$('<button/>', {
|
|
|
|
'type': 'button',
|
|
|
|
'class': 'btn btn-outline-secondary w-100 load-more text-center',
|
|
|
|
'text': lang('load_more'),
|
|
|
|
'click': () => {
|
|
|
|
filterLimit += 20;
|
|
|
|
filter(keyword, selectId, show);
|
2023-12-22 13:35:41 +03:00
|
|
|
},
|
2023-11-03 20:25:23 +03:00
|
|
|
}).appendTo('#filter-blocked-periods .results');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectId) {
|
|
|
|
select(selectId, show);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a blocked-period record to the database (via AJAX post).
|
|
|
|
*
|
|
|
|
* @param {Object} blockedPeriod Contains the blocked-period data.
|
|
|
|
*/
|
|
|
|
function save(blockedPeriod) {
|
|
|
|
App.Http.BlockedPeriods.save(blockedPeriod).then((response) => {
|
|
|
|
App.Layouts.Backend.displayNotification(lang('blocked_period_saved'));
|
|
|
|
resetForm();
|
|
|
|
$filterBlockedPeriods.find('.key').val('');
|
|
|
|
filter('', response.id, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete blocked-period record.
|
|
|
|
*
|
|
|
|
* @param {Number} id Record ID to be deleted.
|
|
|
|
*/
|
|
|
|
function remove(id) {
|
|
|
|
App.Http.BlockedPeriods.destroy(id).then(() => {
|
|
|
|
App.Layouts.Backend.displayNotification(lang('blocked_period_deleted'));
|
|
|
|
resetForm();
|
|
|
|
filter($('#filter-blocked-periods .key').val());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a blocked-period record on the form.
|
|
|
|
*
|
|
|
|
* @param {Object} blockedPeriod Contains the blocked-period data.
|
|
|
|
*/
|
|
|
|
function display(blockedPeriod) {
|
|
|
|
$id.val(blockedPeriod.id);
|
|
|
|
$name.val(blockedPeriod.name);
|
2024-01-06 12:46:57 +03:00
|
|
|
App.Utils.UI.setDateTimePickerValue($startDateTime, new Date(blockedPeriod.start_datetime));
|
|
|
|
App.Utils.UI.setDateTimePickerValue($endDateTime, new Date(blockedPeriod.end_datetime));
|
2023-11-17 11:37:38 +03:00
|
|
|
$notes.val(blockedPeriod.notes);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate blocked-period data before save (insert or update).
|
|
|
|
*
|
|
|
|
* @return {Boolean} Returns the validation result.
|
|
|
|
*/
|
|
|
|
function validate() {
|
|
|
|
$blockedPeriods.find('.is-invalid').removeClass('is-invalid');
|
|
|
|
$blockedPeriods.find('.form-message').removeClass('alert-danger').hide();
|
|
|
|
|
|
|
|
try {
|
|
|
|
let missingRequired = false;
|
|
|
|
|
|
|
|
$blockedPeriods.find('.required').each((index, fieldEl) => {
|
|
|
|
if (!$(fieldEl).val()) {
|
|
|
|
$(fieldEl).addClass('is-invalid');
|
|
|
|
missingRequired = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missingRequired) {
|
|
|
|
throw new Error(lang('fields_are_required'));
|
|
|
|
}
|
|
|
|
|
2024-01-06 12:46:57 +03:00
|
|
|
const startDateTimeObject = App.Utils.UI.getDateTimePickerValue($startDateTime);
|
|
|
|
const endDateTimeObject = App.Utils.UI.getDateTimePickerValue($endDateTime);
|
2023-11-17 10:10:03 +03:00
|
|
|
|
|
|
|
if (startDateTimeObject >= endDateTimeObject) {
|
|
|
|
$startDateTime.addClass('is-invalid');
|
|
|
|
$endDateTime.addClass('is-invalid');
|
|
|
|
throw new Error(lang('start_date_before_end_error'));
|
|
|
|
}
|
|
|
|
|
2023-11-03 20:25:23 +03:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
$blockedPeriods.find('.form-message').addClass('alert-danger').text(error.message).show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bring the blocked-period form back to its initial state.
|
|
|
|
*/
|
|
|
|
function resetForm() {
|
|
|
|
$filterBlockedPeriods.find('.selected').removeClass('selected');
|
|
|
|
$filterBlockedPeriods.find('button').prop('disabled', false);
|
|
|
|
$filterBlockedPeriods.find('.results').css('color', '');
|
|
|
|
|
|
|
|
$blockedPeriods.find('.add-edit-delete-group').show();
|
|
|
|
$blockedPeriods.find('.save-cancel-group').hide();
|
|
|
|
$blockedPeriods.find('.record-details').find('input, select, textarea').val('').prop('disabled', true);
|
|
|
|
$blockedPeriods.find('.record-details .form-label span').prop('hidden', true);
|
|
|
|
$('#edit-blocked-period, #delete-blocked-period').prop('disabled', true);
|
|
|
|
|
|
|
|
$blockedPeriods.find('.record-details .is-invalid').removeClass('is-invalid');
|
|
|
|
$blockedPeriods.find('.record-details .form-message').hide();
|
2024-04-15 15:40:03 +03:00
|
|
|
|
|
|
|
backupStartDateTimeObject = undefined;
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the filter results row HTML code.
|
|
|
|
*
|
|
|
|
* @param {Object} blockedPeriod Contains the blocked-period data.
|
|
|
|
*
|
|
|
|
* @return {String} Returns the record HTML code.
|
|
|
|
*/
|
|
|
|
function getFilterHtml(blockedPeriod) {
|
|
|
|
return $('<div/>', {
|
|
|
|
'class': 'blocked-period-row entry',
|
|
|
|
'data-id': blockedPeriod.id,
|
|
|
|
'html': [
|
|
|
|
$('<strong/>', {
|
2023-12-22 13:35:41 +03:00
|
|
|
'text': blockedPeriod.name,
|
2023-11-03 20:25:23 +03:00
|
|
|
}),
|
2023-12-22 13:35:41 +03:00
|
|
|
$('<br/>'),
|
|
|
|
],
|
2023-11-03 20:25:23 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a specific record from the current filter results.
|
|
|
|
*
|
|
|
|
* If the blocked-period ID does not exist in the list then no record will be selected.
|
|
|
|
*
|
|
|
|
* @param {Number} id The record ID to be selected from the filter results.
|
|
|
|
* @param {Boolean} show Optional (false), if true then the method will display the record on the form.
|
|
|
|
*/
|
|
|
|
function select(id, show = false) {
|
|
|
|
$filterBlockedPeriods.find('.selected').removeClass('selected');
|
|
|
|
|
|
|
|
$filterBlockedPeriods.find('.blocked-period-row[data-id="' + id + '"]').addClass('selected');
|
|
|
|
|
|
|
|
if (show) {
|
|
|
|
const blockedPeriod = filterResults.find((blockedPeriod) => Number(blockedPeriod.id) === Number(id));
|
|
|
|
|
|
|
|
display(blockedPeriod);
|
|
|
|
|
|
|
|
$('#edit-blocked-period, #delete-blocked-period').prop('disabled', false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the module.
|
|
|
|
*/
|
|
|
|
function initialize() {
|
|
|
|
resetForm();
|
|
|
|
filter('');
|
|
|
|
addEventListeners();
|
2024-01-06 12:46:57 +03:00
|
|
|
App.Utils.UI.initializeDateTimePicker($startDateTime);
|
|
|
|
App.Utils.UI.initializeDateTimePicker($endDateTime);
|
2023-11-03 20:25:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', initialize);
|
|
|
|
|
|
|
|
return {
|
|
|
|
filter,
|
|
|
|
save,
|
|
|
|
remove,
|
|
|
|
getFilterHtml,
|
|
|
|
resetForm,
|
2023-12-22 13:35:41 +03:00
|
|
|
select,
|
2023-11-03 20:25:23 +03:00
|
|
|
};
|
|
|
|
})();
|