Skip to content

Commit

Permalink
Prepare migration to new ICS parser
Browse files Browse the repository at this point in the history
  • Loading branch information
lochmueller committed Feb 24, 2019
1 parent ad2e843 commit 81aee33
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 854 deletions.
110 changes: 103 additions & 7 deletions Classes/Service/IcsReaderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

namespace HDNET\Calendarize\Service;

use HDNET\Calendarize\Service\TimeTable\ExternalTimeTable;
use HDNET\Calendarize\Utility\DateTimeUtility;
use JMBTechnologyLimited\ICalDissect\ICalEvent;
use JMBTechnologyLimited\ICalDissect\ICalParser;
use Sabre\VObject\Reader;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -17,21 +20,114 @@
class IcsReaderService extends AbstractService
{
/**
* Get the ICS events in an array.
* Generate the times of the given URL.
*
* @param string $paramUrl
* @param string $url
*
* @return array
*/
public function getTimes(string $url): array
{
$fileName = $this->getCachedUrlFile($url);
$times = $this->buildWithICalDissect($fileName);
// $test = $this->buildWithVObject($fileName);
return $times;
}

/**
* Build with iCal dissect.
*
* @param string $filename
*
* @return array
*/
public function toArray($paramUrl)
protected function buildWithICalDissect(string $filename): array
{
$tempFileName = $this->getCachedUrlFile($paramUrl);
$backend = new ICalParser();
if ($backend->parseFromFile($tempFileName)) {
return $backend->getEvents();
if (!$backend->parseFromFile($filename)) {
return [];
}
$events = $backend->getEvents();
$times = [];
foreach ($events as $event) {
/** @var $event ICalEvent */
$startTime = DateTimeUtility::getDaySecondsOfDateTime($event->getStart());
$endTime = DateTimeUtility::getDaySecondsOfDateTime($event->getEnd());
if (ExternalTimeTable::DAY_END === $endTime) {
$endTime = 0;
}

$times[] = [
'start_date' => $event->getStart(),
'end_date' => $this->getEventsFixedEndDate($event),
'start_time' => $startTime,
'end_time' => $endTime,
'all_day' => 0 === $endTime,
];
}

return $times;
}

/**
* Build with Vobject.
*
* @param string $filename
*
* @return array
*
* @todo implement
*/
protected function buildWithVObject(string $filename): array
{
$vcalendar = Reader::read(
GeneralUtility::getUrl($filename)
);
$times = [];
foreach ($vcalendar->VEVENT as $event) {
/*DTSTAMP => array(1 item)
UID => array(1 item)
DTSTART => array(1 item)
DTEND => array(1 item)*/
//DebuggerUtility::var_dump($event->TESTST);
//DebuggerUtility::var_dump($event->DTSTAMP);
//DebuggerUtility::var_dump($event->DTSTART);
//DebuggerUtility::var_dump($event->DTEND);

$times[] = [
'start_date' => $event->DTSTART->getDateTime(),
'end_date' => 0, // $this->getEventsFixedEndDate($event),
'start_time' => 0,
'end_time' => 0,
'all_day' => true,
];
}

return $times;
}

/**
* Fixes a parser related bug where the DTEND is EXCLUSIVE.
* The parser uses it inclusive so every event is one day
* longer than it should be.
*
* @param ICalEvent $event
*
* @return \DateTime
*/
protected function getEventsFixedEndDate(ICalEvent $event)
{
if (!$event->getEnd() instanceof \DateTimeInterface) {
return $event->getStart();
}

$end = clone $event->getEnd();
$end->sub(new \DateInterval('P1D'));
if ($end->format('Ymd') === $event->getStart()->format('Ymd')) {
return $end;
}

return [];
return $event->getEnd();
}

/**
Expand Down
50 changes: 5 additions & 45 deletions Classes/Service/TimeTable/ExternalTimeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

use HDNET\Calendarize\Domain\Model\Configuration;
use HDNET\Calendarize\Service\IcsReaderService;
use HDNET\Calendarize\Utility\DateTimeUtility;
use HDNET\Calendarize\Utility\HelperUtility;
use JMBTechnologyLimited\ICalDissect\ICalEvent;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand Down Expand Up @@ -58,49 +56,11 @@ public function handleConfiguration(array &$times, Configuration $configuration)
return;
}

$events = $this->icsReaderService->toArray($url);
foreach ($events as $event) {
/** @var $event ICalEvent */
$startTime = DateTimeUtility::getDaySecondsOfDateTime($event->getStart());
$endTime = DateTimeUtility::getDaySecondsOfDateTime($event->getEnd());
if (self::DAY_END === $endTime) {
$endTime = 0;
}

$entry = [
'pid' => $configuration->getPid(),
'start_date' => $event->getStart(),
'end_date' => $this->getEventsFixedEndDate($event),
'start_time' => $startTime,
'end_time' => $endTime,
'all_day' => 0 === $endTime,
'state' => $configuration->getState(),
];
$times[$this->calculateEntryKey($entry)] = $entry;
}
}

/**
* Fixes a parser related bug where the DTEND is EXCLUSIVE.
* The parser uses it inclusive so every event is one day
* longer than it should be.
*
* @param ICalEvent $event
*
* @return \DateTime
*/
protected function getEventsFixedEndDate(ICalEvent $event)
{
if (!$event->getEnd() instanceof \DateTimeInterface) {
return $event->getStart();
$externalTimes = $this->icsReaderService->getTimes($url);
foreach ($externalTimes as $time) {
$time['pid'] = $configuration->getPid();
$time['state'] = $configuration->getState();
$times[$this->calculateEntryKey($time)] = $time;
}

$end = clone $event->getEnd();
$end->sub(new \DateInterval('P1D'));
if ($end->format('Ymd') === $event->getStart()->format('Ymd')) {
return $end;
}

return $event->getEnd();
}
}
Loading

0 comments on commit 81aee33

Please sign in to comment.