You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.3 KiB
PHP

<?php
require_once ('ics.php');
$ical = null;
if (isset ($_GET["ics_url"])) {
$ical = getCalendar($_GET["ics_url"]);
}
/**
* Fetches the user requested start date for the flow
*/
function getRequestedStartDate () {
global $_GET, $requested_start_date;
if (isset ($requested_start_date)) return $requested_start_date;
if (
! isset ($_GET["start_date"]) ||
! ($requested_start_date = new DateTime ($_GET["start_date"]))
) {
$requested_start_date = new DateTime ();
}
return $requested_start_date;
}
/**
* Fetches the user requested amount of days to display
*/
function getRequestedDisplayedDays () {
global $_GET, $requested_display_days;
if (isset ($requested_display_days)) return $requested_display_days;
if (
! isset ($_GET["days"]) ||
! ($requested_display_days = intval($_GET["days"]))
) {
$requested_display_days = 7;
}
return $requested_display_days;
}
/**
* Return HTML for a flow day entry
*
* @param ICal $ical ICal object
* @param DateTime $date DateTime object
*
* @return string Returns HTML with flow day entry
* @return false False on failure
*/
function generateFlowEntryForDay ($ical, $date) {
if ($ical == null || $date == null) return false;
$html = sprintf (
'
<div class="flow-element">
<div class="flow-day-name">
<div class="day">%s</div>
<div class="weekday">%s</div>
</div>
<div class="flow-day-entries">
%%s
</div>
</div>
',
$date -> format ("d"),
getLocalizedShortDayName (
intval ($date -> format ("N"))
)
);
// Fetch events for the specified date
$events = getEventsForDate ($ical, $date);
// Skip empty days
if (count ($events) == 0) return "";
// Iterate over each event and add an event entry to the HTML
foreach ($events as $event) {
$dtstart = $ical -> iCalDateToDateTime($event -> dtstart_array[3]);
$dtend = $ical -> iCalDateToDateTime($event -> dtend_array[3]);
$today_end = clone $date; $today_end -> modify("+1 day");
$time_separator = " - ";
$time_prefix = "";
$time_start = $dtstart -> format('H:i');
$time_end = $dtend -> format('H:i');
// When start date is in past, don't display start time
if ($dtstart < $date) {
$time_start = "";
$time_separator = "Bis ";
}
// When end date is in future, don't display end time
if ($dtend > $today_end) {
$time_end = "";
$time_separator = "";
$time_prefix = "Ab ";
}
// Assemble time display string
$time_str = "{$time_prefix}{$time_start}{$time_separator}{$time_end}";
// Check if event is all day
if ($time_start == "00:00" && $time_end == "00:00") {
$time_str = "Ganztägig";
}
// Check if event is multi day
if ($time_start == "" && $time_end == "") {
$time_str = "Mehrtägig";
}
// Check if multi day event without start times
if ($time_start . $time_end == "00:00") {
$time_str = "Mehrtägig";
}
$html = sprintf (
$html,
sprintf (
'<div class="flow-day-entry">
<span class="description">%s</span>
<span class="time">%s</span>
</div>
%%s',
$event -> summary,
$time_str
),
);
}
$html = sprintf ($html, '');
return $html;
}
/**
* Return HTML with flow header for specific date
*
* @param DateTime $date DateTime object
*
* @return string Returns HTML with flow header
* @return false False on failure
*/
function generateFlowHeader ($date) {
if ($date == null) return false;
return sprintf (
'<div class="flow-header">
<div class="flow-header-spacer"></div>
<div class="flow-header-value">%s</div>
</div>',
getLocalizedMonthName (intval ($date -> format ("m")))
);
}
/**
* Return HTML for the flow body
*
* @param ICal $ical ICal object
* @param DateTime $date DateTime object
* @param int $days Days to display
*
* @return string Returns HTML with the entire flow data
* @return false False on failure
*/
function generateFlowData ($ical, $date, $days) {
if ($ical == null || $date == null) return false;
$html = generateFlowHeader ($date) . "\n%s";
$end_date = clone $date; $end_date -> modify ("+{$days} days");
while ($date <= $end_date) {
if ($date -> format ("d") == "01") $html = sprintf (
$html,
generateFlowHeader ($date) . "\n%s"
);
$html = sprintf (
$html,
generateFlowEntryForDay($ical, $date) . "%s"
);
$date -> modify ("+1 day");
}
$html = sprintf ($html, "");
return $html;
}
/**
* Echoes the entire HTML for the flow body
*/
function echoFlowBody () {
global $ical;
echo generateFlowData (
$ical,
getRequestedStartDate(),
getRequestedDisplayedDays()
);
}