FEATURE Import groups from CSV in admin/security through the new GroupImportForm class (and GroupCsvBulkLoader)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@98711 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-02-10 23:06:35 +00:00
parent 49dd8ddfa5
commit 19de418156
3 changed files with 170 additions and 22 deletions

68
code/GroupImportForm.php Normal file
View File

@ -0,0 +1,68 @@
<?php
/**
* 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) {
$fields = new FieldSet(
$fileField = new FileField(
'CsvFile',
_t(
'SecurityAdmin_MemberImportForm.FileFieldLabel',
'CSV File <small>(Allowed extensions: *.csv)</small>'
)
)
);
$fileField->setAllowedExtensions(array('csv'));
}
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'),
$result->CreatedCount()
);
if($result->UpdatedCount()) $msgArr[] = sprintf(
_t('GroupImportForm.ResultUpdated', 'Updated %d groups'),
$result->UpdatedCount()
);
if($result->DeletedCount()) $msgArr[] = sprintf(
_t('GroupImportForm.ResultDeleted', 'Deleted %d groups'),
$result->DeletedCount()
);
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
$this->sessionMessage($msg, 'good');
Director::redirectBack();
}
}
?>

View File

@ -25,7 +25,9 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
'MemberForm',
'EditForm',
'MemberImportForm',
'memberimport'
'memberimport',
'GroupImportForm',
'groupimport',
);
/**
@ -44,30 +46,65 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
function getEditForm($id = null) {
$form = parent::getEditForm($id);
$fields = $form->Fields();
if($id && is_numeric($id)) {
$fields = $form->Fields();
if($fields->hasTabSet()) {
$fields->findOrMakeTab('Root.Import',_t('Group.IMPORTTABTITLE', 'Import'));
$fields->addFieldToTab('Root.Import',
new LiteralField(
'MemberImportFormIframe',
sprintf(
'<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="400px" border="0"></iframe>',
$this->Link('memberimport')
if($fields->hasTabSet()) {
$fields->findOrMakeTab('Root.Import',_t('Group.IMPORTTABTITLE', 'Import'));
$fields->addFieldToTab('Root.Import',
new LiteralField(
'MemberImportFormIframe',
sprintf(
'<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="400px" border="0"></iframe>',
$this->Link('memberimport')
)
)
)
);
}
$form->Actions()->insertBefore(
new FormAction('addmember',_t('SecurityAdmin.ADDMEMBER','Add Member')),
'action_save'
);
// Filter permissions
$permissionField = $form->Fields()->dataFieldByName('Permissions');
if($permissionField) $permissionField->setHiddenPermissions(self::$hidden_permissions);
}
$form->Actions()->insertBefore(
new FormAction('addmember',_t('SecurityAdmin.ADDMEMBER','Add Member')),
'action_save'
return $form;
}
/**
* @return FieldSet
*/
function RootForm() {
$fields = new FieldSet(
new TabSet(
'Root',
new Tab('Import', _t('SecurityAdmin.TABIMPORT', 'Import'),
new LiteralField(
'GroupImportFormIframe',
sprintf(
'<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="400px" border="0"></iframe>',
$this->Link('groupimport')
)
)
)
),
// necessary for tree node selection in LeftAndMain.EditForm.js
new HiddenField('ID', false, 0)
);
$actions = new FieldSet();
// Filter permissions
$permissionField = $form->Fields()->dataFieldByName('Permissions');
if($permissionField) $permissionField->setHiddenPermissions(self::$hidden_permissions);
$form = new Form(
$this,
'Form',
$fields,
$actions
);
return $form;
}
@ -99,6 +136,33 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
return $form;
}
public function groupimport() {
Requirements::clear();
Requirements::css(SAPPHIRE_DIR . '/css/Form.css');
Requirements::css(CMS_DIR . '/css/typography.css');
Requirements::css(CMS_DIR . '/css/cms_right.css');
Requirements::javascript(CMS_DIR . '/javascript/MemberImportForm.js');
return $this->renderWith('BlankPage', array(
'Form' => $this->GroupImportForm()
));
}
/**
* @see SecurityAdmin_MemberImportForm
*
* @return Form
*/
public function GroupImportForm() {
$form = new GroupImportForm(
$this,
'GroupImportForm'
);
return $form;
}
public function AddRecordForm() {
$m = Object::create('MemberTableField',

View File

@ -1,14 +1,30 @@
(function($) {
var refreshAfterImport = function(e) {
// Check for a message <div>, an indication that the form has been submitted.
var existingFormMessage = $($(this).contents()).find('.message');
if(existingFormMessage && existingFormMessage.html()) {
// Refresh member listing
var memberTableField = $(window.parent.document).find('#Form_EditForm_Members').get(0);
if(memberTableField) memberTableField.refresh();
// Refresh tree
var tree = $(window.parent.document).find('#sitetree').get(0);
if(tree) tree.reload();
}
};
/**
* Refresh the member listing every time the import iframe is loaded,
* which is most likely a form submission.
*/
$(window).bind('load', function(e) {
$('#MemberImportFormIframe').bind('load', function(e) {
// Check for a message <div>, an indication that the form has been submitted.
if($($(this).contents()).find('.message').length) {
$(window.parent.document).find('#Form_EditForm_Members').get(0).refresh();
$('#MemberImportFormIframe,#GroupImportFormIframe').concrete({
onmatch: function() {
this._super();
// TODO concrete can't seem to bind to iframe load events
$(this).bind('load', refreshAfterImport);
}
});
})