mirror of
https://github.com/silverstripe/silverstripe-subsites
synced 2024-10-22 09:05:55 +00:00
FEATURE Support for VirtualPage-style linkage between subsites with new SubsiteVirtualPage class
ENHANCEMENT Allowing to select a subsite explicitly through admin-URLs, e.g. /admin/show/22/?SubsiteID=2
This commit is contained in:
parent
fd8c93be2d
commit
e17ecfbdee
@ -13,6 +13,7 @@ class LeftAndMainSubsites extends Extension {
|
||||
function augmentInit() {
|
||||
Requirements::css('subsites/css/LeftAndMain_Subsites.css');
|
||||
Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
|
||||
Requirements::javascript('subsites/javascript/VirtualPage_Subsites.js');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -76,11 +77,14 @@ class LeftAndMainSubsites extends Extension {
|
||||
public function SubsiteList() {
|
||||
$list = $this->Subsites();
|
||||
|
||||
$requestSubsiteID = Controller::curr()->getRequest()->getVar('SubsiteID');
|
||||
$currentSubsiteID = ($requestSubsiteID) ? $requestSubsiteID : Session::get('SubsiteID');
|
||||
|
||||
if($list->Count() > 1) {
|
||||
$output = '<select id="SubsitesSelect">';
|
||||
|
||||
foreach($list as $subsite) {
|
||||
$selected = $subsite->ID == Session::get('SubsiteID') ? ' selected="selected"' : '';
|
||||
$selected = $subsite->ID == $currentSubsiteID ? ' selected="selected"' : '';
|
||||
|
||||
$output .= "\n<option value=\"{$subsite->ID}\"$selected>{$subsite->Title}</option>";
|
||||
}
|
||||
|
52
code/SubsitesTreeDropdownField.php
Normal file
52
code/SubsitesTreeDropdownField.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Wraps around a TreedropdownField to add ability for temporary
|
||||
* switching of subsite sessions.
|
||||
*
|
||||
* @package subsites
|
||||
*/
|
||||
class SubsitesTreeDropdownField extends TreeDropdownField {
|
||||
|
||||
protected $subsiteID = 0;
|
||||
|
||||
protected $extraClasses = array('SubsitesTreeDropdownField');
|
||||
|
||||
function Field() {
|
||||
$html = parent::Field();
|
||||
|
||||
Requirements::javascript('subsites/javascript/SubsitesTreeDropdownField.js');
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
function setSubsiteID($id) {
|
||||
$this->subsiteID = $id;
|
||||
}
|
||||
|
||||
function getSubsiteID() {
|
||||
return $this->subsiteID;
|
||||
}
|
||||
|
||||
function gettree() {
|
||||
$oldSubsiteID = Session::get('SubsiteID');
|
||||
Session::set('SubsiteID', $this->subsiteID);
|
||||
|
||||
$results = parent::gettree();
|
||||
|
||||
Session::set('SubsiteID', $oldSubsiteID);
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
function getsubtree() {
|
||||
$oldSubsiteID = Session::get('SubsiteID');
|
||||
Session::set('SubsiteID', $this->subsiteID);
|
||||
|
||||
$results = parent::getsubtree();
|
||||
|
||||
Session::set('SubsiteID', $oldSubsiteID);
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
?>
|
97
code/SubsitesVirtualPage.php
Normal file
97
code/SubsitesVirtualPage.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
class SubsitesVirtualPage extends VirtualPage {
|
||||
|
||||
function getCMSFields() {
|
||||
$fields = parent::getCMSFields();
|
||||
|
||||
$subsites = Subsite::getSubsitesForMember();
|
||||
if(!$subsites) $subsites = new DataObjectSet();
|
||||
if(Subsite::hasMainSitePermission()) {
|
||||
$subsites->push(new ArrayData(array('Title' => 'Main site', 'ID' => 0)));
|
||||
}
|
||||
|
||||
if($subsites->Count()) $subsiteSelectionField = new DropdownField(
|
||||
"CopyContentFromID_SubsiteID",
|
||||
"Subsite",
|
||||
$subsites->toDropdownMap('ID', 'Title'),
|
||||
($this->CopyContentFromID) ? $this->CopyContentFrom()->SubsiteID : Session::get('SubsiteID')
|
||||
);
|
||||
$fields->addFieldToTab(
|
||||
'Root.Content.Main',
|
||||
$subsiteSelectionField,
|
||||
'CopyContentFromID'
|
||||
);
|
||||
|
||||
// Setup the linking to the original page.
|
||||
$pageSelectionField = new SubsitesTreeDropdownField(
|
||||
"CopyContentFromID",
|
||||
_t('VirtualPage.CHOOSE', "Choose a page to link to"),
|
||||
"SiteTree"
|
||||
);
|
||||
$pageSelectionField->setFilterFunction(create_function('$item', 'return $item->ClassName != "VirtualPage";'));
|
||||
$subsiteID = Controller::curr()->getRequest()->getVar('TreeDropdownField_Form_EditForm_CopyContentFromID_SubsiteID');
|
||||
if($subsiteID) {
|
||||
$pageSelectionField->setSubsiteID($subsiteID);
|
||||
}
|
||||
$fields->replaceField('CopyContentFromID', $pageSelectionField);
|
||||
|
||||
// Create links back to the original object in the CMS
|
||||
if($this->CopyContentFromID) {
|
||||
$editLink = "admin/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID;
|
||||
$linkToContent = "
|
||||
<a class=\"cmsEditlink\" href=\"$editLink\">" .
|
||||
_t('VirtualPage.EDITCONTENT', 'Click here to edit the content') .
|
||||
"</a>";
|
||||
$fields->removeByName("VirtualPageContentLinkLabel");
|
||||
$fields->addFieldToTab(
|
||||
"Root.Content.Main",
|
||||
$linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent),
|
||||
'Title'
|
||||
);
|
||||
$linkToContentLabelField->setAllowHTML(true);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
function getVirtualFields() {
|
||||
$fields = parent::getVirtualFields();
|
||||
foreach($fields as $k => $v) {
|
||||
if($v == 'SubsiteID') unset($fields[$k]);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
function onBeforeWrite() {
|
||||
Subsite::$disable_subsite_filter = true;
|
||||
|
||||
parent::onBeforeWrite();
|
||||
}
|
||||
|
||||
function onAfterWrite() {
|
||||
Subsite::$disable_subsite_filter = false;
|
||||
|
||||
parent::onAfterWrite();
|
||||
}
|
||||
}
|
||||
|
||||
class SubsitesVirtualPage_Controller extends VirtualPage_Controller {
|
||||
|
||||
function reloadContent() {
|
||||
$this->failover->nextWriteDoesntCustomise();
|
||||
$this->failover->copyFrom($this->failover->CopyContentFrom());
|
||||
$this->failover->nextWriteDoesntCustomise();
|
||||
$this->failover->write();
|
||||
return;
|
||||
}
|
||||
|
||||
function init(){
|
||||
Subsite::$disable_subsite_filter = true;
|
||||
|
||||
parent::init();
|
||||
|
||||
Subsite::$disable_subsite_filter = false;
|
||||
}
|
||||
}
|
||||
?>
|
37
javascript/SubsitesTreeDropdownField.js
Normal file
37
javascript/SubsitesTreeDropdownField.js
Normal file
@ -0,0 +1,37 @@
|
||||
SubsitesTreeDropdownField = Class.extend('TreeDropdownField');
|
||||
SubsitesTreeDropdownField.prototype = {
|
||||
|
||||
//subsiteID: null,
|
||||
|
||||
ajaxGetTree: function(after) {
|
||||
var ajaxURL = this.helperURLBase() + 'gettree?forceValues=' + this.inputTag.value;
|
||||
|
||||
// Customized: Append subsiteid (evaluated in SubsitesVirtualPage.php)
|
||||
if(this.subsiteID) ajaxURL += '&' + this.id + '_SubsiteID=' + this.subsiteID;
|
||||
|
||||
ajaxURL += $('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '';
|
||||
new Ajax.Request(ajaxURL, {
|
||||
method : 'get',
|
||||
onSuccess : after,
|
||||
onFailure : function(response) { errorMessage("Error getting data", response); }
|
||||
})
|
||||
},
|
||||
|
||||
ajaxExpansion: function() {
|
||||
this.addNodeClass('loading');
|
||||
var ul = this.treeNodeHolder();
|
||||
ul.innerHTML = ss.i18n._t('LOADING');
|
||||
|
||||
var ajaxURL = this.options.dropdownField.helperURLBase() + 'getsubtree?&SubtreeRootID=' + this.getIdx();
|
||||
|
||||
// Customized: Append subsiteid (evaluated in SubsitesVirtualPage.php)
|
||||
if(this.subsiteID) ajaxURL += '&' + this.id + '_SubsiteID=' + this.subsiteID;
|
||||
|
||||
ajaxURL += $('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '';
|
||||
new Ajax.Request(ajaxURL, {
|
||||
onSuccess : this.installSubtree.bind(this),
|
||||
onFailure : function(response) { errorMessage('error loading subtree', response); }
|
||||
});
|
||||
}
|
||||
}
|
||||
SubsitesTreeDropdownField.applyTo('div.SubsitesTreeDropdownField');
|
17
javascript/VirtualPage_Subsites.js
Normal file
17
javascript/VirtualPage_Subsites.js
Normal file
@ -0,0 +1,17 @@
|
||||
Behaviour.register({
|
||||
'#CopyContentFromID_SubsiteID select' : {
|
||||
initialize: function() {
|
||||
var treeField = $('TreeDropdownField_Form_EditForm_CopyContentFromID');
|
||||
if(!treeField) return false;
|
||||
|
||||
treeField.subsiteID = this.value;
|
||||
},
|
||||
onchange: function() {
|
||||
var treeField = $('TreeDropdownField_Form_EditForm_CopyContentFromID');
|
||||
if(!treeField) return false;
|
||||
|
||||
treeField.subsiteID = this.value;
|
||||
treeField.refresh();
|
||||
}
|
||||
}
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user