| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\ExcelImportExport; |
||
| 4 | |||
| 5 | use SilverStripe\Core\Convert; |
||
| 6 | use SilverStripe\ORM\DataObject; |
||
| 7 | use SilverStripe\Security\Group; |
||
| 8 | use SilverStripe\Security\Member; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Imports member records, and checks/updates duplicates based on their |
||
| 12 | * 'Email' property. |
||
| 13 | */ |
||
| 14 | class ExcelMemberBulkLoader extends ExcelBulkLoader |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Import into a specific group. |
||
| 18 | * Is overruled by any "Groups" columns in the import. |
||
| 19 | * |
||
| 20 | * @var array<Group> |
||
| 21 | */ |
||
| 22 | protected $groups = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array<string,string> |
||
| 26 | */ |
||
| 27 | public $duplicateChecks = [ |
||
| 28 | 'Email' => 'Email', |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param class-string $objectClass |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 33 | */ |
||
| 34 | public function __construct($objectClass = null) |
||
| 35 | { |
||
| 36 | if (!$objectClass) { |
||
| 37 | $objectClass = Member::class; |
||
| 38 | } |
||
| 39 | parent::__construct($objectClass); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array<string,mixed> $record |
||
| 44 | * @param array<string,string> $columnMap |
||
| 45 | * @param mixed $results |
||
| 46 | * @param boolean $preview |
||
| 47 | * @param boolean $makeRelations |
||
| 48 | * @return int |
||
| 49 | */ |
||
| 50 | protected function processRecord( |
||
| 51 | $record, |
||
| 52 | $columnMap, |
||
| 53 | &$results, |
||
| 54 | $preview = false, |
||
| 55 | $makeRelations = false |
||
| 56 | ) { |
||
| 57 | $objID = parent::processRecord($record, $columnMap, $results, $preview); |
||
| 58 | |||
| 59 | $_cache_groupByCode = []; |
||
| 60 | |||
| 61 | // Add to predefined groups |
||
| 62 | $member = Member::get_by_id($objID); |
||
| 63 | foreach ($this->groups as $group) { |
||
| 64 | // TODO This isnt the most memory effective way to add members to a group |
||
| 65 | $member->Groups()->add($group); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Add to groups defined in CSV |
||
| 69 | if (isset($record['Groups']) && $record['Groups']) { |
||
| 70 | $groupCodes = explode(',', $record['Groups']); |
||
| 71 | foreach ($groupCodes as $groupCode) { |
||
| 72 | $groupCode = Convert::raw2url($groupCode); |
||
| 73 | if (!isset($_cache_groupByCode[$groupCode])) { |
||
| 74 | $group = Group::get()->filter('Code', $groupCode)->first(); |
||
| 75 | if (!$group) { |
||
| 76 | $group = new Group(); |
||
| 77 | $group->Code = $groupCode; |
||
| 78 | $group->Title = $groupCode; |
||
| 79 | $group->write(); |
||
| 80 | } |
||
| 81 | $member->Groups()->add($group); |
||
| 82 | $_cache_groupByCode[$groupCode] = $group; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | $member->destroy(); |
||
| 88 | unset($member); |
||
| 89 | |||
| 90 | return $objID; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param array<Group> $groups |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | public function setGroups($groups) |
||
| 98 | { |
||
| 99 | $this->groups = $groups; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return array<Group> |
||
| 104 | */ |
||
| 105 | public function getGroups() |
||
| 106 | { |
||
| 107 | return $this->groups; |
||
| 108 | } |
||
| 109 | } |
||
| 110 |