From 63a1b066bcebc998bd4a6a4c8cc56f38983b0639 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 13 May 2012 15:05:57 +0200 Subject: [PATCH] add first version of drop import --- apps/calendar/ajax/import/dropimport.php | 74 ++++++++++++++++++++++++ apps/calendar/js/calendar.js | 45 ++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 apps/calendar/ajax/import/dropimport.php diff --git a/apps/calendar/ajax/import/dropimport.php b/apps/calendar/ajax/import/dropimport.php new file mode 100644 index 0000000000..e98c282ef4 --- /dev/null +++ b/apps/calendar/ajax/import/dropimport.php @@ -0,0 +1,74 @@ +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(), 1); +$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); + } +} +OCP\JSON::success(); +?> \ No newline at end of file diff --git a/apps/calendar/js/calendar.js b/apps/calendar/js/calendar.js index 80b5dd88c5..8a3f714c5f 100644 --- a/apps/calendar/js/calendar.js +++ b/apps/calendar/js/calendar.js @@ -601,6 +601,50 @@ Calendar={ }); /*var permissions = (this.checked) ? 1 : 0;*/ } + }, + Drop:{ + init:function(){ + if (typeof window.FileReader === 'undefined') { + console.log('The drop-import feature is not supported in your browser :('); + return false; + } + droparea = document.getElementById('calendar_holder'); + droparea.ondrop = function(e){ + e.preventDefault(); + Calendar.UI.Drop.drop(e); + } + console.log('Drop initialized successfully'); + }, + drop:function(e){ + var files = e.dataTransfer.files; + for(var i = 0;i < files.length;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); + } + } + reader.readAsDataURL(file); + } + $('#calendar_holder').fullCalendar('refetchEvents'); + }, + import:function(data){ + $.post(OC.filePath('calendar', 'ajax/import', 'dropimport.php'), {'data':data},function(result) { + if(result.data == 'success'){ + return true; + }else{ + $('#notification').html('ownCloud wasn\'t able to import at least one file. File skipped.'); + $('#notification').slideDown(); + window.setTimeout(function(){$('#notification').slideUp();}, 5000); + } + }); + } } } } @@ -858,4 +902,5 @@ $(document).ready(function(){ $('#calendar_holder').fullCalendar('next'); }); Calendar.UI.Share.init(); + Calendar.UI.Drop.init(); });