1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Admin\LeftAndMain; |
|
|
|
|
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
8
|
|
|
use SilverStripe\Control\HTTPResponse; |
9
|
|
|
use SilverStripe\Control\RequestHandler; |
10
|
|
|
use SilverStripe\Forms\CompositeField; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use SilverStripe\Forms\FormAction; |
14
|
|
|
use SilverStripe\Forms\HiddenField; |
15
|
|
|
use SilverStripe\Forms\LiteralField; |
16
|
|
|
use SilverStripe\ORM\ArrayList; |
17
|
|
|
use SilverStripe\ORM\DataObject; |
18
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
19
|
|
|
use SilverStripe\ORM\HasManyList; |
20
|
|
|
use SilverStripe\ORM\ManyManyList; |
21
|
|
|
use SilverStripe\ORM\RelationList; |
22
|
|
|
use SilverStripe\ORM\SS_List; |
23
|
|
|
use SilverStripe\ORM\ValidationException; |
24
|
|
|
use SilverStripe\ORM\ValidationResult; |
25
|
|
|
use SilverStripe\View\ArrayData; |
26
|
|
|
use SilverStripe\View\HTML; |
27
|
|
|
use SilverStripe\View\SSViewer; |
28
|
|
|
|
29
|
|
|
class GridFieldDetailForm_ItemRequest extends RequestHandler |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
private static $allowed_actions = array( |
33
|
|
|
'edit', |
34
|
|
|
'view', |
35
|
|
|
'ItemEditForm' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var GridField |
41
|
|
|
*/ |
42
|
|
|
protected $gridField; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var GridFieldDetailForm |
47
|
|
|
*/ |
48
|
|
|
protected $component; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var DataObject |
52
|
|
|
*/ |
53
|
|
|
protected $record; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* This represents the current parent RequestHandler (which does not necessarily need to be a Controller). |
57
|
|
|
* It allows us to traverse the RequestHandler chain upwards to reach the Controller stack. |
58
|
|
|
* |
59
|
|
|
* @var RequestHandler |
60
|
|
|
*/ |
61
|
|
|
protected $popupController; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $popupFormName; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var String |
71
|
|
|
*/ |
72
|
|
|
protected $template = null; |
73
|
|
|
|
74
|
|
|
private static $url_handlers = array( |
75
|
|
|
'$Action!' => '$Action', |
76
|
|
|
'' => 'edit', |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @param GridField $gridField |
82
|
|
|
* @param GridFieldDetailForm $component |
83
|
|
|
* @param DataObject $record |
84
|
|
|
* @param RequestHandler $requestHandler |
85
|
|
|
* @param string $popupFormName |
86
|
|
|
*/ |
87
|
|
|
public function __construct($gridField, $component, $record, $requestHandler, $popupFormName) |
88
|
|
|
{ |
89
|
|
|
$this->gridField = $gridField; |
90
|
|
|
$this->component = $component; |
91
|
|
|
$this->record = $record; |
92
|
|
|
$this->popupController = $requestHandler; |
93
|
|
|
$this->popupFormName = $popupFormName; |
94
|
|
|
parent::__construct(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function Link($action = null) |
98
|
|
|
{ |
99
|
|
|
return Controller::join_links( |
100
|
|
|
$this->gridField->Link('item'), |
101
|
|
|
$this->record->ID ? $this->record->ID : 'new', |
102
|
|
|
$action |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param HTTPRequest $request |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
public function view($request) |
111
|
|
|
{ |
112
|
|
|
if (!$this->record->canView()) { |
113
|
|
|
$this->httpError(403); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$controller = $this->getToplevelController(); |
117
|
|
|
|
118
|
|
|
$form = $this->ItemEditForm(); |
119
|
|
|
$form->makeReadonly(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$data = new ArrayData(array( |
122
|
|
|
'Backlink' => $controller->Link(), |
123
|
|
|
'ItemEditForm' => $form |
124
|
|
|
)); |
125
|
|
|
$return = $data->renderWith($this->getTemplates()); |
126
|
|
|
|
127
|
|
|
if ($request->isAjax()) { |
128
|
|
|
return $return; |
129
|
|
|
} else { |
130
|
|
|
return $controller->customise(array('Content' => $return)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param HTTPRequest $request |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function edit($request) |
139
|
|
|
{ |
140
|
|
|
$controller = $this->getToplevelController(); |
141
|
|
|
$form = $this->ItemEditForm(); |
142
|
|
|
|
143
|
|
|
$return = $this->customise(array( |
144
|
|
|
'Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(), |
|
|
|
|
145
|
|
|
'ItemEditForm' => $form, |
146
|
|
|
))->renderWith($this->getTemplates()); |
147
|
|
|
|
148
|
|
|
if ($request->isAjax()) { |
149
|
|
|
return $return; |
150
|
|
|
} else { |
151
|
|
|
// If not requested by ajax, we need to render it within the controller context+template |
152
|
|
|
return $controller->customise(array( |
153
|
|
|
// TODO CMS coupling |
154
|
|
|
'Content' => $return, |
155
|
|
|
)); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Builds an item edit form. The arguments to getCMSFields() are the popupController and |
161
|
|
|
* popupFormName, however this is an experimental API and may change. |
162
|
|
|
* |
163
|
|
|
* @todo In the future, we will probably need to come up with a tigher object representing a partially |
164
|
|
|
* complete controller with gaps for extra functionality. This, for example, would be a better way |
165
|
|
|
* of letting Security/login put its log-in form inside a UI specified elsewhere. |
166
|
|
|
* |
167
|
|
|
* @return Form|HTTPResponse |
168
|
|
|
*/ |
169
|
|
|
public function ItemEditForm() |
170
|
|
|
{ |
171
|
|
|
$list = $this->gridField->getList(); |
172
|
|
|
|
173
|
|
|
if (empty($this->record)) { |
174
|
|
|
$controller = $this->getToplevelController(); |
175
|
|
|
$url = $controller->getRequest()->getURL(); |
176
|
|
|
$noActionURL = $controller->removeAction($url); |
177
|
|
|
$controller->getResponse()->removeHeader('Location'); //clear the existing redirect |
178
|
|
|
return $controller->redirect($noActionURL, 302); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// If we are creating a new record in a has-many list, then |
182
|
|
|
// pre-populate the record's foreign key. |
183
|
|
|
if ($list instanceof HasManyList && !$this->record->isInDB()) { |
184
|
|
|
$key = $list->getForeignKey(); |
185
|
|
|
$id = $list->getForeignID(); |
186
|
|
|
$this->record->$key = $id; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (!$this->record->canView()) { |
190
|
|
|
$controller = $this->getToplevelController(); |
191
|
|
|
// TODO More friendly error |
192
|
|
|
return $controller->httpError(403); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$fields = $this->component->getFields(); |
196
|
|
|
if (!$fields) { |
|
|
|
|
197
|
|
|
$fields = $this->record->getCMSFields(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// If we are creating a new record in a has-many list, then |
201
|
|
|
// Disable the form field as it has no effect. |
202
|
|
|
if ($list instanceof HasManyList) { |
203
|
|
|
$key = $list->getForeignKey(); |
204
|
|
|
|
205
|
|
|
if ($field = $fields->dataFieldByName($key)) { |
206
|
|
|
$fields->makeFieldReadonly($field); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$form = new Form( |
211
|
|
|
$this, |
212
|
|
|
'ItemEditForm', |
213
|
|
|
$fields, |
214
|
|
|
$this->getFormActions(), |
215
|
|
|
$this->component->getValidator() |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$form->loadDataFrom($this->record, $this->record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT); |
219
|
|
|
|
220
|
|
|
if ($this->record->ID && !$this->record->canEdit()) { |
221
|
|
|
// Restrict editing of existing records |
222
|
|
|
$form->makeReadonly(); |
223
|
|
|
// Hack to re-enable delete button if user can delete |
224
|
|
|
if ($this->record->canDelete()) { |
225
|
|
|
$form->Actions()->fieldByName('action_doDelete')->setReadonly(false); |
226
|
|
|
} |
227
|
|
|
} elseif (!$this->record->ID |
228
|
|
|
&& !$this->record->canCreate(null, $this->getCreateContext()) |
229
|
|
|
) { |
230
|
|
|
// Restrict creation of new records |
231
|
|
|
$form->makeReadonly(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// Load many_many extraData for record. |
235
|
|
|
// Fields with the correct 'ManyMany' namespace need to be added manually through getCMSFields(). |
236
|
|
|
if ($list instanceof ManyManyList) { |
237
|
|
|
$extraData = $list->getExtraData('', $this->record->ID); |
238
|
|
|
$form->loadDataFrom(array('ManyMany' => $extraData)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// TODO Coupling with CMS |
242
|
|
|
$toplevelController = $this->getToplevelController(); |
243
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
244
|
|
|
// Always show with base template (full width, no other panels), |
245
|
|
|
// regardless of overloaded CMS controller templates. |
246
|
|
|
// TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller |
247
|
|
|
$form->setTemplate([ |
248
|
|
|
'type' => 'Includes', |
249
|
|
|
'SilverStripe\\Admin\\LeftAndMain_EditForm', |
250
|
|
|
]); |
251
|
|
|
$form->addExtraClass('cms-content cms-edit-form center fill-height flexbox-area-grow'); |
252
|
|
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content'); |
253
|
|
|
if ($form->Fields()->hasTabSet()) { |
254
|
|
|
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet'); |
255
|
|
|
$form->addExtraClass('cms-tabset'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$form->Backlink = $this->getBackLink(); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$cb = $this->component->getItemEditFormCallback(); |
262
|
|
|
if ($cb) { |
|
|
|
|
263
|
|
|
$cb($form, $this); |
264
|
|
|
} |
265
|
|
|
$this->extend("updateItemEditForm", $form); |
266
|
|
|
return $form; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Build context for verifying canCreate |
271
|
|
|
* @see GridFieldAddNewButton::getHTMLFragments() |
272
|
|
|
* |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
protected function getCreateContext() |
276
|
|
|
{ |
277
|
|
|
$gridField = $this->gridField; |
278
|
|
|
$context = []; |
279
|
|
|
if ($gridField->getList() instanceof RelationList) { |
280
|
|
|
$record = $gridField->getForm()->getRecord(); |
281
|
|
|
if ($record && $record instanceof DataObject) { |
|
|
|
|
282
|
|
|
$context['Parent'] = $record; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
return $context; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return CompositeField Returns the right aligned toolbar group field along with its FormAction's |
290
|
|
|
*/ |
291
|
|
|
protected function getRightGroupField() |
292
|
|
|
{ |
293
|
|
|
$rightGroup = CompositeField::create()->setName('RightGroup'); |
294
|
|
|
$rightGroup->addExtraClass('ml-auto'); |
295
|
|
|
$rightGroup->setFieldHolderTemplate(get_class($rightGroup) . '_holder_buttongroup'); |
296
|
|
|
|
297
|
|
|
$previousAndNextGroup = CompositeField::create()->setName('PreviousAndNextGroup'); |
298
|
|
|
$previousAndNextGroup->addExtraClass('btn-group--circular mr-2'); |
299
|
|
|
$previousAndNextGroup->setFieldHolderTemplate(CompositeField::class . '_holder_buttongroup'); |
300
|
|
|
|
301
|
|
|
/** @var GridFieldDetailForm $component */ |
302
|
|
|
$component = $this->gridField->getConfig()->getComponentByType(GridFieldDetailForm::class); |
303
|
|
|
$paginator = $this->getGridField()->getConfig()->getComponentByType(GridFieldPaginator::class); |
304
|
|
|
$gridState = $this->getRequest()->requestVar('gridState'); |
305
|
|
|
if ($component && $paginator && $component->getShowPagination()) { |
|
|
|
|
306
|
|
|
$previousIsDisabled = !$this->getPreviousRecordID(); |
307
|
|
|
$nextIsDisabled = !$this->getNextRecordID(); |
308
|
|
|
|
309
|
|
|
$previousAndNextGroup->push( |
310
|
|
|
LiteralField::create( |
311
|
|
|
'previous-record', |
312
|
|
|
HTML::createTag($previousIsDisabled ? 'span' : 'a', [ |
313
|
|
|
'href' => $previousIsDisabled ? '#' : $this->getEditLink($this->getPreviousRecordID()), |
314
|
|
|
'data-grid-state' => $gridState, |
315
|
|
|
'title' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'), |
316
|
|
|
'aria-label' => _t(__CLASS__ . '.PREVIOUS', 'Go to previous record'), |
317
|
|
|
'class' => 'btn btn-secondary font-icon-left-open action--previous discard-confirmation' |
318
|
|
|
. ($previousIsDisabled ? ' disabled' : ''), |
319
|
|
|
]) |
320
|
|
|
) |
321
|
|
|
); |
322
|
|
|
|
323
|
|
|
$previousAndNextGroup->push( |
324
|
|
|
LiteralField::create( |
325
|
|
|
'next-record', |
326
|
|
|
HTML::createTag($nextIsDisabled ? 'span' : 'a', [ |
327
|
|
|
'href' => $nextIsDisabled ? '#' : $this->getEditLink($this->getNextRecordID()), |
328
|
|
|
'data-grid-state' => $gridState, |
329
|
|
|
'title' => _t(__CLASS__ . '.NEXT', 'Go to next record'), |
330
|
|
|
'aria-label' => _t(__CLASS__ . '.NEXT', 'Go to next record'), |
331
|
|
|
'class' => 'btn btn-secondary font-icon-right-open action--next discard-confirmation' |
332
|
|
|
. ($nextIsDisabled ? ' disabled' : ''), |
333
|
|
|
]) |
334
|
|
|
) |
335
|
|
|
); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
$rightGroup->push($previousAndNextGroup); |
339
|
|
|
|
340
|
|
|
if ($component && $component->getShowAdd() && $this->record->canCreate()) { |
341
|
|
|
$rightGroup->push( |
342
|
|
|
LiteralField::create( |
343
|
|
|
'new-record', |
344
|
|
|
HTML::createTag('a', [ |
345
|
|
|
'href' => Controller::join_links($this->gridField->Link('item'), 'new'), |
346
|
|
|
'data-grid-state' => $gridState, |
347
|
|
|
'title' => _t(__CLASS__ . '.NEW', 'Add new record'), |
348
|
|
|
'aria-label' => _t(__CLASS__ . '.NEW', 'Add new record'), |
349
|
|
|
'class' => 'btn btn-primary font-icon-plus-thin btn--circular action--new discard-confirmation', |
350
|
|
|
]) |
351
|
|
|
) |
352
|
|
|
); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $rightGroup; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Build the set of form field actions for this DataObject |
360
|
|
|
* |
361
|
|
|
* @return FieldList |
362
|
|
|
*/ |
363
|
|
|
protected function getFormActions() |
364
|
|
|
{ |
365
|
|
|
$actions = FieldList::create(); |
366
|
|
|
|
367
|
|
|
$majorActions = CompositeField::create()->setName('MajorActions'); |
368
|
|
|
$majorActions->setFieldHolderTemplate(get_class($majorActions) . '_holder_buttongroup'); |
369
|
|
|
$actions->push($majorActions); |
370
|
|
|
|
371
|
|
|
if ($this->record->ID !== 0) { // existing record |
372
|
|
|
if ($this->record->canEdit()) { |
373
|
|
|
$noChangesClasses = 'btn-outline-primary font-icon-tick'; |
374
|
|
|
$majorActions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Save', 'Save')) |
375
|
|
|
->addExtraClass($noChangesClasses) |
376
|
|
|
->setAttribute('data-btn-alternate-add', 'btn-primary font-icon-save') |
377
|
|
|
->setAttribute('data-btn-alternate-remove', $noChangesClasses) |
378
|
|
|
->setUseButtonTag(true) |
379
|
|
|
->setAttribute('data-text-alternate', _t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVEDRAFT', 'Save'))); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
if ($this->record->canDelete()) { |
383
|
|
|
$actions->insertAfter('MajorActions', FormAction::create('doDelete', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Delete', 'Delete')) |
384
|
|
|
->setUseButtonTag(true) |
385
|
|
|
->addExtraClass('btn-outline-danger btn-hide-outline font-icon-trash-bin action--delete')); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
$gridState = $this->getRequest()->requestVar('gridState'); |
389
|
|
|
$this->gridField->getState(false)->setValue($gridState); |
390
|
|
|
$actions->push(HiddenField::create('gridState', null, $gridState)); |
391
|
|
|
|
392
|
|
|
$actions->push($this->getRightGroupField()); |
393
|
|
|
} else { // adding new record |
394
|
|
|
//Change the Save label to 'Create' |
395
|
|
|
$majorActions->push(FormAction::create('doSave', _t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Create', 'Create')) |
396
|
|
|
->setUseButtonTag(true) |
397
|
|
|
->addExtraClass('btn-primary font-icon-plus-thin')); |
398
|
|
|
|
399
|
|
|
// Add a Cancel link which is a button-like link and link back to one level up. |
400
|
|
|
$crumbs = $this->Breadcrumbs(); |
401
|
|
|
if ($crumbs && $crumbs->count() >= 2) { |
402
|
|
|
$oneLevelUp = $crumbs->offsetGet($crumbs->count() - 2); |
403
|
|
|
$text = sprintf( |
404
|
|
|
"<a class=\"%s\" href=\"%s\">%s</a>", |
405
|
|
|
"crumb btn btn-secondary cms-panel-link", // CSS classes |
406
|
|
|
$oneLevelUp->Link, // url |
|
|
|
|
407
|
|
|
_t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.CancelBtn', 'Cancel') // label |
408
|
|
|
); |
409
|
|
|
$actions->insertAfter('MajorActions', new LiteralField('cancelbutton', $text)); |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$this->extend('updateFormActions', $actions); |
414
|
|
|
|
415
|
|
|
return $actions; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest. |
420
|
|
|
* This allows us to access the Controller responsible for invoking the top-level GridField. |
421
|
|
|
* This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(), |
422
|
|
|
* but allows us to avoid accessing the global state. |
423
|
|
|
* |
424
|
|
|
* GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack. |
425
|
|
|
* |
426
|
|
|
* @return Controller |
427
|
|
|
*/ |
428
|
|
|
protected function getToplevelController() |
429
|
|
|
{ |
430
|
|
|
$c = $this->popupController; |
431
|
|
|
while ($c && $c instanceof GridFieldDetailForm_ItemRequest) { |
432
|
|
|
$c = $c->getController(); |
433
|
|
|
} |
434
|
|
|
return $c; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
protected function getBackLink() |
438
|
|
|
{ |
439
|
|
|
// TODO Coupling with CMS |
440
|
|
|
$backlink = ''; |
441
|
|
|
$toplevelController = $this->getToplevelController(); |
442
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
443
|
|
|
if ($toplevelController->hasMethod('Backlink')) { |
444
|
|
|
$backlink = $toplevelController->Backlink(); |
445
|
|
|
} elseif ($this->popupController->hasMethod('Breadcrumbs')) { |
446
|
|
|
$parents = $this->popupController->Breadcrumbs(false)->items; |
|
|
|
|
447
|
|
|
$backlink = array_pop($parents)->Link; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
if (!$backlink) { |
451
|
|
|
$backlink = $toplevelController->Link(); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return $backlink; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Get the list of extra data from the $record as saved into it by |
459
|
|
|
* {@see Form::saveInto()} |
460
|
|
|
* |
461
|
|
|
* Handles detection of falsey values explicitly saved into the |
462
|
|
|
* DataObject by formfields |
463
|
|
|
* |
464
|
|
|
* @param DataObject $record |
465
|
|
|
* @param SS_List $list |
466
|
|
|
* @return array List of data to write to the relation |
467
|
|
|
*/ |
468
|
|
|
protected function getExtraSavedData($record, $list) |
469
|
|
|
{ |
470
|
|
|
// Skip extra data if not ManyManyList |
471
|
|
|
if (!($list instanceof ManyManyList)) { |
472
|
|
|
return null; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
$data = array(); |
476
|
|
|
foreach ($list->getExtraFields() as $field => $dbSpec) { |
477
|
|
|
$savedField = "ManyMany[{$field}]"; |
478
|
|
|
if ($record->hasField($savedField)) { |
479
|
|
|
$data[$field] = $record->getField($savedField); |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
return $data; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
public function doSave($data, $form) |
486
|
|
|
{ |
487
|
|
|
$isNewRecord = $this->record->ID == 0; |
488
|
|
|
|
489
|
|
|
// Check permission |
490
|
|
|
if (!$this->record->canEdit()) { |
491
|
|
|
return $this->httpError(403); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
// Save from form data |
495
|
|
|
$this->saveFormIntoRecord($data, $form); |
496
|
|
|
|
497
|
|
|
$link = '<a href="' . $this->Link('edit') . '">"' |
498
|
|
|
. htmlspecialchars($this->record->Title, ENT_QUOTES) |
499
|
|
|
. '"</a>'; |
500
|
|
|
$message = _t( |
501
|
|
|
'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Saved', |
502
|
|
|
'Saved {name} {link}', |
503
|
|
|
array( |
504
|
|
|
'name' => $this->record->i18n_singular_name(), |
505
|
|
|
'link' => $link |
506
|
|
|
) |
507
|
|
|
); |
508
|
|
|
|
509
|
|
|
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
510
|
|
|
|
511
|
|
|
// Redirect after save |
512
|
|
|
return $this->redirectAfterSave($isNewRecord); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Gets the edit link for a record |
517
|
|
|
* |
518
|
|
|
* @param int $id The ID of the record in the GridField |
519
|
|
|
* @return string |
520
|
|
|
*/ |
521
|
|
|
public function getEditLink($id) |
522
|
|
|
{ |
523
|
|
|
return Controller::join_links( |
524
|
|
|
$this->gridField->Link(), |
525
|
|
|
'item', |
526
|
|
|
$id, |
527
|
|
|
'?gridState=' . urlencode($this->gridField->getState(false)->Value()) |
528
|
|
|
); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* @param int $offset The offset from the current record |
533
|
|
|
* @return int|bool |
534
|
|
|
*/ |
535
|
|
|
private function getAdjacentRecordID($offset) |
536
|
|
|
{ |
537
|
|
|
$gridField = $this->getGridField(); |
538
|
|
|
$list = $gridField->getManipulatedList(); |
539
|
|
|
$state = $gridField->getState(false); |
540
|
|
|
$gridStateStr = $this->getRequest()->requestVar('gridState'); |
541
|
|
|
if (!empty($gridStateStr)) { |
542
|
|
|
$state->setValue($gridStateStr); |
543
|
|
|
} |
544
|
|
|
$data = $state->getData(); |
545
|
|
|
$paginator = $data->getData('GridFieldPaginator'); |
546
|
|
|
if (!$paginator) { |
547
|
|
|
return false; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
$currentPage = $paginator->getData('currentPage'); |
551
|
|
|
$itemsPerPage = $paginator->getData('itemsPerPage'); |
552
|
|
|
|
553
|
|
|
$limit = $itemsPerPage + 2; |
554
|
|
|
$limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1); |
555
|
|
|
|
556
|
|
|
$map = $list->limit($limit, $limitOffset)->column('ID'); |
|
|
|
|
557
|
|
|
$index = array_search($this->record->ID, $map); |
558
|
|
|
return isset($map[$index+$offset]) ? $map[$index+$offset] : false; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Gets the ID of the previous record in the list. |
563
|
|
|
* |
564
|
|
|
* @return int |
565
|
|
|
*/ |
566
|
|
|
public function getPreviousRecordID() |
567
|
|
|
{ |
568
|
|
|
return $this->getAdjacentRecordID(-1); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Gets the ID of the next record in the list. |
573
|
|
|
* |
574
|
|
|
* @return int |
575
|
|
|
*/ |
576
|
|
|
public function getNextRecordID() |
577
|
|
|
{ |
578
|
|
|
return $this->getAdjacentRecordID(1); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Response object for this request after a successful save |
583
|
|
|
* |
584
|
|
|
* @param bool $isNewRecord True if this record was just created |
585
|
|
|
* @return HTTPResponse|DBHTMLText |
586
|
|
|
*/ |
587
|
|
|
protected function redirectAfterSave($isNewRecord) |
588
|
|
|
{ |
589
|
|
|
$controller = $this->getToplevelController(); |
590
|
|
|
if ($isNewRecord) { |
591
|
|
|
return $controller->redirect($this->Link()); |
592
|
|
|
} elseif ($this->gridField->getList()->byID($this->record->ID)) { |
|
|
|
|
593
|
|
|
// Return new view, as we can't do a "virtual redirect" via the CMS Ajax |
594
|
|
|
// to the same URL (it assumes that its content is already current, and doesn't reload) |
595
|
|
|
return $this->edit($controller->getRequest()); |
596
|
|
|
} else { |
597
|
|
|
// Changes to the record properties might've excluded the record from |
598
|
|
|
// a filtered list, so return back to the main view if it can't be found |
599
|
|
|
$url = $controller->getRequest()->getURL(); |
600
|
|
|
$noActionURL = $controller->removeAction($url); |
601
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); |
602
|
|
|
return $controller->redirect($noActionURL, 302); |
603
|
|
|
} |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
public function httpError($errorCode, $errorMessage = null) |
607
|
|
|
{ |
608
|
|
|
$controller = $this->getToplevelController(); |
609
|
|
|
return $controller->httpError($errorCode, $errorMessage); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Loads the given form data into the underlying dataobject and relation |
614
|
|
|
* |
615
|
|
|
* @param array $data |
616
|
|
|
* @param Form $form |
617
|
|
|
* @throws ValidationException On error |
618
|
|
|
* @return DataObject Saved record |
619
|
|
|
*/ |
620
|
|
|
protected function saveFormIntoRecord($data, $form) |
621
|
|
|
{ |
622
|
|
|
$list = $this->gridField->getList(); |
623
|
|
|
|
624
|
|
|
// Check object matches the correct classname |
625
|
|
|
if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) { |
626
|
|
|
$newClassName = $data['ClassName']; |
627
|
|
|
// The records originally saved attribute was overwritten by $form->saveInto($record) before. |
628
|
|
|
// This is necessary for newClassInstance() to work as expected, and trigger change detection |
629
|
|
|
// on the ClassName attribute |
630
|
|
|
$this->record->setClassName($this->record->ClassName); |
631
|
|
|
// Replace $record with a new instance |
632
|
|
|
$this->record = $this->record->newClassInstance($newClassName); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
// Save form and any extra saved data into this dataobject |
636
|
|
|
$form->saveInto($this->record); |
637
|
|
|
$this->record->write(); |
638
|
|
|
$this->extend('onAfterSave', $this->record); |
639
|
|
|
|
640
|
|
|
$extraData = $this->getExtraSavedData($this->record, $list); |
641
|
|
|
$list->add($this->record, $extraData); |
|
|
|
|
642
|
|
|
|
643
|
|
|
return $this->record; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* @param array $data |
648
|
|
|
* @param Form $form |
649
|
|
|
* @return HTTPResponse |
650
|
|
|
* @throws ValidationException |
651
|
|
|
*/ |
652
|
|
|
public function doDelete($data, $form) |
653
|
|
|
{ |
654
|
|
|
$title = $this->record->Title; |
655
|
|
|
if (!$this->record->canDelete()) { |
656
|
|
|
throw new ValidationException( |
657
|
|
|
_t('SilverStripe\\Forms\\GridField\\GridFieldDetailForm.DeletePermissionsFailure', "No delete permissions") |
658
|
|
|
); |
659
|
|
|
} |
660
|
|
|
$this->record->delete(); |
661
|
|
|
|
662
|
|
|
$message = _t( |
663
|
|
|
'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Deleted', |
664
|
|
|
'Deleted {type} {name}', |
665
|
|
|
[ |
666
|
|
|
'type' => $this->record->i18n_singular_name(), |
667
|
|
|
'name' => htmlspecialchars($title, ENT_QUOTES) |
668
|
|
|
] |
669
|
|
|
); |
670
|
|
|
|
671
|
|
|
$toplevelController = $this->getToplevelController(); |
672
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
673
|
|
|
$backForm = $toplevelController->getEditForm(); |
|
|
|
|
674
|
|
|
$backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
675
|
|
|
} else { |
676
|
|
|
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
//when an item is deleted, redirect to the parent controller |
680
|
|
|
$controller = $this->getToplevelController(); |
681
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
682
|
|
|
|
683
|
|
|
return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @param string $template |
688
|
|
|
* @return $this |
689
|
|
|
*/ |
690
|
|
|
public function setTemplate($template) |
691
|
|
|
{ |
692
|
|
|
$this->template = $template; |
693
|
|
|
return $this; |
694
|
|
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* @return string |
698
|
|
|
*/ |
699
|
|
|
public function getTemplate() |
700
|
|
|
{ |
701
|
|
|
return $this->template; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Get list of templates to use |
706
|
|
|
* |
707
|
|
|
* @return array |
708
|
|
|
*/ |
709
|
|
|
public function getTemplates() |
710
|
|
|
{ |
711
|
|
|
$templates = SSViewer::get_templates_by_class($this, '', __CLASS__); |
712
|
|
|
// Prefer any custom template |
713
|
|
|
if ($this->getTemplate()) { |
714
|
|
|
array_unshift($templates, $this->getTemplate()); |
715
|
|
|
} |
716
|
|
|
return $templates; |
717
|
|
|
} |
718
|
|
|
|
719
|
|
|
/** |
720
|
|
|
* @return Controller |
721
|
|
|
*/ |
722
|
|
|
public function getController() |
723
|
|
|
{ |
724
|
|
|
return $this->popupController; |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* @return GridField |
729
|
|
|
*/ |
730
|
|
|
public function getGridField() |
731
|
|
|
{ |
732
|
|
|
return $this->gridField; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* @return DataObject |
737
|
|
|
*/ |
738
|
|
|
public function getRecord() |
739
|
|
|
{ |
740
|
|
|
return $this->record; |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* CMS-specific functionality: Passes through navigation breadcrumbs |
745
|
|
|
* to the template, and includes the currently edited record (if any). |
746
|
|
|
* see {@link LeftAndMain->Breadcrumbs()} for details. |
747
|
|
|
* |
748
|
|
|
* @param boolean $unlinked |
749
|
|
|
* @return ArrayList |
750
|
|
|
*/ |
751
|
|
|
public function Breadcrumbs($unlinked = false) |
752
|
|
|
{ |
753
|
|
|
if (!$this->popupController->hasMethod('Breadcrumbs')) { |
754
|
|
|
return null; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** @var ArrayList $items */ |
758
|
|
|
$items = $this->popupController->Breadcrumbs($unlinked); |
759
|
|
|
|
760
|
|
|
if ($this->record && $this->record->ID) { |
761
|
|
|
$title = ($this->record->Title) ? $this->record->Title : "#{$this->record->ID}"; |
762
|
|
|
$items->push(new ArrayData(array( |
763
|
|
|
'Title' => $title, |
764
|
|
|
'Link' => $this->Link() |
765
|
|
|
))); |
766
|
|
|
} else { |
767
|
|
|
$items->push(new ArrayData(array( |
768
|
|
|
'Title' => _t('SilverStripe\\Forms\\GridField\\GridField.NewRecord', 'New {type}', ['type' => $this->record->i18n_singular_name()]), |
769
|
|
|
'Link' => false |
770
|
|
|
))); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
$this->extend('updateBreadcrumbs', $items); |
774
|
|
|
return $items; |
775
|
|
|
} |
776
|
|
|
} |
777
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths