silverstripe-contentreview/code/extensions/ContentReviewCMSExtension.php

116 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
2015-11-02 00:27:42 +01:00
* CMSPageEditController extension to receive the additional action button from
* SiteTreeContentReview::updateCMSActions()
*/
2015-11-02 00:27:42 +01:00
class ContentReviewCMSExtension extends LeftAndMainExtension
{
2015-11-03 04:57:07 +01:00
2015-11-02 00:27:42 +01:00
/**
* @var array
*/
private static $allowed_actions = array(
"reviewed",
2015-11-03 04:57:07 +01:00
"AddReviewForm",
2015-11-02 00:27:42 +01:00
);
/**
* Shows a form with review notes.
*
* @param array $data
* @param Form $form
*
* @return SS_HTTPResponse
* @throws SS_HTTPResponse_Exception
*/
public function reviewed($data, Form $form)
{
2015-11-03 04:57:07 +01:00
$record = $this->findRecord($data);
2015-11-02 00:27:42 +01:00
if (!$record->canEdit()) {
return Security::permissionFailure($this->owner);
}
2015-11-03 04:57:07 +01:00
// Populate and respond
$form = $this->AddReviewForm();
$form->loadDataFrom($record);
return $form->forTemplate();
}
/**
* Form handler for this page
*
* @return CMSForm
*/
public function AddReviewForm()
{
$reviewNotes = TextareaField::create("ReviewNotes", _t("ContentReview.REVIEWNOTES", "Review notes"));
2015-11-03 04:57:07 +01:00
$reviewNotes->setDescription(_t("ContentReview.REVIEWNOTESDESCRIPTION ", "Add comments for the content of this page."));
$fields = new FieldList();
$fields->push(HiddenField::create("ID"));
$fields->push($reviewNotes);
2015-11-02 00:27:42 +01:00
$actions = new FieldList(
FormAction::create("save_review", _t("ContentReview.SAVE", "Save"))
2015-11-02 00:27:42 +01:00
);
2015-11-03 04:57:07 +01:00
$form = CMSForm::create($this->owner, "AddReviewForm", $fields, $actions)->setHTMLID("Form_EditForm");
2015-11-02 00:27:42 +01:00
$form->setResponseNegotiator($this->owner->getResponseNegotiator());
$form->disableDefaultAction();
// TODO Can't merge $FormAttributes in template at the moment
$form->setTemplate($this->owner->getTemplatesWithSuffix("LeftAndMain_EditForm"));
2015-11-03 04:57:07 +01:00
return $form;
2015-11-02 00:27:42 +01:00
}
/**
* Save the review notes and redirect back to the page edit form.
*
* @param array $data
* @param Form $form
*
* @return string
*
* @throws SS_HTTPResponse_Exception
*/
public function save_review($data, Form $form)
{
2015-11-03 04:57:07 +01:00
$page = $this->findRecord($data);
if (!$page->canEdit()) {
return Security::permissionFailure($this->owner);
2015-11-02 00:27:42 +01:00
}
2015-11-03 04:57:07 +01:00
$page->addReviewNote(Member::currentUser(), $data["ReviewNotes"]);
$page->advanceReviewDate();
return $this->owner->redirect($this->owner->Link("show/" . $page->ID));
}
/**
* Find the page this form is updating
*
* @param array $data Form data
* @return SiteTree Record
* @throws SS_HTTPResponse_Exception
*/
protected function findRecord($data)
{
if (empty($data["ID"])) {
throw new SS_HTTPResponse_Exception("No record ID", 404);
}
2015-11-02 00:27:42 +01:00
2015-11-03 04:57:07 +01:00
$page = null;
$id = $data["ID"];
if (is_numeric($id)) {
$page = SiteTree::get()->byID($id);
2015-11-02 00:27:42 +01:00
}
if (!$page || !$page->ID) {
throw new SS_HTTPResponse_Exception("Bad record ID #{$id}", 404);
}
2015-11-03 04:57:07 +01:00
return $page;
2015-11-02 00:27:42 +01:00
}
}