2009-12-02 10:40:33 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Dev\CsvBulkLoader;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2009-12-02 10:40:33 +01: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
|
|
|
|
2009-12-02 10:40:33 +01: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
|
|
|
|
2012-09-19 12:07:39 +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
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
parent::__construct($objectClass);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-12-02 10:40:33 +01:00
|
|
|
public $duplicateChecks = array(
|
|
|
|
'Email' => 'Email',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function processRecord($record, $columnMap, &$results, $preview = false) {
|
2009-12-02 10:40:33 +01:00
|
|
|
$objID = parent::processRecord($record, $columnMap, $results, $preview);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
$_cache_groupByCode = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
// Add to predefined groups
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var Member $member */
|
2009-12-02 10:40:33 +01:00
|
|
|
$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
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
// Add to groups defined in CSV
|
|
|
|
if(isset($record['Groups']) && $record['Groups']) {
|
|
|
|
$groupCodes = explode(',', $record['Groups']);
|
|
|
|
foreach($groupCodes as $groupCode) {
|
2015-12-02 05:31:25 +01:00
|
|
|
$groupCode = Convert::raw2url($groupCode);
|
2009-12-02 10:40:33 +01:00
|
|
|
if(!isset($_cache_groupByCode[$groupCode])) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$group = Group::get()->filter('Code', $groupCode)->first();
|
2009-12-02 10:40:33 +01:00
|
|
|
if(!$group) {
|
|
|
|
$group = new Group();
|
|
|
|
$group->Code = $groupCode;
|
|
|
|
$group->Title = $groupCode;
|
|
|
|
$group->write();
|
|
|
|
}
|
|
|
|
$member->Groups()->add($group);
|
2015-12-02 05:31:25 +01:00
|
|
|
$_cache_groupByCode[$groupCode] = $group;
|
2009-12-02 10:40:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
$member->destroy();
|
|
|
|
unset($member);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
return $objID;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param array $groups
|
2009-12-02 10:40:33 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setGroups($groups) {
|
2009-12-02 10:40:33 +01:00
|
|
|
$this->groups = $groups;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-12-02 10:40:33 +01:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return array
|
2009-12-02 10:40:33 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getGroups() {
|
2009-12-02 10:40:33 +01:00
|
|
|
return $this->groups;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|