Merge pull request #1827 from fspringveldt/new/issue-1756

FIX beforeUpdateCMSFields added to ErrorPage and VirtualPage
This commit is contained in:
Chris Joe 2017-05-26 15:29:29 +12:00 committed by GitHub
commit af1b2fc37f
3 changed files with 109 additions and 95 deletions

View File

@ -41,6 +41,7 @@ before_script:
- 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 update
- 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

View File

@ -218,45 +218,19 @@ class ErrorPage extends Page
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab(
"Root.Main",
new DropdownField(
"ErrorCode",
$this->fieldLabel('ErrorCode'),
array(
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'),
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->addFieldToTab(
'Root.Main',
new DropdownField(
'ErrorCode',
$this->fieldLabel('ErrorCode'),
$this->getCodes(),
'Content'
)
),
"Content"
);
);
});
return $fields;
return parent::getCMSFields();
}
/**
@ -355,6 +329,37 @@ class ErrorPage extends Page
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.
* Used when handling responses under error conditions.

View File

@ -3,11 +3,11 @@
namespace SilverStripe\CMS\Model;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\ReadonlyTransformation;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Hierarchy\MarkedSet;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Versioned\Versioned;
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.
* 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..
*
* @method SiteTree CopyContentFrom()
@ -157,7 +159,8 @@ class VirtualPage extends Page
public function syncLinkTracking()
{
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 {
$this->HasBrokenLink = true;
}
@ -188,7 +191,11 @@ class VirtualPage extends Page
}
// 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;
}
@ -201,70 +208,71 @@ class VirtualPage extends Page
*/
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.
$copyContentFromField = new TreeDropdownField(
"CopyContentFromID",
_t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', "Linked Page"),
"SilverStripe\\CMS\\Model\\SiteTree"
);
// filter doesn't let you select children of virtual pages as as source page
//$copyContentFromField->setFilterFunction(function ($item) { return !($item instanceof VirtualPage); });
// 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));
// 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
if ($this->CopyContentFrom()->exists()) {
$link = "<a class=\"cmsEditlink\" href=\"admin/pages/edit/show/$this->CopyContentFromID\">"
. _t('SilverStripe\\CMS\\Model\\VirtualPage.EditLink', 'edit')
. "</a>";
$msgs[] = _t(
'SilverStripe\\CMS\\Model\\VirtualPage.HEADERWITHLINK',
"This is a virtual page copying content from \"{title}\" ({link})",
array(
'title' => $this->CopyContentFrom()->obj('Title'),
'link' => $link
)
);
} else {
$msgs[] = _t('SilverStripe\\CMS\\Model\\VirtualPage.HEADER', "This is a virtual page");
$msgs[] = _t(
'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)
) {
$msgs[] = _t(
'SilverStripe\\CMS\\Model\\SiteTree.VIRTUALPAGEDRAFTWARNING',
'Please publish the linked page in order to publish the virtual page'
);
}
// Create links back to the original object in the CMS
if ($this->CopyContentFrom()->exists()) {
$link = "<a class=\"cmsEditlink\" href=\"admin/pages/edit/show/$this->CopyContentFromID\">" . _t(
'SilverStripe\\CMS\\Model\\VirtualPage.EditLink',
'edit'
) . "</a>";
$msgs[] = _t(
'SilverStripe\\CMS\\Model\\VirtualPage.HEADERWITHLINK',
"This is a virtual page copying content from \"{title}\" ({link})",
array(
'title' => $this->CopyContentFrom()->obj('Title'),
'link' => $link,
)
);
} else {
$msgs[] = _t('SilverStripe\\CMS\\Model\\VirtualPage.HEADER', "This is a virtual page");
$msgs[] = _t(
'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
)
) {
$msgs[] = _t(
'SilverStripe\\CMS\\Model\\SiteTree.VIRTUALPAGEDRAFTWARNING',
'Please publish the linked page in order to publish the virtual page'
);
}
$fields->addFieldToTab(
"Root.Main",
new LiteralField(
$fields->addFieldToTab("Root.Main", new LiteralField(
'VirtualPageMessage',
'<div class="message notice">' . implode('. ', $msgs) . '.</div>'
),
'CopyContentFromID'
);
), 'CopyContentFromID');
});
return $fields;
return parent::getCMSFields();
}
public function onBeforeWrite()