Completed
Pull Request — master (#420)
by
unknown
34:14
created

UserFormAddNewClassesList::getFieldClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * A dropdown and button which allows objects to be created that was selected from the dropdown
5
 */
6
class UserFormAddNewClassesList extends GridFieldAddClassesButton implements GridField_ActionProvider {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
8
	/**
9
	 * default value for the dropdown
10
	 */
11
	protected $defaultClass;
12
13
	/**
14
	 * @param array $default Class to be selected by default.
15
	 * @param string $targetFragment The fragment to render the button into
16
	 */
17
	public function __construct($default = null, $targetFragment = 'buttons-before-left') {
18
		parent::__construct(array(), $targetFragment);
19
		$this->setDefaultClass($default);
0 ignored issues
show
Documentation introduced by
$default is of type array|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
20
	}
21
22
	/**
23
	 * {@inheritDoc}
24
	 */
25
	public function getHTMLFragments($grid) {
26
		$classes = $this->getFieldClasses();
27
28
		if(!count($classes)) {
29
			return array();
30
		}
31
32
		$field = new DropdownField(sprintf('%s[ClassName]', __CLASS__), '', $classes, $this->defaultClass);
33
		$field->addExtraClass('no-change-track');
34
35
		$formAction = new GridField_FormAction(
36
			$grid,
37
			$this->getAction(),
38
			$this->getButtonName(),
39
			$this->getAction(),
40
			array()
41
		);
42
		$formAction->setAttribute('data-icon', 'add');
43
44
		if($this->getButtonClass()) {
45
			$formAction->addExtraClass($this->getButtonClass());
46
		}
47
48
		$data = new ArrayData(array(
49
			'FormAction' => $formAction,
50
			'ClassField' => $field
51
		));
52
53
		return array(
54
			$this->getFragment() => $data->renderWith('UserFormAddNewClassesList')
55
		);
56
	}
57
58
	/**
59
	 * Handles adding a new instance of a selected class.
60
	 *
61
	 * @param GridField $grid
62
	 * @param Array $data from request
63
	 * @return null
64
	 */
65
	public function handleAdd($grid, $data) {
66
		$class = $this->getSelectedClass($data);
67
68
		if(!$class) {
69
			throw new SS_HTTPResponse_Exception(400);
70
		}
71
72
		$this->setClasses($class);
73
74
		// Should trigger a simple reload
75
		return parent::handleAdd($grid);
76
	}
77
78
	/**
79
	 * Sets the default class that is selected automatically.
80
	 *
81
	 * @param string $default the class name to use as default
82
	 * @return UserFormAddNewClassesList $this
83
	 */
84
	public function setDefaultClass($default) {
85
		$this->defaultClass = $default;
86
		return $this;
87
	}
88
89
	/**
90
	 * Get the action suburl for this component
91
	 *
92
	 * @return string
93
	 */
94
	protected function getAction() {
95
		return 'add-classes-list';
96
	}
97
98
	/**
99
	 * {@inheritDoc}
100
	 */
101 View Code Duplication
	public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
		switch(strtolower($actionName)) {
103
			case $this->getAction():
104
				return $this->handleAdd($gridField, $data);
105
			default:
106
				return null;
107
		}
108
	}
109
110
	/**
111
	 * Get the list of classes that can be selected and created
112
	 *
113
	 * @return array
114
	 */
115
	public function getFieldClasses() {
116
		return singleton('EditableFormField')->getEditableFieldClasses();
117
	}
118
119
	/**
120
	 * Gets the selected value from the request data array
121
	 *
122
	 * @param array $data from request
123
	 * @return string|null;
0 ignored issues
show
Documentation introduced by
The doc-type string|null; could not be parsed: Expected "|" or "end of type", but got ";" at position 11. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
124
	 */
125
	public function getSelectedClass($data = null) {
126
		$classes = $this->getFieldClasses();
127
128
		$class = null;
129
		if (is_array($data) && isset($data[__CLASS__]['ClassName'])) {
130
			$class = $data[__CLASS__]['ClassName'];
131
		}
132
133
		if ($class && !array_key_exists($class, $classes)) {
134
			throw new SS_HTTPResponse_Exception(400);
135
		}
136
137
		return $class;
138
	}
139
}