<?php
 class vB_Library_Calendar extends vB_Library { public function deleteOldHolidays(float $cutoffYears = 2) : array { if ($cutoffYears <= 0) { return [ 'deleted' => [], ]; } $assertor = vB::getDbAssertor(); $timenow = vB::getRequest()->getTimeNow(); $cutOffDate = $timenow - ($cutoffYears * 365 * 24 * 60 * 60); $cutoffMonth = gmdate('m', $cutOffDate); $cutoffYear = gmdate('Y', $cutOffDate); $cutoffDay = gmdate('d', $cutOffDate); $conditions = [ 'year' => $cutoffYear, 'month' => $cutoffMonth, 'day' => $cutoffDay, ]; $eventidsAndTitles = $assertor->getRows('getOldHolidays', $conditions); $eventids = array_column($eventidsAndTitles, 'eventid'); if (!empty($eventidsAndTitles)) { $assertor->delete('calendarevent', ['eventid' => $eventids]); } return [ 'deleted' => $eventidsAndTitles, ]; } public function convertEventToIcs(array $node) : string { vB::includeLibraryFile('vendor/autoload.php'); $event = $this->createEvent($node); $calendar = new \Eluceo\iCal\Domain\Entity\Calendar([$event]); $calendar->setProductIdentifier('-//vBulletin 6//EN'); $componentFactory = new \Eluceo\iCal\Presentation\Factory\CalendarFactory(); $calendarComponent = $componentFactory->createCalendar($calendar); $ics_file_contents = $calendarComponent->__toString(); return $ics_file_contents; } private function createEvent(array $node) : \Eluceo\iCal\Domain\Entity\Event { vB::includeLibraryFile('vendor/autoload.php'); $content = $node['content']; $uid = $this->getEventUID($node); $event = new \Eluceo\iCal\Domain\Entity\Event($uid); $event->setSummary($this->process_string_for_ics($node['title'])); $previewtext = ''; if ($content['rawtext']) { $stripquotes = true; $fast_and_dirty = false; $showlinks = true; $stripimg = true; $keepquotetags = false; $previewtext = vB_String::stripBbcode($content['rawtext'], $stripquotes, $fast_and_dirty, $showlinks, $stripimg, $keepquotetags); $previewtext = vB_String::stripTags($previewtext); } $event->setDescription($this->process_string_for_ics($previewtext)); if (!empty($content['location'])) { $location = new \Eluceo\iCal\Domain\ValueObject\Location($this->process_string_for_ics($content['location'])); $event->setLocation($location); } $occurence = $this->getOccurrence($content); $event->setOccurrence($occurence); $nodeUrl = vB5_Route::buildUrl('node|fullurl', ['nodeid' => $node['nodeid']]); $event->setUrl(new \Eluceo\iCal\Domain\ValueObject\Uri($nodeUrl)); return $event; } private function getEventUID(array $node) : \Eluceo\iCal\Domain\ValueObject\UniqueIdentifier { $uid = $node['content']['eventuid']; return new \Eluceo\iCal\Domain\ValueObject\UniqueIdentifier($uid); } private function getOccurrence(array $content) : \Eluceo\iCal\Domain\ValueObject\Occurrence { vB::includeLibraryFile('vendor/autoload.php'); $utcTimezone = new DateTimeZone('UTC'); $startPhpDateTime = new DateTime('@' . $content['eventstartdate']); $startPhpDateTime->setTimezone($utcTimezone); $endPhpDateTime = new DateTime('@' . ($content['eventenddate'] + 1)); $endPhpDateTime->setTimezone($utcTimezone); if ($content['allday']) { $timeDiff = $content['eventenddate'] - $content['eventstartdate']; if ($timeDiff > 86400) { $startdate = new \Eluceo\iCal\Domain\ValueObject\Date(DateTimeImmutable::createFromFormat('Y-m-d', $content['alldaystartdate'])); $enddate = new \Eluceo\iCal\Domain\ValueObject\Date(DateTimeImmutable::createFromFormat('Y-m-d', $content['alldayenddate'])); $occurence = new \Eluceo\iCal\Domain\ValueObject\MultiDay($startdate, $enddate); } else { $date = new \Eluceo\iCal\Domain\ValueObject\Date(DateTimeImmutable::createFromFormat('Y-m-d', $content['alldaystartdate'])); $occurence = new \Eluceo\iCal\Domain\ValueObject\SingleDay($date); } } else { $applyTimezone = true; $startDate = new \Eluceo\iCal\Domain\ValueObject\DateTime($startPhpDateTime, $applyTimezone); $endDate = new \Eluceo\iCal\Domain\ValueObject\DateTime($endPhpDateTime, $applyTimezone); $occurence = new \Eluceo\iCal\Domain\ValueObject\TimeSpan($startDate, $endDate); } return $occurence; } private function process_string_for_ics(?string $str) : string { $str ??= ''; $stringUtil = vB::getString(); $str = $stringUtil->toUtf8($str); return $str; } } 