1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\GridField; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\Control\HTTPResponse; |
8
|
|
|
use SilverStripe\Control\RequestHandler; |
9
|
|
|
use SilverStripe\Core\ClassInfo; |
10
|
|
|
use SilverStripe\Core\Extensible; |
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
12
|
|
|
use SilverStripe\Forms\FieldList; |
13
|
|
|
use SilverStripe\Forms\Validator; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
use SilverStripe\ORM\Filterable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Provides view and edit forms at GridField-specific URLs. |
19
|
|
|
* |
20
|
|
|
* These can be placed into pop-ups by an appropriate front-end. |
21
|
|
|
* |
22
|
|
|
* Usually added to a {@link GridField} alongside of a |
23
|
|
|
* {@link GridFieldEditButton} which takes care of linking the |
24
|
|
|
* individual rows to their edit view. |
25
|
|
|
* |
26
|
|
|
* The URLs provided will be off the following form: |
27
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID> |
28
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>/edit |
29
|
|
|
*/ |
30
|
|
|
class GridFieldDetailForm implements GridField_URLHandler |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
use Extensible; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $template = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $name; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected $showPagination; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
protected $showAdd; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Validator The form validator used for both add and edit fields. |
57
|
|
|
*/ |
58
|
|
|
protected $validator; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var FieldList Falls back to {@link DataObject->getCMSFields()} if not defined. |
62
|
|
|
*/ |
63
|
|
|
protected $fields; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $itemRequestClass; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var callable With two parameters: $form and $component |
72
|
|
|
*/ |
73
|
|
|
protected $itemEditFormCallback; |
74
|
|
|
|
75
|
|
|
public function getURLHandlers($gridField) |
76
|
|
|
{ |
77
|
|
|
return array( |
78
|
|
|
'item/$ID' => 'handleItem' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a popup component. The two arguments will specify how the popup form's HTML and |
84
|
|
|
* behaviour is created. The given controller will be customised, putting the edit form into the |
85
|
|
|
* template with the given name. |
86
|
|
|
* |
87
|
|
|
* The arguments are experimental API's to support partial content to be passed back to whatever |
88
|
|
|
* controller who wants to display the getCMSFields |
89
|
|
|
* |
90
|
|
|
* @param string $name The name of the edit form to place into the pop-up form |
91
|
|
|
* @param bool $showPagination Whether the `Previous` and `Next` buttons should display or not, leave as null to use default |
92
|
|
|
* @param bool $showAdd Whether the `Add` button should display or not, leave as null to use default |
93
|
|
|
*/ |
94
|
|
|
public function __construct($name = null, $showPagination = null, $showAdd = null) |
95
|
|
|
{ |
96
|
|
|
$this->setName($name ?: 'DetailForm'); |
97
|
|
|
$this->setShowPagination($showPagination); |
98
|
|
|
$this->setShowAdd($showAdd); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
* @param GridField $gridField |
104
|
|
|
* @param HTTPRequest $request |
105
|
|
|
* @return HTTPResponse |
106
|
|
|
*/ |
107
|
|
|
public function handleItem($gridField, $request) |
108
|
|
|
{ |
109
|
|
|
if ($gridStateStr = $request->getVar('gridState')) { |
110
|
|
|
$gridField->getState(false)->setValue($gridStateStr); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Our getController could either give us a true Controller, if this is the top-level GridField. |
114
|
|
|
// It could also give us a RequestHandler in the form of GridFieldDetailForm_ItemRequest if this is a |
115
|
|
|
// nested GridField. |
116
|
|
|
$requestHandler = $gridField->getForm()->getController(); |
117
|
|
|
|
118
|
|
|
/** @var DataObject $record */ |
119
|
|
|
if (is_numeric($request->param('ID'))) { |
120
|
|
|
/** @var Filterable $dataList */ |
121
|
|
|
$dataList = $gridField->getList(); |
122
|
|
|
$record = $dataList->byID($request->param('ID')); |
|
|
|
|
123
|
|
|
} else { |
124
|
|
|
$record = Injector::inst()->create($gridField->getModelClass()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$handler = $this->getItemRequestHandler($gridField, $record, $requestHandler); |
128
|
|
|
|
129
|
|
|
// if no validator has been set on the GridField and the record has a |
130
|
|
|
// CMS validator, use that. |
131
|
|
|
if (!$this->getValidator() && ClassInfo::hasMethod($record, 'getCMSValidator')) { |
132
|
|
|
$this->setValidator($record->getCMSValidator()); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $handler->handleRequest($request); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Build a request handler for the given record |
140
|
|
|
* |
141
|
|
|
* @param GridField $gridField |
142
|
|
|
* @param DataObject $record |
143
|
|
|
* @param RequestHandler $requestHandler |
144
|
|
|
* @return GridFieldDetailForm_ItemRequest |
145
|
|
|
*/ |
146
|
|
|
protected function getItemRequestHandler($gridField, $record, $requestHandler) |
147
|
|
|
{ |
148
|
|
|
$class = $this->getItemRequestClass(); |
149
|
|
|
$assignedClass = $this->itemRequestClass; |
150
|
|
|
$this->extend('updateItemRequestClass', $class, $gridField, $record, $requestHandler, $assignedClass); |
151
|
|
|
/** @var GridFieldDetailForm_ItemRequest $handler */ |
152
|
|
|
$handler = Injector::inst()->createWithArgs( |
153
|
|
|
$class, |
154
|
|
|
array($gridField, $this, $record, $requestHandler, $this->name) |
155
|
|
|
); |
156
|
|
|
if ($template = $this->getTemplate()) { |
157
|
|
|
$handler->setTemplate($template); |
158
|
|
|
} |
159
|
|
|
$this->extend('updateItemRequestHandler', $handler); |
160
|
|
|
return $handler; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $template |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function setTemplate($template) |
168
|
|
|
{ |
169
|
|
|
$this->template = $template; |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return String |
175
|
|
|
*/ |
176
|
|
|
public function getTemplate() |
177
|
|
|
{ |
178
|
|
|
return $this->template; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $name |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function setName($name) |
186
|
|
|
{ |
187
|
|
|
$this->name = $name; |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return String |
193
|
|
|
*/ |
194
|
|
|
public function getName() |
195
|
|
|
{ |
196
|
|
|
return $this->name; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
protected function getDefaultShowPagination() |
203
|
|
|
{ |
204
|
|
|
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions'); |
205
|
|
|
return isset($formActionsConfig['showPagination']) ? (bool) $formActionsConfig['showPagination'] : false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return bool |
210
|
|
|
*/ |
211
|
|
|
public function getShowPagination() |
212
|
|
|
{ |
213
|
|
|
if ($this->showPagination === null) { |
214
|
|
|
return $this->getDefaultShowPagination(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return (bool) $this->showPagination; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param bool|null $showPagination |
222
|
|
|
* @return GridFieldDetailForm |
223
|
|
|
*/ |
224
|
|
|
public function setShowPagination($showPagination) |
225
|
|
|
{ |
226
|
|
|
$this->showPagination = $showPagination; |
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
protected function getDefaultShowAdd() |
234
|
|
|
{ |
235
|
|
|
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions'); |
236
|
|
|
return isset($formActionsConfig['showAdd']) ? (bool) $formActionsConfig['showAdd'] : false; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public function getShowAdd() |
243
|
|
|
{ |
244
|
|
|
if ($this->showAdd === null) { |
245
|
|
|
return $this->getDefaultShowAdd(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return (bool) $this->showAdd; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param bool|null $showAdd |
253
|
|
|
* @return GridFieldDetailForm |
254
|
|
|
*/ |
255
|
|
|
public function setShowAdd($showAdd) |
256
|
|
|
{ |
257
|
|
|
$this->showAdd = $showAdd; |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param Validator $validator |
263
|
|
|
* @return $this |
264
|
|
|
*/ |
265
|
|
|
public function setValidator(Validator $validator) |
266
|
|
|
{ |
267
|
|
|
$this->validator = $validator; |
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return Validator |
273
|
|
|
*/ |
274
|
|
|
public function getValidator() |
275
|
|
|
{ |
276
|
|
|
return $this->validator; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param FieldList $fields |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
|
|
public function setFields(FieldList $fields) |
284
|
|
|
{ |
285
|
|
|
$this->fields = $fields; |
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return FieldList |
291
|
|
|
*/ |
292
|
|
|
public function getFields() |
293
|
|
|
{ |
294
|
|
|
return $this->fields; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param string $class |
299
|
|
|
* @return $this |
300
|
|
|
*/ |
301
|
|
|
public function setItemRequestClass($class) |
302
|
|
|
{ |
303
|
|
|
$this->itemRequestClass = $class; |
304
|
|
|
return $this; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return string name of {@see GridFieldDetailForm_ItemRequest} subclass |
309
|
|
|
*/ |
310
|
|
|
public function getItemRequestClass() |
311
|
|
|
{ |
312
|
|
|
if ($this->itemRequestClass) { |
313
|
|
|
return $this->itemRequestClass; |
314
|
|
|
} elseif (ClassInfo::exists(static::class . '_ItemRequest')) { |
315
|
|
|
return static::class . '_ItemRequest'; |
316
|
|
|
} |
317
|
|
|
return GridFieldDetailForm_ItemRequest::class; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param Closure $cb Make changes on the edit form after constructing it. |
322
|
|
|
* @return $this |
323
|
|
|
*/ |
324
|
|
|
public function setItemEditFormCallback(Closure $cb) |
325
|
|
|
{ |
326
|
|
|
$this->itemEditFormCallback = $cb; |
327
|
|
|
return $this; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return Closure |
332
|
|
|
*/ |
333
|
|
|
public function getItemEditFormCallback() |
334
|
|
|
{ |
335
|
|
|
return $this->itemEditFormCallback; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|