diff options
author | Georg Ehrke <dev@georgswebsite.de> | 2012-07-01 21:36:09 +0200 |
---|---|---|
committer | Georg Ehrke <dev@georgswebsite.de> | 2012-07-01 21:36:09 +0200 |
commit | 803f2c2517c0874ca5fd3526e8056d3d919712f2 (patch) | |
tree | 231d5d6a36c953fea3bc085b8e468301e0c002be /apps | |
parent | 6a060ecd230f5b94b94dd08fc16845303627a1ab (diff) | |
download | nextcloud-server-803f2c2517c0874ca5fd3526e8056d3d919712f2.tar.gz nextcloud-server-803f2c2517c0874ca5fd3526e8056d3d919712f2.zip |
some work on calendar import
Diffstat (limited to 'apps')
-rw-r--r-- | apps/calendar/ajax/import/import.php | 60 | ||||
-rw-r--r-- | apps/calendar/js/loader.js | 145 | ||||
-rw-r--r-- | apps/calendar/lib/import.php | 8 | ||||
-rw-r--r-- | apps/calendar/templates/part.import.php | 21 |
4 files changed, 151 insertions, 83 deletions
diff --git a/apps/calendar/ajax/import/import.php b/apps/calendar/ajax/import/import.php index 1facedfe0da..210bcb34d2c 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; +$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); +if(!$file){ + OCP\JSON::error(array('error'=>'404')); } - -function writeProgress($pct) { - global $progresskey; - OC_Cache::set($progresskey, $pct, 300); +$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; } -writeProgress('10'); -$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); +$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 cef95afc3aa..f54e3fa2628 100644 --- a/apps/calendar/js/loader.js +++ b/apps/calendar/js/loader.js @@ -5,72 +5,113 @@ * See the COPYING-README file. */ Calendar_Import={ - importdialog: function(filename){ - var path = $('#dir').val(); - $('body').append('<div id="calendar_import"></div>'); - $('#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('<div id="calendar_import"></div>'); + $('#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(); - if($('#calendar option:selected').val() == 'newcal'){ - var method = 'new'; - var calname = $('#newcalendar').val(); - var calname = $.trim(calname); - if(calname == ''){ - $('#newcalendar').css('background-color', '#FF2626'); - $('#newcalendar').focus(function(){ - $('#newcalendar').css('background-color', '#F8F8F8'); - }); - return false; + }, + 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{ - var method = 'old'; + Calendar.Dialog.warning(validation); } - $('#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){ + }, + 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.getimportstatus(\'' + progresskey + '\')', 500); - }); - $('#calendar').change(function(){ + window.setTimeout('Calendar_Import.Dialog.update', 500); + }, + prepare: function(){ + Calendar_Import.Store.id = $('#calendar option:selected').val(); 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); + 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'); + }); + return false; + } }else{ - $('#import_done').css('display', 'block'); + var method = 'old'; } - }); + + } } } $(document).ready(function(){ diff --git a/apps/calendar/lib/import.php b/apps/calendar/lib/import.php index dccb8955af8..92bcc312616 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 70ff9612157..2a3ca9dae92 100644 --- a/apps/calendar/templates/part.import.php +++ b/apps/calendar/templates/part.import.php @@ -1,9 +1,19 @@ -<div id="calendar_import_dialog" title="<?php echo $l->t("Import a calendar file"); ?>"> +<?php +//Prerendering for iCalendar file +$file = OC_Filesystem::file_get_contents($_['path'] . '/' . $_['filename']); +if(!$file){ + OCP\JSON::error(array('error'=>'404')); +} +$import = new OC_Calendar_Import($file); +$newcalendarname = strip_tags($import->createCalendarName()); +$guessedcalendarname = $import->guessCalendarName(); +?> +<div id="calendar_import_dialog" title="<?php echo $l->t("Import a calendar file");?>"> <div id="form_container"> <input type="hidden" id="filename" value="<?php echo $_['filename'];?>"> <input type="hidden" id="path" value="<?php echo $_['path'];?>"> <input type="hidden" id="progresskey" value="<?php echo rand() ?>"> -<p style="text-align:center;"><b><?php echo $l->t('Please choose the calendar'); ?></b></p> +<p style="text-align:center;"><b><?php echo $l->t('Please choose a calendar'); ?></b></p> <select style="width:100%;" id="calendar" name="calendar"> <?php $calendar_options = OC_Calendar_Calendar::allCalendars(OCP\USER::getUser()); @@ -15,9 +25,12 @@ echo OCP\html_select_options($calendar_options, $calendar_options[0]['id'], arra ?> </select> <div id="newcalform" style="display: none;"> - <input type="text" style="width: 97%;" placeholder="<?php echo $l->t('Name of new calendar'); ?>" id="newcalendar" name="newcalendar"> + <input type="text" style="width: 97%;" placeholder="<?php echo $l->t('Name of new calendar'); ?>" id="newcalendar" name="newcalendar" value="<?php echo $newcalendarname ?>"> +</div> +<div id="namealreadyused" style="display: none;text-align:center;"> + <span class="hint"><?php echo $l->t('A Calendar with this name already exists. If you continue anyhow, these calendars will be merged.'); ?></span> </div> -<input type="button" value="<?php echo $l->t("Import");?>!" id="startimport"> +<input type="button" value="<?php echo $l->t("Import") . ' ' . $_['filename']; ?>!" id="startimport"> </div> <div id="progressbar_container" style="display: none"> <p style="text-align:center;"><b><?php echo $l->t('Importing calendar'); ?></b></p> |