This repository has been archived on 2020-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
ical-drupal-calendar/includes/ICSEchoWriter.php

62 lines
1.7 KiB
PHP

<?php
namespace Pandy06269\iCalDrupal\includes;
class ICSEchoWriter implements IEventWriter
{
public function close()
{
echo ICSFileReader::HEADER_VCALENDAR_END . PHP_EOL;
}
public function open()
{
echo ICSFileReader::HEADER_VCALENDAR_BEGIN . PHP_EOL;
}
public function write(Event $event)
{
echo ICSFileReader::HEADER_VEVENT_BEGIN . PHP_EOL;
echo sprintf('%s%s' . PHP_EOL, ICSFileReader::HEADER_TITLE, $event->getTitle());
if (!empty($event->getDescription()))
{
$matches = [];
preg_match_all('/<p>(.+)<\/p>/', $event->getDescription(), $matches);
$isDescriptionStarted = false;
foreach ($matches[1] as $match)
{
if (strlen(trim($match)) > 0)
{
if ($isDescriptionStarted)
{
echo $match . PHP_EOL;
}
else
{
echo sprintf('%s%s' . PHP_EOL, ICSFileReader::HEADER_DESCRIPTION, $match);
$isDescriptionStarted = true;
}
}
}
}
if (!is_null($event->getStartDate()))
{
$date = $event->getStartDate()->format('Ymd\\THis\\Z');
echo sprintf('%s%s' . PHP_EOL, ICSFileReader::HEADER_DATE, $date);
}
if (!is_null($event->getEndDate()))
{
$date = $event->getEndDate()->format('Ymd\\THis\\Z');
echo sprintf('%s%s' . PHP_EOL, ICSFileReader::HEADER_DATE_END, $date);
}
echo ICSFileReader::HEADER_VEVENT_END . PHP_EOL;
}
}