From 08e707d25821fd6111ed8f8b91aa5f77ee1b490f Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Fri, 29 Jun 2012 14:32:07 +0200 Subject: [PATCH 01/96] some work on OC_Calendar_Import --- apps/calendar/lib/import.php | 139 +++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 apps/calendar/lib/import.php diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php new file mode 100644 index 0000000000..e64a9e91eb --- /dev/null +++ b/apps/calendar/lib/import.php @@ -0,0 +1,139 @@ + + * 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{ + /* + * @brief var to check if errors happend while initialization + */ + private $error; + + /* + * @brief Sabre_VObject_Component_VCalendar object - for documentation see http://code.google.com/p/sabredav/wiki/Sabre_VObject_Component_VCalendar + */ + private $calobject; + + /* + * @brief var saves the ical string that was submitted with the __construct function + */ + private $ical; + + /* + * @brief var saves the ical string that was submitted with the __construct function + */ + private $tz; + + /* + * @brief var saves the percentage of the import's progress + */ + private $progress; + + /* + * public methods + */ + + /* + * @brief does general initialization for import object + * @param string $calendar + * @return boolean + */ + public function __construct($ical, $tz){ + $this->error = null; + $this->ical = $ical; + try{ + $this->calobject = OC_VObject::parse($this->ical); + }catch(Exception $e){ + //MISSING: write some log + $this->error = true; + return false; + } + $this->tz = $tz; + } + + /* + * @brief imports a calendar + * @param string $force force import even though calendar is not valid + * @return boolean + */ + public function import($force = false){ + if(!$this->isValid() && !$force){ + return false; + } + + } + + /* + * @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; + } + + /* + * private methods + */ + + /* + * @brief + * @return + */ + private function (){ + + } + + + /* + * methods for X-... + */ + + /* + * @brief guesses the calendar color + * @return mixed - string or boolean + */ + private function guessCalendarColor(){ + if(!is_null($this->calobject->__get('X-APPLE-CALENDAR-COLOR'))){ + return $this->calobject->__get('X-APPLE-CALENDAR-COLOR'); + } + return false; + } + + /* + * @brief guesses the calendar description + * @return mixed - string or boolean + */ + private function guessCalendarDescription(){ + if(!is_null($this->calobject->__get('X-WR-CALDESC'))){ + return $this->calobject->__get('X-WR-CALDESC'); + } + return false; + } + + /* + * @brief guesses the calendar name + * @return mixed - string or boolean + */ + private function guessCalendarName(){ + if(!is_null($this->calobject->__get('X-WR-CALNAME'))){ + return $this->calobject->__get('X-WR-CALNAME'); + } + return false; + } +} From 34d4eb8edd4b485f65e50fbd4c66f852268f1dab Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sat, 30 Jun 2012 12:42:36 +0200 Subject: [PATCH 02/96] some more work on calendar import class --- apps/calendar/lib/import.php | 107 ++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 15 deletions(-) diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index e64a9e91eb..864fc02fdf 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -10,29 +10,34 @@ */ class OC_Calendar_Import{ /* - * @brief var to check if errors happend while initialization + * @brief var saves if the percentage should be saved with OC_Cache */ - private $error; + private $cacheprogress; /* * @brief Sabre_VObject_Component_VCalendar object - for documentation see http://code.google.com/p/sabredav/wiki/Sabre_VObject_Component_VCalendar */ private $calobject; + /* + * @brief var to check if errors happend while initialization + */ + private $error; + /* * @brief var saves the ical string that was submitted with the __construct function */ private $ical; - /* - * @brief var saves the ical string that was submitted with the __construct function - */ - private $tz; - /* * @brief var saves the percentage of the import's progress */ private $progress; + + /* + * @brief var saves the timezone the events shell converted to + */ + private $tz; /* * public methods @@ -40,10 +45,11 @@ class OC_Calendar_Import{ /* * @brief does general initialization for import object - * @param string $calendar + * @param string $calendar content of ical file + * @param string $tz timezone of the user * @return boolean */ - public function __construct($ical, $tz){ + public function __construct($ical){ $this->error = null; $this->ical = $ical; try{ @@ -53,7 +59,7 @@ class OC_Calendar_Import{ $this->error = true; return false; } - $this->tz = $tz; + return true; } /* @@ -65,7 +71,21 @@ class OC_Calendar_Import{ if(!$this->isValid() && !$force){ return false; } - + 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; + } + + } + } + + /* + * @brief sets the timezone + * @return boolean + */ + public function setTimeZone($tz){ + $this->tz = $tz; + return true; } /* @@ -86,11 +106,61 @@ class OC_Calendar_Import{ public function getProgress(){ return $this->progress; } + + /* + * @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; + } /* * private methods */ + /* + * @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; + } + /* * @brief * @return @@ -99,16 +169,23 @@ class OC_Calendar_Import{ } + /* + * @brief + * @return + */ + private function (){ + + } /* - * methods for X-... + * public methods for prerendering of X-... Attributes */ /* * @brief guesses the calendar color * @return mixed - string or boolean */ - private function guessCalendarColor(){ + public function guessCalendarColor(){ if(!is_null($this->calobject->__get('X-APPLE-CALENDAR-COLOR'))){ return $this->calobject->__get('X-APPLE-CALENDAR-COLOR'); } @@ -119,7 +196,7 @@ class OC_Calendar_Import{ * @brief guesses the calendar description * @return mixed - string or boolean */ - private function guessCalendarDescription(){ + public function guessCalendarDescription(){ if(!is_null($this->calobject->__get('X-WR-CALDESC'))){ return $this->calobject->__get('X-WR-CALDESC'); } @@ -130,7 +207,7 @@ class OC_Calendar_Import{ * @brief guesses the calendar name * @return mixed - string or boolean */ - private function guessCalendarName(){ + public function guessCalendarName(){ if(!is_null($this->calobject->__get('X-WR-CALNAME'))){ return $this->calobject->__get('X-WR-CALNAME'); } From 390f5cc89fc956b021c0566534ca969ed0377f11 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sat, 30 Jun 2012 15:17:29 +0200 Subject: [PATCH 03/96] some work on (drop)import) --- apps/calendar/ajax/import/dropimport.php | 81 +++++------------------- apps/calendar/appinfo/app.php | 1 + apps/calendar/js/calendar.js | 13 +--- apps/calendar/lib/calendar.php | 16 +++++ apps/calendar/lib/import.php | 69 ++++++++++++++++---- 5 files changed, 94 insertions(+), 86 deletions(-) diff --git a/apps/calendar/ajax/import/dropimport.php b/apps/calendar/ajax/import/dropimport.php index 87667d4de6..41dfd7d934 100644 --- a/apps/calendar/ajax/import/dropimport.php +++ b/apps/calendar/ajax/import/dropimport.php @@ -1,73 +1,26 @@ + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ $data = $_POST['data']; $data = explode(',', $data); $data = end($data); $data = base64_decode($data); OCP\JSON::checkLoggedIn(); OCP\App::checkAppEnabled('calendar'); -$nl="\r\n"; -$comps = array('VEVENT'=>true, 'VTODO'=>true, 'VJOURNAL'=>true); -$data = str_replace(array("\r","\n\n"), array("\n","\n"), $data); -$lines = explode("\n", $data); -unset($data); -$comp=$uid=$cal=false; -$cals=$uids=array(); -$i = 0; -foreach($lines as $line) { - if(strpos($line, ':')!==false) { - list($attr, $val) = explode(':', strtoupper($line)); - if ($attr == 'BEGIN' && $val == 'VCALENDAR') { - $cal = $i; - $cals[$cal] = array('first'=>$i,'last'=>$i,'end'=>$i); - } elseif ($attr =='BEGIN' && $cal!==false && isset($comps[$val])) { - $comp = $val; - $beginNo = $i; - } elseif ($attr == 'END' && $cal!==false && $val == 'VCALENDAR') { - if($comp!==false) { - unset($cals[$cal]); // corrupt calendar, unset it - } else { - $cals[$cal]['end'] = $i; - } - $comp=$uid=$cal=false; // reset calendar - } elseif ($attr == 'END' && $comp!==false && $val == $comp) { - if(! $uid) { - $uid = OC_Calendar_Object::createUID(); - } - $uids[$uid][$beginNo] = array('end'=>$i, 'cal'=>$cal); - if ($cals[$cal]['first'] == $cal) { - $cals[$cal]['first'] = $beginNo; - } - $cals[$cal]['last'] = $i; - $comp=$uid=false; // reset component - } elseif ($attr =="UID" && $comp!==false) { - list($attr, $uid) = explode(':', $line); - } - } - $i++; -} -$calendars = OC_Calendar_Calendar::allCalendars(OCP\USER::getUser(), true); -$id = $calendars[0]['id']; -foreach($uids as $uid) { - $prefix=$suffix=$content=array(); - foreach($uid as $begin=>$details) { - $cal = $details['cal']; - if(!isset($cals[$cal])) { - continue; // from corrupt/incomplete calendar - } - $cdata = $cals[$cal]; - // if we have multiple components from different calendar objects, - // we should really merge their elements (enhancement?) -- 1st one wins for now. - if(! count($prefix)) { - $prefix = array_slice($lines, $cal, $cdata['first'] - $cal); - } - if(! count($suffix)) { - $suffix = array_slice($lines, $cdata['last']+1, $cdata['end'] - $cdata['last']); - } - $content = array_merge($content, array_slice($lines, $begin, $details['end'] - $begin + 1)); - } - if(count($content)) { - $import = join($nl, array_merge($prefix, $content, $suffix)) . $nl; - OC_Calendar_Object::add($id, $import); - } +$import = new OC_Calendar_Import($data); +$import->setTimeZone(OC_Calendar_App::$tz); +$import->disableProgressCache(); +if(!$import->isValid()){ + OCP\JSON::error(); + exit; } +$calendarname = $import->createCalendarName(OCP\User::getUser()); +$calendarcolor = $import->createCalendarColor(); +$newid = OC_Calendar_Calendar::addCalendar(OCP\User::getUser(),strip_tags($calendarname),'VEVENT,VTODO,VJOURNAL',null,0,$calendarcolor); +$import->setCalendarID($newid); +$import->import(); OCP\JSON::success(); \ No newline at end of file diff --git a/apps/calendar/appinfo/app.php b/apps/calendar/appinfo/app.php index 3c8cc76133..b9357d574e 100644 --- a/apps/calendar/appinfo/app.php +++ b/apps/calendar/appinfo/app.php @@ -9,6 +9,7 @@ OC::$CLASSPATH['OC_Calendar_Repeat'] = 'apps/calendar/lib/repeat.php'; OC::$CLASSPATH['OC_Calendar_Share'] = 'apps/calendar/lib/share.php'; OC::$CLASSPATH['OC_Search_Provider_Calendar'] = 'apps/calendar/lib/search.php'; OC::$CLASSPATH['OC_Calendar_Export'] = 'apps/calendar/lib/export.php'; +OC::$CLASSPATH['OC_Calendar_Import'] = 'apps/calendar/lib/import.php'; //General Hooks OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OC_Calendar_Hooks', 'deleteUser'); //Repeating Events Hooks diff --git a/apps/calendar/js/calendar.js b/apps/calendar/js/calendar.js index e17f88e38b..251522802a 100644 --- a/apps/calendar/js/calendar.js +++ b/apps/calendar/js/calendar.js @@ -622,18 +622,11 @@ Calendar={ drop:function(e){ var files = e.dataTransfer.files; for(var i = 0;i < files.length;i++){ - var file = files[i] + var file = files[i]; reader = new FileReader(); reader.onload = function(event){ - if(file.type != 'text/calendar'){ - $('#notification').html('At least one file don\'t seems to be a calendar file. File skipped.'); - $('#notification').slideDown(); - window.setTimeout(function(){$('#notification').slideUp();}, 5000); - return false; - }else{ - Calendar.UI.Drop.import(event.target.result); - $('#calendar_holder').fullCalendar('refetchEvents'); - } + Calendar.UI.Drop.import(event.target.result); + $('#calendar_holder').fullCalendar('refetchEvents'); } reader.readAsDataURL(file); } diff --git a/apps/calendar/lib/calendar.php b/apps/calendar/lib/calendar.php index 5274397c76..c73e11224d 100644 --- a/apps/calendar/lib/calendar.php +++ b/apps/calendar/lib/calendar.php @@ -271,4 +271,20 @@ class OC_Calendar_Calendar{ 'cache' => true, ); } + + /* + * @brief checks if a calendar name is available for a user + * @param string $calendarname + * @param string $userid + * @return boolean + */ + public static function isCalendarNameavailable($calendarname, $userid){ + $calendars = self::allCalendars($userid); + foreach($calendars as $calendar){ + if($calendar['displayname'] == $calendarname){ + return false; + } + } + return true; + } } diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index 864fc02fdf..d23a55a620 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -29,6 +29,11 @@ class OC_Calendar_Import{ */ private $ical; + /* + * @brief calendar id for import + */ + private $id; + /* * @brief var saves the percentage of the import's progress */ @@ -64,7 +69,7 @@ class OC_Calendar_Import{ /* * @brief imports a calendar - * @param string $force force import even though calendar is not valid + * @param boolean $force force import even though calendar is not valid * @return boolean */ public function import($force = false){ @@ -76,7 +81,10 @@ class OC_Calendar_Import{ continue; } + + } + return true; } /* @@ -124,6 +132,43 @@ class OC_Calendar_Import{ $this->cacheprogress = false; return false; } + + /* + * @brief generates a new calendar name + * @param string $userid + * @return string + */ + public function createCalendarName($userid){ + $calendars = OC_Calendar_Calendar::allCalendars($userid); + $calendarname = $guessedcalendarname = !is_null($this->guessCalendarName())?($this->guessCalendarName()):(OC_Calendar_App::$l10n->t('New Calendar')); + $i = 1; + while(!OC_Calendar_Calendar::isCalendarNameavailable($calendarname, $userid)){ + $calendarname = $guessedcalendarname . ' (' . $i . ')'; + $i++; + } + return $calendarname; + } + + /* + * @brief generates a new calendar color + * @return string + */ + public function createCalendarColor($userid){ + if(is_null($this->guessCalendarColor()))){ + return '#9fc6e7'; + } + return $this->guessCalendarColor(); + } + + /* + * @brief sets the id for the calendar + * @param integer $id of the calendar + * @return boolean + */ + public static function setCalendarID($id){ + $this->id = $id; + return true: + } /* * private methods @@ -165,52 +210,52 @@ class OC_Calendar_Import{ * @brief * @return */ - private function (){ + //private function (){ - } + //} /* * @brief * @return */ - private function (){ + //private function (){ - } + //} /* - * public methods for prerendering of X-... Attributes + * private methods for prerendering of X-... Attributes */ /* * @brief guesses the calendar color * @return mixed - string or boolean */ - public function guessCalendarColor(){ + private function guessCalendarColor(){ if(!is_null($this->calobject->__get('X-APPLE-CALENDAR-COLOR'))){ return $this->calobject->__get('X-APPLE-CALENDAR-COLOR'); } - return false; + return null; } /* * @brief guesses the calendar description * @return mixed - string or boolean */ - public function guessCalendarDescription(){ + private function guessCalendarDescription(){ if(!is_null($this->calobject->__get('X-WR-CALDESC'))){ return $this->calobject->__get('X-WR-CALDESC'); } - return false; + return null; } /* * @brief guesses the calendar name * @return mixed - string or boolean */ - public function guessCalendarName(){ + private function guessCalendarName(){ if(!is_null($this->calobject->__get('X-WR-CALNAME'))){ return $this->calobject->__get('X-WR-CALNAME'); } - return false; + return null; } } From 376431fa0d20f6fade8d4e9cb7b1240e3e6eaf34 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sat, 30 Jun 2012 22:08:30 +0200 Subject: [PATCH 04/96] import works now for dropimport --- apps/calendar/ajax/import/dropimport.php | 5 +-- apps/calendar/lib/import.php | 53 +++++++++++++++--------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/apps/calendar/ajax/import/dropimport.php b/apps/calendar/ajax/import/dropimport.php index 41dfd7d934..25b404787f 100644 --- a/apps/calendar/ajax/import/dropimport.php +++ b/apps/calendar/ajax/import/dropimport.php @@ -12,15 +12,14 @@ $data = base64_decode($data); OCP\JSON::checkLoggedIn(); OCP\App::checkAppEnabled('calendar'); $import = new OC_Calendar_Import($data); +$import->setUserID(OCP\User::getUser()); $import->setTimeZone(OC_Calendar_App::$tz); $import->disableProgressCache(); if(!$import->isValid()){ OCP\JSON::error(); exit; } -$calendarname = $import->createCalendarName(OCP\User::getUser()); -$calendarcolor = $import->createCalendarColor(); -$newid = OC_Calendar_Calendar::addCalendar(OCP\User::getUser(),strip_tags($calendarname),'VEVENT,VTODO,VJOURNAL',null,0,$calendarcolor); +$newid = OC_Calendar_Calendar::addCalendar(OCP\User::getUser(),strip_tags($import->createCalendarName()),'VEVENT,VTODO,VJOURNAL',null,0,$import->createCalendarColor()); $import->setCalendarID($newid); $import->import(); OCP\JSON::success(); \ No newline at end of file diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index d23a55a620..969725f527 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -43,6 +43,11 @@ class OC_Calendar_Import{ * @brief var saves the timezone the events shell converted to */ private $tz; + + /* + * @brief var saves the userid + */ + private $userid; /* * public methods @@ -69,20 +74,21 @@ class OC_Calendar_Import{ /* * @brief imports a calendar - * @param boolean $force force import even though calendar is not valid * @return boolean */ - public function import($force = false){ - if(!$this->isValid() && !$force){ + public function import(){ + if(!$this->isValid()){ return false; } 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; } - - - + $object->DTSTART->getDateTime()->setTimezone(new DateTimeZone($this->tz)); + $object->DTEND = OC_Calendar_Object::getDTEndFromVEvent($object); + $object->DTEND->getDateTime()->setTimezone(new DateTimeZone($this->tz)); + $vcalendar = $this->createVCalendar($object->serialize()); + OC_Calendar_Object::add($this->id, $vcalendar); } return true; } @@ -135,14 +141,13 @@ class OC_Calendar_Import{ /* * @brief generates a new calendar name - * @param string $userid * @return string */ - public function createCalendarName($userid){ - $calendars = OC_Calendar_Calendar::allCalendars($userid); + public function createCalendarName(){ + $calendars = OC_Calendar_Calendar::allCalendars($this->userid); $calendarname = $guessedcalendarname = !is_null($this->guessCalendarName())?($this->guessCalendarName()):(OC_Calendar_App::$l10n->t('New Calendar')); $i = 1; - while(!OC_Calendar_Calendar::isCalendarNameavailable($calendarname, $userid)){ + while(!OC_Calendar_Calendar::isCalendarNameavailable($calendarname, $this->userid)){ $calendarname = $guessedcalendarname . ' (' . $i . ')'; $i++; } @@ -153,8 +158,8 @@ class OC_Calendar_Import{ * @brief generates a new calendar color * @return string */ - public function createCalendarColor($userid){ - if(is_null($this->guessCalendarColor()))){ + public function createCalendarColor(){ + if(is_null($this->guessCalendarColor())){ return '#9fc6e7'; } return $this->guessCalendarColor(); @@ -165,9 +170,19 @@ class OC_Calendar_Import{ * @param integer $id of the calendar * @return boolean */ - public static function setCalendarID($id){ + public function setCalendarID($id){ $this->id = $id; - return true: + 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; } /* @@ -207,12 +222,12 @@ class OC_Calendar_Import{ } /* - * @brief - * @return + * @brief checks if an event already exists in the user's calendars + * @return boolean */ - //private function (){ + private function isDuplicate(){ - //} + } /* * @brief @@ -223,7 +238,7 @@ class OC_Calendar_Import{ //} /* - * private methods for prerendering of X-... Attributes + * private methods for (pre)rendering of X-... Attributes */ /* From f15941a967669c9a7e1e1c391b55e61a39e44484 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sat, 30 Jun 2012 22:26:29 +0200 Subject: [PATCH 05/96] improve dropimport --- apps/calendar/ajax/import/dropimport.php | 5 +++-- apps/calendar/js/calendar.js | 4 ++++ apps/calendar/lib/import.php | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/apps/calendar/ajax/import/dropimport.php b/apps/calendar/ajax/import/dropimport.php index 25b404787f..a3bef1ac29 100644 --- a/apps/calendar/ajax/import/dropimport.php +++ b/apps/calendar/ajax/import/dropimport.php @@ -19,7 +19,8 @@ if(!$import->isValid()){ OCP\JSON::error(); exit; } -$newid = OC_Calendar_Calendar::addCalendar(OCP\User::getUser(),strip_tags($import->createCalendarName()),'VEVENT,VTODO,VJOURNAL',null,0,$import->createCalendarColor()); +$newcalendarname = strip_tags($import->createCalendarName()); +$newid = OC_Calendar_Calendar::addCalendar(OCP\User::getUser(),$newcalendarname,'VEVENT,VTODO,VJOURNAL',null,0,$import->createCalendarColor()); $import->setCalendarID($newid); $import->import(); -OCP\JSON::success(); \ No newline at end of file +OCP\JSON::success(array('newcalendarname'=>$newcalendarname, 'count'=>$import->getCount(), 'newcalendarid'=>$newid, 'eventSource'=>OC_Calendar_Calendar::getEventSourceInfo(OC_Calendar_Calendar::find($newid)))); \ No newline at end of file diff --git a/apps/calendar/js/calendar.js b/apps/calendar/js/calendar.js index 251522802a..0c4d44769c 100644 --- a/apps/calendar/js/calendar.js +++ b/apps/calendar/js/calendar.js @@ -634,6 +634,10 @@ Calendar={ import:function(data){ $.post(OC.filePath('calendar', 'ajax/import', 'dropimport.php'), {'data':data},function(result) { if(result.status == 'success'){ + $('#calendar_holder').fullCalendar('addEventSource', result.eventSource); + $('#notification').html(result.count + ' Events has been saved in the new calendar ' + result.newcalendarname); + $('#notification').slideDown(); + window.setTimeout(function(){$('#notification').slideUp();}, 5000); return true; }else{ $('#notification').html('ownCloud wasn\'t able to import at least one file. File skipped.'); diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index 969725f527..9076a747a4 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -19,6 +19,11 @@ class OC_Calendar_Import{ */ private $calobject; + /* + * @brief var counts the number of imported elements + */ + private $count; + /* * @brief var to check if errors happend while initialization */ @@ -62,6 +67,7 @@ class OC_Calendar_Import{ public function __construct($ical){ $this->error = null; $this->ical = $ical; + $this->count = 0; try{ $this->calobject = OC_VObject::parse($this->ical); }catch(Exception $e){ @@ -89,6 +95,7 @@ class OC_Calendar_Import{ $object->DTEND->getDateTime()->setTimezone(new DateTimeZone($this->tz)); $vcalendar = $this->createVCalendar($object->serialize()); OC_Calendar_Object::add($this->id, $vcalendar); + $this->count++; } return true; } @@ -184,6 +191,15 @@ class OC_Calendar_Import{ $this->userid = $userid; return true; } + + /* + * @brief returns the private + * @param string $id of the user + * @return boolean + */ + public function getCount(){ + return $this->count; + } /* * private methods From a705b19cd598b538d0b82d97166d5d0ba03ff835 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sat, 30 Jun 2012 22:45:12 +0200 Subject: [PATCH 06/96] little fix for import --- apps/calendar/lib/import.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index 9076a747a4..3a7af8e4bf 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -90,8 +90,9 @@ class OC_Calendar_Import{ if(!($object instanceof Sabre_VObject_Component_VEvent) && !($object instanceof Sabre_VObject_Component_VJournal) && !($object instanceof Sabre_VObject_Component_VTodo)){ continue; } + $dtend = OC_Calendar_Object::getDTEndFromVEvent($object); $object->DTSTART->getDateTime()->setTimezone(new DateTimeZone($this->tz)); - $object->DTEND = OC_Calendar_Object::getDTEndFromVEvent($object); + $object->DTEND->setDateTime($dtend->getDateTime(), $object->DTSTART->getDateType()); $object->DTEND->getDateTime()->setTimezone(new DateTimeZone($this->tz)); $vcalendar = $this->createVCalendar($object->serialize()); OC_Calendar_Object::add($this->id, $vcalendar); From ce331bd1d39192d5a062f27c2a63af581063e0b6 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sat, 30 Jun 2012 23:18:41 +0200 Subject: [PATCH 07/96] improvements for dropoimport --- apps/calendar/ajax/import/dropimport.php | 8 +++++++- apps/calendar/js/calendar.js | 4 ++-- apps/calendar/lib/import.php | 20 ++++++++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/calendar/ajax/import/dropimport.php b/apps/calendar/ajax/import/dropimport.php index a3bef1ac29..b58b3f91cc 100644 --- a/apps/calendar/ajax/import/dropimport.php +++ b/apps/calendar/ajax/import/dropimport.php @@ -23,4 +23,10 @@ $newcalendarname = strip_tags($import->createCalendarName()); $newid = OC_Calendar_Calendar::addCalendar(OCP\User::getUser(),$newcalendarname,'VEVENT,VTODO,VJOURNAL',null,0,$import->createCalendarColor()); $import->setCalendarID($newid); $import->import(); -OCP\JSON::success(array('newcalendarname'=>$newcalendarname, 'count'=>$import->getCount(), 'newcalendarid'=>$newid, 'eventSource'=>OC_Calendar_Calendar::getEventSourceInfo(OC_Calendar_Calendar::find($newid)))); \ No newline at end of file +$count = $import->getCount(); +if($count == 0){ + OC_Calendar_Calendar::deleteCalendar($newid); + OCP\JSON::error(array('message'=>OC_Calendar_App::$l10n->t('The file contained either no events or all events are already saved in your account'))); +}else{ + OCP\JSON::success(array('message'=>$count . ' ' . OC_Calendar_App::$l10n->t('events has been saved in the new calendar') . ' ' . $newcalendarname, 'eventSource'=>OC_Calendar_Calendar::getEventSourceInfo(OC_Calendar_Calendar::find($newid)))); +} \ No newline at end of file diff --git a/apps/calendar/js/calendar.js b/apps/calendar/js/calendar.js index 0c4d44769c..aaf168844e 100644 --- a/apps/calendar/js/calendar.js +++ b/apps/calendar/js/calendar.js @@ -635,12 +635,12 @@ Calendar={ $.post(OC.filePath('calendar', 'ajax/import', 'dropimport.php'), {'data':data},function(result) { if(result.status == 'success'){ $('#calendar_holder').fullCalendar('addEventSource', result.eventSource); - $('#notification').html(result.count + ' Events has been saved in the new calendar ' + result.newcalendarname); + $('#notification').html(result.message); $('#notification').slideDown(); window.setTimeout(function(){$('#notification').slideUp();}, 5000); return true; }else{ - $('#notification').html('ownCloud wasn\'t able to import at least one file. File skipped.'); + $('#notification').html(result.message); $('#notification').slideDown(); window.setTimeout(function(){$('#notification').slideUp();}, 5000); } diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index 3a7af8e4bf..dccb8955af 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -95,8 +95,12 @@ class OC_Calendar_Import{ $object->DTEND->setDateTime($dtend->getDateTime(), $object->DTSTART->getDateType()); $object->DTEND->getDateTime()->setTimezone(new DateTimeZone($this->tz)); $vcalendar = $this->createVCalendar($object->serialize()); - OC_Calendar_Object::add($this->id, $vcalendar); - $this->count++; + $insertid = OC_Calendar_Object::add($this->id, $vcalendar); + if($this->isDuplicate($insertid)){ + OC_Calendar_Object::delete($insertid); + }else{ + $this->count++; + } } return true; } @@ -240,10 +244,18 @@ class OC_Calendar_Import{ /* * @brief checks if an event already exists in the user's calendars + * @param integer $insertid id of the new object * @return boolean */ - private function isDuplicate(){ - + private function isDuplicate($insertid){ + $newobject = OC_Calendar_Object::find($insertid); + $stmt = OCP\DB::prepare('SELECT COUNT(*) as count FROM *PREFIX*calendar_objects WHERE objecttype=? AND startdate=? AND enddate=? AND repeating=? AND summary=? AND calendardata=?'); + $result = $stmt->execute(array($newobject['objecttype'],$newobject['startdate'],$newobject['enddate'],$newobject['repeating'],$newobject['summary'],$newobject['calendardata'])); + $result = $result->fetchRow(); + if($result['count'] >= 2){ + return true; + } + return false; } /* From 601237a0c3056156e272598b853827f615be86a4 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 1 Jul 2012 00:09:33 +0200 Subject: [PATCH 08/96] add algorithm to generate the calendar's text color --- apps/calendar/lib/calendar.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/apps/calendar/lib/calendar.php b/apps/calendar/lib/calendar.php index 3fc51e3900..c852740596 100644 --- a/apps/calendar/lib/calendar.php +++ b/apps/calendar/lib/calendar.php @@ -267,7 +267,7 @@ class OC_Calendar_Calendar{ 'url' => OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id='.$calendar['id'], 'backgroundColor' => $calendar['calendarcolor'], 'borderColor' => '#888', - 'textColor' => 'black', + 'textColor' => self::generateTextColor($calendar['calendarcolor']), 'cache' => true, ); } @@ -287,4 +287,21 @@ class OC_Calendar_Calendar{ } return true; } + + /* + * @brief generates the text color for the calendar + * @param integer $calendarname + * @return boolean + */ + public static function generateTextColor($calendarcolor){ + if(substr_count($calendarcolor, '#') == 1){ + $calendarcolor = substr($calendarcolor,1); + } + $red = hexdec(substr($calendarcolor,0,2)); + $green = hexdec(substr($calendarcolor,2,2)); + $blue = hexdec(substr($calendarcolor,2,2)); + //recommendation by W3C + $computation = ((($red * 299) + ($green * 587) + ($blue * 114)) / 1000); + return ($computation > 130)?'black':'white'; + } } From be11f71d27e704a2d3cc4958e1dda66441659390 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 1 Jul 2012 00:31:05 +0200 Subject: [PATCH 09/96] fix doc for OC_Calendar_Calendar::generateTextColor() --- apps/calendar/lib/calendar.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/calendar/lib/calendar.php b/apps/calendar/lib/calendar.php index c852740596..6cac99ba48 100644 --- a/apps/calendar/lib/calendar.php +++ b/apps/calendar/lib/calendar.php @@ -290,7 +290,8 @@ class OC_Calendar_Calendar{ /* * @brief generates the text color for the calendar - * @param integer $calendarname + * @param string $calendarcolor rgb calendar color code in hex format (with or without the leading #) + * (this function doesn't pay attention on the alpha value of rgba color codes) * @return boolean */ public static function generateTextColor($calendarcolor){ From 6a060ecd230f5b94b94dd08fc16845303627a1ab Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 1 Jul 2012 01:02:54 +0200 Subject: [PATCH 10/96] use light grey instead of white --- apps/calendar/lib/calendar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/calendar/lib/calendar.php b/apps/calendar/lib/calendar.php index 6cac99ba48..7778242464 100644 --- a/apps/calendar/lib/calendar.php +++ b/apps/calendar/lib/calendar.php @@ -303,6 +303,6 @@ class OC_Calendar_Calendar{ $blue = hexdec(substr($calendarcolor,2,2)); //recommendation by W3C $computation = ((($red * 299) + ($green * 587) + ($blue * 114)) / 1000); - return ($computation > 130)?'black':'white'; + return ($computation > 130)?'#000000':'#FAFAFA'; } } From 803f2c2517c0874ca5fd3526e8056d3d919712f2 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 1 Jul 2012 21:36:09 +0200 Subject: [PATCH 11/96] some work on calendar import --- apps/calendar/ajax/import/import.php | 62 ++++++---- apps/calendar/js/loader.js | 151 +++++++++++++++--------- apps/calendar/lib/import.php | 8 +- apps/calendar/templates/part.import.php | 21 +++- 4 files changed, 155 insertions(+), 87 deletions(-) diff --git a/apps/calendar/ajax/import/import.php b/apps/calendar/ajax/import/import.php index 1facedfe0d..210bcb34d2 100644 --- a/apps/calendar/ajax/import/import.php +++ b/apps/calendar/ajax/import/import.php @@ -5,41 +5,56 @@ * later. * See the COPYING-README file. */ -//check for calendar rights or create new one -ob_start(); - OCP\JSON::checkLoggedIn(); OCP\App::checkAppEnabled('calendar'); session_write_close(); - -$nl="\r\n"; -$comps = array('VEVENT'=>true, 'VTODO'=>true, 'VJOURNAL'=>true); - -global $progresskey; -$progresskey = 'calendar.import-' . $_POST['progresskey']; - -if (isset($_POST['progress']) && $_POST['progress']) { - echo OC_Cache::get($progresskey); - die; -} - -function writeProgress($pct) { - global $progresskey; - OC_Cache::set($progresskey, $pct, 300); -} -writeProgress('10'); $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); +if(!$file){ + OCP\JSON::error(array('error'=>'404')); +} +$import = new OC_Calendar_Import($file); +$import->setUserID(OCP\User::getUser()); +$import->setTimeZone(OC_Calendar_App::$tz); +$import->enableProgressCache(); +if(!$import->isValid()){ + OCP\JSON::error(array('error'=>'notvalid')); + exit; +} +$newcal = false; if($_POST['method'] == 'new'){ - $id = OC_Calendar_Calendar::addCalendar(OCP\USER::getUser(), $_POST['calname']); - OC_Calendar_Calendar::setCalendarActive($id, 1); + $calendars = OC_Calendar_Calendar::allCalendars(OCP\User::getUser()); + foreach($calendars as $calendar){ + if($calendar['dispalyname'] == $_POST['calname']){ + $id = $calendar['id']; + $newcal = false; + break; + } + $newcal = true; + } + if($newcal){ + $id = OC_Calendar_Calendar::addCalendar(OCP\USER::getUser(), strip_tags($_POST['calname'],'VEVENT,VTODO,VJOURNAL',null,0,$import->createCalendarColor())); + OC_Calendar_Calendar::setCalendarActive($id, 1); + } }else{ $calendar = OC_Calendar_App::getCalendar($_POST['id']); if($calendar['userid'] != OCP\USER::getUser()){ - OCP\JSON::error(); + OCP\JSON::error(array('error'=>'missingcalendarrights')); exit(); } $id = $_POST['id']; } +$import->setCalendarID($id); +$import->import(); +$count = $import->getCount(); +if($count == 0){ + if($newcal){ + OC_Calendar_Calendar::deleteCalendar($id); + } + OCP\JSON::error(array('message'=>OC_Calendar_App::$l10n->t('The file contained either no events or all events are already saved in your account'))); +}else{ + OCP\JSON::success(array('message'=>$count . ' ' . OC_Calendar_App::$l10n->t('events has been saved in the new calendar') . ' ' . $newcalendarname)); +} +/* //////////////////////////// Attention: following code is quite painfull !!! /////////////////////// writeProgress('20'); // normalize the newlines $file = str_replace(array("\r","\n\n"), array("\n","\n"), $file); @@ -91,7 +106,6 @@ foreach($lines as $line) { // import the calendar writeProgress('60'); foreach($uids as $uid) { - $prefix=$suffix=$content=array(); foreach($uid as $begin=>$details) { diff --git a/apps/calendar/js/loader.js b/apps/calendar/js/loader.js index cef95afc3a..f54e3fa262 100644 --- a/apps/calendar/js/loader.js +++ b/apps/calendar/js/loader.js @@ -5,33 +5,102 @@ * See the COPYING-README file. */ Calendar_Import={ - importdialog: function(filename){ - var path = $('#dir').val(); - $('body').append('
'); - $('#calendar_import').load(OC.filePath('calendar', 'ajax/import', 'dialog.php'), {filename:filename, path:path}, function(){Calendar_Import.initdialog(filename);}); - }, - initdialog: function(filename){ - $('#calendar_import_dialog').dialog({ - width : 500, - close : function() { - $(this).dialog('destroy').remove(); - $('#calendar_import').remove(); - } - }); - $('#import_done_button').click(function(){ - $('#calendar_import_dialog').dialog('destroy').remove(); + Store:{ + file: '', + path: '', + id: 0, + method: '', + calname: '', + progresskey: '', + percentage: 0 + } + Dialog:{ + open: function(filename){ + Calendar_Import.Store.file = filename; + Calendar_Import.Store.path = $('#dir').val(); + $('body').append('
'); + $('#calendar_import').load(OC.filePath('calendar', 'ajax/import', 'dialog.php'), {filename:Calendar_Import.Store.file, path:Calendar_Import.Store.path},function(){ + Calendar_Import.Dialog.init(); + }); + }, + close: function(){ + $(this).dialog('destroy').remove(); $('#calendar_import').remove(); - }); - $('#progressbar').progressbar({value: 0}); - $('#startimport').click(function(){ - var filename = $('#filename').val(); - var path = $('#path').val(); - var calid = $('#calendar option:selected').val(); + }, + init: function(){ + //init dialog + $('#calendar_import_dialog').dialog({ + width : 500, + close : function() { + Calendar_Import.Dialog.close(); + } + }); + //init buttons + $('#import_done_button').click(function(){ + Calendar_Import.closedialog(); + }); + $('#startimport').click(function(){ + Calendar_import.Core.process(); + } + $('#calendar').change(function(){ + if($('#calendar option:selected').val() == 'newcal'){ + $('#newcalform').slideDown('slow'); + }else{ + $('#newcalform').slideUp('slow'); + } + }); + //init progressbar + $('#progressbar').progressbar({value: Calendar_Import.Store.percentage}); + Calendar_Import.Store.progresskey = $('#progresskey').val(); + }, + mergewarning: function(){ + + }, + update: function(){ + /*$.post(OC.filePath('calendar', 'ajax/import', 'import.php'), {progress:1,progresskey: progresskey}, function(percent){ + $('#progressbar').progressbar('option', 'value', parseInt(percent)); + if(percent < 100){ + window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500); + }else{ + $('#import_done').css('display', 'block'); + } + });*/ + return 0; + }, + warning: function(validation){ + + } + }, + Core:{ + process: function(){ + var validation = Calendar.Core.prepare(); + if(validation){ + $('#newcalendar').attr('readonly', 'readonly'); + $('#calendar').attr('disabled', 'disabled'); + Calendar.Core.send(); + }else{ + Calendar.Dialog.warning(validation); + } + }, + send: function(){ + $.post(OC.filePath('calendar', 'ajax/import', 'import.php'), + {progresskey: Calendar_Import.Store.progresskey, method: String (Calendar_Import.Store.method), calname: String (Calendar_Import.Store.calname), path: String (Calendar_Import.Store.path), file: String (Calendar_Import.Store.filename), id: String (Calendar_Import.Store.calid)}, function(data){ + if(data.status == 'success'){ + $('#progressbar').progressbar('option', 'value', 100); + $('#import_done').css('display', 'block'); + $('#status').html(data.message); + } + }); + $('#form_container').css('display', 'none'); + $('#progressbar_container').css('display', 'block'); + window.setTimeout('Calendar_Import.Dialog.update', 500); + }, + prepare: function(){ + Calendar_Import.Store.id = $('#calendar option:selected').val(); if($('#calendar option:selected').val() == 'newcal'){ - var method = 'new'; - var calname = $('#newcalendar').val(); - var calname = $.trim(calname); - if(calname == ''){ + Calendar_Import.Store.method = 'new'; + Calendar_Import.Store.calname = $.trim($('#newcalendar').val()); + if(Calendar_Import.Store.calname == ''){ $('#newcalendar').css('background-color', '#FF2626'); $('#newcalendar').focus(function(){ $('#newcalendar').css('background-color', '#F8F8F8'); @@ -41,36 +110,8 @@ Calendar_Import={ }else{ var method = 'old'; } - $('#newcalendar').attr('readonly', 'readonly'); - $('#calendar').attr('disabled', 'disabled'); - var progresskey = $('#progresskey').val(); - $.post(OC.filePath('calendar', 'ajax/import', 'import.php'), {progresskey: progresskey, method: String (method), calname: String (calname), path: String (path), file: String (filename), id: String (calid)}, function(data){ - if(data.status == 'success'){ - $('#progressbar').progressbar('option', 'value', 100); - $('#import_done').css('display', 'block'); - } - }); - $('#form_container').css('display', 'none'); - $('#progressbar_container').css('display', 'block'); - window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500); - }); - $('#calendar').change(function(){ - if($('#calendar option:selected').val() == 'newcal'){ - $('#newcalform').slideDown('slow'); - }else{ - $('#newcalform').slideUp('slow'); - } - }); - }, - getimportstatus: function(progresskey){ - $.post(OC.filePath('calendar', 'ajax/import', 'import.php'), {progress:1,progresskey: progresskey}, function(percent){ - $('#progressbar').progressbar('option', 'value', parseInt(percent)); - if(percent < 100){ - window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500); - }else{ - $('#import_done').css('display', 'block'); - } - }); + + } } } $(document).ready(function(){ diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index dccb8955af..92bcc31261 100644 --- a/apps/calendar/lib/import.php +++ b/apps/calendar/lib/import.php @@ -267,14 +267,14 @@ class OC_Calendar_Import{ //} /* - * private methods for (pre)rendering of X-... Attributes + * public methods for (pre)rendering of X-... Attributes */ /* * @brief guesses the calendar color * @return mixed - string or boolean */ - private function guessCalendarColor(){ + public function guessCalendarColor(){ if(!is_null($this->calobject->__get('X-APPLE-CALENDAR-COLOR'))){ return $this->calobject->__get('X-APPLE-CALENDAR-COLOR'); } @@ -285,7 +285,7 @@ class OC_Calendar_Import{ * @brief guesses the calendar description * @return mixed - string or boolean */ - private function guessCalendarDescription(){ + public function guessCalendarDescription(){ if(!is_null($this->calobject->__get('X-WR-CALDESC'))){ return $this->calobject->__get('X-WR-CALDESC'); } @@ -296,7 +296,7 @@ class OC_Calendar_Import{ * @brief guesses the calendar name * @return mixed - string or boolean */ - private function guessCalendarName(){ + public function guessCalendarName(){ if(!is_null($this->calobject->__get('X-WR-CALNAME'))){ return $this->calobject->__get('X-WR-CALNAME'); } diff --git a/apps/calendar/templates/part.import.php b/apps/calendar/templates/part.import.php index 70ff961215..2a3ca9dae9 100644 --- a/apps/calendar/templates/part.import.php +++ b/apps/calendar/templates/part.import.php @@ -1,9 +1,19 @@ -
"> +'404')); +} +$import = new OC_Calendar_Import($file); +$newcalendarname = strip_tags($import->createCalendarName()); +$guessedcalendarname = $import->guessCalendarName(); +?> +
">
-

t('Please choose the calendar'); ?>

+

t('Please choose a calendar'); ?>

-!" id="startimport"> + +!" id="startimport">