2012-06-03 16:54:53 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Georg Ehrke <ownclouddev@georgswebsite.de>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-28 20:54:13 +04:00
|
|
|
* This class does export and converts all times to UTC
|
2012-06-03 16:54:53 +04:00
|
|
|
*/
|
|
|
|
class OC_Calendar_Export{
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-28 20:54:13 +04:00
|
|
|
* @brief Use one of these constants as second parameter if you call OC_Calendar_Export::export()
|
|
|
|
*/
|
2012-06-03 16:54:53 +04:00
|
|
|
const CALENDAR = 'calendar';
|
|
|
|
const EVENT = 'event';
|
|
|
|
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-03 16:54:53 +04:00
|
|
|
* @brief export a calendar or an event
|
2012-06-28 00:27:48 +04:00
|
|
|
* @param integer $id id of calendar / event
|
|
|
|
* @param string $type use OC_Calendar_Export constants
|
|
|
|
* @return string
|
2012-06-03 16:54:53 +04:00
|
|
|
*/
|
|
|
|
public static function export($id, $type){
|
|
|
|
if($type == self::EVENT){
|
2012-06-28 20:54:13 +04:00
|
|
|
$return = self::event($id);
|
2012-06-03 16:54:53 +04:00
|
|
|
}else{
|
|
|
|
$return = self::calendar($id);
|
|
|
|
}
|
2012-06-28 20:54:13 +04:00
|
|
|
return self::fixLineBreaks($return);
|
2012-06-03 16:54:53 +04:00
|
|
|
}
|
|
|
|
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-28 20:54:13 +04:00
|
|
|
* @brief exports a calendar and convert all times to UTC
|
2012-06-28 00:27:48 +04:00
|
|
|
* @param integer $id id of the calendar
|
|
|
|
* @return string
|
2012-06-03 16:54:53 +04:00
|
|
|
*/
|
|
|
|
private static function calendar($id){
|
|
|
|
$events = OC_Calendar_Object::all($id);
|
2012-06-30 17:23:21 +04:00
|
|
|
$calendar = OC_Calendar_Calendar::find($id);
|
|
|
|
$return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\nX-WR-CALNAME:" . $calendar['displayname'] . "\n";
|
2012-06-03 16:54:53 +04:00
|
|
|
foreach($events as $event){
|
2012-06-28 20:54:13 +04:00
|
|
|
$return .= self::generateEvent($event);
|
2012-06-03 16:54:53 +04:00
|
|
|
}
|
|
|
|
$return .= "END:VCALENDAR";
|
|
|
|
return $return;
|
|
|
|
}
|
2012-06-28 20:54:13 +04:00
|
|
|
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-28 20:54:13 +04:00
|
|
|
* @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);
|
2012-06-30 17:23:21 +04:00
|
|
|
$return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\nX-WR-CALNAME:" . $event['summary'] . "\n";
|
2012-06-28 20:54:13 +04:00
|
|
|
$return .= self::generateEvent($event);
|
|
|
|
$return .= "END:VCALENDAR";
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-28 20:54:13 +04:00
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
|
2012-06-30 19:08:00 +04:00
|
|
|
/**
|
2012-06-28 20:54:13 +04:00
|
|
|
* @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;
|
|
|
|
}
|
2012-06-04 14:28:05 +04:00
|
|
|
}
|