2014-02-14 02:49:43 +01:00
|
|
|
<?php
|
|
|
|
|
2017-09-06 05:49:23 +02:00
|
|
|
namespace SilverStripe\ContentReview\Extensions;
|
|
|
|
|
2017-09-11 02:01:41 +02:00
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Admin\LeftAndMainExtension;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-09-11 02:01:41 +02:00
|
|
|
use SilverStripe\ContentReview\Forms\ReviewContentHandler;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-09-11 07:53:59 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2017-09-11 02:01:41 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Forms\Form;
|
2017-09-11 02:01:41 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2017-09-06 05:49:23 +02:00
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
|
2014-02-14 02:49:43 +01:00
|
|
|
/**
|
2015-11-02 00:27:42 +01:00
|
|
|
* CMSPageEditController extension to receive the additional action button from
|
2014-02-14 02:49:43 +01:00
|
|
|
* SiteTreeContentReview::updateCMSActions()
|
|
|
|
*/
|
2015-11-02 00:27:42 +01:00
|
|
|
class ContentReviewCMSExtension extends LeftAndMainExtension
|
|
|
|
{
|
2017-09-11 02:01:41 +02:00
|
|
|
private static $allowed_actions = [
|
|
|
|
'ReviewContentForm',
|
2017-09-11 07:53:59 +02:00
|
|
|
'savereview',
|
2017-09-11 02:01:41 +02:00
|
|
|
];
|
|
|
|
|
2015-11-02 00:27:42 +01:00
|
|
|
/**
|
2017-09-11 02:01:41 +02:00
|
|
|
* URL handler for the "content due for review" form
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return Form|null
|
2015-11-02 00:27:42 +01:00
|
|
|
*/
|
2017-09-11 02:01:41 +02:00
|
|
|
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);
|
|
|
|
}
|
2015-11-02 00:27:42 +01:00
|
|
|
|
|
|
|
/**
|
2017-09-11 02:01:41 +02:00
|
|
|
* Return a handler for "content due for review" forms, according to the given object ID
|
2015-11-02 00:27:42 +01:00
|
|
|
*
|
2017-09-11 02:01:41 +02:00
|
|
|
* @param int $id
|
|
|
|
* @return Form|null
|
2015-11-02 00:27:42 +01:00
|
|
|
*/
|
2017-09-11 02:01:41 +02:00
|
|
|
public function getReviewContentForm($id)
|
2015-11-02 00:27:42 +01:00
|
|
|
{
|
2017-09-11 07:53:59 +02:00
|
|
|
$page = $this->findRecord(['ID' => $id]);
|
2017-09-11 02:01:41 +02:00
|
|
|
$user = Security::getCurrentUser();
|
2017-09-11 07:53:59 +02:00
|
|
|
if (!$page->canEdit() || ($page->hasMethod('canBeReviewedBy') && !$page->canBeReviewedBy($user))) {
|
2017-09-11 02:01:41 +02:00
|
|
|
$this->owner->httpError(403, _t(
|
|
|
|
__CLASS__.'.ErrorItemPermissionDenied',
|
|
|
|
'It seems you don\'t have the necessary permissions to review this content'
|
|
|
|
));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-09-11 07:53:59 +02:00
|
|
|
$form = $this->getReviewContentHandler()->Form($page);
|
2017-09-11 02:01:41 +02:00
|
|
|
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($form, $id) {
|
|
|
|
$schemaId = $this->owner->join_links($this->owner->Link('schema/ReviewContentForm'), $id);
|
2017-09-11 07:53:59 +02:00
|
|
|
return $this->getSchemaResponse($schemaId, $form, $errors);
|
2017-09-11 02:01:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $form;
|
2015-11-03 04:57:07 +01:00
|
|
|
}
|
2017-09-06 05:49:23 +02:00
|
|
|
|
2015-11-03 04:57:07 +01:00
|
|
|
/**
|
2017-09-11 02:01:41 +02:00
|
|
|
* Action handler for processing the submitted content review
|
2015-11-03 04:57:07 +01:00
|
|
|
*
|
2017-09-11 02:01:41 +02:00
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
2017-09-11 07:53:59 +02:00
|
|
|
* @return DBHTMLText|HTTPResponse|null
|
2015-11-03 04:57:07 +01:00
|
|
|
*/
|
2017-09-11 07:53:59 +02:00
|
|
|
public function savereview($data, Form $form)
|
2015-11-03 04:57:07 +01:00
|
|
|
{
|
2017-09-11 07:53:59 +02:00
|
|
|
$page = $this->findRecord($data);
|
2017-09-11 02:01:41 +02:00
|
|
|
|
2017-09-11 07:53:59 +02:00
|
|
|
$results = $this->getReviewContentHandler()->submitReview($page, $data);
|
2017-09-11 02:01:41 +02:00
|
|
|
if (is_null($results)) {
|
|
|
|
return null;
|
2015-11-03 04:57:07 +01:00
|
|
|
}
|
2017-09-11 02:01:41 +02:00
|
|
|
if ($this->getSchemaRequested()) {
|
|
|
|
// Send extra "message" data with schema response
|
|
|
|
$extraData = ['message' => $results];
|
2017-09-11 07:53:59 +02:00
|
|
|
$schemaId = $this->owner->join_links($this->owner->Link('schema/ReviewContentForm'), $page->ID);
|
2017-09-11 02:01:41 +02:00
|
|
|
return $this->getSchemaResponse($schemaId, $form, null, $extraData);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
2017-09-11 02:01:41 +02:00
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2017-09-11 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2017-09-11 02:01:41 +02:00
|
|
|
/**
|
|
|
|
* Check if the current request has a X-Formschema-Request header set.
|
|
|
|
* Used by conditional logic that responds to validation results
|
|
|
|
*
|
|
|
|
* @todo Remove duplication. See https://github.com/silverstripe/silverstripe-admin/issues/240
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*
|
|
|
|
* @todo Remove duplication. See https://github.com/silverstripe/silverstripe-admin/issues/240
|
|
|
|
*
|
|
|
|
* @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);
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
|
|
|
|
2018-10-28 22:32:15 +01:00
|
|
|
$response = HTTPResponse::create(json_encode($data));
|
2017-09-11 02:01:41 +02:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
return $response;
|
2015-11-02 00:27:42 +01:00
|
|
|
}
|
2014-02-14 02:49:43 +01:00
|
|
|
}
|