2009-12-02 10:40:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Imports member records, and checks/updates duplicates based on their
|
|
|
|
* 'Email' property.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-12-02 10:40:33 +01:00
|
|
|
* @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();
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($objectClass = null) {
|
2009-12-02 10:40:33 +01:00
|
|
|
if(!$objectClass) $objectClass = 'Member';
|
|
|
|
|
|
|
|
parent::__construct($objectClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
public $duplicateChecks = array(
|
|
|
|
'Email' => 'Email',
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$_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])) {
|
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);
|
|
|
|
}
|
|
|
|
$_cache_groupByCode[$groupCode] = $group;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$member->destroy();
|
|
|
|
unset($member);
|
|
|
|
|
|
|
|
return $objID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Array $groups
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setGroups($groups) {
|
2009-12-02 10:40:33 +01:00
|
|
|
$this->groups = $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getGroups() {
|
2009-12-02 10:40:33 +01:00
|
|
|
return $this->groups;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|