silverstripe-framework/Security/MemberCsvBulkLoader.php

87 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2016-06-23 01:37:22 +02:00
namespace SilverStripe\Security;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\CsvBulkLoader;
use SilverStripe\ORM\DataObject;
2016-06-23 01:37:22 +02:00
/**
* Imports member records, and checks/updates duplicates based on their
* 'Email' property.
*/
class MemberCsvBulkLoader extends CsvBulkLoader {
2014-08-15 08:53:05 +02:00
/**
* @var array Array of {@link Group} records. Import into a specific group.
* Is overruled by any "Groups" columns in the import.
*/
protected $groups = array();
2014-08-15 08:53:05 +02:00
public function __construct($objectClass = null) {
2016-06-23 01:37:22 +02:00
if(!$objectClass) $objectClass = 'SilverStripe\\Security\\Member';
2014-08-15 08:53:05 +02:00
parent::__construct($objectClass);
}
2014-08-15 08:53:05 +02:00
/**
* @skipUpgrade
* @var array
*/
public $duplicateChecks = array(
'Email' => 'Email',
);
2014-08-15 08:53:05 +02:00
public function processRecord($record, $columnMap, &$results, $preview = false) {
$objID = parent::processRecord($record, $columnMap, $results, $preview);
2014-08-15 08:53:05 +02:00
$_cache_groupByCode = array();
2014-08-15 08:53:05 +02:00
// Add to predefined groups
/** @var Member $member */
$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);
}
2014-08-15 08:53:05 +02:00
// Add to groups defined in CSV
if(isset($record['Groups']) && $record['Groups']) {
$groupCodes = explode(',', $record['Groups']);
foreach($groupCodes as $groupCode) {
$groupCode = Convert::raw2url($groupCode);
if(!isset($_cache_groupByCode[$groupCode])) {
$group = Group::get()->filter('Code', $groupCode)->first();
if(!$group) {
$group = new Group();
$group->Code = $groupCode;
$group->Title = $groupCode;
$group->write();
}
$member->Groups()->add($group);
$_cache_groupByCode[$groupCode] = $group;
}
}
}
2014-08-15 08:53:05 +02:00
$member->destroy();
unset($member);
2014-08-15 08:53:05 +02:00
return $objID;
}
2014-08-15 08:53:05 +02:00
/**
2016-06-23 01:37:22 +02:00
* @param array $groups
*/
public function setGroups($groups) {
$this->groups = $groups;
}
2014-08-15 08:53:05 +02:00
/**
2016-06-23 01:37:22 +02:00
* @return array
*/
public function getGroups() {
return $this->groups;
}
2014-08-15 08:53:05 +02:00
}