Completed
Push — 3.7 ( 81b2d8...ef0909 )
by
unknown
09:42
created

GroupImportForm::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 5
dl 0
loc 52
rs 9.0472
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
/**
4
 * Imports {@link Group} records by CSV upload, as defined in
5
 * {@link GroupCsvBulkLoader}.
6
 *
7
 * @package framework
8
 * @subpackage admin
9
 */
10
class GroupImportForm extends Form {
11
12
	/**
13
	 * @var Group Optional group relation
14
	 */
15
	protected $group;
16
17
	public function __construct($controller, $name, $fields = null, $actions = null, $validator = null) {
18
		if(!$fields) {
19
			$helpHtml = _t(
20
				'GroupImportForm.Help1',
21
				'<p>Import one or more groups in <em>CSV</em> format (comma-separated values).'
22
				. ' <small><a href="#" class="toggle-advanced">Show advanced usage</a></small></p>'
23
			);
24
			$helpHtml .= _t(
25
				'GroupImportForm.Help2',
26
				'<div class="advanced">'
27
				. '<h4>Advanced usage</h4>'
28
				. '<ul>'
29
				. '<li>Allowed columns: <em>%s</em></li>'
30
				. '<li>Existing groups are matched by their unique <em>Code</em> value, and updated with any new values from the '
31
				. 'imported file</li>'
32
				. '<li>Group hierarchies can be created by using a <em>ParentCode</em> column.</li>'
33
				. '<li>Permission codes can be assigned by the <em>PermissionCode</em> column. Existing permission codes are not '
34
				. 'cleared.</li>'
35
				. '</ul>'
36
				. '</div>'
37
			);
38
39
			$importer = new GroupCsvBulkLoader();
40
			$importSpec = $importer->getImportSpec();
41
			$helpHtml = sprintf($helpHtml, implode(', ', array_keys($importSpec['fields'])));
42
43
			$fields = new FieldList(
44
				new LiteralField('Help', $helpHtml),
45
				$fileField = new FileField(
46
					'CsvFile',
47
					_t(
48
						'SecurityAdmin_MemberImportForm.FileFieldLabel',
49
						'CSV File <small>(Allowed extensions: *.csv)</small>'
50
					)
51
				)
52
			);
53
			$fileField->getValidator()->setAllowedExtensions(array('csv'));
54
		}
55
56
		if(!$actions) {
57
			$action = new FormAction('doImport', _t('SecurityAdmin_MemberImportForm.BtnImport', 'Import from CSV'));
58
			$action->addExtraClass('ss-ui-button');
59
			$actions = new FieldList($action);
60
		}
61
62
		if(!$validator) $validator = new RequiredFields('CsvFile');
63
64
		parent::__construct($controller, $name, $fields, $actions, $validator);
65
66
		$this->addExtraClass('cms');
67
		$this->addExtraClass('import-form');
68
	}
69
70
	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...
71
		$loader = new GroupCsvBulkLoader();
72
73
		// load file
74
		$result = $loader->load($data['CsvFile']['tmp_name']);
75
76
		// result message
77
		$msgArr = array();
78
		if($result->CreatedCount()) $msgArr[] = _t(
79
			'GroupImportForm.ResultCreated', 'Created {count} groups',
80
			array('count' => $result->CreatedCount())
81
		);
82
		if($result->UpdatedCount()) $msgArr[] = _t(
83
			'GroupImportForm.ResultUpdated', 'Updated %d groups',
84
			array('count' => $result->UpdatedCount())
85
		);
86
		if($result->DeletedCount()) $msgArr[] = _t(
87
			'GroupImportForm.ResultDeleted', 'Deleted %d groups',
88
			array('count' => $result->DeletedCount())
89
		);
90
		$msg = ($msgArr) ? implode(',', $msgArr) : _t('MemberImportForm.ResultNone', 'No changes');
91
92
		$this->sessionMessage($msg, 'good');
93
94
		$this->controller->redirectBack();
95
	}
96
97
}
98