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.

206 lines
5.2 KiB
PHP

<?php
require_once ('html-components.php');
require_once ('vendor/autoload.php');
use ICal\ICal;
date_default_timezone_set("Europe/Berlin");
/**
* Returns the name of a month
*
* @param int $month Month index, 1 to 12
*
* @return string Month name
* @return string Blank string if invalid month was given
*/
function getLocalizedMonthName ($month) {
if (! $month || $month > 12 || $month < 1) return "";
return [
"Januar",
"Februar",
"März",
"April",
"Mai",
"Juni",
"Juli",
"August",
"September",
"Oktober",
"November",
"Dezember"
][$month - 1];
}
/**
* Returns the short name of a day of week
*
* @param int $day_of_week DoW index, 1 to 7
*
* @return string Day of week name
* @return string Blank string if invalid DoW was given
*/
function getLocalizedShortDayName ($day_of_week) {
if ($day_of_week < 1 || $day_of_week > 7) return "";
return [
"Mo.",
"Di.",
"Mi.",
"Do.",
"Fr.",
"Sa.",
"So."
][$day_of_week - 1];
}
/**
* Tests if a given date is today
*
* @param DateTime $date DateTime object to compare against
*
* @return true when the DateTime object is today
* @return false when the DateTime object is not today
*/
function isDateTimeToday ($date) {
return $date -> format ("Y-m-d") == date ("Y-m-d");
}
/**
* Tests if a given date is within the current month
*
* @param DateTime $date DateTime object to compare against
* @param int $month Month to check against (defaults to current month)
*
* @return true when the DateTime object is within the current month
* @return false when the DateTime object is not within the current month
*/
function isDateTimeWithinMonth ($date, $month = null) {
$compareDate = new DateTime ();
if ($month != null) $compareDate = new DateTime ("this year $month/01");
return $date -> format ("Y-m") == $compareDate -> format ("Y-m");
}
/**
* Tests if a given date is during the weekend
*
* @param DateTime $date DateTime object to test
*
* @return true when the DateTime object is a weekend day
* @return false when the DateTime object is a week day
*/
function isDateTimeDuringWeekend ($date) {
return intval ($date -> format ("N")) > 5;
}
/**
* Fetches an ICal object from an ICS URL
*
* @param string $url ICS URL to fetch from
*
* @return ICal ICal object
*/
function getCalendar ($url) {
try {
$ical = new ICal (false, array(
'defaultSpan' => 2, // Default value
'defaultTimeZone' => 'Europe/Berlin',
'defaultWeekStart' => 'MO', // Default value
'disableCharacterReplacement' => false, // Default value
'filterDaysAfter' => null, // Default value
'filterDaysBefore' => null
));
$ical -> initUrl(
$url,
$username = null,
$password = null,
$userAgent = null
);
} catch (Exception $e) {
showError( "ICal error: " . $e -> getMessage() );
return $ical;
}
return $ical;
}
/**
* Returns all events from an iCal object
*
* @param ICal $ical ICal object
*
* @return array Array of event objects
* @return false False on failure
*/
function getEvents ($ical) {
if (! $ical) return false;
return $ical -> sortEventsWithOrder (
$ical -> events ()
);
}
/**
* Returns all events from an ICal object for a specific day
*
* @param ICal $ical ICal object
* @param int $day Day
* @param int $month Month (defaults to current month)
* @param int $year Year (default to current year)
*
* @return array Array of event objects
* @return false False on failure
*/
function getEventsForDay ($ical, $day, $month = null, $year = null) {
if (! $ical) return false;
$date_format = "Y-m-d h:i:s";
return $ical -> sortEventsWithOrder (
$ical -> eventsFromRange (
date ($date_format, mktime (0, 0, 0, $month, $day, $year)),
date ($date_format, mktime (23, 59, 59, $month, $day, $year)),
)
);
}
/**
* Wrapper for getEventsForDay.
* Returns all events from an ICal object for a specific date
*
* @param ICal $ical ICal object
* @param DateTime $date DateTime object
*
* @return array Array of event objects
* @return false False on failure
*/
function getEventsForDate ($ical, $date) {
return getEventsForDay (
$ical,
intval($date -> format ("d")),
intval($date -> format ("m")),
intval($date -> format ("Y"))
);
}
/**
* Get events from ICal object limited by a start date
* and amount to days forward from the start date
*
* @param ICal $ical ICal object
* @param DateTime $startDate Date to start from
* @param int $days Amount of days to get
*/
function getEventsFromRange ($ical, $startDate, $days) {
if (! $ical) return false;
return $ical -> sortEventsWithOrder (
$ical -> getEventsFromRange (
$startDate -> format ("Y-m-d H:i:s"),
$startDate -> modify ("+ {$days} days") -> format ("Y-m-d H:i:s")
)
);
}