1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\VersionedAdmin\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\Form; |
8
|
|
|
use SilverStripe\Forms\FormAction; |
9
|
|
|
use SilverStripe\ORM\DataExtension; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverStripe\ORM\ValidationResult; |
12
|
|
|
use SilverStripe\Versioned\RestoreAction; |
13
|
|
|
use SilverStripe\Versioned\Versioned; |
14
|
|
|
use SilverStripe\VersionedAdmin\ArchiveAdmin; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Adds a restore action to the item edit form of ArchiveAdmin |
18
|
|
|
*/ |
19
|
|
|
class ArchiveRestoreAction extends DataExtension |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Updates the edit form with a restore button if it is being viewed |
23
|
|
|
* |
24
|
|
|
* @param Form $form |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
|
|
public function updateItemEditForm(Form $form) |
28
|
|
|
{ |
29
|
|
|
$record = $this->owner->getRecord(); |
30
|
|
|
|
31
|
|
|
if ($this->shouldDisplayAction($record)) { |
32
|
|
|
$restoreToRoot = RestoreAction::shouldRestoreToRoot($record); |
33
|
|
|
|
34
|
|
|
$title = $restoreToRoot |
35
|
|
|
? _t('SilverStripe\\Versioned\\RestoreAction.RESTORE_TO_ROOT', 'Restore to draft at top level') |
36
|
|
|
: _t('SilverStripe\\Versioned\\RestoreAction.RESTORE', 'Restore to draft'); |
37
|
|
|
$description = $restoreToRoot |
38
|
|
|
? _t( |
39
|
|
|
'SilverStripe\\Versioned\\RestoreAction.RESTORE_TO_ROOT_DESC', |
40
|
|
|
'Restore the archived version to draft as a top level item' |
41
|
|
|
) |
42
|
|
|
: _t( |
43
|
|
|
'SilverStripe\\Versioned\\RestoreAction.RESTORE_DESC', |
44
|
|
|
'Restore the archived version to draft' |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$form->actions = FieldList::create( |
48
|
|
|
FormAction::create('doRestore', $title) |
49
|
|
|
->setDescription($description) |
50
|
|
|
->setAttribute('data-to-root', $restoreToRoot) |
51
|
|
|
->addExtraClass('btn-warning font-icon-back-in-time ArchiveAdmin__action--restore') |
52
|
|
|
->setUseButtonTag(true) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$form->unsetValidator(); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns whether the restore action should be displayed |
61
|
|
|
* |
62
|
|
|
* @param $record |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
protected function shouldDisplayAction($record) |
66
|
|
|
{ |
67
|
|
|
$admin = $this->owner->popupController; |
68
|
|
|
// If the record is a File, check if the file binary was archived |
69
|
|
|
$hasFileSaved = $record instanceof File ? $record->exists() : true; |
70
|
|
|
return ( |
71
|
|
|
$hasFileSaved && |
72
|
|
|
$admin instanceof ArchiveAdmin && |
73
|
|
|
DataObject::has_extension($record, Versioned::class) && |
74
|
|
|
$record->canRestoreToDraft() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Restore the record to its original place or top level if that's not possible |
80
|
|
|
* |
81
|
|
|
* @param array $data |
82
|
|
|
* @param Form $form |
83
|
|
|
* @return HTTPResponse |
|
|
|
|
84
|
|
|
*/ |
85
|
|
|
public function doRestore($data, $form) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$record = $this->owner->getRecord(); |
88
|
|
|
|
89
|
|
|
$message = RestoreAction::restore($record); |
90
|
|
|
|
91
|
|
|
$controller = $this->owner->popupController; |
92
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); |
|
|
|
|
93
|
|
|
$controller->getEditForm()->sessionMessage($message['text'], $message['type'], ValidationResult::CAST_HTML); |
94
|
|
|
|
95
|
|
|
return $controller->redirect($controller->Link(), 'index'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|