0010449: allow to ignore data when importing ics
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 10 Nov 2014 21:03:25 +0000 (22:03 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 12 Nov 2014 13:29:43 +0000 (14:29 +0100)
* allows to ignore alarms, attachments, attendee, ... on ics import
* improves CLI error message if import fails

https://forge.tine20.org/mantisbt/view.php?id=10449

Change-Id: I5fd775039041937bf2b5342130f0672878351c59
Reviewed-on: http://gerrit.tine20.com/customers/1351
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
Tested-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Calendar/Import/ICalTest.php
tine20/Calendar/Convert/Event/VCalendar/Abstract.php
tine20/Calendar/Import/Ical.php

index a45795f..c3c34da 100644 (file)
@@ -128,11 +128,11 @@ class Calendar_Import_ICalTest extends Calendar_TestCase
      * @param boolean $fromString
      * @return Tinebase_Record_RecordSet
      */
-    protected function _importHelper($filename, $expectedNumber = 1, $fromString = false)
+    protected function _importHelper($filename, $expectedNumber = 1, $fromString = false, $additionalOptions = array())
     {
-        $importer = new Calendar_Import_Ical(array(
+        $importer = new Calendar_Import_Ical(array_merge($additionalOptions, array(
             'importContainerId' => $this->_testCalendar->getId(),
-        ));
+        )));
         
         if ($fromString) {
             $icalData = file_get_contents(dirname(__FILE__) . '/files/' . $filename);
@@ -222,4 +222,22 @@ class Calendar_Import_ICalTest extends Calendar_TestCase
         
         $this->assertEquals('2014-04-30 22:00:00', $firstOfMay2014->dtstart);
     }
+
+    /**
+     * @see 0010449: allow to ignore data when importing ics
+     */
+    public function testImportWithOnlyBasicData()
+    {
+        $importEvents = $this->_importHelper('exchange2007_mail_anhang.ics', 1, false, array(
+            'onlyBasicData' => true
+        ));
+        $event = $importEvents->getFirstRecord();
+        
+        Tinebase_Alarm::getInstance()->getAlarmsOfRecord('Calendar_Model_Event', $event);
+        
+        $emptyFields = array('alarms', 'attendee', 'uid');
+        foreach ($emptyFields as $field) {
+            $this->assertTrue(empty($event->alarms), 'field ' . $field . ' should be empty in event ' . print_r($event->toArray(), true));
+        }
+    }
 }
index 2dd559e..df129f2 100644 (file)
@@ -34,6 +34,26 @@ class Calendar_Convert_Event_VCalendar_Abstract extends Tinebase_Convert_VCalend
     protected $_method;
     
     /**
+     * options array
+     * @var array
+     * 
+     * TODO allow more options
+     * 
+     * current options:
+     *  - onlyBasicData (only use basic event data when converting from VCALENDAR to Tine 2.0)
+     */
+    protected $_options = array();
+    
+    /**
+     * set options
+     * @param array $options
+     */
+    public function setOptions($options)
+    {
+        $this->_options = $options;
+    }
+    
+    /**
      * convert Tinebase_Record_RecordSet to Sabre\VObject\Component
      *
      * @param  Tinebase_Record_RecordSet  $_records
@@ -665,8 +685,19 @@ class Calendar_Convert_Event_VCalendar_Abstract extends Tinebase_Convert_VCalend
         $shortenedFields = array();
         $attachments = new Tinebase_Record_RecordSet('Tinebase_Model_Tree_Node');
         $event->alarms = new Tinebase_Record_RecordSet('Tinebase_Model_Alarm');
+        $skipFieldsIfOnlyBasicData = array('ATTENDEE', 'UID', 'ORGANIZER', 'VALARM', 'ATTACH', 'CATEGORIES');
         
         foreach ($vevent->children() as $property) {
+            if (isset($this->_options['onlyBasicData'])
+                && $this->_options['onlyBasicData']
+                && in_array((string) $property->name, $skipFieldsIfOnlyBasicData))
+            {
+                if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG))
+                    Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Skipping '
+                            . $property->name . ' (using option onlyBasicData)');
+                continue;
+            }
+            
             switch ($property->name) {
                 case 'CREATED':
                 case 'DTSTAMP':
@@ -746,7 +777,6 @@ class Calendar_Convert_Event_VCalendar_Abstract extends Tinebase_Convert_VCalend
                     
                 case 'DESCRIPTION':
                 case 'LOCATION':
-                case 'UID':
                 case 'SUMMARY':
                     $key = strtolower($property->name);
                     $value = $property->getValue();
index 2d9f6e2..ec04409 100644 (file)
@@ -42,6 +42,11 @@ class Calendar_Import_Ical extends Tinebase_Import_Abstract
          * @var string
          */
         'importContainerId'     => NULL,
+        /**
+         * import only basic data (i.e. without attendee, alarms, uid, ...)
+         * @var string
+         */
+        'onlyBasicData'         => NULL,
     );
     
     /**
@@ -103,12 +108,15 @@ class Calendar_Import_Ical extends Tinebase_Import_Abstract
         }
         
         $converter = Calendar_Convert_Event_VCalendar_Factory::factory(Calendar_Convert_Event_VCalendar_Factory::CLIENT_GENERIC);
+        if (isset($this->_options['onlyBasicData'])) {
+            $converter->setOptions(array('onlyBasicData' => $this->_options['onlyBasicData']));
+        }
         
         try {
             $events = $converter->toTine20RecordSet($_resource);
         } catch (Exception $e) {
             Tinebase_Exception::log($e);
-            $isce = new Calendar_Exception_IcalParser();
+            $isce = new Calendar_Exception_IcalParser('Can not parse ics file: ' . $e->getMessage());
             $isce->setParseError($e);
             throw $isce;
         }