From 1c312d0ceb8d8831a18f41bed90534cb8c97ae8c Mon Sep 17 00:00:00 2001 From: Pack Date: Mon, 22 Jul 2024 17:42:21 +1200 Subject: [PATCH 1/4] CMS-5: support cms 5 --- README.md | 17 +++++++-- composer.json | 4 +-- src/extensions/DataObjectHistory.php | 25 +++++++------- src/forms/GridFieldHistoryButton.php | 19 +++++----- src/forms/HistoryGridFieldItemRequest.php | 42 +++++++++++++++-------- 5 files changed, 67 insertions(+), 40 deletions(-) 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..953cfcf 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,21 @@ */ class GridFieldHistoryButton extends GridFieldViewButton { - public function getColumnContent($field, $record, $col) + public function getColumnContent($field, DataObject $record, string $col): ?DBHTMLText { 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..2e2c6ec 100644 --- a/src/forms/HistoryGridFieldItemRequest.php +++ b/src/forms/HistoryGridFieldItemRequest.php @@ -1,4 +1,5 @@ versionID = $requestHandler->getRequest()->requestVar('VersionID')) { if (!$record = Versioned::get_version(get_class($record), $record->ID, $this->versionID)) { @@ -45,28 +50,33 @@ public function __construct($gridField, $component, $record, $requestHandler, $p ); } } + parent::__construct($gridField, $component, $record, $requestHandler, $popupFormName); } - public function view($request) + public function view(HTTPRequest $request): HTTPResponse { if (!$this->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 +113,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 +130,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 +150,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 +162,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 +185,6 @@ public function getFormActions() } }); - $actions = parent::getFormActions(); return $actions; } } From c363a4e57308ac602b5cf661ce15468dbdf33d08 Mon Sep 17 00:00:00 2001 From: Pack Date: Mon, 22 Jul 2024 18:00:51 +1200 Subject: [PATCH 2/4] CMS-5: enhance code --- src/forms/HistoryGridFieldItemRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/forms/HistoryGridFieldItemRequest.php b/src/forms/HistoryGridFieldItemRequest.php index 2e2c6ec..b9de629 100644 --- a/src/forms/HistoryGridFieldItemRequest.php +++ b/src/forms/HistoryGridFieldItemRequest.php @@ -40,7 +40,7 @@ class HistoryGridFieldItemRequest extends VersionedGridFieldItemRequest */ protected ?int $versionID = null; - public function __construct(GridField $gridField, $component, DataObject $record, $requestHandler, $popupFormName) + public function __construct($gridField, $component, $record, $requestHandler, $popupFormName) { if ($this->versionID = $requestHandler->getRequest()->requestVar('VersionID')) { if (!$record = Versioned::get_version(get_class($record), $record->ID, $this->versionID)) { From fd33027ebba193b2a474d252094728edaef8d3e5 Mon Sep 17 00:00:00 2001 From: Pack Date: Thu, 25 Jul 2024 12:58:21 +1200 Subject: [PATCH 3/4] adjust functions to be inherit function --- src/forms/GridFieldHistoryButton.php | 21 ++++++++++++++++++++- src/forms/HistoryGridFieldItemRequest.php | 5 ++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/forms/GridFieldHistoryButton.php b/src/forms/GridFieldHistoryButton.php index 953cfcf..0e34c43 100644 --- a/src/forms/GridFieldHistoryButton.php +++ b/src/forms/GridFieldHistoryButton.php @@ -16,7 +16,10 @@ */ class GridFieldHistoryButton extends GridFieldViewButton { - public function getColumnContent($field, DataObject $record, string $col): ?DBHTMLText + /* + * @inheritDoc + */ + public function getColumnContent($field, $record, $col) { if ($record->isLatestVersion()) { return null; @@ -38,6 +41,22 @@ public function getColumnContent($field, DataObject $record, string $col): ?DBHT GridFieldViewButton::class ); + return $data->renderWith($template); $data = new ArrayData( + [ + 'Link' => Controller::join_links( + $field->Link('item'), + $record->ID, + 'view?VersionID=' . $record->Version + ) + ] + ); + + $template = SSViewer::get_templates_by_class( + $this, + '', + GridFieldViewButton::class + ); + return $data->renderWith($template); } } diff --git a/src/forms/HistoryGridFieldItemRequest.php b/src/forms/HistoryGridFieldItemRequest.php index b9de629..53dcfff 100644 --- a/src/forms/HistoryGridFieldItemRequest.php +++ b/src/forms/HistoryGridFieldItemRequest.php @@ -54,7 +54,10 @@ public function __construct($gridField, $component, $record, $requestHandler, $p parent::__construct($gridField, $component, $record, $requestHandler, $popupFormName); } - public function view(HTTPRequest $request): HTTPResponse + /* + * @inheritDoc + */ + public function view($request) { if (!$this->record->canView()) { return $this->httpError(403); From a0cfa852f55733b07307df7a954f4d10f28a5d2e Mon Sep 17 00:00:00 2001 From: Pack Date: Mon, 29 Jul 2024 11:07:38 +1200 Subject: [PATCH 4/4] remove duplicate code GridFieldHistoryButton --- src/forms/GridFieldHistoryButton.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/forms/GridFieldHistoryButton.php b/src/forms/GridFieldHistoryButton.php index 0e34c43..fc6cf41 100644 --- a/src/forms/GridFieldHistoryButton.php +++ b/src/forms/GridFieldHistoryButton.php @@ -41,22 +41,6 @@ public function getColumnContent($field, $record, $col) GridFieldViewButton::class ); - return $data->renderWith($template); $data = new ArrayData( - [ - 'Link' => Controller::join_links( - $field->Link('item'), - $record->ID, - 'view?VersionID=' . $record->Version - ) - ] - ); - - $template = SSViewer::get_templates_by_class( - $this, - '', - GridFieldViewButton::class - ); - return $data->renderWith($template); } }