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