From 89c926221f34db78baada582cae649ba5980d968 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 10 Feb 2010 23:06:31 +0000 Subject: [PATCH] FEATURE Import members and their group assignments from CSV in admin/security through the new MemberImportForm class git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@98708 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/MemberImportForm.php | 84 ++++++++++++++++++++++++++++++++++ code/SecurityAdmin.php | 46 +++++++++++++++++++ javascript/SecurityAdmin.js | 14 ++++++ tests/MemberImportFormTest.php | 21 +++++++++ tests/MemberImportFormTest.yml | 3 ++ 5 files changed, 168 insertions(+) create mode 100644 code/MemberImportForm.php create mode 100644 tests/MemberImportFormTest.php create mode 100644 tests/MemberImportFormTest.yml diff --git a/code/MemberImportForm.php b/code/MemberImportForm.php new file mode 100644 index 00000000..85da09a8 --- /dev/null +++ b/code/MemberImportForm.php @@ -0,0 +1,84 @@ +(Allowed extensions: *.csv)' + ) + ) + ); + $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; + } +} +?> \ No newline at end of file diff --git a/code/SecurityAdmin.php b/code/SecurityAdmin.php index 527fce99..9c0c1e0e 100644 --- a/code/SecurityAdmin.php +++ b/code/SecurityAdmin.php @@ -24,6 +24,8 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { 'AddRecordForm', 'MemberForm', 'EditForm', + 'MemberImportForm', + 'memberimport' ); /** @@ -42,6 +44,21 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider { function getEditForm($id = null) { $form = parent::getEditForm($id); + $fields = $form->Fields(); + + if($fields->hasTabSet()) { + $fields->findOrMakeTab('Root.Import',_t('Group.IMPORTTABTITLE', 'Import')); + $fields->addFieldToTab('Root.Import', + new LiteralField( + 'MemberImportFormIframe', + sprintf( + '', + $this->Link('memberimport') + ) + ) + ); + } + $form->Actions()->insertBefore( new FormAction('addmember',_t('SecurityAdmin.ADDMEMBER','Add Member')), 'action_save' @@ -53,6 +70,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', diff --git a/javascript/SecurityAdmin.js b/javascript/SecurityAdmin.js index bbceb813..d97119d5 100755 --- a/javascript/SecurityAdmin.js +++ b/javascript/SecurityAdmin.js @@ -1,4 +1,18 @@ (function($) { + + /** + * 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
, an indication that the form has been submitted. + if($($(this).contents()).find('.message').length) { + $(window.parent.document).find('#Form_EditForm_Members').get(0).refresh(); + } + }); + }) + /** * Delete selected folders through "batch actions" tab. */ diff --git a/tests/MemberImportFormTest.php b/tests/MemberImportFormTest.php new file mode 100644 index 00000000..4426a951 --- /dev/null +++ b/tests/MemberImportFormTest.php @@ -0,0 +1,21 @@ + array( + 'tmp_name' => 'cms/tests/MemberImportFormTest.yml' + ) + ); + $form->doImport($data, $form); + } + +} \ No newline at end of file diff --git a/tests/MemberImportFormTest.yml b/tests/MemberImportFormTest.yml new file mode 100644 index 00000000..94463f79 --- /dev/null +++ b/tests/MemberImportFormTest.yml @@ -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" \ No newline at end of file