From 7bf6e893208ce689b45ab34a9c1de14e5d5201bf Mon Sep 17 00:00:00 2001 From: Frank Mullenger Date: Fri, 5 Jul 2013 13:57:58 +1200 Subject: [PATCH] BUGFIX: Session var for active subsite out of sync with current subsite. Refs silverstripe/silverstripe-subsites#93. --- code/extensions/LeftAndMainSubsites.php | 4 ++++ code/extensions/SiteTreeSubsites.php | 10 ---------- code/model/Subsite.php | 23 +++++++++++++++++++---- tests/BaseSubsiteTest.php | 7 +++++++ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/code/extensions/LeftAndMainSubsites.php b/code/extensions/LeftAndMainSubsites.php index 0456430..5950914 100644 --- a/code/extensions/LeftAndMainSubsites.php +++ b/code/extensions/LeftAndMainSubsites.php @@ -9,6 +9,10 @@ class LeftAndMainSubsites extends Extension { private static $allowed_actions = array('CopyToSubsite'); function init() { + + //Use the session variable for current subsite in the CMS only + Subsite::$use_session_subsiteid = true; + Requirements::css('subsites/css/LeftAndMain_Subsites.css'); Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js'); Requirements::javascript('subsites/javascript/VirtualPage_Subsites.js'); diff --git a/code/extensions/SiteTreeSubsites.php b/code/extensions/SiteTreeSubsites.php index 4e2c00b..945a14a 100644 --- a/code/extensions/SiteTreeSubsites.php +++ b/code/extensions/SiteTreeSubsites.php @@ -198,19 +198,9 @@ class SiteTreeSubsites extends DataExtension { * Called by ContentController::init(); */ static function contentcontrollerInit($controller) { - // Need to set the SubsiteID to null incase we've been in the CMS - Session::set('SubsiteID', null); $subsite = Subsite::currentSubsite(); if($subsite && $subsite->Theme) SSViewer::set_theme(Subsite::currentSubsite()->Theme); } - - /** - * Called by ModelAsController::init(); - */ - static function modelascontrollerInit($controller) { - // Need to set the SubsiteID to null incase we've been in the CMS - Session::set('SubsiteID', null); - } function alternateAbsoluteLink() { // Generate the existing absolute URL and replace the domain with the subsite domain. diff --git a/code/model/Subsite.php b/code/model/Subsite.php index ecdb268..21078af 100644 --- a/code/model/Subsite.php +++ b/code/model/Subsite.php @@ -7,6 +7,15 @@ */ class Subsite extends DataObject implements PermissionProvider { + /** + * @var $use_session_subsiteid Boolean Set to TRUE when using the CMS and FALSE + * when browsing the frontend of a website. + * + * @todo Remove flag once the Subsite CMS works without session state, + * similarly to the Translatable module. + */ + public static $use_session_subsiteid = false; + /** * @var boolean $disable_subsite_filter If enabled, bypasses the query decoration * to limit DataObject::get*() calls to a specific subsite. Useful for debugging. @@ -286,19 +295,25 @@ JS; * @return int ID of the current subsite instance */ static function currentSubsiteID() { - if(isset($_GET['SubsiteID'])) $id = (int)$_GET['SubsiteID']; - else $id = Session::get('SubsiteID'); + $id = NULL; + + if(isset($_GET['SubsiteID'])) { + $id = (int)$_GET['SubsiteID']; + } + else if (Subsite::$use_session_subsiteid) { + $id = Session::get('SubsiteID'); + } if($id === NULL) { $id = self::getSubsiteIDForDomain(); - Session::set('SubsiteID', $id); } return (int)$id; } /** - * Switch to another subsite. + * Switch to another subsite through storing the subsite identifier in the current PHP session. + * Only takes effect when {@link Subsite::$use_session_subsiteid} is set to TRUE. * * @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself */ diff --git a/tests/BaseSubsiteTest.php b/tests/BaseSubsiteTest.php index 9f2a5f0..de0940e 100644 --- a/tests/BaseSubsiteTest.php +++ b/tests/BaseSubsiteTest.php @@ -1,5 +1,12 @@