2007-08-16 08:38:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension for the SiteTree object to add subsites support
|
|
|
|
*/
|
2012-03-25 18:35:01 +02:00
|
|
|
class SiteTreeSubsites extends DataExtension {
|
2013-05-06 12:21:09 +02:00
|
|
|
|
|
|
|
private static $has_one = array(
|
2013-05-01 02:35:17 +02:00
|
|
|
'Subsite' => 'Subsite', // The subsite that this page belongs to
|
2012-07-10 15:43:53 +02:00
|
|
|
);
|
2013-05-06 12:21:09 +02:00
|
|
|
|
|
|
|
private static $many_many = array(
|
2013-05-01 02:35:17 +02:00
|
|
|
'CrossSubsiteLinkTracking' => 'SiteTree' // Stored separately, as the logic for URL rewriting is different
|
2012-07-10 15:43:53 +02:00
|
|
|
);
|
2013-05-06 12:21:09 +02:00
|
|
|
|
|
|
|
private static $many_many_extraFields = array(
|
2012-07-10 15:43:53 +02:00
|
|
|
"CrossSubsiteLinkTracking" => array("FieldName" => "Varchar")
|
|
|
|
);
|
2013-05-06 12:21:09 +02:00
|
|
|
|
2010-06-28 23:40:36 +02:00
|
|
|
function isMainSite() {
|
|
|
|
if($this->owner->SubsiteID == 0) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
/**
|
|
|
|
* Update any requests to limit the results to the current site
|
|
|
|
*/
|
2015-11-20 03:32:52 +01:00
|
|
|
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) {
|
2007-09-05 06:47:05 +02:00
|
|
|
if(Subsite::$disable_subsite_filter) return;
|
2013-09-12 05:23:21 +02:00
|
|
|
if($dataQuery->getQueryParam('Subsite.filter') === false) return;
|
2007-09-05 06:47:05 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
|
2010-03-02 02:15:11 +01:00
|
|
|
// if(!$query->where || (strpos($query->where[0], ".\"ID\" = ") === false && strpos($query->where[0], ".`ID` = ") === false && strpos($query->where[0], ".ID = ") === false && strpos($query->where[0], "ID = ") !== 0)) {
|
2014-08-26 01:37:16 +02:00
|
|
|
if($query->filtersOnID()) return;
|
|
|
|
|
|
|
|
if (Subsite::$force_subsite) $subsiteID = Subsite::$force_subsite;
|
|
|
|
else {
|
|
|
|
/*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
|
|
|
|
else */$subsiteID = (int)Subsite::currentSubsiteID();
|
|
|
|
}
|
2010-03-02 02:15:11 +01:00
|
|
|
|
2014-08-26 01:37:16 +02:00
|
|
|
// The foreach is an ugly way of getting the first key :-)
|
|
|
|
foreach($query->getFrom() as $tableName => $info) {
|
|
|
|
// The tableName should be SiteTree or SiteTree_Live...
|
|
|
|
if(strpos($tableName,'SiteTree') === false) break;
|
|
|
|
$query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
|
|
|
|
break;
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-02 02:15:11 +01:00
|
|
|
function onBeforeWrite() {
|
|
|
|
if(!$this->owner->ID && !$this->owner->SubsiteID) $this->owner->SubsiteID = Subsite::currentSubsiteID();
|
|
|
|
|
|
|
|
parent::onBeforeWrite();
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
|
2012-04-25 14:29:32 +02:00
|
|
|
function updateCMSFields(FieldList $fields) {
|
2012-08-10 11:23:19 +02:00
|
|
|
$subsites = Subsite::accessible_sites("CMS_ACCESS_CMSMain");
|
|
|
|
$subsitesMap = array();
|
|
|
|
if($subsites && $subsites->Count()) {
|
2013-01-03 17:38:51 +01:00
|
|
|
$subsitesMap = $subsites->map('ID', 'Title');
|
2012-08-10 11:23:19 +02:00
|
|
|
unset($subsitesMap[$this->owner->SubsiteID]);
|
2014-07-30 23:51:06 +02:00
|
|
|
}
|
2012-08-10 11:23:19 +02:00
|
|
|
|
|
|
|
// Master page edit field (only allowed from default subsite to avoid inconsistent relationships)
|
|
|
|
$isDefaultSubsite = $this->owner->SubsiteID == 0 || $this->owner->Subsite()->DefaultSite;
|
|
|
|
if($isDefaultSubsite && $subsitesMap) {
|
|
|
|
$fields->addFieldToTab(
|
2013-01-03 17:38:51 +01:00
|
|
|
'Root.Main',
|
2012-08-10 11:23:19 +02:00
|
|
|
new DropdownField(
|
|
|
|
"CopyToSubsiteID",
|
|
|
|
_t('SiteTreeSubsites.CopyToSubsite', "Copy page to subsite"),
|
|
|
|
$subsitesMap,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$fields->addFieldToTab(
|
2013-01-03 17:38:51 +01:00
|
|
|
'Root.Main',
|
2012-08-10 11:23:19 +02:00
|
|
|
$copyAction = new InlineFormAction(
|
|
|
|
"copytosubsite",
|
|
|
|
_t('SiteTreeSubsites.CopyAction', "Copy")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$copyAction->includeDefaultJS(false);
|
|
|
|
}
|
2013-05-01 02:35:17 +02:00
|
|
|
|
2008-12-02 05:06:58 +01:00
|
|
|
// replace readonly link prefix
|
|
|
|
$subsite = $this->owner->Subsite();
|
2013-05-06 12:32:40 +02:00
|
|
|
$nested_urls_enabled = Config::inst()->get('SiteTree', 'nested_urls');
|
2008-12-02 05:06:58 +01:00
|
|
|
if($subsite && $subsite->ID) {
|
2014-08-18 11:03:52 +02:00
|
|
|
$baseUrl = Director::protocol() . $subsite->domain() . '/';
|
2012-07-10 15:43:53 +02:00
|
|
|
$baseLink = Controller::join_links (
|
|
|
|
$baseUrl,
|
2013-05-06 12:32:40 +02:00
|
|
|
($nested_urls_enabled && $this->owner->ParentID ? $this->owner->Parent()->RelativeLink(true) : null)
|
2012-07-10 15:43:53 +02:00
|
|
|
);
|
|
|
|
|
2012-07-11 15:32:10 +02:00
|
|
|
$urlsegment = $fields->dataFieldByName('URLSegment');
|
2014-06-04 14:12:28 +02:00
|
|
|
$urlsegment->setURLPrefix($baseLink);
|
2008-12-02 05:06:58 +01:00
|
|
|
}
|
2010-03-01 22:59:08 +01:00
|
|
|
}
|
|
|
|
|
2010-03-01 23:27:21 +01:00
|
|
|
function alternateSiteConfig() {
|
2010-03-11 22:51:06 +01:00
|
|
|
if(!$this->owner->SubsiteID) return false;
|
2010-04-30 00:58:10 +02:00
|
|
|
$sc = DataObject::get_one('SiteConfig', '"SubsiteID" = ' . $this->owner->SubsiteID);
|
2010-03-01 23:28:50 +01:00
|
|
|
if(!$sc) {
|
|
|
|
$sc = new SiteConfig();
|
|
|
|
$sc->SubsiteID = $this->owner->SubsiteID;
|
2013-10-30 13:43:59 +01:00
|
|
|
$sc->Title = _t('Subsite.SiteConfigTitle','Your Site Name');
|
|
|
|
$sc->Tagline = _t('Subsite.SiteConfigSubtitle','Your tagline here');
|
2010-03-01 23:28:50 +01:00
|
|
|
$sc->write();
|
|
|
|
}
|
|
|
|
return $sc;
|
2010-03-01 23:27:21 +01:00
|
|
|
}
|
|
|
|
|
2008-11-24 07:37:22 +01:00
|
|
|
/**
|
|
|
|
* Only allow editing of a page if the member satisfies one of the following conditions:
|
|
|
|
* - Is in a group which has access to the subsite this page belongs to
|
|
|
|
* - Is in a group with edit permissions on the "main site"
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canEdit($member = null) {
|
2014-01-09 21:28:35 +01:00
|
|
|
|
2010-08-23 02:07:05 +02:00
|
|
|
if(!$member) $member = Member::currentUser();
|
2009-05-15 05:12:43 +02:00
|
|
|
|
2010-03-01 22:37:56 +01:00
|
|
|
// Find the sites that this user has access to
|
|
|
|
$goodSites = Subsite::accessible_sites('CMS_ACCESS_CMSMain',true,'all',$member)->column('ID');
|
2014-01-09 21:28:35 +01:00
|
|
|
|
|
|
|
if (!is_null($this->owner->SubsiteID)) {
|
|
|
|
$subsiteID = $this->owner->SubsiteID;
|
|
|
|
} else {
|
|
|
|
// The relationships might not be available during the record creation when using a GridField.
|
|
|
|
// In this case the related objects will have empty fields, and SubsiteID will not be available.
|
|
|
|
//
|
|
|
|
// We do the second best: fetch the likely SubsiteID from the session. The drawback is this might
|
|
|
|
// make it possible to force relations to point to other (forbidden) subsites.
|
|
|
|
$subsiteID = Subsite::currentSubsiteID();
|
|
|
|
}
|
|
|
|
|
2010-03-01 22:37:56 +01:00
|
|
|
// Return true if they have access to this object's site
|
2014-01-09 21:28:35 +01:00
|
|
|
if(!(in_array(0, $goodSites) || in_array($subsiteID, $goodSites))) return false;
|
2008-11-24 07:37:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canDelete($member = null) {
|
2008-11-26 05:03:14 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentUser();
|
2008-11-24 07:37:22 +01:00
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canAddChildren($member = null) {
|
2008-11-26 05:03:14 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentUser();
|
2008-11-24 07:37:22 +01:00
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function canPublish($member = null) {
|
2008-11-26 05:03:14 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentUser();
|
|
|
|
|
2008-11-24 07:37:22 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-01 08:49:36 +02:00
|
|
|
* Does the basic duplication, but doesn't write anything
|
|
|
|
* this means we can subclass this easier and do more complex
|
|
|
|
* relation duplication.
|
2007-08-16 08:38:29 +02:00
|
|
|
*/
|
2015-08-01 08:49:36 +02:00
|
|
|
public function duplicateToSubsitePrep($subsiteID) {
|
|
|
|
if (is_object($subsiteID)) {
|
|
|
|
$subsiteID = $subsiteID->ID;
|
2012-07-16 01:14:13 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 08:49:36 +02:00
|
|
|
$oldSubsite = Subsite::currentSubsiteID();
|
|
|
|
if ($subsiteID) {
|
|
|
|
Subsite::changeSubsite($subsiteID);
|
|
|
|
} else {
|
|
|
|
$subsiteID = $oldSubsite;
|
|
|
|
}
|
|
|
|
// doesn't write as we need to reset the SubsiteID, ParentID etc
|
|
|
|
$clone = $this->owner->duplicate(false);
|
|
|
|
$clone->CheckedPublicationDifferences = $clone->AddedToStage = true;
|
2012-07-11 15:32:10 +02:00
|
|
|
$subsiteID = ($subsiteID ? $subsiteID : $oldSubsite);
|
2015-08-01 08:49:36 +02:00
|
|
|
$clone->SubsiteID = $subsiteID;
|
|
|
|
// We have no idea what the parentID should be, so as a workaround use the url-segment and subsite ID
|
|
|
|
if ($this->owner->Parent()) {
|
|
|
|
$parentSeg = $this->owner->Parent()->URLSegment;
|
|
|
|
$newParentPage = Page::get()->filter('URLSegment', $parentSeg)->first();
|
|
|
|
if ($newParentPage) {
|
|
|
|
$clone->ParentID = $newParentPage->ID;
|
|
|
|
} else {
|
|
|
|
// reset it to the top level, so the user can decide where to put it
|
|
|
|
$clone->ParentID = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-05-01 02:35:17 +02:00
|
|
|
// MasterPageID is here for legacy purposes, to satisfy the subsites_relatedpages module
|
2015-08-01 08:49:36 +02:00
|
|
|
$clone->MasterPageID = $this->owner->ID;
|
|
|
|
return $clone;
|
|
|
|
}
|
2012-07-16 01:14:13 +02:00
|
|
|
|
2015-08-01 08:49:36 +02:00
|
|
|
/**
|
|
|
|
* Create a duplicate of this page and save it to another subsite
|
|
|
|
* @param $subsiteID int|Subsite The Subsite to copy to, or its ID
|
|
|
|
*/
|
|
|
|
public function duplicateToSubsite($subsiteID = null) {
|
|
|
|
$clone = $this->owner->duplicateToSubsitePrep($subsiteID);
|
|
|
|
$clone->invokeWithExtensions('onBeforeDuplicateToSubsite', $this->owner);
|
|
|
|
$clone->write();
|
|
|
|
$clone->duplicateSubsiteRelations($this->owner);
|
|
|
|
// new extension hooks which happens after write,
|
|
|
|
// onAfterDuplicate isn't reliable due to
|
|
|
|
// https://github.com/silverstripe/silverstripe-cms/issues/1253
|
|
|
|
$clone->invokeWithExtensions('onAfterDuplicateToSubsite', $this->owner);
|
|
|
|
return $clone;
|
|
|
|
}
|
2012-07-16 01:14:13 +02:00
|
|
|
|
2015-08-01 08:49:36 +02:00
|
|
|
/**
|
|
|
|
* Duplicate relations using a static property to define
|
|
|
|
* which ones we want to duplicate
|
|
|
|
*
|
|
|
|
* It may be that some relations are not diostinct to sub site so can stay
|
|
|
|
* whereas others may need to be duplicated
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function duplicateSubsiteRelations($originalPage) {
|
|
|
|
$thisClass = $originalPage->ClassName;
|
|
|
|
$relations = Config::inst()->get($thisClass, 'duplicate_to_subsite_relations');
|
|
|
|
|
|
|
|
if($relations && !empty($relations)) {
|
|
|
|
foreach($relations as $relation) {
|
|
|
|
$items = $originalPage->$relation();
|
|
|
|
foreach($items as $item) {
|
|
|
|
$duplicateItem = $item->duplicate(false);
|
|
|
|
$duplicateItem->{$thisClass.'ID'} = $this->owner->ID;
|
|
|
|
$duplicateItem->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by ContentController::init();
|
|
|
|
*/
|
|
|
|
static function contentcontrollerInit($controller) {
|
2008-11-20 00:25:43 +01:00
|
|
|
$subsite = Subsite::currentSubsite();
|
2015-06-08 23:29:51 +02:00
|
|
|
|
|
|
|
if($subsite && $subsite->Theme){
|
|
|
|
Config::inst()->update('SSViewer', 'theme', Subsite::currentSubsite()->Theme);
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2013-05-26 03:18:52 +02:00
|
|
|
|
2007-09-05 08:42:57 +02:00
|
|
|
function alternateAbsoluteLink() {
|
2009-06-18 06:53:14 +02:00
|
|
|
// Generate the existing absolute URL and replace the domain with the subsite domain.
|
|
|
|
// This helps deal with Link() returning an absolute URL.
|
|
|
|
$url = Director::absoluteURL($this->owner->Link());
|
2010-03-01 03:53:15 +01:00
|
|
|
if($this->owner->SubsiteID) {
|
|
|
|
$url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url);
|
|
|
|
}
|
|
|
|
return $url;
|
2007-09-05 08:42:57 +02:00
|
|
|
}
|
2013-05-26 03:18:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the CMS domain for iframed CMS previews to prevent single-origin violations
|
|
|
|
* and SSL cert problems.
|
|
|
|
*/
|
|
|
|
function alternatePreviewLink($action = null) {
|
|
|
|
$url = Director::absoluteURL($this->owner->Link());
|
|
|
|
if($this->owner->SubsiteID) {
|
|
|
|
$url = HTTP::setGetVar('SubsiteID', $this->owner->SubsiteID, $url);
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inject the subsite ID into the content so it can be used by frontend scripts.
|
|
|
|
*/
|
|
|
|
function MetaTags(&$tags) {
|
|
|
|
if($this->owner->SubsiteID) {
|
|
|
|
$tags .= "<meta name=\"x-subsite-id\" content=\"" . $this->owner->SubsiteID . "\" />\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
2010-03-01 04:15:07 +01:00
|
|
|
function augmentSyncLinkTracking() {
|
|
|
|
// Set LinkTracking appropriately
|
|
|
|
$links = HTTP::getLinksIn($this->owner->Content);
|
|
|
|
$linkedPages = array();
|
|
|
|
|
|
|
|
if($links) foreach($links as $link) {
|
|
|
|
if(substr($link, 0, strlen('http://')) == 'http://') {
|
|
|
|
$withoutHttp = substr($link, strlen('http://'));
|
|
|
|
if(strpos($withoutHttp, '/') && strpos($withoutHttp, '/') < strlen($withoutHttp)) {
|
|
|
|
$domain = substr($withoutHttp, 0, strpos($withoutHttp, '/'));
|
|
|
|
$rest = substr($withoutHttp, strpos($withoutHttp, '/') + 1);
|
|
|
|
|
|
|
|
$subsiteID = Subsite::getSubsiteIDForDomain($domain);
|
|
|
|
if($subsiteID == 0) continue; // We have no idea what the domain for the main site is, so cant track links to it
|
2011-09-12 12:18:56 +02:00
|
|
|
|
|
|
|
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
|
2010-03-01 04:15:07 +01:00
|
|
|
Subsite::disable_subsite_filter(true);
|
2014-08-22 13:21:18 +02:00
|
|
|
$candidatePage = DataObject::get_one("SiteTree", "\"URLSegment\" = '" . Convert::raw2sql(urldecode( $rest)) . "' AND \"SubsiteID\" = " . $subsiteID, false);
|
2011-09-12 12:18:56 +02:00
|
|
|
Subsite::disable_subsite_filter($origDisableSubsiteFilter);
|
2010-03-01 04:15:07 +01:00
|
|
|
|
|
|
|
if($candidatePage) {
|
|
|
|
$linkedPages[] = $candidatePage->ID;
|
|
|
|
} else {
|
|
|
|
$this->owner->HasBrokenLink = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->owner->CrossSubsiteLinkTracking()->setByIDList($linkedPages);
|
|
|
|
}
|
2010-03-01 23:32:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a piece of text to keep DataObject cache keys appropriately specific
|
|
|
|
*/
|
|
|
|
function cacheKeyComponent() {
|
|
|
|
return 'subsite-'.Subsite::currentSubsiteID();
|
|
|
|
}
|
2011-08-30 18:58:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
* @return boolean|null
|
|
|
|
*/
|
|
|
|
function canCreate($member = null) {
|
|
|
|
// Typically called on a singleton, so we're not using the Subsite() relation
|
|
|
|
$subsite = Subsite::currentSubsite();
|
|
|
|
if($subsite && $subsite->exists() && $subsite->PageTypeBlacklist) {
|
|
|
|
$blacklisted = explode(',', $subsite->PageTypeBlacklist);
|
|
|
|
// All subclasses need to be listed explicitly
|
|
|
|
if(in_array($this->owner->class, $blacklisted)) return false;
|
|
|
|
}
|
|
|
|
}
|
2013-05-01 02:35:17 +02:00
|
|
|
}
|