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

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.4@98736 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-02-11 01:20:20 +00:00 committed by Sam Minnee
parent c38a898de3
commit a7c0797a9d
4 changed files with 147 additions and 8 deletions

View File

@ -310,6 +310,8 @@ HTML;
$form->makeReadonly(); $form->makeReadonly();
} }
$this->extend('updateEditForm', $form);
return $form; return $form;
} }
} }

View File

@ -457,6 +457,8 @@ JS;
$readonlyFields = $form->Fields()->makeReadonly(); $readonlyFields = $form->Fields()->makeReadonly();
$form->setFields($readonlyFields); $form->setFields($readonlyFields);
} }
$this->extend('updateEditForm', $form);
return $form; return $form;
} if ($id == 0) { } if ($id == 0) {
@ -467,6 +469,9 @@ JS;
} }
$form = new Form($this, "EditForm", $fields, $siteConfig->getFormActions()); $form = new Form($this, "EditForm", $fields, $siteConfig->getFormActions());
$form->loadDataFrom($siteConfig); $form->loadDataFrom($siteConfig);
$this->extend('updateEditForm', $form);
return $form; return $form;
} else if($id) { } else if($id) {
return new Form($this, "EditForm", new FieldSet( return new Form($this, "EditForm", new FieldSet(

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

@ -26,7 +26,9 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
'MemberForm', 'MemberForm',
'EditForm', 'EditForm',
'MemberImportForm', 'MemberImportForm',
'memberimport' 'memberimport',
'GroupImportForm',
'groupimport',
); );
/** /**
@ -59,13 +61,37 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
public function getEditForm($id) { public function getEditForm($id) {
$record = null; $record = null;
// Root form
if (($id == 'root' || $id == 0)) { if (($id == 'root' || $id == 0)) {
$form = $this->RootForm(); $fields = new FieldSet(
$this->extend('augmentRootForm', $form); 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();
$form = new Form(
$this,
'EditForm',
$fields,
$actions
);
return $form; return $form;
} }
if($id && $id != 'root') { if($id && $id != 'root') {
$record = DataObject::get_by_id($this->stat('tree_class'), $id); $record = DataObject::get_by_id($this->stat('tree_class'), $id);
} }
@ -74,16 +100,27 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
$fields = $record->getCMSFields(); $fields = $record->getCMSFields();
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')
)
)
);
}
$actions = new FieldSet( $actions = new FieldSet(
new FormAction('addmember',_t('SecurityAdmin.ADDMEMBER','Add Member')), new FormAction('addmember',_t('SecurityAdmin.ADDMEMBER','Add Member')),
new FormAction('save',_t('SecurityAdmin.SAVE','Save')) new FormAction('save',_t('SecurityAdmin.SAVE','Save'))
); );
$form = new Form($this, "EditForm", $fields, $actions); $form = new Form($this, "EditForm", $fields, $actions);
$form->loadDataFrom($record); $form->loadDataFrom($record);
if(!$record->canEdit()) { if(!$record->canEdit()) {
$readonlyFields = $form->Fields()->makeReadonly(); $readonlyFields = $form->Fields()->makeReadonly();
$form->setFields($readonlyFields); $form->setFields($readonlyFields);
@ -92,7 +129,7 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
// Filter permissions // Filter permissions
$permissionField = $form->Fields()->dataFieldByName('Permissions'); $permissionField = $form->Fields()->dataFieldByName('Permissions');
if($permissionField) $permissionField->setHiddenPermissions(self::$hidden_permissions); if($permissionField) $permissionField->setHiddenPermissions(self::$hidden_permissions);
return $form; return $form;
} }
@ -124,6 +161,33 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
return $form; 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() { public function AddRecordForm() {
$m = Object::create('MemberTableField', $m = Object::create('MemberTableField',