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 Object implements GridField_HTMLProvider, GridField_ActionProvider { |
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Name of fragment to insert into |
10
|
|
|
* |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $targetFragment; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Button title |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $buttonName; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Additonal CSS classes for the button |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $buttonClass = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* default value for the dropdown |
31
|
|
|
*/ |
32
|
|
|
protected $defaultClass; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $default Class to be selected by default. |
36
|
|
|
* @param string $targetFragment The fragment to render the button into |
37
|
|
|
*/ |
38
|
|
|
public function __construct($default = null, $targetFragment = 'buttons-before-left') { |
39
|
|
|
$this->setFragment($targetFragment); |
40
|
|
|
$this->setDefaultClass($default); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public function getHTMLFragments($grid) { |
47
|
|
|
$classes = $this->getFieldClasses(); |
48
|
|
|
|
49
|
|
|
if(!count($classes)) { |
50
|
|
|
return array(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$field = new DropdownField(sprintf('%s[ClassName]', __CLASS__), '', $classes, $this->defaultClass); |
54
|
|
|
$field->addExtraClass('no-change-track'); |
55
|
|
|
|
56
|
|
|
$formAction = new GridField_FormAction( |
57
|
|
|
$grid, |
58
|
|
|
$this->getAction(), |
59
|
|
|
$this->getButtonName(), |
60
|
|
|
$this->getAction(), |
61
|
|
|
array() |
62
|
|
|
); |
63
|
|
|
$formAction->setAttribute('data-icon', 'add'); |
64
|
|
|
|
65
|
|
|
if($buttonClass = $this->getButtonClass()) { |
66
|
|
|
$formAction->addExtraClass($buttonClass); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$data = new ArrayData(array( |
70
|
|
|
'FormAction' => $formAction, |
71
|
|
|
'ClassField' => $field |
72
|
|
|
)); |
73
|
|
|
|
74
|
|
|
return array( |
75
|
|
|
$this->getFragment() => $data->renderWith('UserFormAddNewClassesList') |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get extra button class |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getButtonClass() { |
85
|
|
|
return $this->buttonClass; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Sets extra CSS classes for this button |
90
|
|
|
* |
91
|
|
|
* @param string $buttonClass |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setButtonClass($buttonClass) { |
95
|
|
|
$this->buttonClass = $buttonClass; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Change the button name |
101
|
|
|
* |
102
|
|
|
* @param string $name |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setButtonName($name) { |
106
|
|
|
$this->buttonName = $name; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the button name |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getButtonName() { |
116
|
|
|
return $this->buttonName; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets the fragment name this button is rendered into. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getFragment() { |
125
|
|
|
return $this->targetFragment; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets the fragment name this button is rendered into. |
130
|
|
|
* |
131
|
|
|
* @param string $fragment |
132
|
|
|
* @return GridFieldAddNewInlineButton $this |
133
|
|
|
*/ |
134
|
|
|
public function setFragment($fragment) { |
135
|
|
|
$this->targetFragment = $fragment; |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Handles adding a new instance of a selected class. |
141
|
|
|
* |
142
|
|
|
* @param GridField $grid |
143
|
|
|
* @param Array $data from request |
144
|
|
|
* @return null |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function handleAdd($grid, $data) { |
|
|
|
|
147
|
|
|
$class = $this->getSelectedClass($data); |
148
|
|
|
|
149
|
|
|
if(!$class) { |
150
|
|
|
throw new SS_HTTPResponse_Exception(400); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Add item to gridfield |
154
|
|
|
$list = $grid->getList(); |
155
|
|
|
$item = $class::create(); |
156
|
|
|
$item->write(); |
157
|
|
|
$list->add($item); |
158
|
|
|
|
159
|
|
|
// Should trigger a simple reload |
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Gets the default class that is selected automatically. |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getDefaultClass() { |
169
|
|
|
return $this->defaultClass; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sets the default class that is selected automatically. |
174
|
|
|
* |
175
|
|
|
* @param string $default the class name to use as default |
176
|
|
|
* @return UserFormAddNewClassesList $this |
177
|
|
|
*/ |
178
|
|
|
public function setDefaultClass($default) { |
179
|
|
|
$this->defaultClass = $default; |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* {@inheritDoc} |
185
|
|
|
*/ |
186
|
|
|
public function getActions($gridField) { |
187
|
|
|
return array( |
188
|
|
|
$this->getAction() |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get the action suburl for this component |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
protected function getAction() { |
198
|
|
|
return 'add-classes-list'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritDoc} |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
public function handleAction(GridField $gridField, $actionName, $arguments, $data) { |
|
|
|
|
205
|
|
|
switch(strtolower($actionName)) { |
206
|
|
|
case $this->getAction(): |
207
|
|
|
return $this->handleAdd($gridField, $data); |
208
|
|
|
default: |
209
|
|
|
return null; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the list of classes that can be selected and created |
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
public function getFieldClasses() { |
219
|
|
|
return singleton('EditableFormField')->getEditableFieldClasses(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Gets the selected value from the request data array |
224
|
|
|
* |
225
|
|
|
* @param array $data from request |
226
|
|
|
* @return string|null; |
|
|
|
|
227
|
|
|
*/ |
228
|
|
|
public function getSelectedClass($data = null) { |
229
|
|
|
$classes = $this->getFieldClasses(); |
230
|
|
|
|
231
|
|
|
$class = null; |
232
|
|
|
if(is_array($data) && isset($data[__CLASS__]['ClassName'])) { |
233
|
|
|
$class = $data[__CLASS__]['ClassName']; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if($class && !array_key_exists($class, $classes)) { |
237
|
|
|
throw new SS_HTTPResponse_Exception(400); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $class; |
241
|
|
|
} |
242
|
|
|
} |
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.