Merge branch '2015.11' into 2015.11-develop
authorPhilipp Schüle <p.schuele@metaways.de>
Mon, 14 Nov 2016 11:50:38 +0000 (12:50 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 14 Nov 2016 11:50:38 +0000 (12:50 +0100)
Change-Id: I79818d27d71703be374de72a2ffd59b3f62e4640

1  2 
tests/tine20/Addressbook/JsonTest.php
tests/tine20/Tinebase/ScheduledImportTest.php
tine20/Calendar/Frontend/Json.php
tine20/Tinebase/Controller/ScheduledImport.php
tine20/Tinebase/Model/Import.php

Simple merge
Simple merge
@@@ -67,15 -65,12 +67,15 @@@ class Tinebase_Controller_ScheduledImpo
      public function runNextScheduledImport()
      {
          if ($record = $this->_getNextScheduledImport()) {
-             return $this->_doScheduledImport($record)->toArray();
+             return $this->doScheduledImport($record)->toArray();
          }
          
 -        return NULL;
 +        return null;
      }
  
 +    /**
 +     * @return Tinebase_Model_ImportFilter
 +     */
      public function getScheduledImportFilter()
      {
          $timestampBefore = null;
          // set user who created the import job
          $importUser = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $record->user_id, 'Tinebase_Model_FullUser');
          Tinebase_Core::set(Tinebase_Core::USER, $importUser);
-         
-         $importer = $record->getOption('plugin');
-         
-         $options = array(
-             'container_id' => $record->container_id,
-             // legacy
-             'importContainerId' => $record->container_id,
-         );
-         
-         if ($record->getOption('importFileByScheduler') === true) {
-             $toImport = $this->_getFileToImport($record->source);
-         } else {
-             $toImport = array(
-                 'remoteUrl'    => $record->source,
-                 'container_id' => $record->container_id,
-                 'options'      => $record->options
-             );
-         }
-         
-         if ($toImport) {
-             try {
-                 $importer = new $importer($options);
-                 $importer->import($toImport);
-                 $record->failcount = 0;
-             } catch (Exception $e) {
-                 if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
-                     Tinebase_Core::getLogger()->notice(__METHOD__ . ' ' . __LINE__
-                         . ' Import failed.');
-                 }
-                 Tinebase_Exception::log($e);
  
-                 $record->lastfail = $e->getMessage();
-                 $record->failcount = $record->failcount + 1;
-             }
+         // handle options
+         $options = Zend_Json::decode($record->options);
+         $options['url'] = $record->source;
  
-             $record->timestamp = Tinebase_DateTime::now();
+         if (isset($options['cid']) && isset($options['ckey'])) {
+             $credentials = new Tinebase_Model_CredentialCache($options);
+             Tinebase_Auth_CredentialCache::getInstance()->getCachedCredentials($credentials);
  
-             $record = $this->update($record);
-             
-         } else {
+             $options['username'] = $credentials->username;
+             $options['password'] = $credentials->password;
+         }
+         $importer = $record->getOption('plugin');
+         $resource = $record->getOption('importFileByScheduler') ? $this->_getFileToImport($options['url']) : null;
+         try {
+             $importer = new $importer($options);
+             $importer->import($resource);
++            $record->failcount = 0;
+         } catch (Exception $e) {
              if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
-                 Tinebase_Core::getLogger()->notice(__METHOD__ . ' ' . __LINE__ . ' The source could not be loaded: "' . $record->source . '"');
+                 Tinebase_Core::getLogger()->notice(__METHOD__ . ' ' . __LINE__
+                     . ' Import failed.');
              }
 -            // TODO log failure message in import record
+             Tinebase_Exception::log($e);
++
++            $record->lastfail = $e->getMessage();
++            $record->failcount = $record->failcount + 1;
          }
-         
+         $record->timestamp = Tinebase_DateTime::now();
+         $record = $this->update($record);
          Tinebase_Core::set(Tinebase_Core::USER, $currentUser);
-         
          return $record;
      }
-     
      /**
-      * Creates a remote import for events
-      * 
-      * @param string $remoteUrl
-      * @param string $interval
-      * @param array $importOptions
-      * @throws Calendar_Exception_InvalidUrl
-      * @return Tinebase_Record_Interface
+      * add one record
+      *
+      * @param   Tinebase_Model_Import $import
+      * @param   boolean $duplicateCheck
+      * @return  Tinebase_Model_Import
+      * @throws  Tinebase_Exception_AccessDenied
       */
-     public function createRemoteImportEvent($data)
+     public function create(Tinebase_Record_Interface $import, $duplicateCheck = true)
      {
-         $possibleIntervals = array(
-             Tinebase_Model_Import::INTERVAL_DAILY,
-             Tinebase_Model_Import::INTERVAL_HOURLY,
-             Tinebase_Model_Import::INTERVAL_ONCE,
-             Tinebase_Model_Import::INTERVAL_WEEKLY
-         );
-         
-         if (! in_array($data['interval'], $possibleIntervals)) {
-             $data['interval'] = Tinebase_Model_Import::INTERVAL_ONCE;
+         // handle credentials
+         if ($import->getOption('password')) {
+             $credentialCache = Tinebase_Auth_CredentialCache::getInstance();
+             $credentials = $credentialCache->cacheCredentials(
+                 $import->getOption('username'),
+                 $import->getOption('password'),
+                 /* key */           null,
+                 /* persist */       true,
+                 /* valid until */   Tinebase_DateTime::now()->addYear(100)
+             );
+             $import->deleteOption('password');
+             $import->deleteOption('username');
+             $import->setOption('cid', $credentials->getId());
+             $import->setOption('ckey', $credentials->key);
          }
-         
-         // find container or create a new one
-         $containerId = $data['options']['container_id'];
-         
+         // options over own field
+         $containerId = $import->getOption('container_id') ?: $import->container_id;
+         // handle container
          try {
              $container = Tinebase_Container::getInstance()->getContainerById($containerId);
          } catch (Tinebase_Exception_InvalidArgument $e) {
Simple merge