| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\ExcelImportExport; |
||
| 4 | |||
| 5 | use SilverStripe\ORM\DataObject; |
||
| 6 | use SilverStripe\Security\Group; |
||
| 7 | use SilverStripe\Security\Permission; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * The same as GroupBulkLoader but with ExcelBulkLoader as base class |
||
| 11 | * |
||
| 12 | * @author Koala |
||
| 13 | */ |
||
| 14 | class ExcelGroupBulkLoader extends ExcelBulkLoader |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var array<string,string> |
||
| 18 | */ |
||
| 19 | public $duplicateChecks = [ |
||
| 20 | 'Code' => 'Code', |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param class-string $objectClass |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 25 | */ |
||
| 26 | public function __construct($objectClass = null) |
||
| 27 | { |
||
| 28 | if (!$objectClass) { |
||
| 29 | $objectClass = Group::class; |
||
| 30 | } |
||
| 31 | parent::__construct($objectClass); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param array<string,mixed> $record |
||
| 36 | * @param array<string,string> $columnMap |
||
| 37 | * @param mixed $results |
||
| 38 | * @param boolean $preview |
||
| 39 | * @param boolean $makeRelations |
||
| 40 | * @return int |
||
| 41 | */ |
||
| 42 | protected function processRecord( |
||
| 43 | $record, |
||
| 44 | $columnMap, |
||
| 45 | &$results, |
||
| 46 | $preview = false, |
||
| 47 | $makeRelations = false |
||
| 48 | ) { |
||
| 49 | // We match by 'Code', the ID property is confusing the importer |
||
| 50 | if (isset($record['ID'])) { |
||
| 51 | unset($record['ID']); |
||
| 52 | } |
||
| 53 | |||
| 54 | $objID = parent::processRecord($record, $columnMap, $results, $preview); |
||
| 55 | |||
| 56 | $group = Group::get_by_id($objID); |
||
| 57 | // set group hierarchies - we need to do this after all records |
||
| 58 | // are imported to avoid missing "early" references to parents |
||
| 59 | // which are imported later on in the CSV file. |
||
| 60 | if (isset($record['ParentCode']) && $record['ParentCode']) { |
||
| 61 | $parentGroup = DataObject::get_one(Group::class, array( |
||
| 62 | '"Group"."Code"' => $record['ParentCode'] |
||
| 63 | )); |
||
| 64 | if ($parentGroup) { |
||
| 65 | $group->ParentID = $parentGroup->ID; |
||
| 66 | $group->write(); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | // set permission codes - these are all additive, meaning |
||
| 71 | // existing permissions arent cleared. |
||
| 72 | if (isset($record['PermissionCodes']) && $record['PermissionCodes']) { |
||
| 73 | foreach (explode(',', $record['PermissionCodes']) as $code) { |
||
| 74 | $p = DataObject::get_one(Permission::class, array( |
||
| 75 | '"Permission"."Code"' => $code, |
||
| 76 | '"Permission"."GroupID"' => $group->ID |
||
| 77 | )); |
||
| 78 | if (!$p) { |
||
| 79 | $p = new Permission(array('Code' => $code)); |
||
| 80 | $p->write(); |
||
| 81 | } |
||
| 82 | $group->Permissions()->add($p); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | return $objID; |
||
| 87 | } |
||
| 88 | } |
||
| 89 |