Version: current

     1 <?php
     2 /**
     3  * Imports {@link Group} records by CSV upload, as defined in
     4  * {@link GroupCsvBulkLoader}.
     5  *
     6  * @package cms
     7  * @subpackage batchactions
     8  */
     9 class GroupImportForm extends Form {
    10
    11     /**
    12      * @var Group Optional group relation
    13      */
    14     protected $group;
    15
    16     function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
    17         if(!$fields) {
    18             $helpHtml = _t(
    19                 'GroupImportForm.Help1',
    20                 '<p>Import one or more groups in <em>CSV</em> format (comma-separated values). <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'
    21             );
    22             $helpHtml .= _t(
    23                 'GroupImportForm.Help2',
    24 '<div class="advanced">
    25     <h4>Advanced usage</h4>
    26     <ul>
    27     <li>Allowed columns: <em>%s</em></li>
    28     <li>Existing groups are matched by their unique <em>Code</em> value, and updated with any new values from the imported file</li>
    29     <li>Group hierarchies can be created by using a <em>ParentCode</em> column.</li>
    30     <li>Permission codes can be assigned by the <em>PermissionCode</em> column. Existing permission codes are not cleared.</li>
    31     </ul>
    32 </div>');
    33
    34             $importer = new GroupCsvBulkLoader();
    35             $importSpec = $importer->getImportSpec();
    36             $helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
    37
    38             $fields = new FieldSet(
    39                 new LiteralField('Help', $helpHtml),
    40                 $fileField = new FileField(
    41                     'CsvFile',
    42                     _t(
    43                         'SecurityAdmin_MemberImportForm.FileFieldLabel',
    44                         'CSV File <small>(Allowed extensions: *.csv)</small>'
    45                     )
    46                 )
    47             );
    48             $fileField->getValidator()->setAllowedExtensions(array('csv'));
    49         }
    50
    51         if(!$actions) $actions = new FieldSet(
    52             new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import'))
    53         );
    54
    55         if(!$validator) $validator = new RequiredFields('CsvFile');
    56
    57         parent::__construct($controller, $name, $fields, $actions, $validator);
    58
    59         $this->addExtraClass('import-form');
    60     }
    61
    62     function doImport($data, $form) {
    63         $loader = new GroupCsvBulkLoader();
    64
    65         // load file
    66         $result = $loader->load($data['CsvFile']['tmp_name']);
    67
    68         // result message
    69         $msgArr = array();
    70         if($result->CreatedCount()) $msgArr[] = sprintf(
    71             _t('GroupImportForm.ResultCreated', 'Created %d groups'),
    72             $result->CreatedCount()
    73         );
    74         if($result->UpdatedCount()) $msgArr[] = sprintf(
    75             _t('GroupImportForm.ResultUpdated', 'Updated %d groups'),
    76             $result->UpdatedCount()
    77         );
    78         if($result->DeletedCount()) $msgArr[] = sprintf(
    79             _t('GroupImportForm.ResultDeleted', 'Deleted %d groups'),
    80             $result->DeletedCount()
    81         );
    82         $msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
    83
    84         $this->sessionMessage($msg, 'good');
    85
    86         Director::redirectBack();
    87     }
    88
    89 }
    90 ?>

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