From 9dd7ca26671d2c6640210e59071135e8f69cca7f Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Thu, 28 Jun 2012 18:54:13 +0200 Subject: [PATCH] some changes and more doc for OC_Calendar_Export class --- apps/calendar/lib/export.php | 74 +++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/apps/calendar/lib/export.php b/apps/calendar/lib/export.php index 7c98d94fcc..cd248ce92a 100644 --- a/apps/calendar/lib/export.php +++ b/apps/calendar/lib/export.php @@ -6,9 +6,12 @@ * See the COPYING-README file. */ /* - * This class does export + * This class does export and converts all times to UTC */ class OC_Calendar_Export{ + /* + * @brief Use one of these constants as second parameter if you call OC_Calendar_Export::export() + */ const CALENDAR = 'calendar'; const EVENT = 'event'; @@ -20,38 +23,71 @@ class OC_Calendar_Export{ */ public static function export($id, $type){ if($type == self::EVENT){ - $data = OC_Calendar_App::getEventObject($_GET['eventid'], false); - $return = $data['calendardata']; + $return = self::event($id); }else{ $return = self::calendar($id); } - return $return; + return self::fixLineBreaks($return); } /* - * @brief export a calendar and convert all times to UTC + * @brief exports a calendar and convert all times to UTC * @param integer $id id of the calendar * @return string */ private static function calendar($id){ $events = OC_Calendar_Object::all($id); - $return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar\n"; + $return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\n"; foreach($events as $event){ - $object = OC_VObject::parse($event['calendardata']); - $dtstart = $object->VEVENT->DTSTART; - $start_dt = $dtstart->getDateTime(); - $dtend = OC_Calendar_Object::getDTEndFromVEvent($object->VEVENT); - $end_dt = $dtend->getDateTime(); - if($dtstart->getDateType() !== Sabre_VObject_Element_DateTime::DATE){ - $object->VEVENT->DTSTART->offsetUnset('VALUE'); - $object->VEVENT->DTEND->offsetUnset('VALUE'); - } - $return .= $object->VEVENT->serialize(); + $return .= self::generateEvent($event); } $return .= "END:VCALENDAR"; - $return = str_replace("\r\n", "\n", $return); - $return = str_replace("\r", "\n", $return); - $return = str_replace("\n", "\r\n", $return); return $return; } + + /* + * @brief exports an event and convert all times to UTC + * @param integer $id id of the event + * @return string + */ + private static function event($id){ + $event = OC_Calendar_Object::find($id); + $return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\n"; + $return .= self::generateEvent($event); + $return .= "END:VCALENDAR"; + return $return; + } + + /* + * @brief generates the VEVENT with UTC dates + * @param array $event + * @return string + */ + private static function generateEvent($event){ + $object = OC_VObject::parse($event['calendardata']); + $dtstart = $object->VEVENT->DTSTART; + $start_dt = $dtstart->getDateTime(); + $dtend = OC_Calendar_Object::getDTEndFromVEvent($object->VEVENT); + $end_dt = $dtend->getDateTime(); + if($dtstart->getDateType() !== Sabre_VObject_Element_DateTime::DATE){ + $start_dt->setTimezone(new DateTimeZone('UTC')); + $end_dt->setTimezone(new DateTimeZone('UTC')); + $object->VEVENT->setDateTime('DTSTART', $start_dt, Sabre_VObject_Property_DateTime::UTC); + $object->VEVENT->setDateTime('DTEND', $end_dt, Sabre_VObject_Property_DateTime::UTC); + } + return $object->VEVENT->serialize(); + } + + /* + * @brief fixes new line breaks + * (fixes problems with Apple iCal) + * @param string $string to fix + * @return string + */ + private static function fixLineBreaks($string){ + $string = str_replace("\r\n", "\n", $string); + $string = str_replace("\r", "\n", $string); + $string = str_replace("\n", "\r\n", $string); + return $string; + } }