|
|
@ -30,23 +30,32 @@ class PublicHolidayCalendar {
|
|
|
|
* - YearStart: int The start year of the calendar (with century). Default : current year
|
|
|
|
* - YearStart: int The start year of the calendar (with century). Default : current year
|
|
|
|
* - YearNumber: int The number of year fo the calendar. Default : 5
|
|
|
|
* - YearNumber: int The number of year fo the calendar. Default : 5
|
|
|
|
* - WeeksNumbers: bool (0/1) Include weeks number ? Default : 0
|
|
|
|
* - WeeksNumbers: bool (0/1) Include weeks number ? Default : 0
|
|
|
|
|
|
|
|
* - debug: bool(0/1)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* All times are UTC
|
|
|
|
* All times are UTC
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
#[NoReturn] public function proceed (): void {
|
|
|
|
#[NoReturn] public function proceed (): void {
|
|
|
|
|
|
|
|
$debug = !isset($_GET['debug']) || $_GET['debug'] == '' ? 0 : $_GET['debug'];
|
|
|
|
|
|
|
|
if (preg_match('#^\s*(?<bool>[01])?\s*$#i', $debug, $match) !== 1) {
|
|
|
|
|
|
|
|
$debug = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
$debug = isset($match['bool']) && $match['bool'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
//region Extract arguments
|
|
|
|
//region Extract arguments
|
|
|
|
//region YearStart
|
|
|
|
//region YearStart
|
|
|
|
$yearStart = $_GET['YearStart'] == '' ? (new DateTimeImmutable('now'))->format('Y') : $_GET['YearStart'];
|
|
|
|
$yearStart = !isset($_GET['YearStart']) || $_GET['YearStart'] == '' ? (new DateTimeImmutable('now'))->format('Y') : $_GET['YearStart'];
|
|
|
|
if (!is_numeric($yearStart)) {
|
|
|
|
if (!is_numeric($yearStart)) {
|
|
|
|
throw new UnexpectedValueException('The "YearStart" argument must be an integer');
|
|
|
|
throw new UnexpectedValueException('The "YearStart" argument must be an integer');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$yearStart = intval($yearStart);
|
|
|
|
$yearStart = intval($yearStart);
|
|
|
|
//endregion
|
|
|
|
//endregion
|
|
|
|
//region YearNumber
|
|
|
|
//region YearNumber
|
|
|
|
$yearNumber = $_GET['YearNumber'] == '' ? 5 : $_GET['YearNumber'];
|
|
|
|
$yearNumber = !isset($_GET['YearNumber']) || $_GET['YearNumber'] == '' ? 5 : $_GET['YearNumber'];
|
|
|
|
if (!is_numeric($yearNumber)) {
|
|
|
|
if (!is_numeric($yearNumber)) {
|
|
|
|
throw new UnexpectedValueException('The "YearNumber" argument must be a strictly positive integer');
|
|
|
|
throw new UnexpectedValueException('The "YearNumber" argument must be a strictly positive integer');
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -56,17 +65,17 @@ class PublicHolidayCalendar {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//endregion
|
|
|
|
//endregion
|
|
|
|
//region WeeksNumbers
|
|
|
|
//region WeeksNumbers
|
|
|
|
$weeksNumbers = $_GET['WeeksNumbers'] == '' ? 0 : $_GET['WeeksNumbers'];
|
|
|
|
$weeksNumbers = !isset($_GET['WeeksNumbers']) || $_GET['WeeksNumbers'] == '' ? 0 : $_GET['WeeksNumbers'];
|
|
|
|
if (preg_match('#^\s*(?<bool>[01])?\s*$#i', $weeksNumbers, $match) !== 1) {
|
|
|
|
if (preg_match('#^\s*(?<bool>[01])?\s*$#i', $weeksNumbers, $match) !== 1) {
|
|
|
|
throw new UnexpectedValueException('The "WeeksNumber" argument must be a boolean (0 or 1)');
|
|
|
|
throw new UnexpectedValueException('The "WeeksNumber" argument must be a boolean (0 or 1)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$weeksNumbers = isset($match['bool']) && $match['false'];
|
|
|
|
$weeksNumbers = isset($match['bool']) && $match['bool'];
|
|
|
|
//endregion
|
|
|
|
//endregion
|
|
|
|
//endregion
|
|
|
|
//endregion
|
|
|
|
|
|
|
|
|
|
|
|
//region Create the calendar
|
|
|
|
//region Create the calendar
|
|
|
|
$calendar = new Calendar();
|
|
|
|
$calendar = new Calendar();
|
|
|
|
$calendar->addTimeZone(TimeZone::createFromPhpDateTimeZone(new DateTimeZone(DateTimeZone::UTC)));
|
|
|
|
$calendar->addTimeZone(TimeZone::createFromPhpDateTimeZone(new DateTimeZone('UTC')));
|
|
|
|
|
|
|
|
|
|
|
|
$yearEnd = $yearStart + $yearNumber;
|
|
|
|
$yearEnd = $yearStart + $yearNumber;
|
|
|
|
for ($yearCurrent = $yearStart; $yearCurrent <= $yearEnd; $yearCurrent++) {
|
|
|
|
for ($yearCurrent = $yearStart; $yearCurrent <= $yearEnd; $yearCurrent++) {
|
|
|
@ -81,16 +90,25 @@ class PublicHolidayCalendar {
|
|
|
|
$icalFactory = new CalendarFactory();
|
|
|
|
$icalFactory = new CalendarFactory();
|
|
|
|
$icalContent = (string)$icalFactory->createCalendar($calendar);
|
|
|
|
$icalContent = (string)$icalFactory->createCalendar($calendar);
|
|
|
|
|
|
|
|
|
|
|
|
header('Content-Type: text/calendar; charset=UTF-8', true, 200);
|
|
|
|
if ($debug) {
|
|
|
|
header('Content-Length: ' . mb_strlen($icalContent));
|
|
|
|
header('Content-Type: text/plain; charset=UTF-8', true, 200);
|
|
|
|
header('Content-Disposition: attachment; filename=PublicHolidayCalendar.ics');
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
header('Content-Type: text/calendar; charset=UTF-8', true, 200);
|
|
|
|
|
|
|
|
header('Content-Length: ' . strlen($icalContent)); // Don't use mb_strlen because we NEED the number of bytes
|
|
|
|
|
|
|
|
header('Content-Disposition: attachment; filename=PublicHolidayCalendar.ics');
|
|
|
|
|
|
|
|
}
|
|
|
|
echo $icalContent;
|
|
|
|
echo $icalContent;
|
|
|
|
exit(0);
|
|
|
|
exit(0);
|
|
|
|
//endregion
|
|
|
|
//endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Throwable $e) {
|
|
|
|
catch (Throwable $e) {
|
|
|
|
header('Content-Type: text/plain; charset=UTF-8', true, 500);
|
|
|
|
header('Content-Type: text/plain; charset=UTF-8', true, 500);
|
|
|
|
echo $e->getMessage();
|
|
|
|
echo 'Exception [' . get_class($e) . ']: ' . $e->getMessage() . PHP_EOL;
|
|
|
|
|
|
|
|
if ($debug) {
|
|
|
|
|
|
|
|
echo $e->getTraceAsString() . PHP_EOL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -182,7 +200,7 @@ class PublicHolidayCalendar {
|
|
|
|
$currentDayOfWeek = (int)$currentDay->format('N');
|
|
|
|
$currentDayOfWeek = (int)$currentDay->format('N');
|
|
|
|
if ($currentDayOfWeek > 1) {
|
|
|
|
if ($currentDayOfWeek > 1) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
$currentDay = $currentDay->sub(new DateInterval('P' . (8 - $currentDayOfWeek) . 'D'));
|
|
|
|
$currentDay = $currentDay->add(new DateInterval('P' . (8 - $currentDayOfWeek) . 'D'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception) {
|
|
|
|
catch (Exception) {
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -192,7 +210,7 @@ class PublicHolidayCalendar {
|
|
|
|
while ($currentDay < $lastDay) {
|
|
|
|
while ($currentDay < $lastDay) {
|
|
|
|
$events[] = static::createEvent(
|
|
|
|
$events[] = static::createEvent(
|
|
|
|
'Semaine ' . (count($events) + 1),
|
|
|
|
'Semaine ' . (count($events) + 1),
|
|
|
|
'Semaine n° ' . (count($events) + 1),
|
|
|
|
'Semaine ' . (count($events) + 1),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
$currentDay
|
|
|
|
$currentDay
|
|
|
|
);
|
|
|
|
);
|
|
|
@ -313,7 +331,7 @@ class PublicHolidayCalendar {
|
|
|
|
private static function createEvent (string $summary, string $description, bool $publicHoliday, DateTimeInterface $date): Event {
|
|
|
|
private static function createEvent (string $summary, string $description, bool $publicHoliday, DateTimeInterface $date): Event {
|
|
|
|
return (new Event(new UniqueIdentifier(hash('sha256', $date->format('Y-m-d')))))
|
|
|
|
return (new Event(new UniqueIdentifier(hash('sha256', $date->format('Y-m-d')))))
|
|
|
|
->setSummary($summary)
|
|
|
|
->setSummary($summary)
|
|
|
|
->setDescription($description . ($publicHoliday ? ' - Férié' : ''))
|
|
|
|
->setDescription($description . ' ' . $date->format('Y') . ($publicHoliday ? ' - Férié' : ''))
|
|
|
|
->setOccurrence(new SingleDay(new Date($date)));
|
|
|
|
->setOccurrence(new SingleDay(new Date($date)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|