/* ---------------------------------------------------------------------------- * Easy!Appointments - Open Source Web Scheduler * * @package EasyAppointments * @author A.Tselegidis * @copyright Copyright (c) 2013 - 2020, Alex Tselegidis * @license http://opensource.org/licenses/GPL-3.0 - GPLv3 * @link http://easyappointments.org * @since v1.2.0 * ---------------------------------------------------------------------------- */ /** * Backend Calendar * * This module implements the default calendar view of backend. * * @module BackendCalendarDefaultView */ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; (function (exports) { 'use strict'; // Constants var FILTER_TYPE_PROVIDER = 'provider'; var FILTER_TYPE_SERVICE = 'service'; // Variables var lastFocusedEventData; // Contains event data for later use. /** * Bind event handlers for the calendar view. */ function bindEventHandlers() { var $calendarPage = $('#calendar-page'); /** * Event: Reload Button "Click" * * When the user clicks the reload button an the calendar items need to be refreshed. */ $('#reload-appointments').on('click', function () { var calendarView = $('#calendar').fullCalendar('getView'); refreshCalendarAppointments( $('#calendar'), $('#select-filter-item').val(), $('#select-filter-item').find('option:selected').attr('type'), calendarView.start, calendarView.end ); }); /** * Event: Popover Close Button "Click" * * Hides the open popover element. */ $calendarPage.on('click', '.close-popover', function () { $(this).parents('.popover').popover('dispose'); }); /** * Event: Popover Edit Button "Click" * * Enables the edit dialog of the selected calendar event. */ $calendarPage.on('click', '.edit-popover', function () { $(this).parents('.popover').popover('dispose'); var $dialog; var startDatetime; var endDatetime; if (lastFocusedEventData.data.workingPlanException) { var date = lastFocusedEventData.data.date; var workingPlanException = lastFocusedEventData.data.workingPlanException; var provider = lastFocusedEventData.data.provider; WorkingPlanExceptionsModal.edit(date, workingPlanException).done(function (date, workingPlanException) { var successCallback = function () { Backend.displayNotification(EALang.working_plan_exception_saved); var workingPlanExceptions = JSON.parse(provider.settings.working_plan_exceptions) || {}; workingPlanExceptions[date] = workingPlanException; for (var index in GlobalVariables.availableProviders) { var availableProvider = GlobalVariables.availableProviders[index]; if (Number(availableProvider.id) === Number(provider.id)) { availableProvider.settings.working_plan_exceptions = JSON.stringify(workingPlanExceptions); break; } } $('#select-filter-item').trigger('change'); // Update the calendar. }; BackendCalendarApi.saveWorkingPlanException( date, workingPlanException, provider.id, successCallback, null ); }); } else if (lastFocusedEventData.data.is_unavailable === '0') { var appointment = lastFocusedEventData.data; $dialog = $('#manage-appointment'); BackendCalendarAppointmentsModal.resetAppointmentDialog(); // Apply appointment data and show modal dialog. $dialog.find('.modal-header h3').text(EALang.edit_appointment_title); $dialog.find('#appointment-id').val(appointment.id); $dialog.find('#select-service').val(appointment.id_services).trigger('change'); $dialog.find('#select-provider').val(appointment.id_users_provider); // Set the start and end datetime of the appointment. startDatetime = Date.parseExact(appointment.start_datetime, 'yyyy-MM-dd HH:mm:ss'); $dialog.find('#start-datetime').datetimepicker('setDate', startDatetime); endDatetime = Date.parseExact(appointment.end_datetime, 'yyyy-MM-dd HH:mm:ss'); $dialog.find('#end-datetime').datetimepicker('setDate', endDatetime); var customer = appointment.customer; $dialog.find('#customer-id').val(appointment.id_users_customer); $dialog.find('#first-name').val(customer.first_name); $dialog.find('#last-name').val(customer.last_name); $dialog.find('#email').val(customer.email); $dialog.find('#phone-number').val(customer.phone_number); $dialog.find('#address').val(customer.address); $dialog.find('#city').val(customer.city); $dialog.find('#zip-code').val(customer.zip_code); $dialog.find('#appointment-location').val(appointment.location); $dialog.find('#appointment-notes').val(appointment.notes); $dialog.find('#customer-notes').val(customer.notes); $dialog.modal('show'); } else { var unavailable = lastFocusedEventData.data; // Replace string date values with actual date objects. unavailable.start_datetime = lastFocusedEventData.start.format('YYYY-MM-DD HH:mm:ss'); startDatetime = Date.parseExact(unavailable.start_datetime, 'yyyy-MM-dd HH:mm:ss'); unavailable.end_datetime = lastFocusedEventData.end.format('YYYY-MM-DD HH:mm:ss'); endDatetime = Date.parseExact(unavailable.end_datetime, 'yyyy-MM-dd HH:mm:ss'); $dialog = $('#manage-unavailable'); BackendCalendarUnavailabilityEventsModal.resetUnavailableDialog(); // Apply unavailable data to dialog. $dialog.find('.modal-header h3').text('Edit Unavailable Period'); $dialog.find('#unavailable-start').datetimepicker('setDate', startDatetime); $dialog.find('#unavailable-id').val(unavailable.id); $dialog.find('#unavailable-provider').val(unavailable.id_users_provider); $dialog.find('#unavailable-end').datetimepicker('setDate', endDatetime); $dialog.find('#unavailable-notes').val(unavailable.notes); $dialog.modal('show'); } }); /** * Event: Popover Delete Button "Click" * * Displays a prompt on whether the user wants the appointment to be deleted. If he confirms the * deletion then an AJAX call is made to the server and deletes the appointment from the database. */ $calendarPage.on('click', '.delete-popover', function () { $(this).parents('.popover').popover('dispose'); var url; var data; if (lastFocusedEventData.data.workingPlanException) { var providerId = $('#select-filter-item').val(); var provider = GlobalVariables.availableProviders.find(function (availableProvider) { return Number(availableProvider.id) === Number(providerId); }); if (!provider) { throw new Error('Provider could not be found: ' + providerId); } var successCallback = function () { Backend.displayNotification(EALang.working_plan_exception_deleted); var workingPlanExceptions = JSON.parse(provider.settings.working_plan_exceptions) || {}; delete workingPlanExceptions[date]; for (var index in GlobalVariables.availableProviders) { var availableProvider = GlobalVariables.availableProviders[index]; if (Number(availableProvider.id) === Number(providerId)) { availableProvider.settings.working_plan_exceptions = JSON.stringify(workingPlanExceptions); break; } } $('#select-filter-item').trigger('change'); // Update the calendar. }; var date = lastFocusedEventData.start.format('YYYY-MM-DD'); BackendCalendarApi.deleteWorkingPlanException(date, providerId, successCallback); } else if (lastFocusedEventData.data.is_unavailable === '0') { var buttons = [ { text: EALang.cancel, click: function () { $('#message-box').dialog('close'); } }, { text: 'OK', click: function () { url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_delete_appointment'; data = { csrfToken: GlobalVariables.csrfToken, appointment_id: lastFocusedEventData.data.id, delete_reason: $('#delete-reason').val() }; $.post(url, data).done(function () { $('#message-box').dialog('close'); // Refresh calendar event items. $('#select-filter-item').trigger('change'); }); } } ]; GeneralFunctions.displayMessageBox( EALang.delete_appointment_title, EALang.write_appointment_removal_reason, buttons ); $('