Add support for DURATION in VEVENT

This commit is contained in:
Bart Visscher 2011-09-28 23:16:32 +02:00
parent 5875b50de0
commit c9c7d5e1aa
4 changed files with 88 additions and 65 deletions

View file

@ -28,7 +28,7 @@ if($calendar['userid'] != OC_User::getUser()){
$object = Sabre_VObject_Reader::read($data['calendardata']);
$vevent = $object->VEVENT;
$dtstart = $vevent->DTSTART;
$dtend = $vevent->DTEND;
$dtend = OC_Calendar_Object::getDTEndFromVEvent($vevent);
switch($dtstart->getDateType()) {
case Sabre_VObject_Element_DateTime::LOCALTZ:
case Sabre_VObject_Element_DateTime::LOCAL:
@ -55,6 +55,11 @@ if (isset($vevent->CATEGORIES)){
$categories = explode(',', $vevent->CATEGORIES->value);
$categories = array_map('trim', $categories);
}
foreach($categories as $category){
if (!in_array($category, $category_options)){
array_unshift($category_options, $category);
}
}
$repeat = isset($vevent->CATEGORY) ? $vevent->CATEGORY->value : '';
$description = isset($vevent->DESCRIPTION) ? $vevent->DESCRIPTION->value : '';

View file

@ -10,6 +10,60 @@ require_once ("../../../lib/base.php");
if(!OC_USER::isLoggedIn()) {
die("<script type=\"text/javascript\">document.location = oc_webroot;</script>");
}
$output = new OC_TEMPLATE("calendar", "part.getcal");
$output -> printpage();
?>
$calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser(), 1);
$events = array();
$return = array('calendars'=>array());
foreach($calendars as $calendar) {
$tmp = OC_Calendar_Object::all($calendar['id']);
$events = array_merge($events, $tmp);
$return['calendars'][$calendar['id']] = array(
'displayname' => $calendar['displayname'],
'color' => $calendar['calendarcolor']
);
}
$select_year = $_GET["year"];
$user_timezone = OC_Preferences::getValue(OC_USER::getUser(), "calendar", "timezone", "Europe/London");
foreach($events as $event)
{
if ($select_year != substr($event['startdate'], 0, 4))
continue;
$object = Sabre_VObject_Reader::read($event['calendardata']);
$vevent = $object->VEVENT;
$dtstart = $vevent->DTSTART;
$dtend = OC_Calendar_Object::getDTEndFromVEvent($vevent);
$start_dt = $dtstart->getDateTime();
$start_dt->setTimezone(new DateTimeZone($user_timezone));
$end_dt = $dtend->getDateTime();
$end_dt->setTimezone(new DateTimeZone($user_timezone));
$year = $start_dt->format('Y');
$month = $start_dt->format('n') - 1; // return is 0 based
$day = $start_dt->format('j');
$hour = $start_dt->format('G');
if ($dtstart->getDateType() == Sabre_VObject_Element_DateTime::DATE) {
$hour = 'allday';
}
$return_event = array();
foreach(array('id', 'calendarid', 'objecttype', 'repeating') as $prop)
{
$return_event[$prop] = $event[$prop];
}
$return_event['startdate'] = explode('|', $start_dt->format('Y|m|d|H|i'));
$return_event['enddate'] = explode('|', $end_dt->format('Y|m|d|H|i'));
$return_event['description'] = $event['summary'];
if ($hour == 'allday')
{
$return_event['allday'] = true;
}
if (isset($return[$year][$month][$day][$hour]))
{
$return[$year][$month][$day][$hour][] = $return_event;
}
else
{
$return[$year][$month][$day][$hour] = array(1 => $return_event);
}
}
OC_JSON::encodedPrint($return);

View file

@ -286,6 +286,30 @@ class OC_Calendar_Object{
}
}
public static function getDTEndFromVEvent($vevent)
{
if ($vevent->DTEND) {
$dtend = $vevent->DTEND;
}else{
$dtend = clone $vevent->DTSTART;
if ($vevent->DURATION){
$duration = strval($vevent->DURATION);
$invert = 0;
if ($duration[0] == '-'){
$duration = substr($duration, 1);
$invert = 1;
}
if ($duration[0] == '+'){
$duration = substr($duration, 1);
}
$interval = new DateInterval($duration);
$interval->invert = $invert;
$dtend->getDateTime()->add($interval);
}
}
return $dtend;
}
public static function getCategoryOptions($l10n)
{
return array(
@ -482,6 +506,7 @@ class OC_Calendar_Object{
}
$vevent->DTSTART = $dtstart;
$vevent->DTEND = $dtend;
unset($vevent->DURATION);
if($location != ""){
$vevent->LOCATION = $location;

View file

@ -1,61 +0,0 @@
<?php
/**
* Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
$calendars = OC_Calendar_Calendar::allCalendars(OC_User::getUser(), 1);
$events = array();
$return = array('calendars'=>array());
foreach($calendars as $calendar) {
$tmp = OC_Calendar_Object::all($calendar['id']);
$events = array_merge($events, $tmp);
$return['calendars'][$calendar['id']] = array(
'displayname' => $calendar['displayname'],
'color' => $calendar['calendarcolor']
);
}
$select_year = $_GET["year"];
$user_timezone = OC_Preferences::getValue(OC_USER::getUser(), "calendar", "timezone", "Europe/London");
foreach($events as $event)
{
if ($select_year != substr($event['startdate'], 0, 4))
continue;
$start_dt = new DateTime($event['startdate'], new DateTimeZone('UTC'));
$start_dt->setTimezone(new DateTimeZone($user_timezone));
$end_dt = new DateTime($event['enddate'], new DateTimeZone('UTC'));
$end_dt->setTimezone(new DateTimeZone($user_timezone));
$year = $start_dt->format('Y');
$month = $start_dt->format('n') - 1; // return is 0 based
$day = $start_dt->format('j');
$hour = $start_dt->format('G');
// hack
if (strstr($event['calendardata'], 'DTSTART;VALUE=DATE:')) {
$hour = 'allday';
}
$return_event = array();
foreach(array('id', 'calendarid', 'objecttype', 'repeating') as $prop)
{
$return_event[$prop] = $event[$prop];
}
$return_event['startdate'] = explode('|', $start_dt->format('Y|m|d|H|i'));
$return_event['enddate'] = explode('|', $end_dt->format('Y|m|d|H|i'));
$return_event['description'] = $event['summary'];
if ($hour == 'allday')
{
$return_event['allday'] = true;
}
if (isset($return[$year][$month][$day][$hour]))
{
$return[$year][$month][$day][$hour][] = $return_event;
}
else
{
$return[$year][$month][$day][$hour] = array(1 => $return_event);
}
}
OC_JSON::encodedPrint($return);
?>