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 { |
|
|
|
|
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); |
|
|
|
|
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) { |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.