|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Versioning; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPResponse; |
|
6
|
|
|
use SilverStripe\Core\Convert; |
|
7
|
|
|
use SilverStripe\Forms\Form; |
|
8
|
|
|
use SilverStripe\Forms\FormAction; |
|
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; |
|
10
|
|
|
use SilverStripe\ORM\DataObject; |
|
11
|
|
|
use SilverStripe\ORM\ValidationResult; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provides versioned dataobject support to {@see GridFieldDetailForm_ItemRequest} |
|
15
|
|
|
* |
|
16
|
|
|
* @property GridFieldDetailForm_ItemRequest $owner |
|
17
|
|
|
*/ |
|
18
|
|
|
class VersionedGridFieldItemRequest extends GridFieldDetailForm_ItemRequest |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
protected function getFormActions() |
|
22
|
|
|
{ |
|
23
|
|
|
$actions = parent::getFormActions(); |
|
24
|
|
|
|
|
25
|
|
|
// Check if record is versionable |
|
26
|
|
|
/** @var Versioned|DataObject $record */ |
|
27
|
|
|
$record = $this->getRecord(); |
|
28
|
|
|
if (!$record || !$record->has_extension(Versioned::class)) { |
|
|
|
|
|
|
29
|
|
|
return $actions; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// Save & Publish action |
|
33
|
|
|
if ($record->canPublish()) { |
|
|
|
|
|
|
34
|
|
|
// "publish", as with "save", it supports an alternate state to show when action is needed. |
|
35
|
|
|
$publish = FormAction::create( |
|
36
|
|
|
'doPublish', |
|
37
|
|
|
_t('VersionedGridFieldItemRequest.BUTTONPUBLISH', 'Publish') |
|
38
|
|
|
) |
|
39
|
|
|
->setUseButtonTag(true) |
|
40
|
|
|
->addExtraClass('ss-ui-action-constructive') |
|
41
|
|
|
->setAttribute('data-icon', 'accept'); |
|
42
|
|
|
|
|
43
|
|
|
// Insert after save |
|
44
|
|
|
if ($actions->fieldByName('action_doSave')) { |
|
45
|
|
|
$actions->insertAfter('action_doSave', $publish); |
|
46
|
|
|
} else { |
|
47
|
|
|
$actions->push($publish); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Unpublish action |
|
52
|
|
|
$isPublished = $record->isPublished(); |
|
|
|
|
|
|
53
|
|
|
if ($isPublished && $record->canUnpublish()) { |
|
|
|
|
|
|
54
|
|
|
$actions->push( |
|
55
|
|
|
FormAction::create( |
|
56
|
|
|
'doUnpublish', |
|
57
|
|
|
_t('VersionedGridFieldItemRequest.BUTTONUNPUBLISH', 'Unpublish') |
|
58
|
|
|
) |
|
59
|
|
|
->setUseButtonTag(true) |
|
60
|
|
|
->setDescription(_t( |
|
61
|
|
|
'VersionedGridFieldItemRequest.BUTTONUNPUBLISHDESC', |
|
62
|
|
|
'Remove this record from the published site' |
|
63
|
|
|
)) |
|
64
|
|
|
->addExtraClass('ss-ui-action-destructive') |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Archive action |
|
69
|
|
|
if ($record->canArchive()) { |
|
|
|
|
|
|
70
|
|
|
// Replace "delete" action |
|
71
|
|
|
$actions->removeByName('action_doDelete'); |
|
72
|
|
|
|
|
73
|
|
|
// "archive" |
|
74
|
|
|
$actions->push( |
|
75
|
|
|
FormAction::create('doArchive', _t('VersionedGridFieldItemRequest.ARCHIVE', 'Archive')) |
|
76
|
|
|
->setDescription(_t( |
|
77
|
|
|
'VersionedGridFieldItemRequest.BUTTONARCHIVEDESC', |
|
78
|
|
|
'Unpublish and send to archive' |
|
79
|
|
|
)) |
|
80
|
|
|
->addExtraClass('delete ss-ui-action-destructive') |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
return $actions; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Archive this versioned record |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $data |
|
90
|
|
|
* @param Form $form |
|
91
|
|
|
* @return HTTPResponse |
|
92
|
|
|
*/ |
|
93
|
|
|
public function doArchive($data, $form) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
/** @var Versioned|DataObject $record */ |
|
96
|
|
|
$record = $this->getRecord(); |
|
97
|
|
|
if (!$record->canArchive()) { |
|
|
|
|
|
|
98
|
|
|
return $this->httpError(403); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Record name before it's deleted |
|
102
|
|
|
$title = $record->Title; |
|
103
|
|
|
$record->doArchive(); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$message = sprintf( |
|
106
|
|
|
_t('VersionedGridFieldItemRequest.Archived', 'Archived %s %s'), |
|
107
|
|
|
$record->i18n_singular_name(), |
|
|
|
|
|
|
108
|
|
|
Convert::raw2xml($title) |
|
109
|
|
|
); |
|
110
|
|
|
$this->setFormMessage($form, $message); |
|
111
|
|
|
|
|
112
|
|
|
//when an item is deleted, redirect to the parent controller |
|
113
|
|
|
$controller = $this->getToplevelController(); |
|
114
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh |
|
115
|
|
|
|
|
116
|
|
|
return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Publish this versioned record |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $data |
|
123
|
|
|
* @param Form $form |
|
124
|
|
|
* @return HTTPResponse |
|
125
|
|
|
*/ |
|
126
|
|
|
public function doPublish($data, $form) |
|
127
|
|
|
{ |
|
128
|
|
|
/** @var Versioned|DataObject $record */ |
|
129
|
|
|
$record = $this->getRecord(); |
|
130
|
|
|
$isNewRecord = $record->ID == 0; |
|
131
|
|
|
|
|
132
|
|
|
// Check permission |
|
133
|
|
|
if (!$record->canPublish()) { |
|
|
|
|
|
|
134
|
|
|
return $this->httpError(403); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Initial save and reload |
|
138
|
|
|
$record = $this->saveFormIntoRecord($data, $form); |
|
139
|
|
|
$record->publishRecursive(); |
|
140
|
|
|
$editURL = $this->Link('edit'); |
|
141
|
|
|
$xmlTitle = Convert::raw2xml($record->Title); |
|
142
|
|
|
$link = "<a href=\"{$editURL}\">{$xmlTitle}</a>"; |
|
143
|
|
|
$message = _t( |
|
144
|
|
|
'VersionedGridFieldItemRequest.Published', |
|
145
|
|
|
'Published {name} {link}', |
|
146
|
|
|
array( |
|
147
|
|
|
'name' => $record->i18n_singular_name(), |
|
148
|
|
|
'link' => $link |
|
149
|
|
|
) |
|
150
|
|
|
); |
|
151
|
|
|
$this->setFormMessage($form, $message); |
|
152
|
|
|
|
|
153
|
|
|
return $this->redirectAfterSave($isNewRecord); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Delete this record from the live site |
|
158
|
|
|
* |
|
159
|
|
|
* @param array $data |
|
160
|
|
|
* @param Form $form |
|
161
|
|
|
* @return HTTPResponse |
|
162
|
|
|
*/ |
|
163
|
|
|
public function doUnpublish($data, $form) |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
/** @var Versioned|DataObject $record */ |
|
166
|
|
|
$record = $this->getRecord(); |
|
167
|
|
|
if (!$record->canUnpublish()) { |
|
|
|
|
|
|
168
|
|
|
return $this->httpError(403); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Record name before it's deleted |
|
172
|
|
|
$title = $record->Title; |
|
173
|
|
|
$record->doUnpublish(); |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
$message = sprintf( |
|
176
|
|
|
_t('VersionedGridFieldItemRequest.Unpublished', 'Unpublished %s %s'), |
|
177
|
|
|
$record->i18n_singular_name(), |
|
|
|
|
|
|
178
|
|
|
Convert::raw2xml($title) |
|
179
|
|
|
); |
|
180
|
|
|
$this->setFormMessage($form, $message); |
|
181
|
|
|
|
|
182
|
|
|
// Redirect back to edit |
|
183
|
|
|
return $this->redirectAfterSave(false); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param Form $form |
|
188
|
|
|
* @param string $message |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function setFormMessage($form, $message) |
|
191
|
|
|
{ |
|
192
|
|
|
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
|
193
|
|
|
$controller = $this->getToplevelController(); |
|
194
|
|
|
if ($controller->hasMethod('getEditForm')) { |
|
195
|
|
|
/** @var Form $backForm */ |
|
196
|
|
|
$backForm = $controller->getEditForm(); |
|
197
|
|
|
$backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: