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

__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 5
nc 3
nop 5
1
<?php
2
3
namespace DNADesign\Elemental\Forms;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\FormAction;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldConfig;
9
use SilverStripe\Forms\GridField\GridFieldPaginator;
10
use SilverStripe\Forms\HiddenField;
11
use SilverStripe\Forms\ReadonlyField;
12
use SilverStripe\ORM\ValidationResult;
13
use SilverStripe\Versioned\VersionedGridFieldItemRequest;
14
use SilverStripe\Versioned\Versioned;
15
use SilverStripe\View\ArrayData;
16
17
/**
18
 * Overrides core Versioned GridField support to provide revert to version
19
 * support.
20
 */
21
class HistoricalVersionedGridFieldItemRequest extends VersionedGridFieldItemRequest
22
{
23
    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...
24
        'view',
25
        'ItemEditForm'
26
    ];
27
28
    /**
29
     * The requested version ID
30
     *
31
     * @var int
32
     */
33
    protected $versionId;
34
35
    public function __construct($gridField, $component, $record, $requestHandler, $popupFormName)
36
    {
37
        if ($this->versionId = $requestHandler->getRequest()->requestVar('VersionID')) {
38
            $record = Versioned::get_version(get_class($record), $record->ID, $this->versionId);
39
40
            if (!$record) {
41
                return $requestHandler->httpError(404, _t(__CLASS__.'.InvalidVersion', 'Invalid version'));
42
            }
43
        }
44
45
        parent::__construct($gridField, $component, $record, $requestHandler, $popupFormName);
46
    }
47
48
    public function view($request)
49
    {
50
        if (!$this->record->canView()) {
51
            $this->httpError(403);
52
        }
53
54
        $controller = $this->getToplevelController();
55
56
        $form = $this->ItemEditForm();
57
58
        $data = ArrayData::create([
59
            'Backlink'     => $controller->Link(),
60
            'ItemEditForm' => $form
61
        ]);
62
        $return = $data->renderWith($this->getTemplates());
63
64
        if ($request->isAjax()) {
65
            return $return;
66
        }
67
68
        return $controller->customise(['Content' => $return]);
69
    }
70
71
    public function ItemEditForm()
72
    {
73
        $form = parent::ItemEditForm();
74
75
        $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

75
        $form->Fields()->push(HiddenField::create(/** @scrutinizer ignore-type */ 'VersionID', '', $this->record->Version));
Loading history...
76
        $form->Fields()->addFieldToTab(
77
            'Root.Main',
78
            ReadonlyField::create('Sort', _t(__CLASS__ .'.Position', 'Position'), $this->record->Sort)
79
        );
80
81
        $fields = $form->Fields()->makeReadonly();
82
        $fields->unshift($this->getVersionGridField()->setForm($form));
83
84
        $form->setFields($fields);
85
86
        return $form;
87
    }
88
89
    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

89
    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...
90
    {
91
        // Check permission
92
        if (!$this->record->canEdit()) {
93
            return $this->httpError(403);
94
        }
95
96
        // Save from form data
97
        $this->record->doRollbackTo($this->record->Version);
98
99
        $link = '<a href="' . $this->Link('edit') . '">"'
100
            . htmlspecialchars($this->record->Title, ENT_QUOTES)
101
            . '"</a>';
102
103
        $message = _t(
104
            __CLASS__ .'.RolledBack',
105
            'Rolled back {name} to version {version} {link}',
106
            array(
107
                'name' => $this->record->i18n_singular_name(),
108
                'version' => $this->record->Version,
109
                'link' => $link
110
            )
111
        );
112
113
        $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
114
        $controller = $this->getToplevelController();
115
116
        return $controller->redirect($this->record->CMSEditLink());
117
    }
118
119
    public function getFormActions()
120
    {
121
        $record = $this->getRecord();
122
123
        if (!$record || !$record->has_extension(Versioned::class)) {
124
            return $actions;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $actions seems to be never defined.
Loading history...
125
        }
126
127
        $this->beforeExtending('updateFormActions', function (FieldList $actions) use ($record) {
128
            if (!$record->isLatestVersion()) {
129
                $actions->removeByName('action_doUnpublish');
130
                $actions->removeByName('action_doDelete');
131
                $actions->removeByName('action_doSave');
132
                $actions->removeByName('action_doPublish');
133
                $actions->removeByName('action_doArchive');
134
            }
135
136
            if ($record->canEdit()) {
137
                $actions->push(
138
                    FormAction::create(
139
                        '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

139
                        /** @scrutinizer ignore-type */ 'doRollback',
Loading history...
140
                        _t(__CLASS__.'.REVERT', 'Revert to this version')
141
                    )
142
                        ->setUseButtonTag(true)
143
                        ->setDescription(_t(
144
                            __CLASS__.'.BUTTONREVERTDESC',
145
                            'Publish this record to the draft site'
146
                        ))
147
                        ->addExtraClass('btn-warning font-icon-back-in-time')
148
                );
149
            }
150
        });
151
152
        $actions = parent::getFormActions();
153
154
        return $actions;
155
    }
156
157
    /**
158
     * Get the specific version in a GridField, as the only record
159
     *
160
     * @return GridField
161
     */
162
    public function getVersionGridField()
163
    {
164
        /** @var GridField $versionGridField */
165
        $versionGridField = $this->getRecord()->getHistoryFields(false)->fieldByName('History');
166
167
        /** @var GridFieldConfig */
168
        $config = $versionGridField->getConfig();
169
170
        $config->removeComponentsByType([
171
            GridFieldPaginator::class,
172
            ElementalGridFieldHistoryButton::class,
173
        ]);
174
175
        // Filter this version ID
176
        $versionGridField->setList(
177
            $versionGridField->getList()->filter(['Version' => $this->versionId])
0 ignored issues
show
Bug introduced by
The method filter() 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

177
            $versionGridField->getList()->/** @scrutinizer ignore-call */ filter(['Version' => $this->versionId])
Loading history...
178
        );
179
180
        // Add a unique class name so we can style
181
        $versionGridField->addExtraClass('elemental-block__history--detail');
182
183
        return $versionGridField;
184
    }
185
}
186