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 0
    public function updateItemEditForm(Form $form)
28
    {
29 0
        $record = $this->owner->getRecord();
30

31 0
        if ($this->shouldDisplayAction($record)) {
32 0
            $restoreToRoot = RestoreAction::shouldRestoreToRoot($record);
33

34 0
            $title = $restoreToRoot
35 0
                ? _t('SilverStripe\\Versioned\\RestoreAction.RESTORE_TO_ROOT', 'Restore to draft at top level')
36 0
                : _t('SilverStripe\\Versioned\\RestoreAction.RESTORE', 'Restore to draft');
37 0
            $description = $restoreToRoot
38 0
                ? _t(
39 0
                    'SilverStripe\\Versioned\\RestoreAction.RESTORE_TO_ROOT_DESC',
40 0
                    'Restore the archived version to draft as a top level item'
41
                )
42 0
                : _t(
43 0
                    'SilverStripe\\Versioned\\RestoreAction.RESTORE_DESC',
44 0
                    'Restore the archived version to draft'
45
                );
46

47 0
            $form->actions = FieldList::create(
48 0
                FormAction::create('doRestore', $title)
49 0
                    ->setDescription($description)
50 0
                    ->setAttribute('data-to-root', $restoreToRoot)
51 0
                    ->addExtraClass('btn-warning font-icon-back-in-time ArchiveAdmin__action--restore')
52 0
                    ->setUseButtonTag(true)
53
            );
54

55 0
            $form->unsetValidator();
56
        }
57
    }
58

59
    /**
60
     * Returns whether the restore action should be displayed
61
     *
62
     * @param $record
63
     * @return bool
64
     */
65 0
    protected function shouldDisplayAction($record)
66
    {
67 0
        $admin = $this->owner->popupController;
68
        // If the record is a File, check if the file binary was archived
69 0
        $hasFileSaved = $record instanceof File ? $record->exists() : true;
70
        return (
71 0
            $hasFileSaved &&
72 0
            $admin instanceof ArchiveAdmin &&
73 0
            DataObject::has_extension($record, Versioned::class) &&
74 0
            $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 0
    public function doRestore($data, $form)
86
    {
87 0
        $record = $this->owner->getRecord();
88

89 0
        $message = RestoreAction::restore($record);
90

91 0
        $controller = $this->owner->popupController;
92 0
        $controller->getRequest()->addHeader('X-Pjax', 'Content');
93 0
        $controller->getEditForm()->sessionMessage($message['text'], $message['type'], ValidationResult::CAST_HTML);
94

95 0
        return $controller->redirect($controller->Link(), 'index');
96
    }
97
}

Read our documentation on viewing source code .

Loading