Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

90 lines (76 sloc) 2.729 kB
* Imports {@link Group} records by CSV upload, as defined in
* {@link GroupCsvBulkLoader}.
* @package cms
* @subpackage batchactions
class GroupImportForm extends Form {
* @var Group Optional group relation
protected $group;
function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
if(!$fields) {
$helpHtml = _t(
'<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>'
$helpHtml .= _t(
'<div class="advanced">
<h4>Advanced usage</h4>
<li>Allowed columns: <em>%s</em></li>
<li>Existing groups are matched by their unique <em>Code</em> value, and updated with any new values from the imported file</li>
<li>Group hierarchies can be created by using a <em>ParentCode</em> column.</li>
<li>Permission codes can be assigned by the <em>PermissionCode</em> column. Existing permission codes are not cleared.</li>
$importer = new GroupCsvBulkLoader();
$importSpec = $importer->getImportSpec();
$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
$fields = new FieldSet(
new LiteralField('Help', $helpHtml),
$fileField = new FileField(
'CSV File <small>(Allowed extensions: *.csv)</small>'
if(!$actions) $actions = new FieldSet(
new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import'))
if(!$validator) $validator = new RequiredFields('CsvFile');
parent::__construct($controller, $name, $fields, $actions, $validator);
function doImport($data, $form) {
$loader = new GroupCsvBulkLoader();
// load file
$result = $loader->load($data['CsvFile']['tmp_name']);
// result message
$msgArr = array();
if($result->CreatedCount()) $msgArr[] = sprintf(
_t('GroupImportForm.ResultCreated', 'Created %d groups'),
if($result->UpdatedCount()) $msgArr[] = sprintf(
_t('GroupImportForm.ResultUpdated', 'Updated %d groups'),
if($result->DeletedCount()) $msgArr[] = sprintf(
_t('GroupImportForm.ResultDeleted', 'Deleted %d groups'),
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
$this->sessionMessage($msg, 'good');
Jump to Line
Something went wrong with that request. Please try again.