Merge branch '2013.10' into 2014.11
[tine20] / tine20 / Calendar / Frontend / Json.php
index 931c03d..1844288 100644 (file)
@@ -118,10 +118,153 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
             $defaultCalendarArray = array();
         }
         
-        return array(
-            // registry setting is called defaultContainer to be compatible to the other apps
-            'defaultContainer' => $defaultCalendarArray
+        $importDefinitions = $this->_getImportDefinitions();
+        
+        $registryData = array(
+            'defaultContainer'          => $defaultCalendarArray,
+            'defaultImportDefinition'   => $importDefinitions['default'],
+            'importDefinitions'         => $importDefinitions
+        );
+        
+        return $registryData;
+    }
+    
+    /**
+     * get default addressbook
+     * 
+     * @return array
+     */
+    public function getDefaultCalendar() 
+   {
+        $defaultCalendar = Calendar_Controller_Event::getInstance()->getDefaultCalendar();
+        $defaultCalendarArray = $defaultCalendar->toArray();
+        $defaultCalendarArray['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $defaultCalendar->getId())->toArray();
+        Tinebase_Core::getLogger()->notice(print_r($defaultCalendar, true));
+        return $defaultCalendarArray;
+    }
+    
+    /**
+     * import contacts
+     * 
+     * @param string $tempFileId to import
+     * @param string $definitionId
+     * @param array $importOptions
+     * @param array $clientRecordData
+     * @return array
+     */
+    public function importEvents($tempFileId, $definitionId, $importOptions, $clientRecordData = array())
+    {
+        return $this->_import($tempFileId, $definitionId, $importOptions, $clientRecordData);
+    }
+    
+    /**
+     * creates a scheduled import
+     * 
+     * @param string $remoteUrl
+     * @param string $interval
+     * @param string $importOptions
+     * @return array
+     */
+    public function importRemoteEvents($remoteUrl, $interval, $importOptions)
+    {
+        // Determine which plugin should be used to import
+        switch ($importOptions['sourceType']) {
+            case 'remote_caldav':
+                $plugin = 'Calendar_Import_CalDAV';
+                break;
+            default:
+                $plugin = 'Calendar_Import_Ical';
+        }
+
+        $credentialCache = Tinebase_Auth_CredentialCache::getInstance();
+        $credentials = $credentialCache->cacheCredentials(
+            $importOptions['username'],
+            $importOptions['password'],
+            null,
+            /* persist */       true,
+            /* valid until */   Tinebase_DateTime::now()->addYear(100)
         );
+
+        $record = Tinebase_Controller_ScheduledImport::getInstance()->createRemoteImportEvent(array(
+            'source'            => $remoteUrl,
+            'interval'          => $interval,
+            'options'           => array_replace($importOptions, array(
+                'plugin' => $plugin,
+                'importFileByScheduler' => $importOptions['sourceType'] != 'remote_caldav',
+                'cid' => $credentials->getId(),
+                'ckey' => $credentials->key
+            )),
+            'model'             => 'Calendar_Model_Event',
+            'user_id'           => Tinebase_Core::getUser()->getId(),
+            'application_id'    => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId(),
+        ));
+
+        $result = $this->_recordToJson($record);
+        Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . 'container_id:'  .  print_r($result['container_id'], true));
+
+        return $result;
+    }
+    
+    /**
+     * get addressbook import definitions
+     * 
+     * @return array
+     * 
+     * @todo generalize this
+     */
+    protected function _getImportDefinitions()
+    {
+        $filter = new Tinebase_Model_ImportExportDefinitionFilter(array(
+            array('field' => 'application_id',  'operator' => 'equals', 'value' => Tinebase_Application::getInstance()->getApplicationByName('Calendar')->getId()),
+            array('field' => 'type',            'operator' => 'equals', 'value' => 'import'),
+        ));
+        
+        $definitionConverter = new Tinebase_Convert_ImportExportDefinition_Json();
+        
+        try {
+            $importDefinitions = Tinebase_ImportExportDefinition::getInstance()->search($filter);
+            $defaultDefinition = $this->_getDefaultImportDefinition($importDefinitions);
+            $result = array(
+                'results'               => $definitionConverter->fromTine20RecordSet($importDefinitions),
+                'totalcount'            => count($importDefinitions),
+                'default'               => ($defaultDefinition) ? $definitionConverter->fromTine20Model($defaultDefinition) : array(),
+            );
+        } catch (Exception $e) {
+            Tinebase_Exception::log($e);
+            $result = array(
+                array(
+                    'results'               => array(),
+                    'totalcount'            => 0,
+                    'default'               => array(),
+                )
+            );
+        }
+        
+        return $result;
+    }
+    
+    /**
+     * get default definition
+     * 
+     * @param Tinebase_Record_RecordSet $_importDefinitions
+     * @return Tinebase_Model_ImportExportDefinition
+     * 
+     * @todo generalize this
+     */
+    protected function _getDefaultImportDefinition($_importDefinitions)
+    {
+        try {
+            $defaultDefinition = Tinebase_ImportExportDefinition::getInstance()->getByName('cal_import_ical');
+        } catch (Tinebase_Exception_NotFound $tenf) {
+            if (count($_importDefinitions) > 0) {
+                $defaultDefinition = $_importDefinitions->getFirstRecord();
+            } else {
+                Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' No import definitions found for Calendar');
+                $defaultDefinition = NULL;
+            }
+        }
+        
+        return $defaultDefinition;
     }
     
     /**
@@ -302,7 +445,7 @@ class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
         
         $iMIPMessage->preconditionsChecked = FALSE;
         $iMIPFrontend->prepareComponent($iMIPMessage);
-        $iMIPMessage->setTimezone(Tinebase_Core::get(Tinebase_Core::USERTIMEZONE));
+        $iMIPMessage->setTimezone(Tinebase_Core::getUserTimezone());
         return $iMIPMessage->toArray();
     }