Passed
Push — master ( 397529...fa6505 )
by Thomas
06:37 queued 04:36
created

ActionsGridFieldItemRequest   F

Complexity

Total Complexity 70

Size/Duplication

Total Lines 521
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 215
c 2
b 0
f 0
dl 0
loc 521
rs 2.8
wmc 70

15 Methods

Rating   Name   Duplication   Size   Complexity  
B moveCancelAndDelete() 0 36 7
A getBtnClassForRecord() 0 6 2
A addSaveAndClose() 0 23 4
B updateItemEditForm() 0 58 9
B addSaveNextAndPrevious() 0 41 7
A getAvailableActions() 0 7 2
A doSaveAndClose() 0 7 1
A doSaveAndNext() 0 19 3
A doSaveAndPrev() 0 19 3
A doCustomLink() 0 4 1
A doCustomAction() 0 4 1
A getToplevelController() 0 7 3
A getBackLink() 0 17 6
F forwardActionToRecord() 0 85 18
A redirectAfterAction() 0 16 3

How to fix   Complexity   

Complex Class

Complex classes like ActionsGridFieldItemRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ActionsGridFieldItemRequest, and based on these observations, apply Extract Interface, too.

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;
0 ignored issues
show
introduced by
The private property $enable_utils_prev_next is not used, and could be removed.
Loading history...
57
58
    /**
59
     * @var array Allowed controller actions
60
     */
61
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
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
0 ignored issues
show
Bug introduced by
The type LeKoala\CmsActions\The was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
     */
88
    public function updateItemEditForm($form)
89
    {
90
        $itemRequest = $this->owner;
91
        $record = $itemRequest->record;
92
        if (!$record) {
0 ignored issues
show
introduced by
$record is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
93
            $record = $form->getRecord();
94
        }
95
        if (!$record) {
0 ignored issues
show
introduced by
$record is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $MajorActions is dead and can be removed.
Loading history...
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');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $RightGroup is correct as $actions->fieldByName('RightGroup') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
158
        // Move delete at the end
159
        $deleteAction = $actions->fieldByName('action_doDelete');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $deleteAction is correct as $actions->fieldByName('action_doDelete') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
160
        if ($deleteAction) {
0 ignored issues
show
introduced by
$deleteAction is of type null, thus it always evaluated to false.
Loading history...
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
                $deleteAction->addExtraClass('align-right');
169
            }
170
            // Set custom titlte
171
            if ($record->hasMethod('getDeleteButtonTitle')) {
172
                $deleteAction->setTitle($record->getDeleteButtonTitle());
173
            }
174
        }
175
        // Move cancel at the end
176
        $cancelButton = $actions->fieldByName('cancelbutton');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $cancelButton is correct as $actions->fieldByName('cancelbutton') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
177
        if ($cancelButton) {
0 ignored issues
show
introduced by
$cancelButton is of type null, thus it always evaluated to false.
Loading history...
178
            // Move at the end of the stack
179
            $actions->remove($cancelButton);
180
            $actions->push($cancelButton);
181
            if ($RightGroup) {
182
                // Stack position is enough to have it on the left
183
            } else {
184
                $deleteAction->addExtraClass('align-right');
185
            }
186
            // Set custom titlte
187
            if ($record->hasMethod('getCancelButtonTitle')) {
188
                $cancelButton->setTitle($record->getCancelButtonTitle());
189
            }
190
        }
191
    }
192
193
    /**
194
     * @param FieldList $actions
195
     * @param DataObject $record
196
     * @return void
197
     */
198
    public function addSaveNextAndPrevious(FieldList $actions, DataObject $record)
199
    {
200
        if (!$record->canEdit()) {
201
            return;
202
        }
203
        if (!$record->ID) {
204
            return;
205
        }
206
207
        $MajorActions = $actions->fieldByName('MajorActions');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $MajorActions is correct as $actions->fieldByName('MajorActions') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
208
209
        // If it doesn't exist, push to default group
210
        if (!$MajorActions) {
0 ignored issues
show
introduced by
$MajorActions is of type null, thus it always evaluated to false.
Loading history...
211
            $MajorActions = $actions;
212
        }
213
214
        $getPreviousRecordID = $this->owner->getPreviousRecordID();
215
        $getNextRecordID = $this->owner->getNextRecordID();
216
217
        // Coupling for HasPrevNextUtils
218
        if (Controller::has_curr()) {
219
            $request =  Controller::curr()->getRequest();
220
            $routeParams = $request->routeParams();
221
            $routeParams['PreviousRecordID'] = $getPreviousRecordID;
222
            $routeParams['NextRecordID'] = $getNextRecordID;
223
            $request->setRouteParams($routeParams);
224
        }
225
226
        if ($getPreviousRecordID) {
227
            $doSaveAndPrev = new FormAction('doSaveAndPrev', _t('ActionsGridFieldItemRequest.SAVEANDPREVIOUS', 'Save and Previous'));
228
            $doSaveAndPrev->addExtraClass($this->getBtnClassForRecord($record));
229
            $doSaveAndPrev->addExtraClass('font-icon-angle-double-left');
230
            $doSaveAndPrev->setUseButtonTag(true);
231
            $MajorActions->push($doSaveAndPrev);
232
        }
233
        if ($getNextRecordID) {
234
            $doSaveAndNext = new FormAction('doSaveAndNext', _t('ActionsGridFieldItemRequest.SAVEANDNEXT', 'Save and Next'));
235
            $doSaveAndNext->addExtraClass($this->getBtnClassForRecord($record));
236
            $doSaveAndNext->addExtraClass('font-icon-angle-double-right');
237
            $doSaveAndNext->setUseButtonTag(true);
238
            $MajorActions->push($doSaveAndNext);
239
        }
240
    }
241
242
    /**
243
     * @param FieldList $actions
244
     * @param DataObject $record
245
     * @return void
246
     */
247
    public function addSaveAndClose(FieldList $actions, DataObject $record)
248
    {
249
        if (!$record->canEdit()) {
250
            return;
251
        }
252
253
        $MajorActions = $actions->fieldByName('MajorActions');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $MajorActions is correct as $actions->fieldByName('MajorActions') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
254
255
        // If it doesn't exist, push to default group
256
        if (!$MajorActions) {
0 ignored issues
show
introduced by
$MajorActions is of type null, thus it always evaluated to false.
Loading history...
257
            $MajorActions = $actions;
258
        }
259
260
        if ($record->ID) {
261
            $label = _t('ActionsGridFieldItemRequest.SAVEANDCLOSE', 'Save and Close');
262
        } else {
263
            $label = _t('ActionsGridFieldItemRequest.CREATEANDCLOSE', 'Create and Close');
264
        }
265
        $saveAndClose = new FormAction('doSaveAndClose', $label);
266
        $saveAndClose->addExtraClass($this->getBtnClassForRecord($record));
267
        $saveAndClose->addExtraClass('font-icon-level-up');
268
        $saveAndClose->setUseButtonTag(true);
269
        $MajorActions->push($saveAndClose);
270
    }
271
272
    /**
273
     * New and existing records have different classes
274
     *
275
     * @param DataObject $record
276
     * @return string
277
     */
278
    protected function getBtnClassForRecord(DataObject $record)
279
    {
280
        if ($record->ID) {
281
            return 'btn-outline-primary';
282
        }
283
        return 'btn-primary';
284
    }
285
286
    /**
287
     * Forward a given action to a DataObject
288
     *
289
     * Action must be declared in getCMSActions to be called
290
     *
291
     * @param string $action
292
     * @param array $data
293
     * @param Form $form
294
     * @return HTTPResponse|DBHTMLText
0 ignored issues
show
Bug introduced by
The type LeKoala\CmsActions\DBHTMLText was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
295
     */
296
    protected function forwardActionToRecord($action, $data = [], $form = null)
297
    {
298
        $controller = $this->getToplevelController();
299
        $record = $this->owner->record;
300
        $definedActions = $record->getCMSActions();
301
        // Check if the action is indeed available
302
        $clickedAction = null;
303
        if (!empty($definedActions)) {
304
            foreach ($definedActions as $definedAction) {
305
                $definedActionName = $definedAction->getName();
306
                if ($definedAction->hasMethod('actionName')) {
307
                    $definedActionName = $definedAction->actionName();
308
                }
309
                if ($definedActionName == $action) {
310
                    $clickedAction = $definedAction;
311
                }
312
            }
313
        }
314
        if (!$clickedAction) {
0 ignored issues
show
introduced by
$clickedAction is of type null, thus it always evaluated to false.
Loading history...
315
            $class = get_class($record);
316
            return $this->owner->httpError(403, 'Action not available on ' . $class . '. It must be one of :' . implode(',', $this->getAvailableActions($definedActions)));
317
        }
318
        $message = null;
319
        $error = false;
320
        try {
321
            $result = $record->$action($data, $form, $controller);
322
323
            // We have a response
324
            if ($result && $result instanceof HTTPResponse) {
325
                return $result;
326
            }
327
328
            if ($result === false) {
329
                // Result returned an error (false)
330
                $error = true;
331
                $message = _t(
332
                    'ActionsGridFieldItemRequest.FAILED',
333
                    'Action {action} failed on {name}',
334
                    array(
335
                        'action' => $clickedAction->getTitle(),
336
                        'name' => $record->i18n_singular_name(),
337
                    )
338
                );
339
            } elseif (is_string($result)) {
340
                // Result is a message
341
                $message = $result;
342
            }
343
        } catch (Exception $ex) {
344
            $error = true;
345
            $message = $ex->getMessage();
346
        }
347
        $isNewRecord = $record->ID == 0;
348
        // Build default message
349
        if (!$message) {
350
            $message = _t(
351
                'ActionsGridFieldItemRequest.DONE',
352
                'Action {action} was done on {name}',
353
                array(
354
                    'action' => $clickedAction->getTitle(),
355
                    'name' => $record->i18n_singular_name(),
356
                )
357
            );
358
        }
359
        $status = 'good';
360
        if ($error) {
361
            $status = 'bad';
362
        }
363
        // We don't have a form, simply return the result
364
        if (!$form) {
365
            if ($error) {
366
                return $this->owner->httpError(403, $message);
367
            }
368
            return $message;
369
        }
370
        if (Director::is_ajax()) {
371
            $controller = $this->getToplevelController();
372
            $controller->getResponse()->addHeader('X-Status', rawurlencode($message));
373
            if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) {
374
                $controller->getResponse()->addHeader('X-Reload', true);
375
            }
376
        } else {
377
            $form->sessionMessage($message, $status, ValidationResult::CAST_HTML);
378
        }
379
        // Redirect after action
380
        return $this->redirectAfterAction($isNewRecord);
381
    }
382
383
    /**
384
     * Handles custom links
385
     *
386
     * Use CustomLink with default behaviour to trigger this
387
     *
388
     * See:
389
     * DefaultLink::getModelLink
390
     * GridFieldCustomLink::getLink
391
     *
392
     * @param HTTPRequest $request
393
     * @return HTTPResponse|DBHTMLText
394
     */
395
    public function doCustomLink(HTTPRequest $request)
396
    {
397
        $action = $request->getVar('CustomLink');
398
        return $this->forwardActionToRecord($action);
399
    }
400
401
    /**
402
     * Handles custom actions
403
     *
404
     * Use CustomAction class to trigger this
405
     *
406
     * @param array The form data
407
     * @param Form The form object
408
     * @return HTTPResponse|DBHTMLText
409
     */
410
    public function doCustomAction($data, $form)
411
    {
412
        $action = key($data['action_doCustomAction']);
413
        return $this->forwardActionToRecord($action, $data, $form);
414
    }
415
416
    /**
417
     * Saves the form and goes back to list view
418
     *
419
     * @param array The form data
420
     * @param Form The form object
421
     */
422
    public function doSaveAndClose($data, $form)
423
    {
424
        $result = $this->owner->doSave($data, $form);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
425
        // Redirect after save
426
        $controller = $this->getToplevelController();
427
        $controller->getResponse()->addHeader("X-Pjax", "Content");
428
        return $controller->redirect($this->getBackLink());
429
    }
430
431
    /**
432
     * Saves the form and goes back to the next item
433
     *
434
     * @param array The form data
435
     * @param Form The form object
436
     */
437
    public function doSaveAndNext($data, $form)
438
    {
439
        $record = $this->owner->record;
440
        $result = $this->owner->doSave($data, $form);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
441
        // Redirect after save
442
        $controller = $this->getToplevelController();
443
        $controller->getResponse()->addHeader("X-Pjax", "Content");
444
445
        $getNextRecordID = $this->owner->getNextRecordID();
446
        $class = get_class($record);
447
        $next = $class::get()->byID($getNextRecordID);
448
449
        $link = $this->owner->getEditLink($getNextRecordID);
450
451
        // Link to a specific tab if set
452
        if ($next && !empty($data['_activetab'])) {
453
            $link .= '#' . $data['_activetab'];
454
        }
455
        return $controller->redirect($link);
456
    }
457
458
    /**
459
     * Saves the form and goes to the previous item
460
     *
461
     * @param array The form data
462
     * @param Form The form object
463
     */
464
    public function doSaveAndPrev($data, $form)
465
    {
466
        $record = $this->owner->record;
467
        $result = $this->owner->doSave($data, $form);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
468
        // Redirect after save
469
        $controller = $this->getToplevelController();
470
        $controller->getResponse()->addHeader("X-Pjax", "Content");
471
472
        $getPreviousRecordID = $this->owner->getPreviousRecordID();
473
        $class = get_class($record);
474
        $prev = $class::get()->byID($getPreviousRecordID);
475
476
        $link = $this->owner->getEditLink($getPreviousRecordID);
477
478
        // Link to a specific tab if set
479
        if ($prev && !empty($data['_activetab'])) {
480
            $link .= '#' . $data['_activetab'];
481
        }
482
        return $controller->redirect($link);
483
    }
484
485
    /**
486
     * Gets the top level controller.
487
     *
488
     * @return Controller
489
     * @todo  This had to be directly copied from {@link GridFieldDetailForm_ItemRequest}
490
     * because it is a protected method and not visible to a decorator!
491
     */
492
    protected function getToplevelController()
493
    {
494
        $c = $this->owner->getController();
495
        while ($c && $c instanceof GridFieldDetailForm_ItemRequest) {
496
            $c = $c->getController();
497
        }
498
        return $c;
499
    }
500
501
    /**
502
     * Gets the back link
503
     *
504
     * @return string
505
     * @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest}
506
     * because it is a protected method and not visible to a decorator!
507
     */
508
    public function getBackLink()
509
    {
510
        // TODO Coupling with CMS
511
        $backlink = '';
512
        $toplevelController = $this->getToplevelController();
513
        if ($toplevelController && $toplevelController instanceof LeftAndMain) {
514
            if ($toplevelController->hasMethod('Backlink')) {
515
                $backlink = $toplevelController->Backlink();
516
            } elseif ($this->owner->getController()->hasMethod('Breadcrumbs')) {
517
                $parents = $this->owner->getController()->Breadcrumbs(false)->items;
518
                $backlink = array_pop($parents)->Link;
519
            }
520
        }
521
        if (!$backlink) {
522
            $backlink = $toplevelController->Link();
523
        }
524
        return $backlink;
525
    }
526
527
    /**
528
     * Response object for this request after a successful save
529
     *
530
     * @param bool $isNewRecord True if this record was just created
531
     * @return HTTPResponse|DBHTMLText
532
     * @todo  This had to be directly copied from {@link GridFieldDetailForm_ItemRequest}
533
     * because it is a protected method and not visible to a decorator!
534
     */
535
    protected function redirectAfterAction($isNewRecord)
536
    {
537
        $controller = $this->getToplevelController();
538
        if ($isNewRecord) {
539
            return $controller->redirect($this->owner->Link());
540
        } elseif ($this->owner->gridField->getList()->byID($this->owner->record->ID)) {
0 ignored issues
show
Bug introduced by
The method byID() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Limitable. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

540
        } elseif ($this->owner->gridField->getList()->/** @scrutinizer ignore-call */ byID($this->owner->record->ID)) {
Loading history...
541
            // Return new view, as we can't do a "virtual redirect" via the CMS Ajax
542
            // to the same URL (it assumes that its content is already current, and doesn't reload)
543
            return $this->owner->edit($controller->getRequest());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->owner->edi...ntroller->getRequest()) returns the type SilverStripe\ORM\FieldTy...ViewableData_Customised which is incompatible with the documented return type LeKoala\CmsActions\DBHTM...pe\Control\HTTPResponse.
Loading history...
544
        } else {
545
            // Changes to the record properties might've excluded the record from
546
            // a filtered list, so return back to the main view if it can't be found
547
            $url = $controller->getRequest()->getURL();
548
            $noActionURL = $controller->removeAction($url);
549
            $controller->getRequest()->addHeader('X-Pjax', 'Content');
550
            return $controller->redirect($noActionURL, 302);
551
        }
552
    }
553
}
554