FIX Subsites VirtualPage link to edit main page and TreeDropdownField API implementations

This commit is contained in:
Robbie Averill 2018-01-30 16:21:45 +13:00
parent 6fb0af0f93
commit 34b3fbf04d
5 changed files with 95 additions and 37 deletions

View File

@ -4,14 +4,14 @@
* Choose a subsite from which to select pages. * Choose a subsite from which to select pages.
* Needs to clear tree dropdowns in case selection is changed. * Needs to clear tree dropdowns in case selection is changed.
*/ */
$('.subsitestreedropdownfield-chooser').entwine({ $('select.subsitestreedropdownfield-chooser').entwine({
onchange: function() { onchange: function() {
// TODO Data binding between two fields // TODO Data binding between two fields
// TODO create resetField method on API instead const name = this.attr('name').replace('_SubsiteID', '');
var fields = $('.SubsitesTreeDropdownField'); let field = $('#Form_EditForm_' + name).first();
fields.setValue(null); field.setValue(0);
fields.setTitle(null); field.refresh();
fields.find('.tree-holder').empty(); field.trigger('change');
} }
}); });
@ -20,11 +20,14 @@
* before asking for the tree. * before asking for the tree.
*/ */
$('.TreeDropdownField.SubsitesTreeDropdownField').entwine({ $('.TreeDropdownField.SubsitesTreeDropdownField').entwine({
getRequestParams: function() { getAttributes() {
var name = this.find(':input[type=hidden]:first').attr('name') + '_SubsiteID', const fieldName = this.attr('id').replace('Form_EditForm_', '');
source = $('[name=' + name + ']'), params = {}; const subsiteID = $('#Form_EditForm_' + fieldName + '_SubsiteID option:selected').val();
params[name] = source.length ? source.val() : null;
return params; let attributes = this._super();
attributes.data.urlTree += "?" + fieldName + "_SubsiteID=" + subsiteID;
attributes.data.cacheKey = attributes.data.cacheKey.substring(0, 19) + "_" + subsiteID;
return attributes;
} }
}); });
}); });

View File

@ -5,6 +5,7 @@ namespace SilverStripe\Subsites\Extensions;
use SilverStripe\Admin\AdminRootController; use SilverStripe\Admin\AdminRootController;
use SilverStripe\Admin\CMSMenu; use SilverStripe\Admin\CMSMenu;
use SilverStripe\Admin\LeftAndMainExtension; use SilverStripe\Admin\LeftAndMainExtension;
use SilverStripe\CMS\Controllers\CMSPagesController;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Controllers\CMSPageEditController; use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
@ -270,13 +271,31 @@ class LeftAndMainSubsites extends LeftAndMainExtension
$session->clear($sessionNamespace . '.currentPage'); $session->clear($sessionNamespace . '.currentPage');
} }
// Subsite ID has already been set to the state via InitStateMiddleware. If the user cannot view // Context: Subsite ID has already been set to the state via InitStateMiddleware
// the current page, or we're in the context of editing a specific page, redirect to the admin landing
// section to prevent a loop of re-loading the original subsite for the current page. // If the user cannot view the current page, redirect to the admin landing section
if (!$this->owner->canView() || Controller::curr() instanceof CMSPageEditController) { if (!$this->owner->canView()) {
return $this->owner->redirect(AdminRootController::config()->get('url_base') . '/'); return $this->owner->redirect(AdminRootController::config()->get('url_base') . '/');
} }
$currentController = Controller::curr();
if ($currentController instanceof CMSPageEditController) {
/** @var SiteTree $page */
$page = $currentController->currentPage();
// If the page exists but doesn't belong to the requested subsite, redirect to admin/pages which
// will show a list of the requested subsite's pages
$currentSubsiteId = $request->getVar('SubsiteID');
if ($page && (int) $page->SubsiteID !== (int) $currentSubsiteId) {
return $this->owner->redirect(CMSPagesController::singleton()->Link());
}
// Page does belong to the current subsite, so remove the query string parameter and refresh the page
// Remove the subsiteID parameter and redirect back to the current URL again
$request->offsetSet('SubsiteID', null);
return $this->owner->redirect($request->getURL(true));
}
// Redirect to clear the current page, retaining the current URL parameters // Redirect to clear the current page, retaining the current URL parameters
return $this->owner->redirect( return $this->owner->redirect(
Controller::join_links($this->owner->Link(), ...array_values($this->owner->getURLParams())) Controller::join_links($this->owner->Link(), ...array_values($this->owner->getURLParams()))
@ -299,10 +318,7 @@ class LeftAndMainSubsites extends LeftAndMainExtension
$canViewElsewhere = SubsiteState::singleton()->withState(function ($newState) use ($record) { $canViewElsewhere = SubsiteState::singleton()->withState(function ($newState) use ($record) {
$newState->setSubsiteId($record->SubsiteID); $newState->setSubsiteId($record->SubsiteID);
if ($this->owner->canView(Security::getCurrentUser())) { return (bool) $this->owner->canView(Security::getCurrentUser());
return true;
}
return false;
}); });
if ($canViewElsewhere) { if ($canViewElsewhere) {

View File

@ -118,15 +118,15 @@ class SiteTreeSubsites extends DataExtension
'SubsiteOperations', 'SubsiteOperations',
_t(__CLASS__ . '.SubsiteOperations', 'Subsite Operations'), _t(__CLASS__ . '.SubsiteOperations', 'Subsite Operations'),
[ [
new DropdownField('CopyToSubsiteID', _t( DropdownField::create('CopyToSubsiteID', _t(
__CLASS__ . '.CopyToSubsite', __CLASS__ . '.CopyToSubsite',
'Copy page to subsite' 'Copy page to subsite'
), $subsitesMap), ), $subsitesMap),
new CheckboxField( CheckboxField::create(
'CopyToSubsiteWithChildren', 'CopyToSubsiteWithChildren',
_t(__CLASS__ . '.CopyToSubsiteWithChildren', 'Include children pages?') _t(__CLASS__ . '.CopyToSubsiteWithChildren', 'Include children pages?')
), ),
$copyAction = new FormAction( $copyAction = FormAction::create(
'copytosubsite', 'copytosubsite',
_t(__CLASS__ . '.CopyAction', 'Copy') _t(__CLASS__ . '.CopyAction', 'Copy')
) )
@ -134,6 +134,8 @@ class SiteTreeSubsites extends DataExtension
)->setHeadingLevel(4) )->setHeadingLevel(4)
); );
$copyAction->addExtraClass('btn btn-primary font-icon-save ml-3');
// @todo check if this needs re-implementation // @todo check if this needs re-implementation
// $copyAction->includeDefaultJS(false); // $copyAction->includeDefaultJS(false);
} }

View File

@ -5,6 +5,7 @@ namespace SilverStripe\Subsites\Forms;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\TreeDropdownField; use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\Subsites\State\SubsiteState;
@ -20,7 +21,10 @@ class SubsitesTreeDropdownField extends TreeDropdownField
'tree' 'tree'
]; ];
protected $subsiteID = 0; /**
* @var int
*/
protected $subsiteId = 0;
/** /**
* Extra HTML classes * Extra HTML classes
@ -39,20 +43,42 @@ class SubsitesTreeDropdownField extends TreeDropdownField
return $html; return $html;
} }
public function setSubsiteID($id) /**
* Sets the subsite ID to use when generating the tree
*
* @param int $id
* @return $this
*/
public function setSubsiteId($id)
{ {
$this->subsiteID = $id; $this->subsiteId = $id;
return $this;
} }
public function getSubsiteID() /**
* Get the subsite ID to use when generating the tree
*
* @return int
*/
public function getSubsiteId()
{ {
return $this->subsiteID; return $this->subsiteId;
} }
/**
* Get the CMS tree with the provided subsite ID applied to the state
*
* {@inheritDoc}
*/
public function tree(HTTPRequest $request) public function tree(HTTPRequest $request)
{ {
$results = SubsiteState::singleton()->withState(function () use ($request) { // Detect subsite ID from the request
SubsiteState::singleton()->setSubsiteId($this->subsiteID); if ($request->getVar($this->getName() . '_SubsiteID')) {
$this->setSubsiteId($request->getVar($this->getName() . '_SubsiteID'));
}
$results = SubsiteState::singleton()->withState(function (SubsiteState $newState) use ($request) {
$newState->setSubsiteId($this->getSubsiteId());
return parent::tree($request); return parent::tree($request);
}); });

View File

@ -2,14 +2,17 @@
namespace SilverStripe\Subsites\Pages; namespace SilverStripe\Subsites\Pages;
use SilverStripe\CMS\Controllers\CMSPageEditController;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\CMS\Model\VirtualPage; use SilverStripe\CMS\Model\VirtualPage;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\LabelField; use SilverStripe\Forms\LabelField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\TextareaField; use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Subsites\Forms\SubsitesTreeDropdownField; use SilverStripe\Subsites\Forms\SubsitesTreeDropdownField;
@ -59,23 +62,32 @@ class SubsitesVirtualPage extends VirtualPage
); );
// Setup the linking to the original page. // Setup the linking to the original page.
$pageSelectionField = new SubsitesTreeDropdownField( $pageSelectionField = SubsitesTreeDropdownField::create(
'CopyContentFromID', 'CopyContentFromID',
_t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', 'Choose a page to link to'), _t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', 'Linked Page'),
"SilverStripe\\CMS\\Model\\SiteTree", SiteTree::class,
'ID', 'ID',
'MenuTitle' 'MenuTitle'
); );
$fields->addFieldToTab(
'Root.Main',
TreeDropdownField::create('CopyContentFromID', 'Linked Page', SiteTree::class)
);
if (Controller::has_curr() && Controller::curr()->getRequest()) { if (Controller::has_curr() && Controller::curr()->getRequest()) {
$subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID'); $subsiteID = (int) Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID');
$pageSelectionField->setSubsiteID($subsiteID); $pageSelectionField->setSubsiteID($subsiteID);
} }
$fields->replaceField('CopyContentFromID', $pageSelectionField); $fields->replaceField('CopyContentFromID', $pageSelectionField);
// Create links back to the original object in the CMS // Create links back to the original object in the CMS
if ($this->CopyContentFromID) { if ($this->CopyContentFromID) {
$editLink = "admin/pages/edit/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID; $editLink = Controller::join_links(
CMSPageEditController::singleton()->Link('show'),
$this->CopyContentFromID
);
$linkToContent = " $linkToContent = "
<a class=\"cmsEditlink\" href=\"$editLink\">" . <a class=\"cmsEditlink\" href=\"$editLink\">" .
_t('SilverStripe\\CMS\\Model\\VirtualPage.EDITCONTENT', 'Click here to edit the content') . _t('SilverStripe\\CMS\\Model\\VirtualPage.EDITCONTENT', 'Click here to edit the content') .
@ -83,10 +95,9 @@ class SubsitesVirtualPage extends VirtualPage
$fields->removeByName('VirtualPageContentLinkLabel'); $fields->removeByName('VirtualPageContentLinkLabel');
$fields->addFieldToTab( $fields->addFieldToTab(
'Root.Main', 'Root.Main',
$linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent), $linkToContentLabelField = LiteralField::create('VirtualPageContentLinkLabel', $linkToContent),
'Title' 'Title'
); );
$linkToContentLabelField->setAllowHTML(true);
} }