Skip to content

Commit

Permalink
BUGFIX: Session var for active subsite out of sync with current subsi…
Browse files Browse the repository at this point in the history
…te. Refs #93.
  • Loading branch information
frankmullenger authored and chillu committed Jul 10, 2013
1 parent ac507dd commit 7bf6e89
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
4 changes: 4 additions & 0 deletions code/extensions/LeftAndMainSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
10 changes: 0 additions & 10 deletions code/extensions/SiteTreeSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,9 @@ public function duplicateToSubsite($subsiteID = null) {
* 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.
Expand Down
23 changes: 19 additions & 4 deletions code/model/Subsite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -286,19 +295,25 @@ static function currentSubsite() {
* @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
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/BaseSubsiteTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php
class BaseSubsiteTest extends SapphireTest {

function setUp() {
parent::setUp();

Subsite::$use_session_subsiteid = true;
}

/**
* Avoid subsites filtering on fixture fetching.
*/
Expand Down

0 comments on commit 7bf6e89

Please sign in to comment.