Import users in CSV format (comma-separated values).'
. ' Show advanced usage
'
);
$helpHtml .= _t(
'MemberImportForm.Help2',
''
. '
Advanced usage
'
. '
'
. '- Allowed columns: %s
'
. '- Existing users are matched by their unique Code property, and updated with any new values from '
. 'the imported file.
'
. '- Groups can be assigned by the Groups column. Groups are identified by their Code property, '
. 'multiple groups can be separated by comma. Existing group memberships are not cleared.
'
. '
'
. '
'
);
$importer = new MemberCsvBulkLoader();
$importSpec = $importer->getImportSpec();
$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
$fields = new FieldList(
new LiteralField('Help', $helpHtml),
$fileField = new FileField(
'CsvFile',
DBField::create_field('HTMLFragment', _t(
'SecurityAdmin_MemberImportForm.FileFieldLabel',
'CSV File (Allowed extensions: *.csv)'
))
)
);
$fileField->getValidator()->setAllowedExtensions(array('csv'));
}
if(!$actions) {
$action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV'));
$action->addExtraClass('btn btn-secondary-outline ss-ui-button');
$actions = new FieldList($action);
}
if(!$validator) $validator = new RequiredFields('CsvFile');
parent::__construct($controller, $name, $fields, $actions, $validator);
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/vendor.js');
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberImportForm.js');
Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css');
$this->addExtraClass('cms');
$this->addExtraClass('import-form');
}
public function doImport($data, $form) {
$loader = new MemberCsvBulkLoader();
// optionally set group relation
if($this->group) $loader->setGroups(array($this->group));
// load file
$result = $loader->load($data['CsvFile']['tmp_name']);
// result message
$msgArr = array();
if($result->CreatedCount()) $msgArr[] = _t(
'MemberImportForm.ResultCreated', 'Created {count} members',
array('count' => $result->CreatedCount())
);
if($result->UpdatedCount()) $msgArr[] = _t(
'MemberImportForm.ResultUpdated', 'Updated {count} members',
array('count' => $result->UpdatedCount())
);
if($result->DeletedCount()) $msgArr[] = _t(
'MemberImportForm.ResultDeleted', 'Deleted %d members',
array('count' => $result->DeletedCount())
);
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
$this->sessionMessage($msg, 'good');
$this->controller->redirectBack();
}
/**
* @param $group Group
*/
public function setGroup($group) {
$this->group = $group;
}
/**
* @return Group
*/
public function getGroup($group) {
return $this->group;
}
}