2011-03-23 10:51:00 +01:00
|
|
|
<?php
|
2013-10-15 00:26:23 +02:00
|
|
|
|
2016-08-11 01:14:02 +02:00
|
|
|
namespace SilverStripe\Admin;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Security\Group;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\MemberCsvBulkLoader;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\FileField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\RequiredFields;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\View\Requirements;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* Imports {@link Member} records by CSV upload, as defined in
|
|
|
|
* {@link MemberCsvBulkLoader}.
|
|
|
|
*/
|
|
|
|
class MemberImportForm extends Form {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* @var Group Optional group relation
|
|
|
|
*/
|
|
|
|
protected $group;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
|
2011-03-23 10:51:00 +01:00
|
|
|
if(!$fields) {
|
|
|
|
$helpHtml = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'MemberImportForm.Help1',
|
2012-09-26 23:34:00 +02:00
|
|
|
'<p>Import users in <em>CSV format</em> (comma-separated values).'
|
|
|
|
. ' <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
$helpHtml .= _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'MemberImportForm.Help2',
|
2015-08-24 06:29:40 +02:00
|
|
|
'<div class="advanced">'
|
|
|
|
. '<h4>Advanced usage</h4>'
|
|
|
|
. '<ul>'
|
|
|
|
. '<li>Allowed columns: <em>%s</em></li>'
|
|
|
|
. '<li>Existing users are matched by their unique <em>Code</em> property, and updated with any new values from '
|
|
|
|
. 'the imported file.</li>'
|
|
|
|
. '<li>Groups can be assigned by the <em>Groups</em> column. Groups are identified by their <em>Code</em> property, '
|
|
|
|
. 'multiple groups can be separated by comma. Existing group memberships are not cleared.</li>'
|
|
|
|
. '</ul>'
|
|
|
|
. '</div>'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$importer = new MemberCsvBulkLoader();
|
|
|
|
$importSpec = $importer->getImportSpec();
|
|
|
|
$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2011-03-23 10:51:00 +01:00
|
|
|
new LiteralField('Help', $helpHtml),
|
|
|
|
$fileField = new FileField(
|
2014-08-15 08:53:05 +02:00
|
|
|
'CsvFile',
|
2011-03-23 10:51:00 +01:00
|
|
|
_t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'SecurityAdmin_MemberImportForm.FileFieldLabel',
|
2011-03-23 10:51:00 +01:00
|
|
|
'CSV File <small>(Allowed extensions: *.csv)</small>'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$fileField->getValidator()->setAllowedExtensions(array('csv'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$actions) {
|
|
|
|
$action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV'));
|
2016-07-01 03:37:29 +02:00
|
|
|
$action->addExtraClass('btn btn-secondary-outline ss-ui-button');
|
2012-09-26 23:34:00 +02:00
|
|
|
$actions = new FieldList($action);
|
|
|
|
}
|
2012-03-24 01:20:19 +01:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
if(!$validator) $validator = new RequiredFields('CsvFile');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator);
|
|
|
|
|
2016-09-23 06:13:09 +02:00
|
|
|
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/vendor.js');
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberImportForm.js');
|
FIX Webpack handles images & fonts.
Responsibility for finding and referencing images and fonts is now
given to webpack. All the url references are now relative to the
component scss file, and point to font & images files in src/, rather
than assuming someone else will place them in dist.
This makes the source more modular, and makes it easier to, for
example, inline images are data URIs, or create a new build script that
builds several modules for a project in a single pass.
Workaround for bad font path in bundle.css:
ExtactTextPlugin didn’t work as well with a subfolder reference in the
filename. This is just a short-term fix and could probably be improved
to put bundle.css back in the styles subfolder.
Webpack handles images & fonts:
Responsibility for finding and referencing images and fonts is now
given to webpack. All the url references are now relative to the
component scss file, and point to font & images files in src/, rather
than assuming someone else will place them in dist.
This makes the source more modular, and makes it easier to, for
example, inline images are data URIs, or create a new build script that
builds several modules for a project in a single pass.
Clarify docs on spriting and webfonts:
We've decided to remove sprity since it comes with hundreds of dependencies,
and needs compilation within the "npm install" - dragging out the already overweight
install process, and making the resulting node_modules/ folder less portable between systems.
2016-08-26 05:37:43 +02:00
|
|
|
Requirements::css(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css');
|
2012-03-24 01:20:19 +01:00
|
|
|
|
|
|
|
$this->addExtraClass('cms');
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->addExtraClass('import-form');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doImport($data, $form) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$loader = new MemberCsvBulkLoader();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// optionally set group relation
|
|
|
|
if($this->group) $loader->setGroups(array($this->group));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// load file
|
|
|
|
$result = $loader->load($data['CsvFile']['tmp_name']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
// result message
|
|
|
|
$msgArr = array();
|
2012-05-01 21:44:54 +02:00
|
|
|
if($result->CreatedCount()) $msgArr[] = _t(
|
|
|
|
'MemberImportForm.ResultCreated', 'Created {count} members',
|
|
|
|
array('count' => $result->CreatedCount())
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-05-01 21:44:54 +02:00
|
|
|
if($result->UpdatedCount()) $msgArr[] = _t(
|
|
|
|
'MemberImportForm.ResultUpdated', 'Updated {count} members',
|
|
|
|
array('count' => $result->UpdatedCount())
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
2012-05-01 21:44:54 +02:00
|
|
|
if($result->DeletedCount()) $msgArr[] = _t(
|
|
|
|
'MemberImportForm.ResultDeleted', 'Deleted %d members',
|
|
|
|
array('count' => $result->DeletedCount())
|
2011-03-23 10:51:00 +01:00
|
|
|
);
|
|
|
|
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->sessionMessage($msg, 'good');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->controller->redirectBack();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* @param $group Group
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setGroup($group) {
|
2011-03-23 10:51:00 +01:00
|
|
|
$this->group = $group;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-23 10:51:00 +01:00
|
|
|
/**
|
|
|
|
* @return Group
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getGroup($group) {
|
2011-03-23 10:51:00 +01:00
|
|
|
return $this->group;
|
|
|
|
}
|
|
|
|
}
|