2007-08-16 08:38:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Decorator designed to add subsites support to LeftAndMain
|
2008-11-24 04:22:01 +01:00
|
|
|
*
|
|
|
|
* @package subsites
|
2007-08-16 08:38:29 +02:00
|
|
|
*/
|
|
|
|
class LeftAndMainSubsites extends Extension {
|
2011-09-05 17:54:40 +02:00
|
|
|
|
2013-05-06 12:21:09 +02:00
|
|
|
private static $allowed_actions = array('CopyToSubsite');
|
2012-08-10 11:23:19 +02:00
|
|
|
|
2011-02-14 04:10:47 +01:00
|
|
|
function init() {
|
2013-07-05 03:57:58 +02:00
|
|
|
|
|
|
|
//Use the session variable for current subsite in the CMS only
|
|
|
|
Subsite::$use_session_subsiteid = true;
|
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
Requirements::css('subsites/css/LeftAndMain_Subsites.css');
|
|
|
|
Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
|
2008-12-02 04:50:06 +01:00
|
|
|
Requirements::javascript('subsites/javascript/VirtualPage_Subsites.js');
|
2011-11-21 13:58:08 +01:00
|
|
|
|
2012-10-26 00:53:40 +02:00
|
|
|
if(isset($_GET['SubsiteID'])) {
|
2011-11-21 13:58:08 +01:00
|
|
|
// Clear current page when subsite changes (or is set for the first time)
|
2012-10-26 00:53:40 +02:00
|
|
|
if(!Session::get('SubsiteID') || $_GET['SubsiteID'] != Session::get('SubsiteID')) {
|
2011-11-21 13:58:08 +01:00
|
|
|
Session::clear("{$this->owner->class}.currentPage");
|
|
|
|
}
|
2012-07-10 15:43:53 +02:00
|
|
|
|
2011-11-21 13:58:08 +01:00
|
|
|
// Update current subsite in session
|
2012-10-26 00:53:40 +02:00
|
|
|
Subsite::changeSubsite($_GET['SubsiteID']);
|
2012-07-10 15:43:53 +02:00
|
|
|
|
|
|
|
//Redirect to clear the current page
|
2013-04-25 19:29:56 +02:00
|
|
|
return $this->owner->redirect('admin/');
|
2013-01-03 17:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set subsite ID based on currently shown record
|
|
|
|
$req = $this->owner->getRequest();
|
|
|
|
$id = $req->param('ID');
|
|
|
|
if($id && is_numeric($id)) {
|
|
|
|
$record = DataObject::get_by_id($this->owner->stat('tree_class'), $id);
|
|
|
|
if($record) Session::set('SubsiteID', $record->SubsiteID);
|
2011-11-21 13:58:08 +01:00
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2012-08-10 11:23:19 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
/**
|
|
|
|
* Set the title of the CMS tree
|
|
|
|
*/
|
|
|
|
function getCMSTreeTitle() {
|
|
|
|
$subsite = Subsite::currentSubSite();
|
2011-09-05 15:11:23 +02:00
|
|
|
return $subsite ? Convert::raw2xml($subsite->Title) : _t('LeftAndMain.SITECONTENTLEFT');
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2010-03-01 22:40:43 +01:00
|
|
|
|
|
|
|
function updatePageOptions(&$fields) {
|
|
|
|
$fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
/*
|
|
|
|
* Returns a list of the subsites accessible to the current user
|
|
|
|
*/
|
2007-08-16 08:38:29 +02:00
|
|
|
public function Subsites() {
|
2013-05-08 04:27:56 +02:00
|
|
|
// figure out what permission the controller needs
|
|
|
|
// Subsite::accessible_sites() expects something, so if there's no permission
|
|
|
|
// then fallback to using CMS_ACCESS_LeftAndMain.
|
|
|
|
$permission = 'CMS_ACCESS_' . $this->owner->class;
|
|
|
|
$available = Permission::get_codes(false);
|
|
|
|
if(!isset($available[$permission])) {
|
|
|
|
$permission = $this->owner->stat('required_permission_codes');
|
|
|
|
if(!$permission) {
|
|
|
|
$permission = 'CMS_ACCESS_LeftAndMain';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
return Subsite::accessible_sites($permission);
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generates a list of subsites with the data needed to
|
|
|
|
* produce a dropdown site switcher
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function ListSubsites(){
|
2007-08-16 08:38:29 +02:00
|
|
|
$list = $this->Subsites();
|
2010-04-06 05:15:44 +02:00
|
|
|
$currentSubsiteID = Subsite::currentSubsiteID();
|
2013-05-08 04:27:56 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
if($list == null || $list->Count() == 1 && $list->First()->DefaultSite == true){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
|
2013-05-08 04:27:56 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
$output = new ArrayList();
|
2013-05-08 04:27:56 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
foreach($list as $subsite) {
|
|
|
|
$CurrentState = $subsite->ID == $currentSubsiteID ? 'selected' : '';
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'CurrentState' => $CurrentState,
|
|
|
|
'ID' => $subsite->ID,
|
|
|
|
'Title' => Convert::raw2xml($subsite->Title)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a subset of the main menu, filtered by admins that have
|
|
|
|
* a subsiteCMSShowInMenu method returning true
|
|
|
|
*
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function SubsiteMainMenu(){
|
|
|
|
if(Subsite::currentSubsiteID() == 0){
|
|
|
|
return $this->owner->MainMenu();
|
|
|
|
}
|
|
|
|
// loop main menu items, add all items that have subsite support
|
|
|
|
$mainMenu = $this->owner->MainMenu();
|
|
|
|
$subsitesMenu = new ArrayList();
|
|
|
|
|
|
|
|
foreach($mainMenu as $menuItem){
|
|
|
|
|
|
|
|
$controllerName = $menuItem->MenuItem->controller;
|
|
|
|
|
|
|
|
if(class_exists($controllerName)){
|
|
|
|
$controller = singleton($controllerName);
|
|
|
|
|
|
|
|
if($controller->hasMethod('subsiteCMSShowInMenu') && $controller->subsiteCMSShowInMenu()){
|
|
|
|
$subsitesMenu->push($menuItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($menuItem->Code == 'Help'){
|
|
|
|
$subsitesMenu->push($menuItem);
|
2013-05-08 04:27:56 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
return $subsitesMenu;
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
public function CanAddSubsites() {
|
|
|
|
return Permission::check("ADMIN", "any", null, "all");
|
2007-08-31 02:29:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alternative security checker for LeftAndMain.
|
|
|
|
* If security isn't found, then it will switch to a subsite where we do have access.
|
|
|
|
*/
|
|
|
|
public function alternateAccessCheck() {
|
|
|
|
$className = $this->owner->class;
|
2008-12-02 04:50:06 +01:00
|
|
|
|
2010-03-01 22:37:56 +01:00
|
|
|
// Switch to the subsite of the current page
|
|
|
|
if ($this->owner->class == 'CMSMain' && $currentPage = $this->owner->currentPage()) {
|
|
|
|
if (Subsite::currentSubsiteID() != $currentPage->SubsiteID) {
|
|
|
|
Subsite::changeSubsite($currentPage->SubsiteID);
|
2007-08-31 02:29:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-26 05:56:03 +02:00
|
|
|
// Switch to a subsite that this user can actually access.
|
|
|
|
$member = Member::currentUser();
|
2012-10-12 04:09:38 +02:00
|
|
|
if($member && Permission::checkMember($member, 'ADMIN')) return true; // admin can access all subsites
|
2010-07-26 05:56:03 +02:00
|
|
|
|
2012-10-12 04:09:38 +02:00
|
|
|
$sites = Subsite::accessible_sites("CMS_ACCESS_{$this->owner->class}", true)->map('ID', 'Title');
|
2012-10-12 00:05:00 +02:00
|
|
|
if(is_object($sites)) $sites = $sites->toArray();
|
|
|
|
|
2010-03-01 22:37:56 +01:00
|
|
|
if($sites && !isset($sites[Subsite::currentSubsiteID()])) {
|
|
|
|
$siteIDs = array_keys($sites);
|
|
|
|
Subsite::changeSubsite($siteIDs[0]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-03-01 22:39:19 +01:00
|
|
|
// Switch to a different top-level menu item
|
|
|
|
$menu = CMSMenu::get_menu_items();
|
|
|
|
foreach($menu as $candidate) {
|
|
|
|
if($candidate->controller != $this->owner->class) {
|
2012-10-12 04:09:38 +02:00
|
|
|
$sites = Subsite::accessible_sites("CMS_ACCESS_{$candidate->controller}", true)->map('ID', 'Title');
|
2012-10-12 00:05:00 +02:00
|
|
|
if(is_object($sites)) $sites = $sites->toArray();
|
2010-03-01 22:39:19 +01:00
|
|
|
|
|
|
|
if($sites && !isset($sites[Subsite::currentSubsiteID()])) {
|
|
|
|
$siteIDs = array_keys($sites);
|
|
|
|
Subsite::changeSubsite($siteIDs[0]);
|
|
|
|
$cClass = $candidate->controller;
|
|
|
|
$cObj = new $cClass();
|
2012-10-12 00:06:23 +02:00
|
|
|
$this->owner->redirect($cObj->Link());
|
2010-03-01 22:39:19 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-01 22:37:56 +01:00
|
|
|
|
2010-03-01 22:39:19 +01:00
|
|
|
// If all of those fail, you really don't have access to the CMS
|
2007-08-31 02:29:25 +02:00
|
|
|
return null;
|
|
|
|
}
|
2007-10-11 06:50:49 +02:00
|
|
|
|
|
|
|
function augmentNewSiteTreeItem(&$item) {
|
2010-03-01 22:40:43 +01:00
|
|
|
$item->SubsiteID = isset($_POST['SubsiteID']) ? $_POST['SubsiteID'] : Subsite::currentSubsiteID();
|
2007-10-11 06:50:49 +02:00
|
|
|
}
|
2010-03-01 04:01:50 +01:00
|
|
|
|
|
|
|
function onAfterSave($record) {
|
2010-03-01 04:11:48 +01:00
|
|
|
if($record->hasMethod('NormalRelated') && ($record->NormalRelated() || $record->ReverseRelated())) {
|
2012-10-12 01:06:09 +02:00
|
|
|
$this->owner->response->addHeader('X-Status', rawurlencode(_t('LeftAndMainSubsites.Saved', 'Saved, please update related pages.')));
|
2010-03-01 04:01:50 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-12 01:06:09 +02:00
|
|
|
|
2012-08-10 11:23:19 +02:00
|
|
|
function copytosubsite($data, $form) {
|
|
|
|
$page = DataObject::get_by_id('SiteTree', $data['ID']);
|
|
|
|
$subsite = DataObject::get_by_id('Subsite', $data['CopyToSubsiteID']);
|
|
|
|
$newPage = $page->duplicateToSubsite($subsite->ID, true);
|
2013-01-03 17:38:51 +01:00
|
|
|
$response = $this->owner->getResponse();
|
|
|
|
$response->addHeader('X-Reload', true);
|
2012-08-10 11:23:19 +02:00
|
|
|
return $this->owner->redirect(Controller::join_links($this->owner->Link('show'), $newPage->ID));
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2013-04-25 19:29:56 +02:00
|
|
|
}
|