Import one or more groups in CSV format (comma-separated values). Show advanced usage
'
);
$helpHtml .= _t(
'GroupImportForm.Help2',
'
Advanced usage
- Allowed columns: %s
- Existing groups are matched by their unique Code value, and updated with any new values from the imported file
- Group hierarchies can be created by using a ParentCode column.
- Permission codes can be assigned by the PermissionCode column. Existing permission codes are not cleared.
');
$importer = new GroupCsvBulkLoader();
$importSpec = $importer->getImportSpec();
$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
$fields = new FieldList(
new LiteralField('Help', $helpHtml),
$fileField = new FileField(
'CsvFile',
_t(
'SecurityAdmin_MemberImportForm.FileFieldLabel',
'CSV File (Allowed extensions: *.csv)'
)
)
);
$fileField->getValidator()->setAllowedExtensions(array('csv'));
}
if(!$actions) $actions = new FieldList(
$importAction = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV'))
);
$importAction->addExtraClass('ss-ui-button');
if(!$validator) $validator = new RequiredFields('CsvFile');
parent::__construct($controller, $name, $fields, $actions, $validator);
$this->addExtraClass('cms');
$this->addExtraClass('import-form');
}
public function doImport($data, $form) {
$loader = new GroupCsvBulkLoader();
// load file
$result = $loader->load($data['CsvFile']['tmp_name']);
// result message
$msgArr = array();
if($result->CreatedCount()) $msgArr[] = _t(
'GroupImportForm.ResultCreated', 'Created {count} groups',
array('count' => $result->CreatedCount())
);
if($result->UpdatedCount()) $msgArr[] = _t(
'GroupImportForm.ResultUpdated', 'Updated %d groups',
array('count' => $result->UpdatedCount())
);
if($result->DeletedCount()) $msgArr[] = _t(
'GroupImportForm.ResultDeleted', 'Deleted %d groups',
array('count' => $result->DeletedCount())
);
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
$this->sessionMessage($msg, 'good');
$this->controller->redirectBack();
}
}