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 = [ |
|
|
|
|
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)); |
|
|
|
|
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) |
|
|
|
|
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; |
|
|
|
|
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', |
|
|
|
|
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]) |
|
|
|
|
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
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.