Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlanningEventManager.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // @flow
  2. export type eventObject = {
  3. id: number,
  4. title: string,
  5. logo: string,
  6. date_begin: string,
  7. date_end: string,
  8. description: string,
  9. club: string,
  10. category_id: number,
  11. url: string,
  12. };
  13. export default class PlanningEventManager {
  14. // Regex used to check date string validity
  15. static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
  16. /**
  17. * Gets the current day string representation in the format
  18. * YYYY-MM-DD
  19. *
  20. * @return {string} The string representation
  21. */
  22. static getCurrentDateString(): string {
  23. return PlanningEventManager.dateToString(new Date(Date.now()));
  24. }
  25. /**
  26. * Checks if the given date is before the other.
  27. *
  28. * @param event1Date Event 1 date in format YYYY-MM-DD HH:MM
  29. * @param event2Date Event 2 date in format YYYY-MM-DD HH:MM
  30. * @return {boolean}
  31. */
  32. static isEventBefore(event1Date: string, event2Date: string): boolean {
  33. let date1 = PlanningEventManager.stringToDate(event1Date);
  34. let date2 = PlanningEventManager.stringToDate(event2Date);
  35. if (date1 !== null && date2 !== null)
  36. return date1 < date2;
  37. else
  38. return false;
  39. }
  40. /**
  41. * Gets only the date part of the given event date string in the format
  42. * YYYY-MM-DD HH:MM
  43. *
  44. * @param dateString The string to get the date from
  45. * @return {string|null} Date in format YYYY:MM:DD or null if given string is invalid
  46. */
  47. static getDateOnlyString(dateString: string): string | null {
  48. if (PlanningEventManager.isEventDateStringFormatValid(dateString))
  49. return dateString.split(" ")[0];
  50. else
  51. return null;
  52. }
  53. /**
  54. * Checks if the given date string is in the format
  55. * YYYY-MM-DD HH:MM
  56. *
  57. * @param dateString The string to check
  58. * @return {boolean}
  59. */
  60. static isEventDateStringFormatValid(dateString: ?string): boolean {
  61. return dateString !== undefined
  62. && dateString !== null
  63. && PlanningEventManager.dateRegExp.test(dateString);
  64. }
  65. /**
  66. * Converts the given date string to a date object.<br>
  67. * Accepted format: YYYY-MM-DD HH:MM
  68. *
  69. * @param dateString The string to convert
  70. * @return {Date|null} The date object or null if the given string is invalid
  71. */
  72. static stringToDate(dateString: string): Date | null {
  73. let date = new Date();
  74. if (PlanningEventManager.isEventDateStringFormatValid(dateString)) {
  75. let stringArray = dateString.split(' ');
  76. let dateArray = stringArray[0].split('-');
  77. let timeArray = stringArray[1].split(':');
  78. date.setFullYear(
  79. parseInt(dateArray[0]),
  80. parseInt(dateArray[1]) - 1, // Month range from 0 to 11
  81. parseInt(dateArray[2])
  82. );
  83. date.setHours(
  84. parseInt(timeArray[0]),
  85. parseInt(timeArray[1]),
  86. 0,
  87. 0,
  88. );
  89. } else
  90. date = null;
  91. return date;
  92. }
  93. /**
  94. * Converts a date object to a string in the format
  95. * YYYY-MM-DD HH-MM-SS
  96. *
  97. * @param date The date object to convert
  98. * @return {string} The converted string
  99. */
  100. static dateToString(date: Date): string {
  101. const day = String(date.getDate()).padStart(2, '0');
  102. const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
  103. const year = date.getFullYear();
  104. const hours = String(date.getHours()).padStart(2, '0');
  105. const minutes = String(date.getMinutes()).padStart(2, '0');
  106. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
  107. }
  108. /**
  109. * Returns a string corresponding to the event start and end times in the following format:
  110. *
  111. * HH:MM - HH:MM
  112. *
  113. * If the end date is not specified or is equal to start time, only start time will be shown.
  114. *
  115. * If the end date is not on the same day, 23:59 will be shown as end time
  116. *
  117. * @param start Start time in YYYY-MM-DD HH:MM:SS format
  118. * @param end End time in YYYY-MM-DD HH:MM:SS format
  119. * @return {string} Formatted string or "/ - /" on error
  120. */
  121. static getFormattedEventTime(start: string, end: string): string {
  122. let formattedStr = '/ - /';
  123. let startDate = PlanningEventManager.stringToDate(start);
  124. let endDate = PlanningEventManager.stringToDate(end);
  125. if (startDate !== null && endDate !== null && startDate.getTime() !== endDate.getTime()) {
  126. formattedStr = String(startDate.getHours()).padStart(2, '0') + ':'
  127. + String(startDate.getMinutes()).padStart(2, '0') + ' - ';
  128. if (endDate.getFullYear() > startDate.getFullYear()
  129. || endDate.getMonth() > startDate.getMonth()
  130. || endDate.getDate() > startDate.getDate())
  131. formattedStr += '23:59';
  132. else
  133. formattedStr += String(endDate.getHours()).padStart(2, '0') + ':'
  134. + String(endDate.getMinutes()).padStart(2, '0');
  135. } else if (startDate !== null)
  136. formattedStr =
  137. String(startDate.getHours()).padStart(2, '0') + ':'
  138. + String(startDate.getMinutes()).padStart(2, '0');
  139. return formattedStr
  140. }
  141. /**
  142. * Checks if the given description can be considered empty.
  143. * <br>
  144. * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
  145. *
  146. *
  147. * @param description The text to check
  148. * @return {boolean}
  149. */
  150. static isDescriptionEmpty(description: ?string): boolean {
  151. if (description !== undefined && description !== null) {
  152. return description
  153. .split('<p>').join('') // Equivalent to a replace all
  154. .split('</p>').join('')
  155. .split('<br>').join('').trim() === '';
  156. } else
  157. return true;
  158. }
  159. /**
  160. * Generates an object with an empty array for each key.
  161. * Each key is a date string in the format
  162. * YYYY-MM-DD
  163. *
  164. * @param numberOfMonths The number of months to create, starting from the current date
  165. * @return {Object}
  166. */
  167. static generateEmptyCalendar(numberOfMonths: number): Object {
  168. let end = new Date(Date.now());
  169. end.setMonth(end.getMonth() + numberOfMonths);
  170. let daysOfYear = {};
  171. for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) {
  172. const dateString = PlanningEventManager.getDateOnlyString(
  173. PlanningEventManager.dateToString(new Date(d)));
  174. if (dateString !== null)
  175. daysOfYear[dateString] = []
  176. }
  177. return daysOfYear;
  178. }
  179. /**
  180. * Generates an object with an array of eventObject at each key.
  181. * Each key is a date string in the format
  182. * YYYY-MM-DD.
  183. *
  184. * If no event is available at the given key, the array will be empty
  185. *
  186. * @param eventList The list of events to map to the agenda
  187. * @param numberOfMonths The number of months to create the agenda for
  188. * @return {Object}
  189. */
  190. static generateEventAgenda(eventList: Array<eventObject>, numberOfMonths: number): Object {
  191. let agendaItems = PlanningEventManager.generateEmptyCalendar(numberOfMonths);
  192. for (let i = 0; i < eventList.length; i++) {
  193. const dateString = PlanningEventManager.getDateOnlyString(eventList[i].date_begin);
  194. if (dateString !== null) {
  195. const eventArray = agendaItems[dateString];
  196. if (eventArray !== undefined)
  197. this.pushEventInOrder(eventArray, eventList[i]);
  198. }
  199. }
  200. return agendaItems;
  201. }
  202. /**
  203. * Adds events to the given array depending on their starting date.
  204. *
  205. * Events starting before are added at the front.
  206. *
  207. * @param eventArray The array to hold sorted events
  208. * @param event The event to add to the array
  209. */
  210. static pushEventInOrder(eventArray: Array<eventObject>, event: eventObject): Object {
  211. if (eventArray.length === 0)
  212. eventArray.push(event);
  213. else {
  214. for (let i = 0; i < eventArray.length; i++) {
  215. if (PlanningEventManager.isEventBefore(event.date_begin, eventArray[i].date_begin)) {
  216. eventArray.splice(i, 0, event);
  217. break;
  218. } else if (i === eventArray.length - 1) {
  219. eventArray.push(event);
  220. break;
  221. }
  222. }
  223. }
  224. }
  225. }