mirror of
https://github.com/silverstripe/silverstripe-reports
synced 2024-10-22 11:05:53 +02:00
FEATURE Import members and their group assignments from CSV in admin/security through the new MemberImportForm class (merged from r98708)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.4@98732 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
a08c77c360
commit
a9d6dec5ec
84
code/MemberImportForm.php
Normal file
84
code/MemberImportForm.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* Imports {@link Member} records by CSV upload, as defined in
|
||||
* {@link MemberCsvBulkLoader}.
|
||||
*
|
||||
* @package cms
|
||||
* @subpackage batchactions
|
||||
*/
|
||||
class MemberImportForm 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 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[] = sprintf(
|
||||
_t('MemberImportForm.ResultCreated', 'Created %d members'),
|
||||
$result->CreatedCount()
|
||||
);
|
||||
if($result->UpdatedCount()) $msgArr[] = sprintf(
|
||||
_t('MemberImportForm.ResultUpdated', 'Updated %d members'),
|
||||
$result->UpdatedCount()
|
||||
);
|
||||
if($result->DeletedCount()) $msgArr[] = sprintf(
|
||||
_t('MemberImportForm.ResultDeleted', 'Deleted %d members'),
|
||||
$result->DeletedCount()
|
||||
);
|
||||
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
|
||||
|
||||
$this->sessionMessage($msg, 'good');
|
||||
|
||||
Director::redirectBack();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $group Group
|
||||
*/
|
||||
function setGroup($group) {
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Group
|
||||
*/
|
||||
function getGroup($group) {
|
||||
return $this->group;
|
||||
}
|
||||
}
|
||||
?>
|
@ -24,7 +24,9 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
'savemember',
|
||||
'AddRecordForm',
|
||||
'MemberForm',
|
||||
'EditForm'
|
||||
'EditForm',
|
||||
'MemberImportForm',
|
||||
'memberimport'
|
||||
);
|
||||
|
||||
/**
|
||||
@ -75,6 +77,21 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
$form = new Form($this, "EditForm", $fields, $actions);
|
||||
$form->loadDataFrom($record);
|
||||
|
||||
$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(!$record->canEdit()) {
|
||||
$readonlyFields = $form->Fields()->makeReadonly();
|
||||
$form->setFields($readonlyFields);
|
||||
@ -87,6 +104,35 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function memberimport() {
|
||||
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->MemberImportForm()
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see SecurityAdmin_MemberImportForm
|
||||
*
|
||||
* @return Form
|
||||
*/
|
||||
public function MemberImportForm() {
|
||||
$group = $this->currentPage();
|
||||
$form = new MemberImportForm(
|
||||
$this,
|
||||
'MemberImportForm'
|
||||
);
|
||||
$form->setGroup($group);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
public function AddRecordForm() {
|
||||
$m = Object::create('MemberTableField',
|
||||
$this,
|
||||
|
@ -7,3 +7,30 @@ function action_addmember_right() {
|
||||
var addLinks = document.getElementsBySelector('#Form_EditForm div.MemberTableField a.addlink');
|
||||
memberTableFields[0].openPopup(null,addLinks[0].href,tables[0]);
|
||||
}
|
||||
|
||||
(function($) {
|
||||
$(document).ready(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.
|
||||
*/
|
||||
$('#MemberImportFormIframe,#GroupImportFormIframe').livequery(
|
||||
'load',
|
||||
refreshAfterImport
|
||||
);
|
||||
})
|
||||
})(jQuery);
|
21
tests/MemberImportFormTest.php
Normal file
21
tests/MemberImportFormTest.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
/**
|
||||
* @package cms
|
||||
* @subpackage tests
|
||||
*/
|
||||
class MemberImportFormTest extends SapphireTest {
|
||||
|
||||
function testLoad() {
|
||||
$form = new MemberImportForm(
|
||||
new Controller(),
|
||||
'Form'
|
||||
);
|
||||
$data = array(
|
||||
'CsvFile' => array(
|
||||
'tmp_name' => 'cms/tests/MemberImportFormTest.yml'
|
||||
)
|
||||
);
|
||||
$form->doImport($data, $form);
|
||||
}
|
||||
|
||||
}
|
3
tests/MemberImportFormTest.yml
Normal file
3
tests/MemberImportFormTest.yml
Normal file
@ -0,0 +1,3 @@
|
||||
FirstName,Surname,Email,Password,PasswordEncryption,Salt,PasswordExpiry,Groups
|
||||
author1_first,author1_last,author1@test.com,21bb196c4d488023cb4e4b9df5f687c9a4d40172,sha1_v2.4,c23a94a878aa825cee69136bfd6185a874bc68801257978194,,existinggroup
|
||||
author2_first,author2_last,author2@test.com,069b6b22dcaded75c045b7e5ceb5d1dd743f9cac,sha1_v2.4,7e8ea602668b5631bac4baaf7174f9966b6d34571257978210,,"existinggroup,newgroup"
|
Loading…
Reference in New Issue
Block a user