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 6400013a..784caf75 100644 --- a/code/SecurityAdmin.php +++ b/code/SecurityAdmin.php @@ -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( + '', + $this->Link('memberimport') + ) + ) + ); + } + if(!$record->canEdit()) { $readonlyFields = $form->Fields()->makeReadonly(); $form->setFields($readonlyFields); @@ -86,6 +103,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_right.js b/javascript/SecurityAdmin_right.js index 03daad63..e9e8e163 100755 --- a/javascript/SecurityAdmin_right.js +++ b/javascript/SecurityAdmin_right.js @@ -6,4 +6,31 @@ function action_addmember_right() { var tables = document.getElementsBySelector('#Form_EditForm div.MemberTableField table'); var addLinks = document.getElementsBySelector('#Form_EditForm div.MemberTableField a.addlink'); memberTableFields[0].openPopup(null,addLinks[0].href,tables[0]); -} \ No newline at end of file +} + +(function($) { + $(document).ready(function() { + var refreshAfterImport = function(e) { + // Check for a message