Passed
Pull Request — master (#123)
by Robbie
01:57
created

doRollback()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 2
eloc 16
nc 2
nop 2
1
<?php
2
3
namespace DNADesign\Elemental\Forms;
4
5
use SilverStripe\Versioned\VersionedGridFieldItemRequest;
6
use SilverStripe\Versioned\Versioned;
7
use SilverStripe\Forms\FormAction;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\HiddenField;
10
use SilverStripe\Forms\ReadonlyField;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\ORM\ValidationResult;
13
14
/**
15
 * Overrides core Versioned GridField support to provide revert to version
16
 * support.
17
 */
18
class HistoricalVersionedGridFieldItemRequest extends VersionedGridFieldItemRequest
19
{
20
    private static $allowed_actions = [
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'view',
22
        'ItemEditForm'
23
    ];
24
25
    public function __construct($gridField, $component, $record, $requestHandler, $popupFormName)
26
    {
27
        if ($version = $requestHandler->getRequest()->requestVar('VersionID')) {
28
            $record = Versioned::get_version(get_class($record), $record->ID, $version);
29
30
            if (!$record) {
31
                return $requestHandler->httpError(404, _t(__CLASS__.'.InvalidVersion', 'Invalid version'));
32
            }
33
        }
34
35
        parent::__construct($gridField, $component, $record, $requestHandler, $popupFormName);
36
    }
37
38
    public function view($request)
39
    {
40
        if (!$this->record->canView()) {
41
            $this->httpError(403);
42
        }
43
44
        $controller = $this->getToplevelController();
45
46
        $form = $this->ItemEditForm();
47
48
        $data = new ArrayData(array(
49
            'Backlink'     => $controller->Link(),
50
            'ItemEditForm' => $form
51
        ));
52
        $return = $data->renderWith($this->getTemplates());
53
54
        if ($request->isAjax()) {
55
            return $return;
56
        } else {
57
            return $controller->customise(array('Content' => $return));
58
        }
59
    }
60
61
    public function ItemEditForm()
62
    {
63
        $form = parent::ItemEditForm();
64
        $form->Fields()->push(HiddenField::create('VersionID', '', $this->record->Version));
0 ignored issues
show
Bug introduced by
'VersionID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

64
        $form->Fields()->push(HiddenField::create(/** @scrutinizer ignore-type */ 'VersionID', '', $this->record->Version));
Loading history...
65
        $form->Fields()->addFieldToTab('Root.Main', ReadonlyField::create('Sort', _t(__CLASS__ .'.Position', 'Position'), $this->record->Sort));
66
67
        $form->setFields($form->Fields()->makeReadonly());
68
69
        return $form;
70
    }
71
72
    public function doRollback($data, $form)
0 ignored issues
show
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

72
    public function doRollback(/** @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...
73
    {
74
        // Check permission
75
        if (!$this->record->canEdit()) {
76
            return $this->httpError(403);
77
        }
78
79
        // Save from form data
80
        $this->record->doRollbackTo($this->record->Version);
81
82
        $link = '<a href="' . $this->Link('edit') . '">"'
83
            . htmlspecialchars($this->record->Title, ENT_QUOTES)
84
            . '"</a>';
85
86
        $message = _t(
87
            __CLASS__ .'.RolledBack',
88
            'Rolled back {name} to version {version} {link}',
89
            array(
90
                'name' => $this->record->i18n_singular_name(),
91
                'version' => $this->record->Version,
92
                'link' => $link
93
            )
94
        );
95
96
        $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
97
        $controller = $this->getToplevelController();
98
99
        return $controller->redirect($this->record->CMSEditLink());
100
    }
101
102
    public function getFormActions()
103
    {
104
        $record = $this->getRecord();
105
106
        if (!$record || !$record->has_extension(Versioned::class)) {
107
            return $actions;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $actions seems to be never defined.
Loading history...
108
        }
109
110
        $this->beforeExtending('updateFormActions', function (FieldList $actions) use ($record) {
111
            if (!$record->isLatestVersion()) {
112
                $actions->removeByName('action_doUnpublish');
113
                $actions->removeByName('action_doDelete');
114
                $actions->removeByName('action_doSave');
115
                $actions->removeByName('action_doPublish');
116
                $actions->removeByName('action_doArchive');
117
            }
118
119
            if ($record->canEdit()) {
120
                $actions->push(
121
                    FormAction::create(
122
                        'doRollback',
0 ignored issues
show
Bug introduced by
'doRollback' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

122
                        /** @scrutinizer ignore-type */ 'doRollback',
Loading history...
123
                        _t(__CLASS__.'.REVERT', 'Revert to this version')
124
                    )
125
                        ->setUseButtonTag(true)
126
                        ->setDescription(_t(
127
                            __CLASS__.'.BUTTONREVERTDESC',
128
                            'Publish this record to the draft site'
129
                        ))
130
                        ->addExtraClass('btn-warning font-icon-back-in-time')
131
                );
132
            }
133
        });
134
135
        $actions = parent::getFormActions();
136
137
        return $actions;
138
    }
139
}
140