GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

GridFieldDropdownAddNewButton   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 12
dl 0
loc 189
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 16 4
A setClasses() 0 4 1
A getClasses() 0 4 1
B getHTMLFragments() 0 56 5
A getActions() 0 3 1
C handleAction() 0 33 8
A isValidClass() 0 8 2
1
<?php
2
3
/**
4
 * This component creates a dropdown of possible data object classes and a button to create a new instance.
5
 *
6
 */
7
class GridFieldDropdownAddNewButton extends GridFieldAddNewButton
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...
Coding Style introduced by
Expected 0 spaces between "GridFieldAddNewButton" and comma; 1 found
Loading history...
8
	implements GridField_ActionProvider {
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
9
10
	/**
11
	 * Class names
12
	 *
13
	 * @var array
14
	 */
15
	protected $modelClasses = null;
16
17
	/**
18
	 * This is because this doesn't extend Object
19
	 *
20
	 */
21
	public static function create($baseClass, $classes, $targetFragment = 'buttons-before-left')
22
	{
23
		return new GridFieldDropdownAddNewButton($baseClass, $classes, $targetFragment);
24
	}
25
26
	/**
27
	 * @param string $baseClass
28
	 * @param array $classes Class or list of classes to create.
29
	 * @param string $targetFragment The fragment to render the button into
30
	 */
31
	public function __construct($baseClass, $classes, $targetFragment = 'buttons-before-left')
32
	{
33
		if (!is_array($classes)) {
34
			user_error('$classes is not an array', E_USER_ERROR);
35
		}
36
37
		$this->setClasses($classes);
38
39
		foreach($this->getClasses() as $class => $nice){
40
			if(!is_subclass_of($class, $baseClass)){
41
				user_error(sprintf('%s is not a subclass of %s', $class, $baseClass), E_USER_ERROR);
42
			}
43
		}
44
45
		parent::__construct($targetFragment);
46
	}
47
48
	/**
49
	 * Specify the classes to create
50
	 *
51
	 * @param array $classes
52
	 */
53
	public function setClasses($classes)
54
	{
55
		$this->modelClasses = $classes;
56
	}
57
58
	/**
59
	 * Get the classes of the objects to create
60
	 *
61
	 * @return array
62
	 */
63
	public function getClasses()
64
	{
65
		return $this->modelClasses;
66
	}
67
68
	/**
69
	 * Abstract method to fill out. Gets the HTML for this component
70
	 * @param $gridField GridField
71
	 */
72
	public function getHTMLFragments($gridField)
73
	{
74
		$state = $gridField->State->GridFieldDropdownAddNewButton;
75
		$classesSource = $this->getClasses();
76
77
		if(empty($classesSource)) {
78
			return [];
79
		} else if(count($classesSource) > 1) {
80
			$dropdown = DropdownField::create(
81
				"Class",
82
				"Class",
83
				$classesSource
84
			)->setFieldHolderTemplate("GridFieldDropdownAddNewButton_holder")
85
			->addExtraClass("gridfield-dropdown no-change-track");
86
87
			if (!$this->buttonName) {
88
				$this->buttonName = 'Add new';
89
			}
90
		} else {
91
			$class = key($classesSource);
92
			$dropdown = HiddenField::create(
93
				"Class",
94
				"Class",
95
				$class
96
			);
97
98
			if (!$this->buttonName) {
99
				$this->buttonName = sprintf('Add new %s', $class);
100
			}
101
		}
102
103
		$state->class = key($classesSource);
104
105
		$action = GridField_FormAction::create(
106
			$gridField,
107
			'add',
108
			$this->buttonName,
109
			'add',
110
			'add'
111
		)->setAttribute(
112
			'data-icon',
113
			'add'
114
		)->addExtraClass("no-ajax ss-ui-action-constructive dropdown-action");
115
116
		Requirements::css(CONTENTBLOCKS_DIR . "/css/GridFieldDropdownAddNewButton.css");
117
		Requirements::javascript(CONTENTBLOCKS_DIR . "/javascript/GridFieldDropdownAddNewButton.js");
118
119
		return [
120
			$this->targetFragment => ArrayData::create([
121
				'Fields' => ArrayList::create([
122
					$dropdown,
123
					$action,
124
				]),
125
			])->renderWith("GridFieldDropdownAddNewButton"),
126
		];
127
	}
128
129
	/**
130
	 * Provide actions to this component.
131
	 *
132
	 * @param GridField $gridField
133
	 * @return array
134
	**/
135
	public function getActions($gridField) {
136
		return ["add"];
137
	}
138
139
	/**
140
	 * Handles the add action
141
	 *
142
	 * @param GridField $gridField
143
	 * @param string $actionName
144
	 * @param mixed $arguments
145
	 * @param array $data
146
	**/
147
	public function handleAction(GridField $gridField, $actionName, $arguments, $data)
148
	{
149
		$response = $gridField->getForm()->controller->response;
150
151
		if(in_array(strtolower($actionName), $this->getActions($gridField))) {
152
			$class = null;
153
			$name = $gridField->getName();
154
			if (isset($data[$name]) && isset($data[$name]['GridState'])){
155
				$state = json_decode($data[$name]['GridState'], true);
156
				// Debug::dump($state);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157
				if (isset($state['GridFieldDropdownAddNewButton'])
158
				&& isset($state['GridFieldDropdownAddNewButton']['class'])) {
159
					$class = $state['GridFieldDropdownAddNewButton']['class'];
160
				}
161
			}
162
163
			if(!$class || !$this->isValidClass($class)) {
164
				return Controller::curr()->redirectBack();
165
			}
166
167
			$list = $gridField->getList();
168
			$object = $class::create();
169
			$object->write();
170
			$list->add($object);
171
172
			$url = Controller::join_links($gridField->link('item'), $object->ID);
173
174
			$response->redirect(Director::absoluteBaseURL() . $url);
175
			return $response;
176
		}
177
178
		return Controller::curr()->redirectBack();
179
	}
180
181
	/**
182
	 * Validates that a class is okay for creation
183
	 *
184
	 * @param string
185
	 * @return bool
186
	 */
187
	public function isValidClass($class)
188
	{
189
		if (!array_key_exists($class, $this->getClasses())) {
190
			return false;
191
		}
192
193
		return singleton($class)->canCreate();
194
	}
195
}
196