Merge branch 'pu/2013.10-groupimport'
authorPhilipp Schüle <p.schuele@metaways.de>
Fri, 29 Aug 2014 17:17:17 +0000 (19:17 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 29 Aug 2014 17:17:17 +0000 (19:17 +0200)
12 files changed:
tests/tine20/Admin/CliTest.php
tests/tine20/Admin/files/import_groups.csv [new file with mode: 0644]
tests/tine20/Courses/JsonTest.php
tine20/Admin/Frontend/Cli.php
tine20/Admin/Import/Group/Csv.php [new file with mode: 0644]
tine20/Admin/Import/User/Csv.php [moved from tine20/Admin/Import/Csv.php with 97% similarity]
tine20/Admin/Import/definitions/admin_group_import_csv.xml [new file with mode: 0644]
tine20/Admin/Import/definitions/admin_user_import_csv.xml
tine20/Admin/Import/definitions/admin_user_import_csv_for_emailuser.xml
tine20/Courses/Controller/Course.php
tine20/Tinebase/Frontend/Cli/Abstract.php
tine20/Tinebase/Import/Csv/Abstract.php

index 4e10540..73bbedb 100644 (file)
@@ -129,7 +129,7 @@ class Admin_CliTest extends TestCase
         $this->objects['configSemicolon'] = '<?xml version="1.0" encoding="UTF-8"?>
         <config>
             <model>Tinebase_Model_FullUser</model>
-            <plugin>Admin_Import_Csv</plugin>
+            <plugin>Admin_Import_User_Csv</plugin>
             <type>import</type>
             <headline>1</headline>
             <dryrun>0</dryrun>
@@ -161,7 +161,7 @@ class Admin_CliTest extends TestCase
         $this->objects['configEmailuser'] = '<?xml version="1.0" encoding="UTF-8"?>
         <config>
             <model>Tinebase_Model_FullUser</model>
-            <plugin>Admin_Import_Csv</plugin>
+            <plugin>Admin_Import_User_Csv</plugin>
             <type>import</type>
             <headline>1</headline>
             <dryrun>0</dryrun>
@@ -303,7 +303,7 @@ class Admin_CliTest extends TestCase
                 'name'              => $_definition,
                 'type'              => 'import',
                 'model'             => 'Tinebase_Model_FullUser',
-                'plugin'            => 'Admin_Import_Csv',
+                'plugin'            => 'Admin_Import_User_Csv',
                 'plugin_options'    => $_config
             )));
         }
@@ -444,4 +444,33 @@ class Admin_CliTest extends TestCase
         }
         unlink("test2.csv");
     }
+    
+    /**
+     * tests if import with members from csv works correctly
+     */
+    public function testImportGroups()
+    {
+        $opts = new Zend_Console_Getopt('abp:');
+        $opts->setArguments(array(dirname(__FILE__) . '/files/import_groups.csv', 'definition=admin_group_import_csv'));
+        
+        // start import (dry run)
+        ob_start();
+        $this->_cli->importGroups($opts);
+        $out = ob_get_clean();
+        
+        $this->assertStringStartsWith('Imported 2 records.', $out);
+        
+        $be = new Tinebase_Group_Sql();
+        $menGroup = $be->getGroupByName('men');
+        $members = $be->getGroupMembers($menGroup);
+        
+        $this->assertEquals(3, count($members));
+        $this->assertEquals('displayed', $menGroup->visibility);
+        
+        $womenGroup = $be->getGroupByName('women');
+        $members = $be->getGroupMembers($womenGroup);
+        
+        $this->assertEquals(2, count($members));
+        $this->assertEquals('displayed', $womenGroup->visibility);
+    }
 }
diff --git a/tests/tine20/Admin/files/import_groups.csv b/tests/tine20/Admin/files/import_groups.csv
new file mode 100644 (file)
index 0000000..b7be8fb
--- /dev/null
@@ -0,0 +1,2 @@
+"women";"Women in our Company";"rwright sclever"
+"men";"Men in our Company";"pwulf jsmith jmcblack"
index 8d133b1..e47b190 100644 (file)
@@ -607,7 +607,7 @@ class Courses_JsonTest extends TestCase
                     'name'              => 'course_user_import_csv',
                     'type'              => 'import',
                     'model'             => 'Tinebase_Model_FullUser',
-                    'plugin'            => 'Admin_Import_Csv',
+                    'plugin'            => 'Admin_Import_User_Csv',
                     'plugin_options'    => '<?xml version="1.0" encoding="UTF-8"?>
             <config>
                 <headline>1</headline>
@@ -647,7 +647,7 @@ class Courses_JsonTest extends TestCase
                     'name'              => 'course_user_import_csv2',
                     'type'              => 'import',
                     'model'             => 'Tinebase_Model_FullUser',
-                    'plugin'            => 'Admin_Import_Csv',
+                    'plugin'            => 'Admin_Import_User_Csv',
                     'plugin_options'    => '<?xml version="1.0" encoding="UTF-8"?>
             <config>
                 <headline>1</headline>
@@ -688,7 +688,7 @@ class Courses_JsonTest extends TestCase
                     'name'              => 'course_user_import_csv',
                     'type'              => 'import',
                     'model'             => 'Tinebase_Model_FullUser',
-                    'plugin'            => 'Admin_Import_Csv',
+                    'plugin'            => 'Admin_Import_User_Csv',
                     'plugin_options'    => '<?xml version="1.0" encoding="UTF-8"?>
             <config>
                 <headline>1</headline>
index c4f7db5..4c9e658 100644 (file)
@@ -48,4 +48,38 @@ class Admin_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
     {
         parent::_import($_opts);
     }
+    
+    /**
+     * import groups
+     *
+     * @param Zend_Console_Getopt $_opts
+     */
+    public function importGroups($_opts)
+    {
+        parent::_import($_opts);
+    }
+    
+    /**
+     * repair groups
+     * add missing lists
+     *
+     */
+    public function repairGroups()
+    {
+        $count = 0;
+        $be = new Tinebase_Group_Sql();
+        
+        $groups = $be->getGroups();
+        
+        foreach ($groups as $group) {
+            if ($group->list_id == null) {
+                $list = Addressbook_Controller_List::getInstance()->createByGroup($group);
+                $group->list_id = $list->getId();
+                $group->visibility = Tinebase_Model_Group::VISIBILITY_DISPLAYED;
+                $be->updateGroupInSqlBackend($group);
+                $count++;
+            }
+        }
+        echo $count . ' groups repaired!';
+    }
 }
diff --git a/tine20/Admin/Import/Group/Csv.php b/tine20/Admin/Import/Group/Csv.php
new file mode 100644 (file)
index 0000000..443381c
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/**
+ * Tine 2.0
+ * 
+ * @package     Admin
+ * @subpackage  Import
+ * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author      Alexander Stintzing <a.stintzing@metaways.de>
+ * @copyright   Copyright (c) 2014 Metaways Infosystems GmbH (http://www.metaways.de)
+ *
+ */
+
+/**
+ * Admin csv import groups class
+ * 
+ * @package     Admin
+ * @subpackage  Import
+ * 
+ */
+class Admin_Import_Group_Csv extends Tinebase_Import_Csv_Abstract
+{
+    /**
+     * creates a new importer from an importexport definition
+     * 
+     * @param  Tinebase_Model_ImportExportDefinition $_definition
+     * @param  array                                 $_options
+     * @return Calendar_Import_Ical
+     * 
+     * @todo move this to abstract when we no longer need to be php 5.2 compatible
+     */
+    public static function createFromDefinition(Tinebase_Model_ImportExportDefinition $_definition, array $_options = array())
+    {
+        return new static(self::getOptionsArrayFromDefinition($_definition, $_options));
+    }
+    
+    /**
+     * import single record (create password if in data)
+     *
+     * @param Tinebase_Record_Abstract $_record
+     * @param string $_resolveStrategy
+     * @param array $_recordData
+     * @return Tinebase_Record_Interface
+     * @throws Tinebase_Exception_Record_Validation
+     */
+    protected function _importRecord($_record, $_resolveStrategy = NULL, $_recordData = array())
+    {
+        $members = explode(' ', $_record->members);
+        $_record->members = null;
+        unset($_record->members);
+        
+        $this->_setController();
+        
+        $record = parent::_importRecord($_record, $_resolveStrategy, $_recordData);
+        
+        $group = Admin_Controller_Group::getInstance()->get($_record->getId());
+        $list = Addressbook_Controller_List::getInstance()->createByGroup($group);
+        $group->list_id = $list->getId();
+        $group->visibility = Tinebase_Model_Group::VISIBILITY_DISPLAYED;
+        $be = new Tinebase_Group_Sql();
+        $be->updateGroupInSqlBackend($group);
+        
+        $memberUids = array();
+        
+        if (! empty($members)) {
+            foreach($members as $member) {
+                try {
+                    $userRecord = Tinebase_User::getInstance()->getUserByLoginName($member);
+                    $be->addGroupMember($_record->getId(), $userRecord->accountId);
+                } catch (Exception $e) {
+                }
+            }
+        }
+        
+        return $record;
+    }
+    
+    /**
+     * overwrite (non-PHPdoc)
+     * @see Tinebase_Import_Abstract::_handleTags()
+     */
+    protected function _handleTags($_record, $_resolveStrategy = NULL)
+    {}
+    
+    /**
+     * set controller
+     */
+    protected function _setController()
+    {
+        $this->_controller = Tinebase_Group::getInstance();
+    }
+}
similarity index 97%
rename from tine20/Admin/Import/Csv.php
rename to tine20/Admin/Import/User/Csv.php
index b85dca6..9784e69 100644 (file)
@@ -17,7 +17,7 @@
  * @subpackage  Import
  * 
  */
-class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
+class Admin_Import_User_Csv extends Tinebase_Import_Csv_Abstract
 {
     /**
      * additional config options
@@ -60,7 +60,7 @@ class Admin_Import_Csv extends Tinebase_Import_Csv_Abstract
      */
     public static function createFromDefinition(Tinebase_Model_ImportExportDefinition $_definition, array $_options = array())
     {
-        return new Admin_Import_Csv(self::getOptionsArrayFromDefinition($_definition, $_options));
+        return new static(self::getOptionsArrayFromDefinition($_definition, $_options));
     }
     
     /**
diff --git a/tine20/Admin/Import/definitions/admin_group_import_csv.xml b/tine20/Admin/Import/definitions/admin_group_import_csv.xml
new file mode 100644 (file)
index 0000000..45c7814
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<config>
+    <model>Tinebase_Model_Group</model>
+    <plugin>Admin_Import_Group_Csv</plugin>
+    <name>admin_group_import_csv</name>
+    <type>import</type>
+    <headline>0</headline>
+    <delimiter>;</delimiter>
+    <dryrun>0</dryrun>
+    <extension>csv</extension>
+    <mapping>
+        <field>
+            <source>name</source>
+            <destination>name</destination>
+        </field>
+        <field>
+            <source>description</source>
+            <destination>description</destination>
+        </field>
+        <field>
+            <source>members</source>
+            <destination>members</destination>
+        </field>
+    </mapping>
+</config>
\ No newline at end of file
index 1cf718b..d59783e 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <config>
     <model>Tinebase_Model_FullUser</model>
-    <plugin>Admin_Import_Csv</plugin>
+    <plugin>Admin_Import_User_Csv</plugin>
     <type>import</type>
     <headline>1</headline>
     <dryrun>0</dryrun>
index c70bcf6..31fa410 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <config>
     <model>Tinebase_Model_FullUser</model>
-    <plugin>Admin_Import_Csv</plugin>
+    <plugin>Admin_Import_User_Csv</plugin>
     <type>import</type>
     <headline>1</headline>
     <dryrun>0</dryrun>
index 8f52d1b..0efa755 100644 (file)
@@ -460,7 +460,7 @@ class Courses_Controller_Course extends Tinebase_Controller_Record_Abstract
         // check if group exists, too
         $group = $this->_groupController->get($course->group_id);
         
-        $importer = Admin_Import_Csv::createFromDefinition($definition, $this->_getNewUserConfig($course));
+        $importer = Admin_Import_User_Csv::createFromDefinition($definition, $this->_getNewUserConfig($course));
         $result = $importer->importFile($tempFile->path);
         
         $groupMembers = $this->_groupController->getGroupMembers($course->group_id);
index 0e4ef96..4dba992 100644 (file)
@@ -301,7 +301,10 @@ class Tinebase_Frontend_Cli_Abstract
             } else {
                 $definition = Tinebase_ImportExportDefinition::getInstance()->getByName($args['definition']);
             }
-        
+            // If old Admin Import plugin is given use the new one!
+            if ($definition->plugin == 'Admin_Import_Csv') {
+                $definition->plugin = 'Admin_Import_User_Csv';
+            }
             $importer = call_user_func($definition->plugin . '::createFromDefinition', $definition, $args);
         } else if ((isset($args['plugin']) || array_key_exists('plugin', $args))) {
             $importer =  new $args['plugin']($args);
index cc026c6..dcb1275 100644 (file)
@@ -87,7 +87,6 @@ abstract class Tinebase_Import_Csv_Abstract extends Tinebase_Import_Abstract
     protected function _getRawData($_resource)
     {
         $delimiter = ((isset($this->_specialDelimiter[$this->_options['delimiter']]) || array_key_exists($this->_options['delimiter'], $this->_specialDelimiter))) ? $this->_specialDelimiter[$this->_options['delimiter']] : $this->_options['delimiter'];
-        
         $lineData = fgetcsv(
             $_resource,
             $this->_options['maxLineLength'],