Completed
Push — master ( bbb282...43d0b8 )
by Daniel
25s
created

GroupImportForm::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 52
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 38
nc 8
nop 5
dl 0
loc 52
rs 8.9408
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\Admin;
4
5
6
use SilverStripe\Security\Group;
7
use SilverStripe\Security\GroupCsvBulkLoader;
8
use Form;
9
use FieldList;
10
use LiteralField;
11
use FileField;
12
use FormAction;
13
use RequiredFields;
14
15
16
/**
17
 * Imports {@link Group} records by CSV upload, as defined in
18
 * {@link GroupCsvBulkLoader}.
19
 *
20
 * @package framework
21
 * @subpackage admin
22
 */
23
class GroupImportForm extends Form {
24
25
	/**
26
	 * @var Group Optional group relation
27
	 */
28
	protected $group;
29
30
	public function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
31
		if(!$fields) {
32
			$helpHtml = _t(
33
				'GroupImportForm.Help1',
34
				'<p>Import one or more groups in <em>CSV</em> format (comma-separated values).'
35
				. ' <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'
36
			);
37
			$helpHtml .= _t(
38
				'GroupImportForm.Help2',
39
				'<div class="advanced">'
40
				. '<h4>Advanced usage</h4>'
41
				. '<ul>'
42
				. '<li>Allowed columns: <em>%s</em></li>'
43
				. '<li>Existing groups are matched by their unique <em>Code</em> value, and updated with any new values from the '
44
				. 'imported file</li>'
45
				. '<li>Group hierarchies can be created by using a <em>ParentCode</em> column.</li>'
46
				. '<li>Permission codes can be assigned by the <em>PermissionCode</em> column. Existing permission codes are not '
47
				. 'cleared.</li>'
48
				. '</ul>'
49
				. '</div>'
50
			);
51
52
			$importer = new GroupCsvBulkLoader();
53
			$importSpec = $importer->getImportSpec();
54
			$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
55
56
			$fields = new FieldList(
57
				new LiteralField('Help', $helpHtml),
58
				$fileField = new FileField(
59
					'CsvFile',
60
					_t(
61
						'SecurityAdmin_MemberImportForm.FileFieldLabel',
62
						'CSV File <small>(Allowed extensions: *.csv)</small>'
63
					)
64
				)
65
			);
66
			$fileField->getValidator()->setAllowedExtensions(array('csv'));
67
		}
68
69
		if(!$actions) {
70
			$action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV'));
71
			$action->addExtraClass('ss-ui-button');
72
			$actions = new FieldList($action);
73
		}
74
75
		if(!$validator) $validator = new RequiredFields('CsvFile');
76
77
		parent::__construct($controller, $name, $fields, $actions, $validator);
78
79
		$this->addExtraClass('cms');
80
		$this->addExtraClass('import-form');
81
	}
82
83
	public function doImport($data, $form) {
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
		$loader = new GroupCsvBulkLoader();
85
86
		// load file
87
		$result = $loader->load($data['CsvFile']['tmp_name']);
88
89
		// result message
90
		$msgArr = array();
91
		if($result->CreatedCount()) $msgArr[] = _t(
92
			'GroupImportForm.ResultCreated', 'Created {count} groups',
93
			array('count' => $result->CreatedCount())
94
		);
95
		if($result->UpdatedCount()) $msgArr[] = _t(
96
			'GroupImportForm.ResultUpdated', 'Updated %d groups',
97
			array('count' => $result->UpdatedCount())
98
		);
99
		if($result->DeletedCount()) $msgArr[] = _t(
100
			'GroupImportForm.ResultDeleted', 'Deleted %d groups',
101
			array('count' => $result->DeletedCount())
102
		);
103
		$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
104
105
		$this->sessionMessage($msg, 'good');
106
107
		$this->controller->redirectBack();
108
	}
109
110
}
111