Version: current

     1 <?php
     2 /**
     3  * @todo Migrate Permission->Arg and Permission->Type values
     4  *
     5  * @package sapphire
     6  * @subpackage security
     7  */
     8 class GroupCsvBulkLoader extends CsvBulkLoader {
     9
    10     public $duplicateChecks = array(
    11         'Code' => 'Code',
    12     );
    13
    14     function __construct($objectClass = null) {
    15         if(!$objectClass) $objectClass = 'Group';
    16
    17         parent::__construct($objectClass);
    18     }
    19
    20     function processRecord($record, $columnMap, &$results, $preview = false) {
    21         // We match by 'Code', the ID property is confusing the importer
    22         if(isset($record['ID'])) unset($record['ID']);
    23
    24         $objID = parent::processRecord($record, $columnMap, $results, $preview);
    25
    26         $group = DataObject::get_by_id($this->objectClass, $objID);
    27         // set group hierarchies - we need to do this after all records
    28         // are imported to avoid missing "early" references to parents
    29         // which are imported later on in the CSV file.
    30         if(isset($record['ParentCode']) && $record['ParentCode']) {
    31             $parentGroup = DataObject::get_one(
    32                 'Group',
    33                 sprintf('"Code" = \'%s\'', Convert::raw2sql($record['ParentCode']))
    34             );
    35             if($parentGroup) {
    36                 $group->ParentID = $parentGroup->ID;
    37                 $group->write();
    38             }
    39         }
    40
    41         // set permission codes - these are all additive, meaning
    42         // existing permissions arent cleared.
    43         if(isset($record['PermissionCodes']) && $record['PermissionCodes']) {
    44             foreach(explode(',', $record['PermissionCodes']) as $code) {
    45                 $p = DataObject::get_one(
    46                     'Permission',
    47                     sprintf(
    48                         '"Code" = \'%s\' AND "GroupID" = %d',
    49                         Convert::raw2sql($code),
    50                         $group->ID
    51                     )
    52                 );
    53                 if(!$p) {
    54                     $p = new Permission(array('Code' => $code));
    55                     $p->write();
    56                 }
    57                 $group->Permissions()->add($p);
    58             }
    59         }
    60
    61         return $objID;
    62     }
    63
    64 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -