silverstripe-contentreview/code/extensions/ContentReviewCMSExtension.php

85 lines
2.2 KiB
PHP
Raw Normal View History

<?php
/**
* CMSPageEditController extension to recieve the additonal action button from
* SiteTreeContentReview::updateCMSActions()
*
*/
class ContentReviewCMSExtension extends LeftAndMainExtension {
/**
*
* @var array
*/
private static $allowed_actions = array(
'reviewed',
'save_review',
);
/**
* Shows a form with review notes
*
* @param array $data
* @param Form $form
* @return SS_HTTPResponse
*/
public function reviewed($data, Form $form) {
if(!isset($data['ID'])) {
throw new SS_HTTPResponse_Exception("No record ID", 404);
}
$SQL_id = Convert::raw2sql($data['ID']);
$record = SiteTree::get()->byID($SQL_id);
2014-02-18 21:51:54 +01:00
if(!$record || !$record->ID) {
throw new SS_HTTPResponse_Exception("Bad record ID #$SQL_id", 404);
}
2014-02-18 21:51:54 +01:00
if(!$record->canEdit()) {
return Security::permissionFailure($this->owner);
}
$fields = new FieldList();
$fields->push(HiddenField::create('ID', 'ID', $SQL_id));
$fields->push(TextareaField::create('ReviewNotes', 'Review notes'));
$actions = new FieldList(
FormAction::create('save_review', 'Save')
);
$form = CMSForm::create($this->owner, "EditForm", $fields, $actions)->setHTMLID('Form_EditForm');
$form->setResponseNegotiator($this->owner->getResponseNegotiator());
$form->loadDataFrom($record);
$form->disableDefaultAction();
// TODO Can't merge $FormAttributes in template at the moment
$form->setTemplate($this->owner->getTemplatesWithSuffix('LeftAndMain_EditForm'));
return $form->forTemplate();
}
/**
* Save the review notes and redirect back to the page edit form
*
* @param array $data
* @param Form $form
* @return string - html
*/
public function save_review($data, Form $form) {
if(!isset($data['ID'])) {
throw new SS_HTTPResponse_Exception("No record ID", 404);
}
$SQL_id = Convert::raw2sql($data['ID']);
2014-02-18 03:39:13 +01:00
$page = SiteTree::get()->byID($SQL_id);
if($page && !$page->canEdit()) {
2015-09-23 00:32:23 +02:00
return Security::permissionFailure();
}
2014-02-18 03:39:13 +01:00
if(!$page || !$page->ID) {
throw new SS_HTTPResponse_Exception("Bad record ID #$SQL_id", 404);
}
2014-02-18 03:39:13 +01:00
$page->addReviewNote(Member::currentUser(), $data['ReviewNotes']);
$page->advanceReviewDate();
return $this->owner->redirect($this->owner->Link('show/'.$page->ID));
}
}