Browse Source

Fixed Calendar does not show recurring events (#1104)

causefx 7 years ago
parent
commit
804e6af8cd
1 changed files with 31 additions and 6 deletions
  1. 31 6
      api/functions/homepage-connect-functions.php

+ 31 - 6
api/functions/homepage-connect-functions.php

@@ -1221,10 +1221,17 @@ function getCalendar()
 				$timeZone = isset($icsEvents [1] ['X-WR-TIMEZONE']) ? trim($icsEvents[1]['X-WR-TIMEZONE']) : date_default_timezone_get();
 				unset($icsEvents [1]);
 				foreach ($icsEvents as $icsEvent) {
-					if ((isset($icsEvent['DTSTART']) || isset($icsEvent['DTSTART;VALUE=DATE'])) && (isset($icsEvent['DTEND']) || isset($icsEvent['DTEND;VALUE=DATE'])) && isset($icsEvent['SUMMARY'])) {
+					$startKeys = array_filter_key($icsEvent, function ($key) {
+						return strpos($key, 'DTSTART') === 0;
+					});
+					$endKeys = array_filter_key($icsEvent, function ($key) {
+						return strpos($key, 'DTEND') === 0;
+					});
+					if (!empty($startKeys) && !empty($endKeys) && isset($icsEvent['SUMMARY'])) {
 						/* Getting start date and time */
 						$repeat = isset($icsEvent ['RRULE']) ? $icsEvent ['RRULE'] : false;
-						$start = isset($icsEvent ['DTSTART;VALUE=DATE']) ? $icsEvent ['DTSTART;VALUE=DATE'] : $icsEvent ['DTSTART'];
+						$start = reset($startKeys);
+						$end = reset($endKeys);
 						$totalDays = $GLOBALS['calendarStart'] + $GLOBALS['calendarEnd'];
 						if ($repeat) {
 							switch (trim(strtolower(getCalenderRepeat($repeat)))) {
@@ -1251,24 +1258,28 @@ function getCalendar()
 						}
 						$calendarTimes = 0;
 						while ($calendarTimes < $repeat) {
+							$currentDate = new DateTime ($GLOBALS['currentTime']);
+							$oldestDay = new DateTime ($GLOBALS['currentTime']);
+							$oldestDay->modify('-' . $GLOBALS['calendarStart'] . ' days');
+							$newestDay = new DateTime ($GLOBALS['currentTime']);
+							$newestDay->modify('+' . $GLOBALS['calendarEnd'] . ' days');
 							/* Converting to datetime and apply the timezone to get proper date time */
 							$startDt = new DateTime ($start);
 							/* Getting end date with time */
-							$end = isset($icsEvent ['DTEND;VALUE=DATE']) ? $icsEvent ['DTEND;VALUE=DATE'] : $icsEvent ['DTEND'];
 							$endDt = new DateTime ($end);
 							if ($calendarTimes !== 0) {
-								$currentDate = new DateTime ($GLOBALS['currentTime']);
 								$dateDiff = date_diff($startDt, $currentDate);
 								$startDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
 								$startDt->modify('+' . $calendarTimes . ' ' . $term);
 								$endDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
 								$endDt->modify('+' . $calendarTimes . ' ' . $term);
 							} elseif ($calendarTimes == 0 && $repeat !== 1) {
-								$currentDate = new DateTime ($GLOBALS['currentTime']);
 								$dateDiff = date_diff($startDt, $currentDate);
 								$startDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
 								$endDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
 							}
+							$calendarStartDiff = date_diff($startDt, $newestDay);
+							$calendarEndDiff = date_diff($startDt, $oldestDay);
 							$startDt->setTimeZone(new DateTimezone ($timeZone));
 							$endDt->setTimeZone(new DateTimezone ($timeZone));
 							$startDate = $startDt->format(DateTime::ATOM);
@@ -1280,6 +1291,9 @@ function getCalendar()
 							}
 							/* Getting the name of event */
 							$eventName = $icsEvent['SUMMARY'];
+							if (!calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
+								break;
+							}
 							$icalEvents[] = array(
 								'title' => $eventName,
 								'imagetype' => 'calendar-o text-warning text-custom-calendar ' . $extraClass,
@@ -1287,7 +1301,7 @@ function getCalendar()
 								'className' => 'bg-calendar calendar-item bg-custom-calendar',
 								'start' => $startDate,
 								'end' => $endDate,
-								'bgColor' => str_replace('text', 'bg', $extraClass)
+								'bgColor' => str_replace('text', 'bg', $extraClass),
 							);
 							$calendarTimes = $calendarTimes + 1;
 						}
@@ -1301,6 +1315,17 @@ function getCalendar()
 	return ($calendarSources) ? $calendarSources : false;
 }
 
+function calendarDaysCheck($entryStart, $entryEnd)
+{
+	$success = false;
+	$entryStart = intval($entryStart);
+	$entryEnd = intval($entryEnd);
+	if ($entryStart >= 0 && $entryEnd <= 0) {
+		$success = true;
+	}
+	return $success;
+}
+
 function getCalenderRepeat($value)
 {
 	//FREQ=DAILY