silverstripe-framework/admin/code/GroupImportForm.php

106 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2013-10-15 00:26:23 +02:00
2016-08-11 01:14:02 +02:00
namespace SilverStripe\Admin;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Form;
2016-06-23 01:37:22 +02:00
use SilverStripe\Security\Group;
use SilverStripe\Security\GroupCsvBulkLoader;
/**
* Imports {@link Group} records by CSV upload, as defined in
* {@link GroupCsvBulkLoader}.
*/
class GroupImportForm extends Form {
2014-08-15 08:53:05 +02:00
/**
* @var Group Optional group relation
*/
protected $group;
2014-08-15 08:53:05 +02:00
public function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
if(!$fields) {
$helpHtml = _t(
2014-08-15 08:53:05 +02:00
'GroupImportForm.Help1',
'<p>Import one or more groups in <em>CSV</em> format (comma-separated values).'
. ' <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'
);
$helpHtml .= _t(
2014-08-15 08:53:05 +02:00
'GroupImportForm.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 groups are matched by their unique <em>Code</em> value, and updated with any new values from the '
. 'imported file</li>'
. '<li>Group hierarchies can be created by using a <em>ParentCode</em> column.</li>'
. '<li>Permission codes can be assigned by the <em>PermissionCode</em> column. Existing permission codes are not '
. 'cleared.</li>'
. '</ul>'
. '</div>'
);
2014-08-15 08:53:05 +02:00
$importer = new GroupCsvBulkLoader();
$importSpec = $importer->getImportSpec();
$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
2014-08-15 08:53:05 +02:00
$fields = new FieldList(
new LiteralField('Help', $helpHtml),
$fileField = new FileField(
2014-08-15 08:53:05 +02:00
'CsvFile',
_t(
2014-08-15 08:53:05 +02:00
'SecurityAdmin_MemberImportForm.FileFieldLabel',
'CSV File <small>(Allowed extensions: *.csv)</small>'
)
)
);
$fileField->getValidator()->setAllowedExtensions(array('csv'));
}
2014-08-15 08:53:05 +02:00
if(!$actions) {
$action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV'));
$action->addExtraClass('ss-ui-button');
$actions = new FieldList($action);
}
2014-08-15 08:53:05 +02:00
if(!$validator) $validator = new RequiredFields('CsvFile');
2014-08-15 08:53:05 +02:00
parent::__construct($controller, $name, $fields, $actions, $validator);
$this->addExtraClass('cms');
$this->addExtraClass('import-form');
}
2014-08-15 08:53:05 +02:00
public function doImport($data, $form) {
$loader = new GroupCsvBulkLoader();
2014-08-15 08:53:05 +02:00
// load file
$result = $loader->load($data['CsvFile']['tmp_name']);
2014-08-15 08:53:05 +02:00
// result message
$msgArr = array();
if($result->CreatedCount()) $msgArr[] = _t(
'GroupImportForm.ResultCreated', 'Created {count} groups',
array('count' => $result->CreatedCount())
);
if($result->UpdatedCount()) $msgArr[] = _t(
'GroupImportForm.ResultUpdated', 'Updated %d groups',
array('count' => $result->UpdatedCount())
);
if($result->DeletedCount()) $msgArr[] = _t(
'GroupImportForm.ResultDeleted', 'Deleted %d groups',
array('count' => $result->DeletedCount())
);
$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
2014-08-15 08:53:05 +02:00
$this->sessionMessage($msg, 'good');
2014-08-15 08:53:05 +02:00
$this->controller->redirectBack();
}
2014-08-15 08:53:05 +02:00
}