diff --git a/code/LeftAndMainSubsites.php b/code/LeftAndMainSubsites.php index cba583d..11827b3 100644 --- a/code/LeftAndMainSubsites.php +++ b/code/LeftAndMainSubsites.php @@ -117,7 +117,6 @@ class LeftAndMainSubsites extends Extension { public function alternateAccessCheck() { $className = $this->owner->class; - // Switch to the subsite of the current page if ($this->owner->class == 'CMSMain' && $currentPage = $this->owner->currentPage()) { if (Subsite::currentSubsiteID() != $currentPage->SubsiteID) { @@ -133,8 +132,24 @@ class LeftAndMainSubsites extends Extension { return true; } - if(!$sites) return null; + // Switch to a different top-level menu item + $menu = CMSMenu::get_menu_items(); + foreach($menu as $candidate) { + if($candidate->controller != $this->owner->class) { + + $sites = Subsite::accessible_sites("CMS_ACCESS_{$candidate->controller}")->toDropdownMap(); + if($sites && !isset($sites[Subsite::currentSubsiteID()])) { + $siteIDs = array_keys($sites); + Subsite::changeSubsite($siteIDs[0]); + $cClass = $candidate->controller; + $cObj = new $cClass(); + Director::redirect($cObj->Link()); + return null; + } + } + } + // If all of those fail, you really don't have access to the CMS return null; } diff --git a/code/SiteConfigSubsites.php b/code/SiteConfigSubsites.php index 39c0a2e..90584d2 100644 --- a/code/SiteConfigSubsites.php +++ b/code/SiteConfigSubsites.php @@ -19,7 +19,10 @@ class SiteConfigSubsites extends DataObjectDecorator { if(Subsite::$disable_subsite_filter) return; // If you're querying by ID, ignore the sub-site - this is a bit ugly... - if (!$query->where || (!preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]))) { + if (!$query->where || ( + !preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]) && + !preg_match('/\.?(\'|"|`|)SubsiteID(\'|"|`|)( ?)=/', $query->where[0]) + )) { if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID; else $subsiteID = (int)Subsite::currentSubsiteID();