-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathContentReviewCMSExtension.php
168 lines (150 loc) · 5.22 KB
/
ContentReviewCMSExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace SilverStripe\ContentReview\Extensions;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Admin\LeftAndMainExtension;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ContentReview\Forms\ReviewContentHandler;
use SilverStripe\ContentReview\Traits\PermissionChecker;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Forms\Form;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Security;
/**
* CMSPageEditController extension to receive the additional action button from
* SiteTreeContentReview::updateCMSActions()
*
* @extends LeftAndMainExtension<CMSMain>
*/
class ContentReviewCMSExtension extends LeftAndMainExtension
{
use PermissionChecker;
private static $allowed_actions = [
'ReviewContentForm',
'savereview',
];
/**
* URL handler for the "content due for review" form
*
* @param HTTPRequest $request
* @return Form|null
*/
public function ReviewContentForm(HTTPRequest $request)
{
// Get ID either from posted back value, or url parameter
$id = $request->param('ID') ?: $request->postVar('ID');
return $this->getReviewContentForm($id);
}
/**
* Return a handler for "content due for review" forms, according to the given object ID
*
* @param int $id
* @return Form|null
*/
public function getReviewContentForm($id)
{
$page = $this->findRecord(['ID' => $id]);
$user = Security::getCurrentUser();
if (!$this->isContentReviewable($page, $user)) {
$this->owner->httpError(403, _t(
__CLASS__.'.ErrorItemPermissionDenied',
'It seems you don\'t have the necessary permissions to review this content'
));
return null;
}
$form = $this->getReviewContentHandler()->Form($page);
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $id) {
$schemaId = $this->owner->join_links($this->owner->Link('schema/ReviewContentForm'), $id);
return $this->getSchemaResponse($schemaId, $form, $errors);
});
return $form;
}
/**
* Action handler for processing the submitted content review
*
* @param array $data
* @param Form $form
* @return DBHTMLText|HTTPResponse|null
*/
public function savereview($data, Form $form)
{
$page = $this->findRecord($data);
$results = $this->getReviewContentHandler()->submitReview($page, $data);
if (is_null($results)) {
return null;
}
if ($this->getSchemaRequested()) {
// Send extra "message" data with schema response
$extraData = ['message' => $results];
$schemaId = $this->owner->join_links($this->owner->Link('schema/ReviewContentForm'), $page->ID);
return $this->getSchemaResponse($schemaId, $form, null, $extraData);
}
return $results;
}
/**
* Return a handler or reviewing content
*
* @return ReviewContentHandler
*/
protected function getReviewContentHandler()
{
return ReviewContentHandler::create($this->owner);
}
/**
* Find the page this form is updating
*
* @param array $data Form data
* @return SiteTree Record
* @throws HTTPResponse_Exception
*/
protected function findRecord($data)
{
if (empty($data["ID"])) {
throw new HTTPResponse_Exception("No record ID", 404);
}
$page = null;
$id = $data["ID"];
if (is_numeric($id)) {
$page = SiteTree::get()->byID($id);
}
if (!$page || !$page->ID) {
throw new HTTPResponse_Exception("Bad record ID #{$id}", 404);
}
return $page;
}
/**
* Check if the current request has a X-Formschema-Request header set.
* Used by conditional logic that responds to validation results
*
* @return bool
*/
protected function getSchemaRequested()
{
$parts = $this->owner->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER);
return !empty($parts);
}
/**
* Generate schema for the given form based on the X-Formschema-Request header value
*
* @param string $schemaID ID for this schema. Required.
* @param Form $form Required for 'state' or 'schema' response
* @param ValidationResult $errors Required for 'error' response
* @param array $extraData Any extra data to be merged with the schema response
* @return HTTPResponse
*/
protected function getSchemaResponse($schemaID, $form = null, ValidationResult $errors = null, $extraData = [])
{
$parts = $this->owner->getRequest()->getHeader(LeftAndMain::SCHEMA_HEADER);
$data = $this->owner
->getFormSchema()
->getMultipartSchema($parts, $schemaID, $form, $errors);
if ($extraData) {
$data = array_merge($data, $extraData);
}
$response = HTTPResponse::create(json_encode($data));
$response->addHeader('Content-Type', 'application/json');
return $response;
}
}