|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\Forms\Form; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
use SilverStripe\Forms\FieldList; |
|
9
|
|
|
use SilverStripe\Control\Director; |
|
10
|
|
|
use SilverStripe\Forms\FormAction; |
|
11
|
|
|
use SilverStripe\Admin\LeftAndMain; |
|
12
|
|
|
use SilverStripe\ORM\DataExtension; |
|
13
|
|
|
use SilverStripe\Control\Controller; |
|
14
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
15
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
16
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
17
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Decorates GridDetailForm_ItemRequest to use new form actions and buttons. |
|
22
|
|
|
* |
|
23
|
|
|
* This is a lightweight version of BetterButtons that use default getCMSActions functionnality |
|
24
|
|
|
* on DataObjects |
|
25
|
|
|
* |
|
26
|
|
|
* @link https://github.com/unclecheese/silverstripe-gridfield-betterbuttons |
|
27
|
|
|
* @link https://github.com/unclecheese/silverstripe-gridfield-betterbuttons/blob/master/src/Extensions/GridFieldBetterButtonsItemRequest.php |
|
28
|
|
|
* @property \SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest $owner |
|
29
|
|
|
*/ |
|
30
|
|
|
class ActionsGridFieldItemRequest extends DataExtension |
|
31
|
|
|
{ |
|
32
|
|
|
use Configurable; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @config |
|
36
|
|
|
* @var boolean |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $enable_save_prev_next = true; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @config |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $enable_save_close = true; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @config |
|
48
|
|
|
* @var boolean |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $enable_delete_right = true; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @config |
|
54
|
|
|
* @var boolean |
|
55
|
|
|
*/ |
|
56
|
|
|
private static $enable_utils_prev_next = false; |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array Allowed controller actions |
|
60
|
|
|
*/ |
|
61
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
62
|
|
|
'doSaveAndClose', |
|
63
|
|
|
'doSaveAndNext', |
|
64
|
|
|
'doSaveAndPrev', |
|
65
|
|
|
'doCustomAction', // For CustomAction |
|
66
|
|
|
'doCustomLink', // For CustomLink |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getAvailableActions($actions) |
|
73
|
|
|
{ |
|
74
|
|
|
$list = []; |
|
75
|
|
|
foreach ($actions as $action) { |
|
76
|
|
|
$list[] = $action->getName(); |
|
77
|
|
|
} |
|
78
|
|
|
return $list; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Updates the detail form to include new form actions and buttons |
|
83
|
|
|
* |
|
84
|
|
|
* Reorganize things a bit |
|
85
|
|
|
* |
|
86
|
|
|
* @param Form The ItemEditForm object |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
|
|
public function updateItemEditForm($form) |
|
89
|
|
|
{ |
|
90
|
|
|
$itemRequest = $this->owner; |
|
91
|
|
|
$record = $itemRequest->record; |
|
92
|
|
|
if (!$record) { |
|
|
|
|
|
|
93
|
|
|
$record = $form->getRecord(); |
|
94
|
|
|
} |
|
95
|
|
|
if (!$record) { |
|
|
|
|
|
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// We get the actions as defined on our record |
|
100
|
|
|
$CMSActions = $record->getCMSActions(); |
|
101
|
|
|
|
|
102
|
|
|
// We can the actions from the GridFieldDetailForm_ItemRequest |
|
103
|
|
|
// It sets the Save and Delete buttons + Right Group |
|
104
|
|
|
$actions = $form->Actions(); |
|
105
|
|
|
|
|
106
|
|
|
// The default button group that contains the Save or Create action |
|
107
|
|
|
// @link https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions |
|
108
|
|
|
$MajorActions = $actions->fieldByName('MajorActions'); |
|
109
|
|
|
|
|
110
|
|
|
// If it doesn't exist, push to default group |
|
111
|
|
|
if (!$MajorActions) { |
|
112
|
|
|
$MajorActions = $actions; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Push our actions that are otherwise ignored by SilverStripe |
|
116
|
|
|
foreach ($CMSActions as $action) { |
|
117
|
|
|
$actions->push($action); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Add extension hook |
|
121
|
|
|
$record->extend('onBeforeUpdateCMSActions', $actions); |
|
122
|
|
|
|
|
123
|
|
|
// We have a 4.4 setup, before that there was no RightGroup |
|
124
|
|
|
$RightGroup = $actions->fieldByName('RightGroup'); |
|
125
|
|
|
|
|
126
|
|
|
// Insert again to make sure our actions are properly placed after apply changes |
|
127
|
|
|
if ($RightGroup) { |
|
128
|
|
|
$actions->remove($RightGroup); |
|
129
|
|
|
$actions->push($RightGroup); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if (self::config()->enable_save_close) { |
|
133
|
|
|
$this->addSaveAndClose($actions, $record); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if (self::config()->enable_save_prev_next) { |
|
137
|
|
|
$this->addSaveNextAndPrevious($actions, $record); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (self::config()->enable_delete_right) { |
|
141
|
|
|
$this->moveCancelAndDelete($actions, $record); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Add extension hook |
|
145
|
|
|
$record->extend('onAfterUpdateCMSActions', $actions); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param FieldList $actions |
|
150
|
|
|
* @param DataObject $record |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
public function moveCancelAndDelete(FieldList $actions, DataObject $record) |
|
154
|
|
|
{ |
|
155
|
|
|
// We have a 4.4 setup, before that there was no RightGroup |
|
156
|
|
|
$RightGroup = $actions->fieldByName('RightGroup'); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
// Move delete at the end |
|
159
|
|
|
$deleteAction = $actions->fieldByName('action_doDelete'); |
|
|
|
|
|
|
160
|
|
|
if ($deleteAction) { |
|
|
|
|
|
|
161
|
|
|
// Move at the end of the stack |
|
162
|
|
|
$actions->remove($deleteAction); |
|
163
|
|
|
$actions->push($deleteAction); |
|
164
|
|
|
|
|
165
|
|
|
if ($RightGroup) { |
|
166
|
|
|
// Stack position is enough to have it on the left |
|
167
|
|
|
} else { |
|
168
|
|
|
// Only necessary pre 4.4 |
|
169
|
|
|
$deleteAction->addExtraClass('align-right'); |
|
170
|
|
|
} |
|
171
|
|
|
// Set custom titlte |
|
172
|
|
|
if ($record->hasMethod('getDeleteButtonTitle')) { |
|
173
|
|
|
$deleteAction->setTitle($record->getDeleteButtonTitle()); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
// Move cancel at the end |
|
177
|
|
|
$cancelButton = $actions->fieldByName('cancelbutton'); |
|
|
|
|
|
|
178
|
|
|
if ($cancelButton) { |
|
|
|
|
|
|
179
|
|
|
// Move at the end of the stack |
|
180
|
|
|
$actions->remove($cancelButton); |
|
181
|
|
|
$actions->push($cancelButton); |
|
182
|
|
|
if ($RightGroup) { |
|
183
|
|
|
// Stack position is enough to have it on the left |
|
184
|
|
|
} else { |
|
185
|
|
|
// Only necessary pre 4.4 |
|
186
|
|
|
$cancelButton->addExtraClass('align-right'); |
|
187
|
|
|
} |
|
188
|
|
|
// Set custom titlte |
|
189
|
|
|
if ($record->hasMethod('getCancelButtonTitle')) { |
|
190
|
|
|
$cancelButton->setTitle($record->getCancelButtonTitle()); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function getCustomPreviousRecordID(DataObject $record) |
|
196
|
|
|
{ |
|
197
|
|
|
if ($record->hasMethod('PrevRecord')) { |
|
198
|
|
|
return $record->PrevRecord()->ID ?? 0; |
|
199
|
|
|
} |
|
200
|
|
|
$this->owner->getPreviousRecordID(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function getCustomNextRecordID(DataObject $record) |
|
204
|
|
|
{ |
|
205
|
|
|
if ($record->hasMethod('NextRecord')) { |
|
206
|
|
|
return $record->NextRecord()->ID ?? 0; |
|
207
|
|
|
} |
|
208
|
|
|
$this->owner->getNextRecordID(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param FieldList $actions |
|
213
|
|
|
* @param DataObject $record |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) |
|
217
|
|
|
{ |
|
218
|
|
|
if (!$record->canEdit()) { |
|
219
|
|
|
return; |
|
220
|
|
|
} |
|
221
|
|
|
if (!$record->ID) { |
|
222
|
|
|
return; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$MajorActions = $actions->fieldByName('MajorActions'); |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
// If it doesn't exist, push to default group |
|
228
|
|
|
if (!$MajorActions) { |
|
|
|
|
|
|
229
|
|
|
$MajorActions = $actions; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
// TODO: check why with paginator, after the first page, getPreviousRecordID/getNextRecordID tend to not work properly |
|
233
|
|
|
$getPreviousRecordID = $this->getCustomPreviousRecordID($record); |
|
234
|
|
|
$getNextRecordID = $this->getCustomNextRecordID($record); |
|
235
|
|
|
|
|
236
|
|
|
// Coupling for HasPrevNextUtils |
|
237
|
|
|
if (Controller::has_curr()) { |
|
238
|
|
|
$request = Controller::curr()->getRequest(); |
|
239
|
|
|
$routeParams = $request->routeParams(); |
|
240
|
|
|
$routeParams['PreviousRecordID'] = $getPreviousRecordID; |
|
241
|
|
|
$routeParams['NextRecordID'] = $getNextRecordID; |
|
242
|
|
|
$request->setRouteParams($routeParams); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
if ($getPreviousRecordID) { |
|
246
|
|
|
$doSaveAndPrev = new FormAction('doSaveAndPrev', _t('ActionsGridFieldItemRequest.SAVEANDPREVIOUS', 'Save and Previous')); |
|
247
|
|
|
$doSaveAndPrev->addExtraClass($this->getBtnClassForRecord($record)); |
|
248
|
|
|
$doSaveAndPrev->addExtraClass('font-icon-angle-double-left'); |
|
249
|
|
|
$doSaveAndPrev->setUseButtonTag(true); |
|
250
|
|
|
$MajorActions->push($doSaveAndPrev); |
|
251
|
|
|
} |
|
252
|
|
|
if ($getNextRecordID) { |
|
253
|
|
|
$doSaveAndNext = new FormAction('doSaveAndNext', _t('ActionsGridFieldItemRequest.SAVEANDNEXT', 'Save and Next')); |
|
254
|
|
|
$doSaveAndNext->addExtraClass($this->getBtnClassForRecord($record)); |
|
255
|
|
|
$doSaveAndNext->addExtraClass('font-icon-angle-double-right'); |
|
256
|
|
|
$doSaveAndNext->setUseButtonTag(true); |
|
257
|
|
|
$MajorActions->push($doSaveAndNext); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param FieldList $actions |
|
263
|
|
|
* @param DataObject $record |
|
264
|
|
|
* @return void |
|
265
|
|
|
*/ |
|
266
|
|
|
public function addSaveAndClose(FieldList $actions, DataObject $record) |
|
267
|
|
|
{ |
|
268
|
|
|
if (!$record->canEdit()) { |
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$MajorActions = $actions->fieldByName('MajorActions'); |
|
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
// If it doesn't exist, push to default group |
|
275
|
|
|
if (!$MajorActions) { |
|
|
|
|
|
|
276
|
|
|
$MajorActions = $actions; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
if ($record->ID) { |
|
280
|
|
|
$label = _t('ActionsGridFieldItemRequest.SAVEANDCLOSE', 'Save and Close'); |
|
281
|
|
|
} else { |
|
282
|
|
|
$label = _t('ActionsGridFieldItemRequest.CREATEANDCLOSE', 'Create and Close'); |
|
283
|
|
|
} |
|
284
|
|
|
$saveAndClose = new FormAction('doSaveAndClose', $label); |
|
285
|
|
|
$saveAndClose->addExtraClass($this->getBtnClassForRecord($record)); |
|
286
|
|
|
$saveAndClose->setAttribute('data-text-alternate', $label); |
|
287
|
|
|
if ($record->ID) { |
|
288
|
|
|
$saveAndClose->setAttribute('data-btn-alternate-add', 'btn-primary'); |
|
289
|
|
|
$saveAndClose->setAttribute('data-btn-alternate-remove', 'btn-outline-primary'); |
|
290
|
|
|
} |
|
291
|
|
|
$saveAndClose->addExtraClass('font-icon-level-up'); |
|
292
|
|
|
$saveAndClose->setUseButtonTag(true); |
|
293
|
|
|
$MajorActions->push($saveAndClose); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* New and existing records have different classes |
|
298
|
|
|
* |
|
299
|
|
|
* @param DataObject $record |
|
300
|
|
|
* @return string |
|
301
|
|
|
*/ |
|
302
|
|
|
protected function getBtnClassForRecord(DataObject $record) |
|
303
|
|
|
{ |
|
304
|
|
|
if ($record->ID) { |
|
305
|
|
|
return 'btn-outline-primary'; |
|
306
|
|
|
} |
|
307
|
|
|
return 'btn-primary'; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Forward a given action to a DataObject |
|
312
|
|
|
* |
|
313
|
|
|
* Action must be declared in getCMSActions to be called |
|
314
|
|
|
* |
|
315
|
|
|
* @param string $action |
|
316
|
|
|
* @param array $data |
|
317
|
|
|
* @param Form $form |
|
318
|
|
|
* @return HTTPResponse|DBHTMLText |
|
|
|
|
|
|
319
|
|
|
*/ |
|
320
|
|
|
protected function forwardActionToRecord($action, $data = [], $form = null) |
|
321
|
|
|
{ |
|
322
|
|
|
$controller = $this->getToplevelController(); |
|
323
|
|
|
$record = $this->owner->record; |
|
324
|
|
|
$definedActions = $record->getCMSActions(); |
|
325
|
|
|
// Check if the action is indeed available |
|
326
|
|
|
$clickedAction = null; |
|
327
|
|
|
if (!empty($definedActions)) { |
|
328
|
|
|
foreach ($definedActions as $definedAction) { |
|
329
|
|
|
$definedActionName = $definedAction->getName(); |
|
330
|
|
|
if ($definedAction->hasMethod('actionName')) { |
|
331
|
|
|
$definedActionName = $definedAction->actionName(); |
|
332
|
|
|
} |
|
333
|
|
|
if ($definedActionName == $action) { |
|
334
|
|
|
$clickedAction = $definedAction; |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
if (!$clickedAction) { |
|
|
|
|
|
|
339
|
|
|
$class = get_class($record); |
|
340
|
|
|
return $this->owner->httpError(403, 'Action not available on ' . $class . '. It must be one of :' . implode(',', $this->getAvailableActions($definedActions))); |
|
341
|
|
|
} |
|
342
|
|
|
$message = null; |
|
343
|
|
|
$error = false; |
|
344
|
|
|
|
|
345
|
|
|
// Check record BEFORE the action |
|
346
|
|
|
// It can be deleted by the action and it will return to the list |
|
347
|
|
|
$isNewRecord = $record->ID == 0; |
|
348
|
|
|
|
|
349
|
|
|
try { |
|
350
|
|
|
$result = $record->$action($data, $form, $controller); |
|
351
|
|
|
|
|
352
|
|
|
// We have a response |
|
353
|
|
|
if ($result && $result instanceof HTTPResponse) { |
|
354
|
|
|
return $result; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
if ($result === false) { |
|
358
|
|
|
// Result returned an error (false) |
|
359
|
|
|
$error = true; |
|
360
|
|
|
$message = _t( |
|
361
|
|
|
'ActionsGridFieldItemRequest.FAILED', |
|
362
|
|
|
'Action {action} failed on {name}', |
|
363
|
|
|
array( |
|
364
|
|
|
'action' => $clickedAction->getTitle(), |
|
365
|
|
|
'name' => $record->i18n_singular_name(), |
|
366
|
|
|
) |
|
367
|
|
|
); |
|
368
|
|
|
} elseif (is_string($result)) { |
|
369
|
|
|
// Result is a message |
|
370
|
|
|
$message = $result; |
|
371
|
|
|
} |
|
372
|
|
|
} catch (Exception $ex) { |
|
373
|
|
|
$error = true; |
|
374
|
|
|
$message = $ex->getMessage(); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
// Build default message |
|
378
|
|
|
if (!$message) { |
|
379
|
|
|
$message = _t( |
|
380
|
|
|
'ActionsGridFieldItemRequest.DONE', |
|
381
|
|
|
'Action {action} was done on {name}', |
|
382
|
|
|
array( |
|
383
|
|
|
'action' => $clickedAction->getTitle(), |
|
384
|
|
|
'name' => $record->i18n_singular_name(), |
|
385
|
|
|
) |
|
386
|
|
|
); |
|
387
|
|
|
} |
|
388
|
|
|
$status = 'good'; |
|
389
|
|
|
if ($error) { |
|
390
|
|
|
$status = 'bad'; |
|
391
|
|
|
} |
|
392
|
|
|
// We don't have a form, simply return the result |
|
393
|
|
|
if (!$form) { |
|
394
|
|
|
if ($error) { |
|
395
|
|
|
return $this->owner->httpError(403, $message); |
|
396
|
|
|
} |
|
397
|
|
|
return $message; |
|
398
|
|
|
} |
|
399
|
|
|
if (Director::is_ajax()) { |
|
400
|
|
|
$controller = $this->getToplevelController(); |
|
401
|
|
|
$controller->getResponse()->addHeader('X-Status', rawurlencode($message)); |
|
402
|
|
|
if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) { |
|
403
|
|
|
$controller->getResponse()->addHeader('X-Reload', true); |
|
404
|
|
|
} |
|
405
|
|
|
} else { |
|
406
|
|
|
$form->sessionMessage($message, $status, ValidationResult::CAST_HTML); |
|
407
|
|
|
} |
|
408
|
|
|
// Redirect after action |
|
409
|
|
|
return $this->redirectAfterAction($isNewRecord); |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Handles custom links |
|
414
|
|
|
* |
|
415
|
|
|
* Use CustomLink with default behaviour to trigger this |
|
416
|
|
|
* |
|
417
|
|
|
* See: |
|
418
|
|
|
* DefaultLink::getModelLink |
|
419
|
|
|
* GridFieldCustomLink::getLink |
|
420
|
|
|
* |
|
421
|
|
|
* @param HTTPRequest $request |
|
422
|
|
|
* @return HTTPResponse|DBHTMLText |
|
423
|
|
|
*/ |
|
424
|
|
|
public function doCustomLink(HTTPRequest $request) |
|
425
|
|
|
{ |
|
426
|
|
|
$action = $request->getVar('CustomLink'); |
|
427
|
|
|
return $this->forwardActionToRecord($action); |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
/** |
|
431
|
|
|
* Handles custom actions |
|
432
|
|
|
* |
|
433
|
|
|
* Use CustomAction class to trigger this |
|
434
|
|
|
* |
|
435
|
|
|
* @param array The form data |
|
436
|
|
|
* @param Form The form object |
|
437
|
|
|
* @return HTTPResponse|DBHTMLText |
|
438
|
|
|
*/ |
|
439
|
|
|
public function doCustomAction($data, $form) |
|
440
|
|
|
{ |
|
441
|
|
|
$action = key($data['action_doCustomAction']); |
|
442
|
|
|
return $this->forwardActionToRecord($action, $data, $form); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* Saves the form and goes back to list view |
|
447
|
|
|
* |
|
448
|
|
|
* @param array The form data |
|
449
|
|
|
* @param Form The form object |
|
450
|
|
|
*/ |
|
451
|
|
|
public function doSaveAndClose($data, $form) |
|
452
|
|
|
{ |
|
453
|
|
|
$result = $this->owner->doSave($data, $form); |
|
|
|
|
|
|
454
|
|
|
// Redirect after save |
|
455
|
|
|
$controller = $this->getToplevelController(); |
|
456
|
|
|
$controller->getResponse()->addHeader("X-Pjax", "Content"); |
|
457
|
|
|
return $controller->redirect($this->getBackLink()); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* Saves the form and goes back to the next item |
|
462
|
|
|
* |
|
463
|
|
|
* @param array The form data |
|
464
|
|
|
* @param Form The form object |
|
465
|
|
|
*/ |
|
466
|
|
|
public function doSaveAndNext($data, $form) |
|
467
|
|
|
{ |
|
468
|
|
|
$record = $this->owner->record; |
|
469
|
|
|
$result = $this->owner->doSave($data, $form); |
|
|
|
|
|
|
470
|
|
|
// Redirect after save |
|
471
|
|
|
$controller = $this->getToplevelController(); |
|
472
|
|
|
$controller->getResponse()->addHeader("X-Pjax", "Content"); |
|
473
|
|
|
|
|
474
|
|
|
$getNextRecordID = $this->getCustomNextRecordID($record); |
|
475
|
|
|
$class = get_class($record); |
|
476
|
|
|
$next = $class::get()->byID($getNextRecordID); |
|
477
|
|
|
|
|
478
|
|
|
$link = $this->owner->getEditLink($getNextRecordID); |
|
479
|
|
|
|
|
480
|
|
|
// Link to a specific tab if set, see cms-actions.js |
|
481
|
|
|
if ($next && !empty($data['_activetab'])) { |
|
482
|
|
|
$link .= '#' . $data['_activetab']; |
|
483
|
|
|
} |
|
484
|
|
|
return $controller->redirect($link); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
/** |
|
488
|
|
|
* Saves the form and goes to the previous item |
|
489
|
|
|
* |
|
490
|
|
|
* @param array The form data |
|
491
|
|
|
* @param Form The form object |
|
492
|
|
|
*/ |
|
493
|
|
|
public function doSaveAndPrev($data, $form) |
|
494
|
|
|
{ |
|
495
|
|
|
$record = $this->owner->record; |
|
496
|
|
|
$result = $this->owner->doSave($data, $form); |
|
|
|
|
|
|
497
|
|
|
// Redirect after save |
|
498
|
|
|
$controller = $this->getToplevelController(); |
|
499
|
|
|
$controller->getResponse()->addHeader("X-Pjax", "Content"); |
|
500
|
|
|
|
|
501
|
|
|
$getPreviousRecordID = $this->getCustomPreviousRecordID($record); |
|
502
|
|
|
$class = get_class($record); |
|
503
|
|
|
$prev = $class::get()->byID($getPreviousRecordID); |
|
504
|
|
|
|
|
505
|
|
|
$link = $this->owner->getEditLink($getPreviousRecordID); |
|
506
|
|
|
|
|
507
|
|
|
// Link to a specific tab if set, see cms-actions.js |
|
508
|
|
|
if ($prev && !empty($data['_activetab'])) { |
|
509
|
|
|
$link .= '#' . $data['_activetab']; |
|
510
|
|
|
} |
|
511
|
|
|
return $controller->redirect($link); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
/** |
|
515
|
|
|
* Gets the top level controller. |
|
516
|
|
|
* |
|
517
|
|
|
* @return Controller |
|
518
|
|
|
* @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
|
519
|
|
|
* because it is a protected method and not visible to a decorator! |
|
520
|
|
|
*/ |
|
521
|
|
|
protected function getToplevelController() |
|
522
|
|
|
{ |
|
523
|
|
|
$c = $this->owner->getController(); |
|
524
|
|
|
while ($c && $c instanceof GridFieldDetailForm_ItemRequest) { |
|
525
|
|
|
$c = $c->getController(); |
|
526
|
|
|
} |
|
527
|
|
|
return $c; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
/** |
|
531
|
|
|
* Gets the back link |
|
532
|
|
|
* |
|
533
|
|
|
* @return string |
|
534
|
|
|
* @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
|
535
|
|
|
* because it is a protected method and not visible to a decorator! |
|
536
|
|
|
*/ |
|
537
|
|
|
public function getBackLink() |
|
538
|
|
|
{ |
|
539
|
|
|
// TODO Coupling with CMS |
|
540
|
|
|
$backlink = ''; |
|
541
|
|
|
$toplevelController = $this->getToplevelController(); |
|
542
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) { |
|
543
|
|
|
if ($toplevelController->hasMethod('Backlink')) { |
|
544
|
|
|
$backlink = $toplevelController->Backlink(); |
|
545
|
|
|
} elseif ($this->owner->getController()->hasMethod('Breadcrumbs')) { |
|
546
|
|
|
$parents = $this->owner->getController()->Breadcrumbs(false)->items; |
|
547
|
|
|
$backlink = array_pop($parents)->Link; |
|
548
|
|
|
} |
|
549
|
|
|
} |
|
550
|
|
|
if (!$backlink) { |
|
551
|
|
|
$backlink = $toplevelController->Link(); |
|
552
|
|
|
} |
|
553
|
|
|
return $backlink; |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
/** |
|
557
|
|
|
* Response object for this request after a successful save |
|
558
|
|
|
* |
|
559
|
|
|
* @param bool $isNewRecord True if this record was just created |
|
560
|
|
|
* @return HTTPResponse|DBHTMLText |
|
561
|
|
|
* @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest} |
|
562
|
|
|
* because it is a protected method and not visible to a decorator! |
|
563
|
|
|
*/ |
|
564
|
|
|
protected function redirectAfterAction($isNewRecord) |
|
565
|
|
|
{ |
|
566
|
|
|
$controller = $this->getToplevelController(); |
|
567
|
|
|
if ($isNewRecord) { |
|
568
|
|
|
return $controller->redirect($this->owner->Link()); |
|
569
|
|
|
} elseif ($this->owner->gridField->getList()->byID($this->owner->record->ID)) { |
|
|
|
|
|
|
570
|
|
|
// Return new view, as we can't do a "virtual redirect" via the CMS Ajax |
|
571
|
|
|
// to the same URL (it assumes that its content is already current, and doesn't reload) |
|
572
|
|
|
return $this->owner->edit($controller->getRequest()); |
|
|
|
|
|
|
573
|
|
|
} else { |
|
574
|
|
|
// Changes to the record properties might've excluded the record from |
|
575
|
|
|
// a filtered list, so return back to the main view if it can't be found |
|
576
|
|
|
$url = $controller->getRequest()->getURL(); |
|
577
|
|
|
$noActionURL = $controller->removeAction($url); |
|
578
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); |
|
579
|
|
|
return $controller->redirect($noActionURL, 302); |
|
580
|
|
|
} |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
|