Passed
Push — master ( b101db...a45b24 )
by Robbie
05:17
created

ArchiveRestoreAction::updateItemEditForm()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 5
nop 1
dl 0
loc 29
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Extensions;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Forms\FormAction;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\ValidationResult;
11
use SilverStripe\Versioned\RestoreAction;
12
use SilverStripe\Versioned\Versioned;
13
use SilverStripe\VersionedAdmin\ArchiveAdmin;
14
15
/**
16
 * Adds a restore action to the item edit form of ArchiveAdmin
17
 */
18
class ArchiveRestoreAction extends DataExtension
19
{
20
    /**
21
     * Updates the edit form with a restore button if it is being viewed
22
     *
23
     * @param Form $form
24
     * @return mixed
25
     */
26
    public function updateItemEditForm(Form $form)
27
    {
28
        $record = $this->owner->getRecord();
29
30
        if ($this->shouldDisplayAction($record)) {
31
            $restoreToRoot = RestoreAction::shouldRestoreToRoot($record);
32
33
            $title = $restoreToRoot
34
                ? _t('SilverStripe\\Forms\\RestoreAction.RESTORE_TO_ROOT', 'Restore draft at top level')
35
                : _t('SilverStripe\\Forms\\RestoreAction.RESTORE', 'Restore draft');
36
            $description = $restoreToRoot
37
                ? _t(
38
                    'SilverStripe\\Forms\\RestoreAction.RESTORE_TO_ROOT_DESC',
39
                    'Restore the archived version to draft as a top level item'
40
                )
41
                : _t(
42
                    'SilverStripe\\Forms\\RestoreAction.RESTORE_DESC',
43
                    'Restore the archived version to draft'
44
                );
45
46
            $form->actions = FieldList::create(
47
                FormAction::create('doRestore', $title)
48
                    ->setDescription($description)
49
                    ->setAttribute('data-to-root', $restoreToRoot)
50
                    ->addExtraClass('btn-warning font-icon-back-in-time ArchiveAdmin__action--restore')
51
                    ->setUseButtonTag(true)
52
            );
53
54
            $form->unsetValidator();
55
        }
56
    }
57
58
    /**
59
     * Returns whether the restore action should be displayed
60
     *
61
     * @param $record
62
     * @return bool
63
     */
64
    protected function shouldDisplayAction($record)
65
    {
66
        $admin = $this->owner->popupController;
67
        return (
68
            $admin instanceof ArchiveAdmin &&
69
            DataObject::has_extension($record, Versioned::class) &&
70
            $record->canRestoreToDraft());
71
    }
72
73
    /**
74
     * Restore the record to its original place or top level if that's not possible
75
     *
76
     * @param array $data
77
     * @param Form $form
78
     * @return HTTPResponse
0 ignored issues
show
Bug introduced by
The type SilverStripe\VersionedAd...Extensions\HTTPResponse was not found. Did you mean HTTPResponse? If so, make sure to prefix the type with \.
Loading history...
79
     */
80
    public function doRestore($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed. ( Ignorable by Annotation )

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

80
    public function doRestore($data, /** @scrutinizer ignore-unused */ $form)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed. ( Ignorable by Annotation )

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

80
    public function doRestore(/** @scrutinizer ignore-unused */ $data, $form)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $record = $this->owner->getRecord();
83
84
        $message = RestoreAction::restore($record);
85
86
        $controller = $this->owner->popupController;
87
        $controller->getRequest()->addHeader('X-Pjax', 'Content');
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on null. ( Ignorable by Annotation )

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

87
        $controller->/** @scrutinizer ignore-call */ 
88
                     getRequest()->addHeader('X-Pjax', 'Content');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        $controller->getEditForm()->sessionMessage($message['text'], $message['type'], ValidationResult::CAST_HTML);
89
90
        return $controller->redirect($controller->Link(), 'index');
91
    }
92
}
93