summaryrefslogtreecommitdiffstats
path: root/apps/calendar
diff options
context:
space:
mode:
authorGeorg Ehrke <dev@georgswebsite.de>2012-06-30 15:17:29 +0200
committerGeorg Ehrke <dev@georgswebsite.de>2012-06-30 15:17:29 +0200
commit390f5cc89fc956b021c0566534ca969ed0377f11 (patch)
treea2ee18958144b77d86e0a1278d67bef341bfa2f4 /apps/calendar
parent433d15d309b0bade290f028b1365eff77f8dd0a8 (diff)
downloadnextcloud-server-390f5cc89fc956b021c0566534ca969ed0377f11.tar.gz
nextcloud-server-390f5cc89fc956b021c0566534ca969ed0377f11.zip
some work on (drop)import)
Diffstat (limited to 'apps/calendar')
-rw-r--r--apps/calendar/ajax/import/dropimport.php81
-rw-r--r--apps/calendar/appinfo/app.php1
-rw-r--r--apps/calendar/js/calendar.js13
-rw-r--r--apps/calendar/lib/calendar.php16
-rw-r--r--apps/calendar/lib/import.php69
5 files changed, 94 insertions, 86 deletions
diff --git a/apps/calendar/ajax/import/dropimport.php b/apps/calendar/ajax/import/dropimport.php
index 87667d4de68..41dfd7d9346 100644
--- a/apps/calendar/ajax/import/dropimport.php
+++ b/apps/calendar/ajax/import/dropimport.php
@@ -1,73 +1,26 @@
<?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.
+ */
$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 3c8cc76133e..b9357d574ea 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 e17f88e38be..251522802a4 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 5274397c763..c73e11224d3 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 864fc02fdf5..d23a55a6209 100644
--- a/apps/calendar/lib/import.php
+++ b/apps/calendar/lib/import.php
@@ -30,6 +30,11 @@ class OC_Calendar_Import{
private $ical;
/*
+ * @brief calendar id for import
+ */
+ private $id;
+
+ /*
* @brief var saves the percentage of the import's progress
*/
private $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;
}
}