Merge branch 'pu/2013.10-groupimport'
authorPhilipp Schüle <p.schuele@metaways.de>
Thu, 11 Sep 2014 10:07:54 +0000 (12:07 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Thu, 11 Sep 2014 10:07:54 +0000 (12:07 +0200)
tests/tine20/Admin/CliTest.php
tests/tine20/Admin/files/import_groups.csv
tests/tine20/Admin/files/import_groups_update.csv [new file with mode: 0644]
tine20/Admin/Import/Group/Csv.php
tine20/Tinebase/Import/Abstract.php

index 73bbedb..acfd1d4 100644 (file)
@@ -457,20 +457,31 @@ class Admin_CliTest extends TestCase
         ob_start();
         $this->_cli->importGroups($opts);
         $out = ob_get_clean();
+        $this->assertStringStartsWith('Imported 4 records.', $out);
         
-        $this->assertStringStartsWith('Imported 2 records.', $out);
+        $expected = array('men' => 3, 'women' => 2, 'highperformers' => 2, 'lowperformers' => 3);
+        $this->_testImportGroupsHelper($expected);
         
-        $be = new Tinebase_Group_Sql();
-        $menGroup = $be->getGroupByName('men');
-        $members = $be->getGroupMembers($menGroup);
+        $opts->setArguments(array(dirname(__FILE__) . '/files/import_groups_update.csv', 'definition=admin_group_import_csv'));
+        ob_start();
+        $this->_cli->importGroups($opts);
+        $out = ob_get_clean();
+        $this->assertStringStartsWith('Imported 0 records.', $out);
         
-        $this->assertEquals(3, count($members));
-        $this->assertEquals('displayed', $menGroup->visibility);
+        $expected = array('men' => 3, 'women' => 2,  'lowperformers' => 2, 'highperformers' => 3);
+        $this->_testImportGroupsHelper($expected);
+    }
+    
+    protected function _testImportGroupsHelper($expected)
+    {
+        $be = new Tinebase_Group_Sql();
         
-        $womenGroup = $be->getGroupByName('women');
-        $members = $be->getGroupMembers($womenGroup);
+        foreach($expected as $name => $count) {
+            $group = $be->getGroupByName($name);
+            $members = $be->getGroupMembers($group);
         
-        $this->assertEquals(2, count($members));
-        $this->assertEquals('displayed', $womenGroup->visibility);
+            $this->assertEquals($count, count($members), 'Group ' . $name . ' should have ' . $count . ' members!');
+            $this->assertEquals('displayed', $group->visibility, 'Group ' . $name . ' should be visible!');
+        }
     }
 }
index b7be8fb..d971c61 100644 (file)
@@ -1,2 +1,4 @@
 "women";"Women in our Company";"rwright sclever"
 "men";"Men in our Company";"pwulf jsmith jmcblack"
+"highperformers";"High Performers in our company";"rwright sclever"
+"lowperformers";"Low Performers in our company";"pwulf jsmith jmcblack"
\ No newline at end of file
diff --git a/tests/tine20/Admin/files/import_groups_update.csv b/tests/tine20/Admin/files/import_groups_update.csv
new file mode 100644 (file)
index 0000000..7f571be
--- /dev/null
@@ -0,0 +1,4 @@
+"women";"Women in our Company";"rwright sclever"
+"men";"Men in our Company";"pwulf jsmith jmcblack"
+"highperformers";"High Performers in our company";"rwright sclever pwulf"
+"lowperformers";"Low Performers in our company";"jsmith jmcblack"
index 443381c..66f10b2 100644 (file)
 class Admin_Import_Group_Csv extends Tinebase_Import_Csv_Abstract
 {
     /**
+     * @var Tinebase_Record_RecordSet $_userRecords
+     */
+    protected $_userRecords = NULL;
+    
+    /**
      * creates a new importer from an importexport definition
      * 
      * @param  Tinebase_Model_ImportExportDefinition $_definition
@@ -44,34 +49,109 @@ class Admin_Import_Group_Csv extends Tinebase_Import_Csv_Abstract
      */
     protected function _importRecord($_record, $_resolveStrategy = NULL, $_recordData = array())
     {
+        $admCfg = Tinebase_Core::getConfig()->get('Admin');
+        $excludeGroups = array();
+        
+        $be = new Tinebase_Group_Sql();
         $members = explode(' ', $_record->members);
+        
         $_record->members = null;
         unset($_record->members);
         
         $this->_setController();
         
-        $record = parent::_importRecord($_record, $_resolveStrategy, $_recordData);
+        try {
+            $group = $be->getGroupByName($_record->name);
+        } catch (Tinebase_Exception_Record_NotDefined $e) {
+            $group = NULL;
+            parent::_importRecord($_record, $_resolveStrategy, $_recordData);
+        }
+
+        if ($group) {
+            $this->_handleGroupMemberShip($group, $members);
+        } else {
+            $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->updateGroupInSqlBackend($group);
+            
+            $memberUids = array();
+            
+            if (! empty($members)) {
+                $users = $this->_resolveUsers($members);
+                foreach($users as $userId) {
+                    try {
+                        $be->addGroupMember($_record->getId(), $userId);
+                    } catch (Exception $e) {
+                    }
+                }
+            }
+        }
+        return $group;
+    }
+    
+    /**
+     * resolves an array with usernames to an array of user ids
+     * 
+     * @param array $users
+     */
+    protected function _resolveUsers($users)
+    {
+        if (! $this->_userRecords) {
+            $this->_userRecords = new Tinebase_Record_RecordSet('Tinebase_Model_User');
+        }
+        
+        $resolved = array();
+        
+        if (is_array($users) && ! empty($users)) {
+            foreach($users as $userName) {
+                $user = $this->_userRecords->filter('name', $userName)->getFirstRecord();
+                
+                if (! $user) {
+                    try {
+                        $user = Tinebase_User::getInstance()->getUserByLoginName($userName);
+                    } catch (Tinebase_Exception_NotFound $tenf) {
+                        if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+                                . ' Skipping user ' . $userName);
+                        Tinebase_Exception::log($tenf);
+                        continue;
+                    }
+                    $this->_userRecords->addRecord($user);
+                }
+                
+                $resolved[] = $user->getId();
+            }
+        }
         
-        $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;
+        return $resolved;
+    }
+    
+    /**
+     * 
+     * @param Tinebase_Record_Abstract $record
+     * @param array $members
+     */
+    protected function _handleGroupMemberShip($record, $members)
+    {
         $be = new Tinebase_Group_Sql();
-        $be->updateGroupInSqlBackend($group);
+        $group = $be->getGroupByName($record->name);
         
-        $memberUids = array();
+        $oldMembers = $be->getGroupMembers($group->getId());
+        $newMembers = $this->_resolveUsers($members);
         
-        if (! empty($members)) {
-            foreach($members as $member) {
-                try {
-                    $userRecord = Tinebase_User::getInstance()->getUserByLoginName($member);
-                    $be->addGroupMember($_record->getId(), $userRecord->accountId);
-                } catch (Exception $e) {
-                }
+        foreach($oldMembers as $oldMember) {
+            if (! in_array($oldMember, $newMembers)) {
+                $be->removeGroupMember($record->getId(), $oldMember);
             }
         }
         
-        return $record;
+        foreach($newMembers as $newMember) {
+            if (! in_array($newMember, $oldMembers)) {
+                $be->addGroupMember($record->getId(), $newMember);
+            }
+        }
     }
     
     /**
index 3546a99..bf66d5c 100644 (file)
@@ -904,8 +904,7 @@ abstract class Tinebase_Import_Abstract implements Tinebase_Import_Interface
      */
     protected function _handleImportException(Exception $e, $recordIndex, $record = null, $allowToResolveDuplicates = true)
     {
-        if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ' . $e->getMessage());
-        if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $e->getTraceAsString());
+        Tinebase_Exception::log($e);
         
         if ($e instanceof Tinebase_Exception_Duplicate) {
             $exception = $this->_handleDuplicateExceptions($e, $recordIndex, $record, $allowToResolveDuplicates);