From 8ca935c412fb7e845ad5ec2c0c6ec0026e04b4cb Mon Sep 17 00:00:00 2001 From: oxteam Date: Mon, 25 Jun 2018 23:53:12 +0200 Subject: [PATCH] Fix display of breaks in the backend for working plans stored in database with Monday as the first weekday (EA! Issue #506). Prior commit 00372f2f1af96df0d4545053ae6f27834e109ea6, the first weekday was Monday. After this commit, it is set to Sunday and the display of breaks in the backend calendar is broken for former working plans. The fix consists in reordering the working plan elements with Sunday as the first day prior displaying the backend calendar. In addition, the working plan elements are also reordered when displaying 'Settings/Business Logic' and 'Users/Providers' tabs. This is to ensure that breaks are always displayed with Sunday first, like done for the Working Plan in those tabs, for consistency purpose.. --- .../js/backend_calendar_default_view.js | 20 +++--- src/assets/js/general_functions.js | 67 +++++++++++++++++++ src/assets/js/working_plan.js | 5 +- 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/assets/js/backend_calendar_default_view.js b/src/assets/js/backend_calendar_default_view.js index 4a76f346..729f78da 100755 --- a/src/assets/js/backend_calendar_default_view.js +++ b/src/assets/js/backend_calendar_default_view.js @@ -855,13 +855,13 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; $calendar.fullCalendar('addEventSource', calendarEvents); var weekDays = [ - 'sunday', - 'monday', - 'tuesday', - 'wednesday', - 'thursday', - 'friday', - 'saturday' + 'sunday', + 'monday', + 'tuesday', + 'wednesday', + 'thursday', + 'friday', + 'saturday' ]; // :: ADD PROVIDER'S UNAVAILABLE TIME PERIODS @@ -870,10 +870,14 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; if (filterType === FILTER_TYPE_PROVIDER && calendarView !== 'month') { $.each(GlobalVariables.availableProviders, function (index, provider) { if (provider.id == recordId) { - var workingPlan = jQuery.parseJSON(provider.settings.working_plan); + var workingPlan={}; + var workingPlanBulk = jQuery.parseJSON(provider.settings.working_plan); var extraWorkingPlan = jQuery.parseJSON(provider.settings.extra_working_plan); var unavailablePeriod; + // Sort the working plan starting with the first day as set in General settings to correctly align breaks in the calendar display + workingPlan = GeneralFunctions.sortWeekDict(workingPlanBulk,0); // 0 is the ID for Sunday + switch (calendarView) { case 'agendaDay': var selectedDayName = weekDays[$calendar.fullCalendar('getView').start.format('d')]; diff --git a/src/assets/js/general_functions.js b/src/assets/js/general_functions.js index eb3f8a5c..6ba97d0d 100755 --- a/src/assets/js/general_functions.js +++ b/src/assets/js/general_functions.js @@ -431,6 +431,73 @@ window.GeneralFunctions = window.GeneralFunctions || {}; return result; }; + /** + * Get the name in lowercase of a Weekday using its Id. + * + * @param {Integer} weekDayId The Id (From 0 for sunday to 6 for saturday). + + * @return {String} Returns the name of the weekday. + */ + exports.getWeekDayName = function (weekDayId) { + var result; + + switch (weekDayId) { + + case 0: + result = 'sunday'; + break; + + case 1: + result = 'monday'; + break; + + case 2: + result = 'tuesday'; + break; + + case 3: + result = 'wednesday'; + break; + + case 4: + result = 'thursday'; + break; + + case 5: + result = 'friday'; + break; + + case 6: + result = 'saturday'; + break; + + default: + throw new Error('Invalid weekday Id provided!', weekDayId); + } + + return result; + }; + + /** + * Sort a dictionary where keys are weekdays + * + * @param {Object} weekDict A dictionnary with weekdays as keys. + * @param {Integer} startDayId Id of the first day to start sorting (From 0 for sunday to 6 for saturday). + + * @return {Object} Returns a sorted dictionary + */ + exports.sortWeekDict = function (weekDict, startDayId) { + var sortedWeekDict={}; + + for (var i = startDayId; i < startDayId+7; i++) + { + var weekDayname = GeneralFunctions.getWeekDayName(i%7); + sortedWeekDict[weekDayname] = weekDict[weekDayname]; + } + + return sortedWeekDict; + }; + /** * Render a map icon that links to Google maps. * diff --git a/src/assets/js/working_plan.js b/src/assets/js/working_plan.js index 8982619d..2560b86a 100755 --- a/src/assets/js/working_plan.js +++ b/src/assets/js/working_plan.js @@ -45,7 +45,10 @@ * @param {Object} workingPlan Contains the working hours and breaks for each day of the week. */ WorkingPlan.prototype.setup = function (workingPlan) { - $.each(workingPlan, function (index, workingDay) { + // Always displaying breaks with Sunday as the first day to be consistent with what is done in the the working plan view. + var workingPlanSorted = GeneralFunctions.sortWeekDict(workingPlan,0); // 0 is the ID for Sunday + + $.each(workingPlanSorted, function (index, workingDay) { if (workingDay != null) { $('#' + index).prop('checked', true); $('#' + index + '-start').val(Date.parse(workingDay.start).toString(GlobalVariables.timeFormat === 'regular' ? 'h:mm tt' : 'HH:mm').toUpperCase());