Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMS-5: support cms 5 #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 12 additions & 13 deletions src/extensions/DataObjectHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()) {
Expand Down Expand Up @@ -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;
}
}
20 changes: 13 additions & 7 deletions src/forms/GridFieldHistoryButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\View\SSViewer;
use SilverStripe\Forms\GridField\GridFieldViewButton;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;

/**
* DataObjectHistory
Expand All @@ -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,
Expand Down
41 changes: 28 additions & 13 deletions src/forms/HistoryGridFieldItemRequest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace gorriecoe\DataObjectHistory\Forms;

use SilverStripe\Forms\FieldList;
Expand All @@ -13,6 +14,10 @@
use SilverStripe\Versioned\VersionedGridFieldItemRequest;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\Form;
use SilverStripe\ORM\DataObject;

/**
* DataObjectHistory
Expand All @@ -31,9 +36,9 @@ class HistoryGridFieldItemRequest extends VersionedGridFieldItemRequest
];

/**
* @var int
* @var int|null
*/
protected $versionID;
protected ?int $versionID = null;

public function __construct($gridField, $component, $record, $requestHandler, $popupFormName)
{
Expand All @@ -45,28 +50,36 @@ public function __construct($gridField, $component, $record, $requestHandler, $p
);
}
}

parent::__construct($gridField, $component, $record, $requestHandler, $popupFormName);
}

/*
* @inheritDoc
*/
public function view($request)
{
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();
Expand Down Expand Up @@ -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;

Expand All @@ -118,18 +133,18 @@ public function doRollback($data, $form)

// Save from form data
$record->doRollbackTo($record->Version);
$link = '<a href="' . $this->Link('edit') . '">"'
$link = '<a href="' . $this->Link('edit') . '">'
. htmlspecialchars($record->Title, ENT_QUOTES)
. '"</a>';
. '</a>';

$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);
Expand All @@ -138,17 +153,18 @@ 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;
}

$this->beforeExtending('updateFormActions', function (FieldList $actions) use ($record) {
if (!$record->isLatestVersion()) {
$actions->removeByName([
'action_doUnpublish',
'action_doUnpublish',
'action_doDelete',
'action_doSave',
Expand All @@ -172,7 +188,6 @@ public function getFormActions()
}
});

$actions = parent::getFormActions();
return $actions;
}
}