Passed
Pull Request — master (#5)
by Robbie
03:18
created

HistoryViewerController::getVersionForm()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 43
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 24
nc 5
nop 1
dl 0
loc 43
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Controllers;
4
5
use InvalidArgumentException;
6
use SilverStripe\Admin\LeftAndMain;
7
use SilverStripe\Admin\LeftAndMainFormRequestHandler;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Control\HTTPResponse;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Forms\FormFactory;
12
use SilverStripe\Versioned\Versioned;
13
use SilverStripe\VersionedAdmin\Forms\DataObjectVersionFormFactory;
14
15
class HistoryViewerController extends LeftAndMain
16
{
17
    private static $url_segment = 'historyviewer';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
18
19
    private static $url_rule = '/$Action';
0 ignored issues
show
introduced by
The private property $url_rule is not used, and could be removed.
Loading history...
20
21
    private static $url_priority = 10;
0 ignored issues
show
introduced by
The private property $url_priority is not used, and could be removed.
Loading history...
22
23
    private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
0 ignored issues
show
introduced by
The private property $required_permission_codes is not used, and could be removed.
Loading history...
24
25
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
26
        'versionForm',
27
        'schema',
28
    ];
29
30
    public function getClientConfig()
31
    {
32
        $clientConfig = parent::getClientConfig();
33
34
        $clientConfig['form']['versionForm'] = [
35
            'schemaUrl' => $this->owner->Link('schema/versionForm')
0 ignored issues
show
Bug Best Practice introduced by
The property owner does not exist on SilverStripe\VersionedAd...HistoryViewerController. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method Link() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            'schemaUrl' => $this->owner->/** @scrutinizer ignore-call */ Link('schema/versionForm')

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
        ];
37
38
        return $clientConfig;
39
    }
40
41
    /**
42
     * Gets a JSON schema representing the current version detail form.
43
     *
44
     * WARNING: Experimental API.
45
     *
46
     * @param HTTPRequest $request
47
     * @return HTTPResponse
48
     */
49
    public function schema($request)
50
    {
51
        $formName = $request->param('FormName');
52
        if ($formName !== 'versionForm') {
53
            return parent::schema($request);
54
        }
55
56
        // Get schema for history form
57
        // @todo Eventually all form scaffolding will be based on context rather than record ID
58
        // See https://github.com/silverstripe/silverstripe-framework/issues/6362
59
        $form = $this->getVersionForm([
60
            'RecordClass' => $request->getVar('RecordClass'),
61
            'RecordID' => $request->getVar('RecordID'),
62
            'RecordVersion' => $request->getVar('RecordVersion'),
63
        ]);
64
65
        // Respond with this schema
66
        $response = $this->getResponse();
67
        $response->addHeader('Content-Type', 'application/json');
68
        $schemaID = $this->getRequest()->getURL();
69
        return $this->getSchemaResponse($schemaID, $form);
70
    }
71
72
    public function getVersionForm(array $context)
73
    {
74
        // Check context
75
        if (!isset($context['RecordClass'], $context['RecordID'], $context['RecordVersion'])) {
76
            throw new InvalidArgumentException('Missing RecordID / RecordVersion / RecordClass for this form');
77
        }
78
79
        $recordClass = $context['RecordClass'];
80
        $recordId = $context['RecordID'];
81
        $recordVersion = $context['RecordVersion'];
82
83
        if (!$recordClass || !$recordId || !$recordVersion) {
84
            $this->jsonError(404);
85
            return null;
86
        }
87
88
        // Load record and perform a canView check
89
        $record = Versioned::get_version($recordClass, $recordId, $recordVersion);
90
        if (!$record) {
0 ignored issues
show
introduced by
$record is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
91
            $this->jsonError(404);
92
            return null;
93
        }
94
95
        if (!$record->canView()) {
96
            $this->jsonError(403, _t(
97
                __CLASS__.'.ErrorItemViewPermissionDenied',
98
                "You don't have the necessary permissions to view {ObjectTitle}",
99
                ['ObjectTitle' => $record->i18n_singular_name()]
100
            ));
101
            return null;
102
        }
103
104
        $effectiveContext = array_merge($context, ['Record' => $record]);
105
        /** @var FormFactory $scaffolder */
106
        $scaffolder = Injector::inst()->get(DataObjectVersionFormFactory::class);
107
        $form = $scaffolder->getForm($this, 'versionForm', $effectiveContext);
108
109
        // Set form handler with class name, ID and VersionID
110
        $form->setRequestHandler(
111
            LeftAndMainFormRequestHandler::create($form, [$recordClass, $recordId, $recordVersion])
112
        );
113
114
        return $form;
115
    }
116
117
    public function versionForm(HTTPRequest $request = null)
118
    {
119
        if (!$request) {
120
            $this->jsonError(400);
121
            return null;
122
        }
123
124
        $recordClass = $request->getVar('RecordClass');
125
        $recordId = $request->getVar('RecordID');
126
        $recordVersion = $request->getVar('RecordVersion');
127
        if (!$recordClass || !$recordId || !$recordVersion) {
128
            $this->jsonError(400);
129
            return null;
130
        }
131
132
        return $this->getVersionForm([
133
            'RecordClass' => $recordClass,
134
            'RecordID' => $recordId,
135
            'RecordVersion' => $recordVersion,
136
        ]);
137
    }
138
}
139