-
Notifications
You must be signed in to change notification settings - Fork 125
/
GridFieldAddNewMultiClass.php
executable file
·301 lines (260 loc) · 7.84 KB
/
GridFieldAddNewMultiClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
namespace Symbiote\GridFieldExtensions;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_URLHandler;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\View\ArrayData;
use ReflectionClass;
use Exception;
/**
* A component which lets the user select from a list of classes to create a new record form.
*
* By default the list of classes that are createable is the grid field's model class, and any
* subclasses. This can be customised using {@link setClasses()}.
*/
class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URLHandler
{
/**
* @skipUpgrade
*/
const POST_KEY = 'GridFieldAddNewMultiClass';
private static $allowed_actions = array(
'handleAdd'
);
// Should we add an empty string to the add class dropdown?
private static $showEmptyString = true;
private $fragment;
private $title;
/**
* @var array
*/
private $classes;
/**
* @var string
*/
private $defaultClass;
/**
* @var string
*/
protected $itemRequestClass = 'Symbiote\\GridFieldExtensions\\GridFieldAddNewMultiClassHandler';
/**
* @param string $fragment the fragment to render the button in
*/
public function __construct($fragment = 'before')
{
$this->setFragment($fragment);
$this->setTitle(_t('GridFieldExtensions.ADD', 'Add'));
}
/**
* Gets the fragment name this button is rendered into.
*
* @return string
*/
public function getFragment()
{
return $this->fragment;
}
/**
* Sets the fragment name this button is rendered into.
*
* @param string $fragment
* @return GridFieldAddNewMultiClass $this
*/
public function setFragment($fragment)
{
$this->fragment = $fragment;
return $this;
}
/**
* Gets the button title text.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets the button title text.
*
* @param string $title
* @return GridFieldAddNewMultiClass $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Gets the classes that can be created using this button, defaulting to the model class and
* its subclasses.
*
* @param GridField $grid
* @return array a map of class name to title
*/
public function getClasses(GridField $grid)
{
$result = array();
if (is_null($this->classes)) {
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()));
sort($classes);
} else {
$classes = $this->classes;
}
$kill_ancestors = array();
foreach ($classes as $class => $title) {
if (!is_string($class)) {
$class = $title;
}
if (!class_exists($class)) {
continue;
}
$is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract());
if (!$is_abstract && $class === $title) {
$title = singleton($class)->i18n_singular_name();
}
if ($ancestor_to_hide = Config::inst()->get($class, 'hide_ancestor')) {
$kill_ancestors[$ancestor_to_hide] = true;
}
if ($is_abstract || !singleton($class)->canCreate()) {
continue;
}
$result[$class] = $title;
}
if ($kill_ancestors) {
foreach ($kill_ancestors as $class => $bool) {
unset($result[$class]);
}
}
$sanitised = array();
foreach ($result as $class => $title) {
$sanitised[$this->sanitiseClassName($class)] = $title;
}
return $sanitised;
}
/**
* Sets the classes that can be created using this button.
*
* @param array $classes a set of class names, optionally mapped to titles
* @param string $default
* @return GridFieldAddNewMultiClass $this
*/
public function setClasses(array $classes, $default = null)
{
$this->classes = $classes;
if ($default) {
$this->defaultClass = $default;
}
return $this;
}
/**
* Sets the default class that is selected automatically.
*
* @param string $default the class name to use as default
* @return GridFieldAddNewMultiClass $this
*/
public function setDefaultClass($default)
{
$this->defaultClass = $default;
return $this;
}
/**
* Handles adding a new instance of a selected class.
*
* @param GridField $grid
* @param HTTPRequest $request
* @return GridFieldAddNewMultiClassHandler
* @throws Exception
* @throws HTTPResponse_Exception
*/
public function handleAdd($grid, $request)
{
$class = $request->param('ClassName');
$classes = $this->getClasses($grid);
/** @var GridFieldDetailForm $component */
$component = $grid->getConfig()->getComponentByType(GridFieldDetailForm::class);
if (!$component) {
throw new Exception('The add new multi class component requires the detail form component.');
}
if (!$class || !array_key_exists($class, $classes)) {
throw new HTTPResponse_Exception(400);
}
$unsanitisedClass = $this->unsanitiseClassName($class);
$handler = Injector::inst()->create(
$this->itemRequestClass,
$grid,
$component,
new $unsanitisedClass(),
$grid->getForm()->getController(),
'add-multi-class'
);
$handler->setTemplate($component->getTemplate());
return $handler;
}
/**
* {@inheritDoc}
*/
public function getHTMLFragments($grid)
{
$classes = $this->getClasses($grid);
if (!count($classes)) {
return array();
}
GridFieldExtensions::include_requirements();
$field = new DropdownField(sprintf('%s[%s]', __CLASS__, $grid->getName()), '', $classes, $this->defaultClass);
if (Config::inst()->get(__CLASS__, 'showEmptyString')) {
$field->setEmptyString(_t('GridFieldExtensions.SELECTTYPETOCREATE', '(Select type to create)'));
}
$field->addExtraClass('no-change-track');
$data = new ArrayData(array(
'Title' => $this->getTitle(),
'Link' => Controller::join_links($grid->Link(), 'add-multi-class', '{class}'),
'ClassField' => $field
));
return array(
$this->getFragment() => $data->renderWith(__CLASS__)
);
}
/**
* {@inheritDoc}
*/
public function getURLHandlers($grid)
{
return array(
'add-multi-class/$ClassName!' => 'handleAdd'
);
}
public function setItemRequestClass($class)
{
$this->itemRequestClass = $class;
return $this;
}
/**
* Sanitise a model class' name for inclusion in a link
*
* @param string $class
* @return string
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class);
}
/**
* Unsanitise a model class' name from a URL param
*
* @param string $class
* @return string
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', $class);
}
}