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.

217 lines
6.0 KiB
PHP

<?php
require_once ('ics.php');
$ical = null;
if (isset ($_GET["ics_url"])) {
$ical = getCalendar($_GET["ics_url"]);
}
/**
* Sets and returns the global $requested_month variable
* to either the GET requested month, or the current month
*
* @return int Month number
*/
function getRequestedMonth () {
global $_GET, $requested_month;
if (isset ($requested_month)) return $requested_month;
if (
! isset ($_GET["month"]) ||
! ($requested_month = intval ($_GET["month"])) ||
$requested_month > 12 ||
$requested_month < 1
) {
$requested_month = idate("m");
}
return $requested_month;
}
/**
* Echoes the requested months' name
*/
function echoMonthName () {
echo getLocalizedMonthName (getRequestedMonth ());
}
/**
* Returns HTML for a days' events list (CSS class day-events-scrollbox)
*
* @param ICal $ical ICal object
* @param int $day Day of month
* @param int $month Month (defaults to current month)
* @param int $year Year (defaults to current year)
*
* @return string HTML containing DIV of class day-events-scrollbox
* @return false False on failure
*/
function generateDayEventsList ($ical, $day, $month = null, $year = null) {
if (! $ical) return false;
$html = '
<div class="day-events-scrollbox">
<div class="day-events autoscroll">
%s
</div>
<div class="day-events-shadow"></div>
</div>
';
foreach (getEventsForDay ($ical, $day, $month, $year) as $event) {
$dtstart = $ical -> iCalDateToDateTime($event -> dtstart_array[3]);
$dtend = $ical -> iCalDateToDateTime($event -> dtend_array[3]);
$today_start = new DateTime(
date('Y-m-d H:i:s', mktime(0, 0, 0, $month, $day, $year)),
$dtstart -> getTimezone()
);
$today_end = clone $today_start; $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 < $today_start) {
$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";
$time_str = "";
}
// 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(
'<span class="day-event-entry">
<span class="time">%s</span>
<span class="description">%s</span>
</span>
%%s',
$time_str,
$event -> summary
),
);
}
$html = sprintf ($html, "<!-- {$day} -->");
return $html;
}
/**
* Returns HTML with all elements going in the DIV with CSS class calendar-body
*
* @param ICal $Ical ICal object
* @param int $month Month
*
* @return string HTML containing elements for calendar body
* @return false False on failure
*/
function generateCalendarBodyItems ($ical, $month) {
if ($month > 12 || $month < 1) return false;
$html = '%s';
$odd_row = false;
// Number of days in month
$month_max_days = idate ("t", mktime (0, 0, 0, $month));
// First of month unix timestamp
$first_of_month = new DateTime (
date('Y-m-d H:i:s', mktime (0, 0, 0, $month, 1))
);
// Days needed to pad the calendar at the start
$pad_days = idate ("N", $first_of_month -> getTimestamp()) - 1;
// First of the month, minus the pad days
$day = $first_of_month -> modify ("-{$pad_days} days");
// Number of total entries in calendar
$num_rows = ceil (($pad_days + $month_max_days) / 7);
$num_entries = ($num_rows * 8);
// Loop over all entry slots and generate entries
for ($item_index = 0; $item_index < $num_entries; $item_index ++) {
// If item_index is beginning of row, add week number entry
if ($item_index % 8 == 0) {
$week = $day -> format ("W");
$html = sprintf (
$html,
"<div class=\"calendar-header-value\">Woche {$week}</div>
%s",
);
// Swap odd and even rows
$odd_row = ! $odd_row;
continue;
}
$calendar_entry_html = '
<div class="calendar-entry %s %s %s">
<div class="day-header %s"><span>%d.</span></div>
%s
</div>
%s
';
$html = sprintf (
$html,
sprintf(
$calendar_entry_html,
$odd_row? "odd-row" : "even-row",
(isDateTimeWithinMonth ($day, $month)) ? "" : "other-month",
(isDateTimeDuringWeekend ($day)) ? "weekend" : "",
(isDateTimeToday ($day))? "today" : "",
$day -> format ("d"),
generateDayEventsList(
$ical,
intval ($day -> format ("d")),
intval ($day -> format ("m")),
),
"%s"
)
);
$day -> modify ("+1 day");
}
return sprintf($html, "");
}
/**
* Echoes HTML to insert all calendar day entries
*/
function echoCalendarEntries() {
global $ical;
echo generateCalendarBodyItems($ical, getRequestedMonth());
}