add first version of drop import

This commit is contained in:
Georg Ehrke 2012-05-13 15:05:57 +02:00
parent 9e6221b229
commit 63a1b066bc
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,74 @@
<?php
$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(), 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();
?>

View File

@ -601,6 +601,50 @@ Calendar={
}); });
/*var permissions = (this.checked) ? 1 : 0;*/ /*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_holder').fullCalendar('next');
}); });
Calendar.UI.Share.init(); Calendar.UI.Share.init();
Calendar.UI.Drop.init();
}); });