diff --git a/README.md b/README.md index 310d9f4..5c8ea7a 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,21 @@ composer require gorriecoe/silverstripe-dataobjecthistory ## Requirements -- silverstripe/framework ^4.0 -- symbiote/silverstripe-gridfieldextensions ^3.1 +This module supports Silverstripe CMS 4 and CMS 5. + +### CMS 5 + +Requires the following module versions: + +- `silverstripe/framework` ^5.0 +- `symbiote/silverstripe-gridfieldextensions` ^4.0 + +### CMS 4 + +Requires the following module versions: + +- `silverstripe/framework` ^4.0 +- `symbiote/silverstripe-gridfieldextensions` ^3.1 ## Maintainers diff --git a/composer.json b/composer.json index e5a6172..0f58a3e 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,8 @@ ], "homepage": "http://github.com/gorriecoe/silverstripe-dataobjecthistory", "require": { - "silverstripe/cms": "^4.0", - "symbiote/silverstripe-gridfieldextensions": "^3.1" + "silverstripe/cms": "^4.0 || ^5.0", + "symbiote/silverstripe-gridfieldextensions": "^3.1 || ^4.0" }, "extra": { "installer-name": "dataobjecthistory" diff --git a/src/extensions/DataObjectHistory.php b/src/extensions/DataObjectHistory.php index 1fe715e..619e251 100644 --- a/src/extensions/DataObjectHistory.php +++ b/src/extensions/DataObjectHistory.php @@ -27,10 +27,9 @@ class DataObjectHistory extends DataExtension { /** - * Update Fields - * @return FieldList + * @inheritDoc */ - public function updateCMSFields(FieldList $fields) + public function updateCMSFields(FieldList $fields): FieldList { $owner = $this->owner; if ($owner->HistoryFields) { @@ -39,15 +38,16 @@ public function updateCMSFields(FieldList $fields) $owner->HistoryFields ); } + return $fields; } - /** - * Returns the history fields for this dataobject. - * @return FieldList + * Returns the history fields for this DataObject. + * + * @return FieldList|null */ - public function getHistoryFields() + public function getHistoryFields(): ?FieldList { $owner = $this->owner; if (!$owner->isLatestVersion()) { @@ -81,23 +81,22 @@ public function getHistoryFields() Versioned::get_all_versions( $owner->ClassName, $owner->ID - ) - ->sort('Version', 'DESC'), + )->sort('Version', 'DESC'), $config - ) - ->addExtraClass('grid-field--history') + )->addExtraClass('grid-field--history') ); } /** - * @return Member + * @return Member|null */ - public function getAuthor() + public function getAuthor(): ?Member { $owner = $this->owner; if ($owner->AuthorID) { return Member::get()->byId($owner->AuthorID); } + return null; } } diff --git a/src/forms/GridFieldHistoryButton.php b/src/forms/GridFieldHistoryButton.php index 9c261e3..fc6cf41 100644 --- a/src/forms/GridFieldHistoryButton.php +++ b/src/forms/GridFieldHistoryButton.php @@ -7,6 +7,7 @@ use SilverStripe\View\SSViewer; use SilverStripe\Forms\GridField\GridFieldViewButton; use SilverStripe\ORM\DataObject; +use SilverStripe\ORM\FieldType\DBHTMLText; /** * DataObjectHistory @@ -15,19 +16,24 @@ */ class GridFieldHistoryButton extends GridFieldViewButton { + /* + * @inheritDoc + */ public function getColumnContent($field, $record, $col) { if ($record->isLatestVersion()) { return null; } - $data = new ArrayData(array( - 'Link' => Controller::join_links( - $field->Link('item'), - $record->ID, - 'view?VersionID='. $record->Version - ) - )); + $data = new ArrayData( + [ + 'Link' => Controller::join_links( + $field->Link('item'), + $record->ID, + 'view?VersionID=' . $record->Version + ) + ] + ); $template = SSViewer::get_templates_by_class( $this, diff --git a/src/forms/HistoryGridFieldItemRequest.php b/src/forms/HistoryGridFieldItemRequest.php index 34f0fb1..53dcfff 100644 --- a/src/forms/HistoryGridFieldItemRequest.php +++ b/src/forms/HistoryGridFieldItemRequest.php @@ -1,4 +1,5 @@ record->canView()) { - $this->httpError(403); + return $this->httpError(403); } + $controller = $this->getToplevelController(); $form = $this->ItemEditForm(); + $data = ArrayData::create([ 'Backlink' => $controller->Link(), 'ItemEditForm' => $form ]); + $return = $data->renderWith($this->getTemplates()); if ($request->isAjax()) { - return $return; + return HTTPResponse::create($return); } + return $controller->customise(['Content' => $return]); } - public function ItemEditForm() + public function ItemEditForm(): Form { $form = parent::ItemEditForm(); $fields = $form->Fields(); @@ -103,11 +116,13 @@ public function ItemEditForm() DBField::create_field('HTMLFragment', $message), 'notice' ); + $form->setFields($fields); + return $form; } - public function doRollback($data, $form) + public function doRollback(array $data, Form $form): HTTPResponse { $record = $this->record; @@ -118,18 +133,18 @@ public function doRollback($data, $form) // Save from form data $record->doRollbackTo($record->Version); - $link = '"' + $link = '' . htmlspecialchars($record->Title, ENT_QUOTES) - . '"'; + . ''; $message = _t( __CLASS__ . '.RolledBack', 'Rolled back {name} to version {version} {link}', - array( + [ 'name' => $record->i18n_singular_name(), 'version' => $record->Version, 'link' => $link - ) + ] ); $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML); @@ -138,9 +153,11 @@ public function doRollback($data, $form) return $controller->redirect($record->CMSEditLink()); } - public function getFormActions() + public function getFormActions(): FieldList { + $actions = parent::getFormActions(); $record = $this->getRecord(); + if (!$record || !$record->has_extension(Versioned::class)) { return $actions; } @@ -148,7 +165,6 @@ public function getFormActions() $this->beforeExtending('updateFormActions', function (FieldList $actions) use ($record) { if (!$record->isLatestVersion()) { $actions->removeByName([ - 'action_doUnpublish', 'action_doUnpublish', 'action_doDelete', 'action_doSave', @@ -172,7 +188,6 @@ public function getFormActions() } }); - $actions = parent::getFormActions(); return $actions; } }