ENHANCEMENT MemberCsvBulkLoader for easy member import with group associations

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@94251 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-12-02 09:40:33 +00:00
parent ef28a27b25
commit fd86a0ba0e
5 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,79 @@
<?php
/**
* Imports member records, and checks/updates duplicates based on their
* 'Email' property.
*
* @package sapphire
* @subpackage security
*/
class MemberCsvBulkLoader extends CsvBulkLoader {
/**
* @var array Array of {@link Group} records. Import into a specific group.
* Is overruled by any "Groups" columns in the import.
*/
protected $groups = array();
function __construct($objectClass = null) {
if(!$objectClass) $objectClass = 'Member';
parent::__construct($objectClass);
}
public $duplicateChecks = array(
'Email' => 'Email',
);
function processRecord($record, $columnMap, &$results, $preview = false) {
$objID = parent::processRecord($record, $columnMap, $results, $preview);
$_cache_groupByCode = array();
// Add to predefined groups
$member = DataObject::get_by_id($this->objectClass, $objID);
foreach($this->groups as $group) {
// TODO This isnt the most memory effective way to add members to a group
$member->Groups()->add($group);
}
// Add to groups defined in CSV
if(isset($record['Groups']) && $record['Groups']) {
$groupCodes = explode(',', $record['Groups']);
foreach($groupCodes as $groupCode) {
if(!isset($_cache_groupByCode[$groupCode])) {
$group = DataObject::get_one(
'Group',
sprintf('"Code" = \'%s\'', Convert::raw2sql($groupCode))
);
if(!$group) {
$group = new Group();
$group->Code = $groupCode;
$group->Title = $groupCode;
$group->write();
}
$member->Groups()->add($group);
}
$_cache_groupByCode[$groupCode] = $group;
}
}
$member->destroy();
unset($member);
return $objID;
}
/**
* @param Array $groups
*/
function setGroups($groups) {
$this->groups = $groups;
}
/**
* @return Array
*/
function getGroups() {
return $this->groups;
}
}

View File

@ -0,0 +1,3 @@
FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry
author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194,
author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210,
1 FirstName Surname Email Password PasswordEncryption Salt PasswordExpiry
2 author1_first author1_last author1@test.com 21bb196c4d488023cb4e4b9df5f687c9a4d40172 sha1_v2.4 c23a94a878aa825cee69136bfd6185a874bc68801257978194
3 author2_first author2_last author2@test.com 069b6b22dcaded75c045b7e5ceb5d1dd743f9cac sha1_v2.4 7e8ea602668b5631bac4baaf7174f9966b6d34571257978210

View File

@ -0,0 +1,61 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class MemberCsvBulkLoaderTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/security/MemberCsvBulkLoaderTest.yml';
function testNewImport() {
$loader = new MemberCsvBulkLoader();
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv');
$created = $results->Created()->toArray();
$this->assertEquals(count($created), 2);
$this->assertEquals($created[0]->Email, 'author1@test.com');
$this->assertEquals($created[1]->Email, 'author2@test.com');
}
function testOverwriteExistingImport() {
$author1 = new Member();
$author1->FirstName = 'author1_first_old';
$author1->Email = 'author1@test.com';
$author1->write();
$loader = new MemberCsvBulkLoader();
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv');
$created = $results->Created()->toArray();
$this->assertEquals(count($created), 1);
$updated = $results->Updated()->toArray();
$this->assertEquals(count($updated), 1);
$this->assertEquals($created[0]->Email, 'author2@test.com');
$this->assertEquals($updated[0]->Email, 'author1@test.com');
$this->assertEquals($updated[0]->FirstName, 'author1_first');
}
function testAddToPredefinedGroups() {
$existinggroup = $this->objFromFixture('Group', 'existinggroup');
$loader = new MemberCsvBulkLoader();
$loader->setGroups(array($existinggroup));
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest.csv');
$created = $results->Created()->toArray();
$this->assertEquals($created[0]->Groups()->column('ID'), array($existinggroup->ID));
$this->assertEquals($created[1]->Groups()->column('ID'), array($existinggroup->ID));
}
function testAddToCsvColumnGroupsByCode() {
$existinggroup = $this->objFromFixture('Group', 'existinggroup');
$loader = new MemberCsvBulkLoader();
$results = $loader->load('sapphire/tests/security/MemberCsvBulkLoaderTest_withGroups.csv');
$newgroup = DataObject::get_one('Group', sprintf('"Code" = \'%s\'', 'newgroup'));
$this->assertEquals($newgroup->Title, 'newgroup');
$created = $results->Created()->toArray();
$this->assertEquals($created[0]->Groups()->column('ID'), array($existinggroup->ID));
$this->assertEquals($created[1]->Groups()->column('ID'), array($existinggroup->ID, $newgroup->ID));
}
}

View File

@ -0,0 +1,7 @@
Group:
existinggroup:
Code: existinggroup
Member:
existingauthor:
Email: existingauthor@test.com
FirstName: Existing Author

View File

@ -0,0 +1,3 @@
FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry,Groups
author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194,,existinggroup
author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210,,"existinggroup,newgroup"
1 FirstName Surname Email Password PasswordEncryption Salt PasswordExpiry Groups
2 author1_first author1_last author1@test.com 21bb196c4d488023cb4e4b9df5f687c9a4d40172 sha1_v2.4 c23a94a878aa825cee69136bfd6185a874bc68801257978194 existinggroup
3 author2_first author2_last author2@test.com 069b6b22dcaded75c045b7e5ceb5d1dd743f9cac sha1_v2.4 7e8ea602668b5631bac4baaf7174f9966b6d34571257978210 existinggroup,newgroup