FIX beforeUpdateCMSFields added to ErrorPage and VirtualPage

References #1756
This commit is contained in:
Franco Springveldt 2017-05-19 13:34:43 +12:00 committed by Christopher Joe
parent 3d238f7f65
commit c034ead6df
3 changed files with 109 additions and 95 deletions

View File

@ -41,6 +41,7 @@ before_script:
- composer install --prefer-dist - composer install --prefer-dist
- composer require --prefer-dist --no-update symfony/config:^3.2 silverstripe/framework:4.0.x-dev silverstripe/siteconfig:4.0.x-dev silverstripe/config:1.0.x-dev silverstripe/admin:1.0.x-dev silverstripe/assets:1.0.x-dev silverstripe/versioned:1.0.x-dev - composer require --prefer-dist --no-update symfony/config:^3.2 silverstripe/framework:4.0.x-dev silverstripe/siteconfig:4.0.x-dev silverstripe/config:1.0.x-dev silverstripe/admin:1.0.x-dev silverstripe/assets:1.0.x-dev silverstripe/versioned:1.0.x-dev
- composer update - composer update
- if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.0.x-dev --prefer-dist; fi - if [[ $DB == PGSQL ]]; then composer require silverstripe/postgresql:2.0.x-dev --prefer-dist; fi
- if [[ $DB == SQLITE ]]; then composer require silverstripe/sqlite3:2.0.x-dev --prefer-dist; fi - if [[ $DB == SQLITE ]]; then composer require silverstripe/sqlite3:2.0.x-dev --prefer-dist; fi

View File

@ -218,45 +218,19 @@ class ErrorPage extends Page
*/ */
public function getCMSFields() public function getCMSFields()
{ {
$fields = parent::getCMSFields(); $this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->addFieldToTab(
$fields->addFieldToTab( 'Root.Main',
"Root.Main", new DropdownField(
new DropdownField( 'ErrorCode',
"ErrorCode", $this->fieldLabel('ErrorCode'),
$this->fieldLabel('ErrorCode'), $this->getCodes(),
array( 'Content'
400 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_400', '400 - Bad Request'),
401 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_401', '401 - Unauthorized'),
403 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_403', '403 - Forbidden'),
404 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_404', '404 - Not Found'),
405 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_405', '405 - Method Not Allowed'),
406 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_406', '406 - Not Acceptable'),
407 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_407', '407 - Proxy Authentication Required'),
408 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_408', '408 - Request Timeout'),
409 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_409', '409 - Conflict'),
410 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_410', '410 - Gone'),
411 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_411', '411 - Length Required'),
412 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_412', '412 - Precondition Failed'),
413 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_413', '413 - Request Entity Too Large'),
414 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_414', '414 - Request-URI Too Long'),
415 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_415', '415 - Unsupported Media Type'),
416 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_416', '416 - Request Range Not Satisfiable'),
417 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_417', '417 - Expectation Failed'),
422 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_422', '422 - Unprocessable Entity'),
429 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_429', '429 - Too Many Requests'),
500 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_500', '500 - Internal Server Error'),
501 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_501', '501 - Not Implemented'),
502 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_502', '502 - Bad Gateway'),
503 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_503', '503 - Service Unavailable'),
504 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_504', '504 - Gateway Timeout'),
505 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_505', '505 - HTTP Version Not Supported'),
) )
), );
"Content" });
);
return $fields; return parent::getCMSFields();
} }
/** /**
@ -355,6 +329,37 @@ class ErrorPage extends Page
return self::get_asset_handler()->getContent($storeFilename); return self::get_asset_handler()->getContent($storeFilename);
} }
protected function getCodes()
{
return [
400 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_400', '400 - Bad Request'),
401 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_401', '401 - Unauthorized'),
403 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_403', '403 - Forbidden'),
404 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_404', '404 - Not Found'),
405 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_405', '405 - Method Not Allowed'),
406 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_406', '406 - Not Acceptable'),
407 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_407', '407 - Proxy Authentication Required'),
408 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_408', '408 - Request Timeout'),
409 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_409', '409 - Conflict'),
410 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_410', '410 - Gone'),
411 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_411', '411 - Length Required'),
412 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_412', '412 - Precondition Failed'),
413 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_413', '413 - Request Entity Too Large'),
414 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_414', '414 - Request-URI Too Long'),
415 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_415', '415 - Unsupported Media Type'),
416 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_416', '416 - Request Range Not Satisfiable'),
417 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_417', '417 - Expectation Failed'),
422 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_422', '422 - Unprocessable Entity'),
429 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_429', '429 - Too Many Requests'),
500 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_500', '500 - Internal Server Error'),
501 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_501', '501 - Not Implemented'),
502 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_502', '502 - Bad Gateway'),
503 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_503', '503 - Service Unavailable'),
504 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_504', '504 - Gateway Timeout'),
505 => _t('SilverStripe\\CMS\\Model\\ErrorPage.CODE_505', '505 - HTTP Version Not Supported'),
];
}
/** /**
* Gets the filename identifier for the given error code. * Gets the filename identifier for the given error code.
* Used when handling responses under error conditions. * Used when handling responses under error conditions.

View File

@ -3,11 +3,11 @@
namespace SilverStripe\CMS\Model; namespace SilverStripe\CMS\Model;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\ReadonlyTransformation; use SilverStripe\Forms\ReadonlyTransformation;
use SilverStripe\Forms\TreeDropdownField; use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Hierarchy\MarkedSet;
use SilverStripe\ORM\ValidationResult; use SilverStripe\ORM\ValidationResult;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
@ -15,7 +15,9 @@ use Page;
/** /**
* Virtual Page creates an instance of a page, with the same fields that the original page had, but readonly. * Virtual Page creates an instance of a page, with the same fields that the original page had, but readonly.
* This allows you can have a page in mulitple places in the site structure, with different children without duplicating the content * This allows you can have a page in mulitple places in the site structure, with different children without
* duplicating the content.
*
* Note: This Only duplicates $db fields and not the $has_one etc.. * Note: This Only duplicates $db fields and not the $has_one etc..
* *
* @method SiteTree CopyContentFrom() * @method SiteTree CopyContentFrom()
@ -157,7 +159,8 @@ class VirtualPage extends Page
public function syncLinkTracking() public function syncLinkTracking()
{ {
if ($this->CopyContentFromID) { if ($this->CopyContentFromID) {
$this->HasBrokenLink = !(bool) DataObject::get_by_id('SilverStripe\\CMS\\Model\\SiteTree', $this->CopyContentFromID); $copyPage = DataObject::get_by_id('SilverStripe\\CMS\\Model\\SiteTree', $this->CopyContentFromID);
$this->HasBrokenLink = !$copyPage;
} else { } else {
$this->HasBrokenLink = true; $this->HasBrokenLink = true;
} }
@ -188,7 +191,11 @@ class VirtualPage extends Page
} }
// Unpublished source // Unpublished source
if (!Versioned::get_versionnumber_by_stage('SilverStripe\\CMS\\Model\\SiteTree', 'Live', $this->CopyContentFromID)) { if (!Versioned::get_versionnumber_by_stage(
'SilverStripe\\CMS\\Model\\SiteTree',
'Live',
$this->CopyContentFromID
)) {
return false; return false;
} }
@ -201,70 +208,71 @@ class VirtualPage extends Page
*/ */
public function getCMSFields() public function getCMSFields()
{ {
$fields = parent::getCMSFields(); $this->beforeUpdateCMSFields(function (FieldList $fields) {
// Setup the linking to the original page.
$copyContentFromField = TreeDropdownField::create(
'CopyContentFromID',
_t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', "Linked Page"),
"SilverStripe\\CMS\\Model\\SiteTree"
);
// Setup the linking to the original page. // Setup virtual fields
$copyContentFromField = new TreeDropdownField( if ($virtualFields = $this->getVirtualFields()) {
"CopyContentFromID", $roTransformation = new ReadonlyTransformation();
_t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', "Linked Page"), foreach ($virtualFields as $virtualField) {
"SilverStripe\\CMS\\Model\\SiteTree" if ($fields->dataFieldByName($virtualField)) {
); $fields->replaceField(
// filter doesn't let you select children of virtual pages as as source page $virtualField,
//$copyContentFromField->setFilterFunction(function ($item) { return !($item instanceof VirtualPage); }); $fields->dataFieldByName($virtualField)->transform($roTransformation)
);
// Setup virtual fields }
if ($virtualFields = $this->getVirtualFields()) {
$roTransformation = new ReadonlyTransformation();
foreach ($virtualFields as $virtualField) {
if ($fields->dataFieldByName($virtualField)) {
$fields->replaceField($virtualField, $fields->dataFieldByName($virtualField)->transform($roTransformation));
} }
} }
}
$msgs = array(); $msgs = array();
$fields->addFieldToTab("Root.Main", $copyContentFromField, "Title"); $fields->addFieldToTab('Root.Main', $copyContentFromField, 'Title');
// Create links back to the original object in the CMS // Create links back to the original object in the CMS
if ($this->CopyContentFrom()->exists()) { if ($this->CopyContentFrom()->exists()) {
$link = "<a class=\"cmsEditlink\" href=\"admin/pages/edit/show/$this->CopyContentFromID\">" $link = "<a class=\"cmsEditlink\" href=\"admin/pages/edit/show/$this->CopyContentFromID\">" . _t(
. _t('SilverStripe\\CMS\\Model\\VirtualPage.EditLink', 'edit') 'SilverStripe\\CMS\\Model\\VirtualPage.EditLink',
. "</a>"; 'edit'
$msgs[] = _t( ) . "</a>";
'SilverStripe\\CMS\\Model\\VirtualPage.HEADERWITHLINK', $msgs[] = _t(
"This is a virtual page copying content from \"{title}\" ({link})", 'SilverStripe\\CMS\\Model\\VirtualPage.HEADERWITHLINK',
array( "This is a virtual page copying content from \"{title}\" ({link})",
'title' => $this->CopyContentFrom()->obj('Title'), array(
'link' => $link 'title' => $this->CopyContentFrom()->obj('Title'),
) 'link' => $link,
); )
} else { );
$msgs[] = _t('SilverStripe\\CMS\\Model\\VirtualPage.HEADER', "This is a virtual page"); } else {
$msgs[] = _t( $msgs[] = _t('SilverStripe\\CMS\\Model\\VirtualPage.HEADER', "This is a virtual page");
'SilverStripe\\CMS\\Model\\SiteTree.VIRTUALPAGEWARNING', $msgs[] = _t(
'Please choose a linked page and save first in order to publish this page' 'SilverStripe\\CMS\\Model\\SiteTree.VIRTUALPAGEWARNING',
); 'Please choose a linked page and save first in order to publish this page'
} );
if ($this->CopyContentFromID }
&& !Versioned::get_versionnumber_by_stage(SiteTree::class, Versioned::LIVE, $this->CopyContentFromID) if ($this->CopyContentFromID && !Versioned::get_versionnumber_by_stage(
) { SiteTree::class,
$msgs[] = _t( Versioned::LIVE,
'SilverStripe\\CMS\\Model\\SiteTree.VIRTUALPAGEDRAFTWARNING', $this->CopyContentFromID
'Please publish the linked page in order to publish the virtual page' )
); ) {
} $msgs[] = _t(
'SilverStripe\\CMS\\Model\\SiteTree.VIRTUALPAGEDRAFTWARNING',
'Please publish the linked page in order to publish the virtual page'
);
}
$fields->addFieldToTab( $fields->addFieldToTab("Root.Main", new LiteralField(
"Root.Main",
new LiteralField(
'VirtualPageMessage', 'VirtualPageMessage',
'<div class="message notice">' . implode('. ', $msgs) . '.</div>' '<div class="message notice">' . implode('. ', $msgs) . '.</div>'
), ), 'CopyContentFromID');
'CopyContentFromID' });
);
return $fields; return parent::getCMSFields();
} }
public function onBeforeWrite() public function onBeforeWrite()