From 19de418156c1e62ed724b3f7672041e59cc0dbb4 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 10 Feb 2010 23:06:35 +0000 Subject: [PATCH] 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 --- code/GroupImportForm.php | 68 ++++++++++++++++++++++++ code/SecurityAdmin.php | 100 +++++++++++++++++++++++++++++------- javascript/SecurityAdmin.js | 24 +++++++-- 3 files changed, 170 insertions(+), 22 deletions(-) create mode 100644 code/GroupImportForm.php diff --git a/code/GroupImportForm.php b/code/GroupImportForm.php new file mode 100644 index 00000000..b5f55eab --- /dev/null +++ b/code/GroupImportForm.php @@ -0,0 +1,68 @@ +(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 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(); + } + +} +?> \ No newline at end of file diff --git a/code/SecurityAdmin.php b/code/SecurityAdmin.php index 9c0c1e0e..0d8b0968 100644 --- a/code/SecurityAdmin.php +++ b/code/SecurityAdmin.php @@ -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( - '', - $this->Link('memberimport') + 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' ); + + // 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( + '', + $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', diff --git a/javascript/SecurityAdmin.js b/javascript/SecurityAdmin.js index d97119d5..62846d61 100755 --- a/javascript/SecurityAdmin.js +++ b/javascript/SecurityAdmin.js @@ -1,14 +1,30 @@ (function($) { + var refreshAfterImport = function(e) { + // Check for a message
, 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
, 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); } }); })