Version: current

     1 <?php
     2 /**
     3  * Imports member records, and checks/updates duplicates based on their
     4  * 'Email' property.
     5  *
     6  * @package sapphire
     7  * @subpackage security
     8  */
     9 class MemberCsvBulkLoader extends CsvBulkLoader {
    10
    11     /**
    12      * @var array Array of {@link Group} records. Import into a specific group.
    13      *  Is overruled by any "Groups" columns in the import.
    14      */
    15     protected $groups = array();
    16
    17     function __construct($objectClass = null) {
    18         if(!$objectClass) $objectClass = 'Member';
    19
    20         parent::__construct($objectClass);
    21     }
    22
    23     public $duplicateChecks = array(
    24         'Email' => 'Email',
    25     );
    26
    27     function processRecord($record, $columnMap, &$results, $preview = false) {
    28         $objID = parent::processRecord($record, $columnMap, $results, $preview);
    29
    30         $_cache_groupByCode = array();
    31
    32         // Add to predefined groups
    33         $member = DataObject::get_by_id($this->objectClass, $objID);
    34         foreach($this->groups as $group) {
    35             // TODO This isnt the most memory effective way to add members to a group
    36             $member->Groups()->add($group);
    37         }
    38
    39         // Add to groups defined in CSV
    40         if(isset($record['Groups']) && $record['Groups']) {
    41             $groupCodes = explode(',', $record['Groups']);
    42             foreach($groupCodes as $groupCode) {
    43                 if(!isset($_cache_groupByCode[$groupCode])) {
    44                     $group = DataObject::get_one(
    45                         'Group',
    46                         sprintf('"Code" = \'%s\'', Convert::raw2sql($groupCode))
    47                     );
    48                     if(!$group) {
    49                         $group = new Group();
    50                         $group->Code = $groupCode;
    51                         $group->Title = $groupCode;
    52                         $group->write();
    53                     }
    54                     $member->Groups()->add($group);
    55                 }
    56                 $_cache_groupByCode[$groupCode] = $group;
    57             }
    58         }
    59
    60         $member->destroy();
    61         unset($member);
    62
    63         return $objID;
    64     }
    65
    66     /**
    67      * @param Array $groups
    68      */
    69     function setGroups($groups) {
    70         $this->groups = $groups;
    71     }
    72
    73     /**
    74      * @return Array
    75      */
    76     function getGroups() {
    77         return $this->groups;
    78     }
    79 }

[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. -