2012-06-29 16:32:07 +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.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This class does import and converts all times to the users current timezone
|
|
|
|
*/
|
|
|
|
class OC_Calendar_Import{
|
|
|
|
/*
|
2012-06-30 14:42:36 +04:00
|
|
|
* @brief var saves if the percentage should be saved with OC_Cache
|
2012-06-29 16:32:07 +04:00
|
|
|
*/
|
2012-06-30 14:42:36 +04:00
|
|
|
private $cacheprogress;
|
2012-06-29 16:32:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief Sabre_VObject_Component_VCalendar object - for documentation see http://code.google.com/p/sabredav/wiki/Sabre_VObject_Component_VCalendar
|
|
|
|
*/
|
|
|
|
private $calobject;
|
|
|
|
|
2012-07-01 00:26:29 +04:00
|
|
|
/*
|
|
|
|
* @brief var counts the number of imported elements
|
|
|
|
*/
|
|
|
|
private $count;
|
|
|
|
|
2012-06-29 16:32:07 +04:00
|
|
|
/*
|
2012-06-30 14:42:36 +04:00
|
|
|
* @brief var to check if errors happend while initialization
|
2012-06-29 16:32:07 +04:00
|
|
|
*/
|
2012-06-30 14:42:36 +04:00
|
|
|
private $error;
|
2012-06-29 16:32:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief var saves the ical string that was submitted with the __construct function
|
|
|
|
*/
|
2012-06-30 14:42:36 +04:00
|
|
|
private $ical;
|
2012-06-29 16:32:07 +04:00
|
|
|
|
2012-06-30 17:17:29 +04:00
|
|
|
/*
|
|
|
|
* @brief calendar id for import
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
|
2012-06-29 16:32:07 +04:00
|
|
|
/*
|
|
|
|
* @brief var saves the percentage of the import's progress
|
|
|
|
*/
|
|
|
|
private $progress;
|
2012-06-30 14:42:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief var saves the timezone the events shell converted to
|
|
|
|
*/
|
|
|
|
private $tz;
|
2012-07-01 00:08:30 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief var saves the userid
|
|
|
|
*/
|
|
|
|
private $userid;
|
2012-06-29 16:32:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* public methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief does general initialization for import object
|
2012-06-30 14:42:36 +04:00
|
|
|
* @param string $calendar content of ical file
|
|
|
|
* @param string $tz timezone of the user
|
2012-06-29 16:32:07 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-06-30 14:42:36 +04:00
|
|
|
public function __construct($ical){
|
2012-06-29 16:32:07 +04:00
|
|
|
$this->error = null;
|
|
|
|
$this->ical = $ical;
|
2012-07-01 00:26:29 +04:00
|
|
|
$this->count = 0;
|
2012-06-29 16:32:07 +04:00
|
|
|
try{
|
|
|
|
$this->calobject = OC_VObject::parse($this->ical);
|
|
|
|
}catch(Exception $e){
|
|
|
|
//MISSING: write some log
|
|
|
|
$this->error = true;
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-30 14:42:36 +04:00
|
|
|
return true;
|
2012-06-29 16:32:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief imports a calendar
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-07-01 00:08:30 +04:00
|
|
|
public function import(){
|
|
|
|
if(!$this->isValid()){
|
2012-06-29 16:32:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-30 14:42:36 +04:00
|
|
|
foreach($this->calobject->getComponents() as $object){
|
|
|
|
if(!($object instanceof Sabre_VObject_Component_VEvent) && !($object instanceof Sabre_VObject_Component_VJournal) && !($object instanceof Sabre_VObject_Component_VTodo)){
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-01 00:45:12 +04:00
|
|
|
$dtend = OC_Calendar_Object::getDTEndFromVEvent($object);
|
2012-07-01 00:08:30 +04:00
|
|
|
$object->DTSTART->getDateTime()->setTimezone(new DateTimeZone($this->tz));
|
2012-07-01 00:45:12 +04:00
|
|
|
$object->DTEND->setDateTime($dtend->getDateTime(), $object->DTSTART->getDateType());
|
2012-07-01 00:08:30 +04:00
|
|
|
$object->DTEND->getDateTime()->setTimezone(new DateTimeZone($this->tz));
|
|
|
|
$vcalendar = $this->createVCalendar($object->serialize());
|
|
|
|
OC_Calendar_Object::add($this->id, $vcalendar);
|
2012-07-01 00:26:29 +04:00
|
|
|
$this->count++;
|
2012-06-30 14:42:36 +04:00
|
|
|
}
|
2012-06-30 17:17:29 +04:00
|
|
|
return true;
|
2012-06-30 14:42:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief sets the timezone
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function setTimeZone($tz){
|
|
|
|
$this->tz = $tz;
|
|
|
|
return true;
|
2012-06-29 16:32:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief checks if something went wrong while initialization
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isValid(){
|
|
|
|
if(is_null($this->error)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief returns the percentage of progress
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getProgress(){
|
|
|
|
return $this->progress;
|
|
|
|
}
|
2012-06-30 14:42:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief enables the cache for the percentage of progress
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function enableProgressCache(){
|
|
|
|
$this->cacheprogress = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief disables the cache for the percentage of progress
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function disableProgressCache(){
|
|
|
|
$this->cacheprogress = false;
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-30 17:17:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief generates a new calendar name
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-07-01 00:08:30 +04:00
|
|
|
public function createCalendarName(){
|
|
|
|
$calendars = OC_Calendar_Calendar::allCalendars($this->userid);
|
2012-06-30 17:17:29 +04:00
|
|
|
$calendarname = $guessedcalendarname = !is_null($this->guessCalendarName())?($this->guessCalendarName()):(OC_Calendar_App::$l10n->t('New Calendar'));
|
|
|
|
$i = 1;
|
2012-07-01 00:08:30 +04:00
|
|
|
while(!OC_Calendar_Calendar::isCalendarNameavailable($calendarname, $this->userid)){
|
2012-06-30 17:17:29 +04:00
|
|
|
$calendarname = $guessedcalendarname . ' (' . $i . ')';
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return $calendarname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief generates a new calendar color
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-07-01 00:08:30 +04:00
|
|
|
public function createCalendarColor(){
|
|
|
|
if(is_null($this->guessCalendarColor())){
|
2012-06-30 17:17:29 +04:00
|
|
|
return '#9fc6e7';
|
|
|
|
}
|
|
|
|
return $this->guessCalendarColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief sets the id for the calendar
|
|
|
|
* @param integer $id of the calendar
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-07-01 00:08:30 +04:00
|
|
|
public function setCalendarID($id){
|
2012-06-30 17:17:29 +04:00
|
|
|
$this->id = $id;
|
2012-07-01 00:08:30 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief sets the userid to import the calendar
|
|
|
|
* @param string $id of the user
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function setUserID($userid){
|
|
|
|
$this->userid = $userid;
|
|
|
|
return true;
|
2012-06-30 17:17:29 +04:00
|
|
|
}
|
2012-07-01 00:26:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief returns the private
|
|
|
|
* @param string $id of the user
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getCount(){
|
|
|
|
return $this->count;
|
|
|
|
}
|
2012-06-29 16:32:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* private methods
|
|
|
|
*/
|
|
|
|
|
2012-06-30 14:42:36 +04:00
|
|
|
/*
|
|
|
|
* @brief generates an unique ID
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function createUID(){
|
|
|
|
return substr(md5(rand().time()),0,10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief checks is the UID is already in use for another event
|
|
|
|
* @param string $uid uid to check
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function isUIDAvailable($uid){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief generates a proper VCalendar string
|
|
|
|
* @param string $vobject
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function createVCalendar($vobject){
|
|
|
|
if(is_object($vobject)){
|
|
|
|
$vobject = @$vobject->serialize();
|
|
|
|
}
|
|
|
|
$vcalendar = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\n";
|
|
|
|
$vcalendar .= $vobject;
|
|
|
|
$vcalendar .= "END:VCALENDAR";
|
|
|
|
return $vcalendar;
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:32:07 +04:00
|
|
|
/*
|
2012-07-01 00:08:30 +04:00
|
|
|
* @brief checks if an event already exists in the user's calendars
|
|
|
|
* @return boolean
|
2012-06-29 16:32:07 +04:00
|
|
|
*/
|
2012-07-01 00:08:30 +04:00
|
|
|
private function isDuplicate(){
|
2012-06-29 16:32:07 +04:00
|
|
|
|
2012-07-01 00:08:30 +04:00
|
|
|
}
|
2012-06-29 16:32:07 +04:00
|
|
|
|
2012-06-30 14:42:36 +04:00
|
|
|
/*
|
|
|
|
* @brief
|
|
|
|
* @return
|
|
|
|
*/
|
2012-06-30 17:17:29 +04:00
|
|
|
//private function (){
|
2012-06-30 14:42:36 +04:00
|
|
|
|
2012-06-30 17:17:29 +04:00
|
|
|
//}
|
2012-06-29 16:32:07 +04:00
|
|
|
|
|
|
|
/*
|
2012-07-01 00:08:30 +04:00
|
|
|
* private methods for (pre)rendering of X-... Attributes
|
2012-06-29 16:32:07 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief guesses the calendar color
|
|
|
|
* @return mixed - string or boolean
|
|
|
|
*/
|
2012-06-30 17:17:29 +04:00
|
|
|
private function guessCalendarColor(){
|
2012-06-29 16:32:07 +04:00
|
|
|
if(!is_null($this->calobject->__get('X-APPLE-CALENDAR-COLOR'))){
|
|
|
|
return $this->calobject->__get('X-APPLE-CALENDAR-COLOR');
|
|
|
|
}
|
2012-06-30 17:17:29 +04:00
|
|
|
return null;
|
2012-06-29 16:32:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief guesses the calendar description
|
|
|
|
* @return mixed - string or boolean
|
|
|
|
*/
|
2012-06-30 17:17:29 +04:00
|
|
|
private function guessCalendarDescription(){
|
2012-06-29 16:32:07 +04:00
|
|
|
if(!is_null($this->calobject->__get('X-WR-CALDESC'))){
|
|
|
|
return $this->calobject->__get('X-WR-CALDESC');
|
|
|
|
}
|
2012-06-30 17:17:29 +04:00
|
|
|
return null;
|
2012-06-29 16:32:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief guesses the calendar name
|
|
|
|
* @return mixed - string or boolean
|
|
|
|
*/
|
2012-06-30 17:17:29 +04:00
|
|
|
private function guessCalendarName(){
|
2012-06-29 16:32:07 +04:00
|
|
|
if(!is_null($this->calobject->__get('X-WR-CALNAME'))){
|
|
|
|
return $this->calobject->__get('X-WR-CALNAME');
|
|
|
|
}
|
2012-06-30 17:17:29 +04:00
|
|
|
return null;
|
2012-06-29 16:32:07 +04:00
|
|
|
}
|
|
|
|
}
|