mirror of
https://github.com/silverstripe/silverstripe-reports
synced 2024-10-22 11:05:53 +02:00
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:
parent
c38a898de3
commit
a7c0797a9d
@ -310,6 +310,8 @@ HTML;
|
||||
$form->makeReadonly();
|
||||
}
|
||||
|
||||
$this->extend('updateEditForm', $form);
|
||||
|
||||
return $form;
|
||||
}
|
||||
}
|
||||
|
@ -457,6 +457,8 @@ JS;
|
||||
$readonlyFields = $form->Fields()->makeReadonly();
|
||||
$form->setFields($readonlyFields);
|
||||
}
|
||||
|
||||
$this->extend('updateEditForm', $form);
|
||||
|
||||
return $form;
|
||||
} if ($id == 0) {
|
||||
@ -467,6 +469,9 @@ JS;
|
||||
}
|
||||
$form = new Form($this, "EditForm", $fields, $siteConfig->getFormActions());
|
||||
$form->loadDataFrom($siteConfig);
|
||||
|
||||
$this->extend('updateEditForm', $form);
|
||||
|
||||
return $form;
|
||||
} else if($id) {
|
||||
return new Form($this, "EditForm", new FieldSet(
|
||||
|
68
code/GroupImportForm.php
Normal file
68
code/GroupImportForm.php
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@ -26,7 +26,9 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
'MemberForm',
|
||||
'EditForm',
|
||||
'MemberImportForm',
|
||||
'memberimport'
|
||||
'memberimport',
|
||||
'GroupImportForm',
|
||||
'groupimport',
|
||||
);
|
||||
|
||||
/**
|
||||
@ -59,13 +61,37 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
public function getEditForm($id) {
|
||||
$record = null;
|
||||
|
||||
// Root form
|
||||
if (($id == 'root' || $id == 0)) {
|
||||
$form = $this->RootForm();
|
||||
$this->extend('augmentRootForm', $form);
|
||||
$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();
|
||||
|
||||
$form = new Form(
|
||||
$this,
|
||||
'EditForm',
|
||||
$fields,
|
||||
$actions
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
||||
if($id && $id != 'root') {
|
||||
$record = DataObject::get_by_id($this->stat('tree_class'), $id);
|
||||
}
|
||||
@ -74,16 +100,27 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
|
||||
$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(
|
||||
new FormAction('addmember',_t('SecurityAdmin.ADDMEMBER','Add Member')),
|
||||
new FormAction('save',_t('SecurityAdmin.SAVE','Save'))
|
||||
);
|
||||
|
||||
|
||||
$form = new Form($this, "EditForm", $fields, $actions);
|
||||
$form->loadDataFrom($record);
|
||||
|
||||
|
||||
|
||||
if(!$record->canEdit()) {
|
||||
$readonlyFields = $form->Fields()->makeReadonly();
|
||||
$form->setFields($readonlyFields);
|
||||
@ -92,7 +129,7 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
// Filter permissions
|
||||
$permissionField = $form->Fields()->dataFieldByName('Permissions');
|
||||
if($permissionField) $permissionField->setHiddenPermissions(self::$hidden_permissions);
|
||||
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
@ -124,6 +161,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',
|
||||
|
Loading…
Reference in New Issue
Block a user