2007-08-16 08:38:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension for the SiteTree object to add subsites support
|
|
|
|
*/
|
2015-11-23 04:53:45 +01:00
|
|
|
class SiteTreeSubsites extends DataExtension
|
|
|
|
{
|
|
|
|
private static $has_one = array(
|
|
|
|
'Subsite' => 'Subsite', // The subsite that this page belongs to
|
|
|
|
);
|
2013-05-06 12:21:09 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
private static $many_many = array(
|
|
|
|
'CrossSubsiteLinkTracking' => 'SiteTree' // Stored separately, as the logic for URL rewriting is different
|
|
|
|
);
|
2013-05-06 12:21:09 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
private static $many_many_extraFields = array(
|
|
|
|
"CrossSubsiteLinkTracking" => array("FieldName" => "Varchar")
|
|
|
|
);
|
2013-05-06 12:21:09 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function isMainSite()
|
|
|
|
{
|
|
|
|
if ($this->owner->SubsiteID == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Update any requests to limit the results to the current site
|
|
|
|
*/
|
|
|
|
public function augmentSQL(SQLQuery &$query, DataQuery &$dataQuery = null)
|
|
|
|
{
|
|
|
|
if (Subsite::$disable_subsite_filter) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-18 07:10:11 +02:00
|
|
|
if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) {
|
2015-11-23 04:53:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
|
|
|
|
// 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)) {
|
|
|
|
if ($query->filtersOnID()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-06 12:21:09 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
if (Subsite::$force_subsite) {
|
|
|
|
$subsiteID = Subsite::$force_subsite;
|
|
|
|
} else {
|
|
|
|
/*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
|
|
|
|
else */$subsiteID = (int)Subsite::currentSubsiteID();
|
|
|
|
}
|
2014-08-26 01:37:16 +02:00
|
|
|
|
2015-11-23 04:53:45 +01: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;
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
if (!$this->owner->ID && !$this->owner->SubsiteID) {
|
|
|
|
$this->owner->SubsiteID = Subsite::currentSubsiteID();
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2010-03-02 02:15:11 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$subsites = Subsite::accessible_sites("CMS_ACCESS_CMSMain");
|
|
|
|
$subsitesMap = array();
|
|
|
|
if ($subsites && $subsites->Count()) {
|
|
|
|
$subsitesMap = $subsites->map('ID', 'Title');
|
|
|
|
unset($subsitesMap[$this->owner->SubsiteID]);
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2015-11-23 04:53:45 +01: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(
|
|
|
|
'Root.Main',
|
2016-05-27 06:35:16 +02:00
|
|
|
$copyField = new DropdownField(
|
2015-11-23 04:53:45 +01:00
|
|
|
"CopyToSubsiteID",
|
|
|
|
_t('SiteTreeSubsites.CopyToSubsite', "Copy page to subsite"),
|
|
|
|
$subsitesMap,
|
|
|
|
''
|
|
|
|
)
|
|
|
|
);
|
2016-05-27 06:35:16 +02:00
|
|
|
$copyField->setDescription(_t(
|
|
|
|
'SiteTreeSubsites.CopyToSubsiteDescription',
|
|
|
|
"Note that this page will be copied to the top level and should be re-organised if necessary."
|
|
|
|
));
|
2015-11-23 04:53:45 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
$copyAction = new InlineFormAction(
|
|
|
|
"copytosubsite",
|
|
|
|
_t('SiteTreeSubsites.CopyAction', "Copy")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$copyAction->includeDefaultJS(false);
|
|
|
|
}
|
2012-08-10 11:23:19 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// replace readonly link prefix
|
|
|
|
$subsite = $this->owner->Subsite();
|
|
|
|
$nested_urls_enabled = Config::inst()->get('SiteTree', 'nested_urls');
|
2015-11-24 05:15:08 +01:00
|
|
|
if ($subsite && $subsite->exists()) {
|
|
|
|
// Use baseurl from domain
|
|
|
|
$baseLink = $subsite->absoluteBaseURL();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-24 05:15:08 +01:00
|
|
|
// Add parent page if enabled
|
|
|
|
if($nested_urls_enabled && $this->owner->ParentID) {
|
|
|
|
$baseLink = Controller::join_links(
|
|
|
|
$baseLink,
|
|
|
|
$this->owner->Parent()->RelativeLink(true)
|
|
|
|
);
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$urlsegment = $fields->dataFieldByName('URLSegment');
|
|
|
|
$urlsegment->setURLPrefix($baseLink);
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function alternateSiteConfig()
|
|
|
|
{
|
|
|
|
if (!$this->owner->SubsiteID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$sc = DataObject::get_one('SiteConfig', '"SubsiteID" = ' . $this->owner->SubsiteID);
|
|
|
|
if (!$sc) {
|
|
|
|
$sc = new SiteConfig();
|
|
|
|
$sc->SubsiteID = $this->owner->SubsiteID;
|
|
|
|
$sc->Title = _t('Subsite.SiteConfigTitle', 'Your Site Name');
|
|
|
|
$sc->Tagline = _t('Subsite.SiteConfigSubtitle', 'Your tagline here');
|
|
|
|
$sc->write();
|
|
|
|
}
|
|
|
|
return $sc;
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +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"
|
2016-05-23 00:41:51 +02:00
|
|
|
*
|
2015-11-23 04:53:45 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
|
|
|
if (!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Find the sites that this user has access to
|
|
|
|
$goodSites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true, 'all', $member)->column('ID');
|
2013-05-01 02:35:17 +02:00
|
|
|
|
2015-11-23 04:53:45 +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();
|
|
|
|
}
|
2014-01-09 21:28:35 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// Return true if they have access to this object's site
|
|
|
|
if (!(in_array(0, $goodSites) || in_array($subsiteID, $goodSites))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canDelete($member = null)
|
|
|
|
{
|
|
|
|
if (!$member && $member !== false) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canAddChildren($member = null)
|
|
|
|
{
|
|
|
|
if (!$member && $member !== false) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canPublish($member = null)
|
|
|
|
{
|
|
|
|
if (!$member && $member !== false) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
2014-01-09 21:28:35 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2014-01-09 21:28:35 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Create a duplicate of this page and save it to another subsite
|
2016-05-27 06:35:16 +02:00
|
|
|
*
|
|
|
|
* @param int|Subsite $subsiteID The Subsite to copy to, or its ID
|
|
|
|
* @return SiteTree duplicated page
|
2015-11-23 04:53:45 +01:00
|
|
|
*/
|
|
|
|
public function duplicateToSubsite($subsiteID = null)
|
|
|
|
{
|
2016-05-27 06:35:16 +02:00
|
|
|
if ($subsiteID instanceof Subsite) {
|
|
|
|
$subsiteID = $subsiteID->ID;
|
2015-11-23 04:53:45 +01:00
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2016-05-27 06:35:16 +02:00
|
|
|
$oldSubsite = Subsite::currentSubsiteID();
|
2015-11-23 04:53:45 +01:00
|
|
|
if ($subsiteID) {
|
|
|
|
Subsite::changeSubsite($subsiteID);
|
|
|
|
} else {
|
2016-05-27 06:35:16 +02:00
|
|
|
$subsiteID = $oldSubsite;
|
2015-11-23 04:53:45 +01:00
|
|
|
}
|
2008-11-26 05:03:14 +01:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$page = $this->owner->duplicate(false);
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$page->CheckedPublicationDifferences = $page->AddedToStage = true;
|
|
|
|
$subsiteID = ($subsiteID ? $subsiteID : $oldSubsite);
|
|
|
|
$page->SubsiteID = $subsiteID;
|
2012-07-16 01:14:13 +02:00
|
|
|
|
2016-05-27 06:35:16 +02:00
|
|
|
// Remove parent ID, since this parent belongs to another subsite
|
|
|
|
$page->ParentID = 0;
|
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
// MasterPageID is here for legacy purposes, to satisfy the subsites_relatedpages module
|
|
|
|
$page->MasterPageID = $this->owner->ID;
|
|
|
|
$page->write();
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
Subsite::changeSubsite($oldSubsite);
|
2013-05-01 02:35:17 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
return $page;
|
|
|
|
}
|
2012-07-16 01:14:13 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Called by ContentController::init();
|
|
|
|
*/
|
|
|
|
public static function contentcontrollerInit($controller)
|
|
|
|
{
|
|
|
|
$subsite = Subsite::currentSubsite();
|
2012-07-16 01:14:13 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
if ($subsite && $subsite->Theme) {
|
|
|
|
Config::inst()->update('SSViewer', 'theme', Subsite::currentSubsite()->Theme);
|
|
|
|
}
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function alternateAbsoluteLink()
|
|
|
|
{
|
|
|
|
// 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());
|
|
|
|
if ($this->owner->SubsiteID) {
|
|
|
|
$url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url);
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
2015-06-08 23:29:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Use the CMS domain for iframed CMS previews to prevent single-origin violations
|
|
|
|
* and SSL cert problems.
|
|
|
|
*/
|
|
|
|
public function alternatePreviewLink($action = null)
|
|
|
|
{
|
|
|
|
$url = Director::absoluteURL($this->owner->Link());
|
|
|
|
if ($this->owner->SubsiteID) {
|
|
|
|
$url = HTTP::setGetVar('SubsiteID', $this->owner->SubsiteID, $url);
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
2013-05-26 03:18:52 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Inject the subsite ID into the content so it can be used by frontend scripts.
|
|
|
|
*/
|
|
|
|
public function MetaTags(&$tags)
|
|
|
|
{
|
|
|
|
if ($this->owner->SubsiteID) {
|
|
|
|
$tags .= "<meta name=\"x-subsite-id\" content=\"" . $this->owner->SubsiteID . "\" />\n";
|
|
|
|
}
|
2013-05-26 03:18:52 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
return $tags;
|
|
|
|
}
|
2013-05-26 03:18:52 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
public function augmentSyncLinkTracking()
|
|
|
|
{
|
|
|
|
// Set LinkTracking appropriately
|
|
|
|
$links = HTTP::getLinksIn($this->owner->Content);
|
|
|
|
$linkedPages = array();
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
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);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$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
|
2013-05-26 03:18:52 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
|
|
|
|
Subsite::disable_subsite_filter(true);
|
|
|
|
$candidatePage = DataObject::get_one("SiteTree", "\"URLSegment\" = '" . Convert::raw2sql(urldecode($rest)) . "' AND \"SubsiteID\" = " . $subsiteID, false);
|
|
|
|
Subsite::disable_subsite_filter($origDisableSubsiteFilter);
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
if ($candidatePage) {
|
|
|
|
$linkedPages[] = $candidatePage->ID;
|
|
|
|
} else {
|
|
|
|
$this->owner->HasBrokenLink = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
$this->owner->CrossSubsiteLinkTracking()->setByIDList($linkedPages);
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2016-05-27 06:35:16 +02:00
|
|
|
/**
|
|
|
|
* Ensure that valid url segments are checked within the correct subsite of the owner object,
|
|
|
|
* even if the current subsiteID is set to some other subsite.
|
|
|
|
*
|
|
|
|
* @return null|bool Either true or false, or null to not influence result
|
|
|
|
*/
|
|
|
|
public function augmentValidURLSegment()
|
|
|
|
{
|
|
|
|
// If this page is being filtered in the current subsite, then no custom validation query is required.
|
|
|
|
$subsite = Subsite::$force_subsite ?: Subsite::currentSubsiteID();
|
|
|
|
if (empty($this->owner->SubsiteID) || $subsite == $this->owner->SubsiteID) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup forced subsite
|
|
|
|
$prevForceSubsite = Subsite::$force_subsite;
|
|
|
|
Subsite::$force_subsite = $this->owner->SubsiteID;
|
|
|
|
|
|
|
|
// Repeat validation in the correct subsite
|
|
|
|
$isValid = $this->owner->validURLSegment();
|
|
|
|
|
|
|
|
// Restore
|
|
|
|
Subsite::$force_subsite = $prevForceSubsite;
|
|
|
|
|
|
|
|
return (bool)$isValid;
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* Return a piece of text to keep DataObject cache keys appropriately specific
|
|
|
|
*/
|
|
|
|
public function cacheKeyComponent()
|
|
|
|
{
|
|
|
|
return 'subsite-'.Subsite::currentSubsiteID();
|
|
|
|
}
|
2016-05-23 00:41:51 +02:00
|
|
|
|
2015-11-23 04:53:45 +01:00
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
* @return boolean|null
|
|
|
|
*/
|
|
|
|
public 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
|
|
|
}
|