silverstripe-subsites/code/extensions/SiteTreeSubsites.php

436 lines
14 KiB
PHP
Raw Normal View History

<?php
2017-05-29 13:44:19 +02:00
namespace SilverStripe\Subsites\Extensions;
2017-05-24 15:26:28 +02:00
use SilverStripe\CMS\Model\SiteTree;
2016-09-22 16:38:29 +02:00
use SilverStripe\Control\Controller;
2017-05-24 15:26:28 +02:00
use SilverStripe\Control\Director;
2016-09-22 16:38:29 +02:00
use SilverStripe\Control\HTTP;
use SilverStripe\Core\Config\Config;
2017-06-01 15:57:53 +02:00
use SilverStripe\Core\Convert;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\InlineFormAction;
use SilverStripe\Forms\ToggleCompositeField;
2016-09-22 16:38:29 +02:00
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\Security\Member;
use SilverStripe\SiteConfig\SiteConfig;
2017-05-29 13:44:19 +02:00
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\SSViewer;
2017-05-29 13:44:19 +02:00
2016-09-22 16:38:29 +02:00
/**
* Extension for the SiteTree object to add subsites support
*/
2017-05-24 15:26:28 +02:00
class SiteTreeSubsites extends DataExtension
{
2017-05-29 13:44:19 +02:00
private static $has_one = [
'Subsite' => Subsite::class, // The subsite that this page belongs to
];
2017-05-24 15:26:28 +02:00
2017-06-01 15:57:53 +02:00
private static $many_many = [
2017-05-29 13:42:42 +02:00
'CrossSubsiteLinkTracking' => SiteTree::class // Stored separately, as the logic for URL rewriting is different
2017-06-01 15:57:53 +02:00
];
2017-05-24 15:26:28 +02:00
2017-06-01 15:57:53 +02:00
private static $many_many_extraFields = [
'CrossSubsiteLinkTracking' => ['FieldName' => 'Varchar']
];
2017-05-24 15:26:28 +02:00
2017-05-29 13:42:42 +02:00
public function isMainSite()
2017-05-24 15:26:28 +02:00
{
return $this->owner->SubsiteID == 0;
2017-05-24 15:26:28 +02:00
}
/**
* Update any requests to limit the results to the current site
* @param SQLSelect $query
* @param DataQuery $dataQuery
*/
2017-05-24 15:26:28 +02:00
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
{
if (Subsite::$disable_subsite_filter) {
return;
}
2017-05-29 13:42:42 +02:00
if ($dataQuery && $dataQuery->getQueryParam('Subsite.filter') === false) {
2017-05-24 15:26:28 +02:00
return;
}
// 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;
}
if (Subsite::$force_subsite) {
$subsiteID = Subsite::$force_subsite;
} else {
/*if($context = DataObject::context_obj()) $subsiteID = (int)$context->SubsiteID;
2017-06-01 15:57:53 +02:00
else */
$subsiteID = (int)Subsite::currentSubsiteID();
2017-05-24 15:26:28 +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...
2017-05-29 13:44:19 +02:00
$siteTreeTableName = SiteTree::getSchema()->tableName(SiteTree::class);
if (strpos($tableName, $siteTreeTableName) === false) {
2017-05-24 15:26:28 +02:00
break;
}
$query->addWhere("\"$tableName\".\"SubsiteID\" IN ($subsiteID)");
break;
}
}
2017-05-29 13:42:42 +02:00
public function onBeforeWrite()
2017-05-24 15:26:28 +02:00
{
if (!$this->owner->ID && !$this->owner->SubsiteID) {
$this->owner->SubsiteID = Subsite::currentSubsiteID();
}
parent::onBeforeWrite();
}
2017-05-29 13:42:42 +02:00
public function updateCMSFields(FieldList $fields)
2017-05-24 15:26:28 +02:00
{
$subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain');
2017-06-01 15:57:53 +02:00
$subsitesMap = [];
if ($subsites && $subsites->count()) {
2017-05-29 13:42:42 +02:00
$subsitesToMap = $subsites->exclude('ID', $this->owner->SubsiteID);
$subsitesMap = $subsitesToMap->map('ID', 'Title');
2017-05-24 15:26:28 +02:00
}
// Master page edit field (only allowed from default subsite to avoid inconsistent relationships)
$isDefaultSubsite = $this->owner->SubsiteID == 0 || $this->owner->Subsite()->DefaultSite;
2017-05-29 13:42:42 +02:00
2017-05-24 15:26:28 +02:00
if ($isDefaultSubsite && $subsitesMap) {
2017-05-29 13:42:42 +02:00
$fields->addFieldsToTab(
2017-05-24 15:26:28 +02:00
'Root.Main',
2017-05-29 13:42:42 +02:00
ToggleCompositeField::create('SubsiteOperations',
_t('SiteTreeSubsites.SubsiteOperations', 'Subsite Operations'),
2017-06-01 15:57:53 +02:00
[
new DropdownField('CopyToSubsiteID', _t('SiteTreeSubsites.CopyToSubsite',
'Copy page to subsite'), $subsitesMap),
2017-06-01 15:57:53 +02:00
new CheckboxField('CopyToSubsiteWithChildren',
_t('SiteTreeSubsites.CopyToSubsiteWithChildren', 'Include children pages?')),
$copyAction = new InlineFormAction(
'copytosubsite',
_t('SiteTreeSubsites.CopyAction', 'Copy')
)
2017-06-01 15:57:53 +02:00
]
)->setHeadingLevel(4)
2017-05-24 15:26:28 +02:00
);
2017-05-29 13:42:42 +02:00
// $copyAction->includeDefaultJS(false);
2017-05-24 15:26:28 +02:00
}
// replace readonly link prefix
$subsite = $this->owner->Subsite();
2017-05-29 13:42:42 +02:00
$nested_urls_enabled = Config::inst()->get(SiteTree::class, 'nested_urls');
if ($subsite && $subsite->exists()) {
// Use baseurl from domain
$baseLink = $subsite->absoluteBaseURL();
// Add parent page if enabled
2017-06-01 15:57:53 +02:00
if ($nested_urls_enabled && $this->owner->ParentID) {
2017-05-29 13:42:42 +02:00
$baseLink = Controller::join_links(
$baseLink,
$this->owner->Parent()->RelativeLink(true)
);
}
2017-05-24 15:26:28 +02:00
$urlsegment = $fields->dataFieldByName('URLSegment');
$urlsegment->setURLPrefix($baseLink);
}
}
2017-05-29 13:42:42 +02:00
/**
* @return SiteConfig
*/
public function alternateSiteConfig()
2017-05-24 15:26:28 +02:00
{
if (!$this->owner->SubsiteID) {
return false;
}
2017-05-29 13:42:42 +02:00
$sc = DataObject::get_one(SiteConfig::class, '"SubsiteID" = ' . $this->owner->SubsiteID);
2017-05-24 15:26:28 +02:00
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;
}
/**
* 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"
*
2017-06-01 15:10:07 +02:00
* @param null $member
* @return bool
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function canEdit($member = null)
2017-05-24 15:26:28 +02:00
{
if (!$member) {
$member = Member::currentUser();
}
// Find the sites that this user has access to
$goodSites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true, 'all', $member)->column('ID');
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();
}
// Return true if they have access to this object's site
if (!(in_array(0, $goodSites) || in_array($subsiteID, $goodSites))) {
return false;
}
}
/**
2017-06-01 15:10:07 +02:00
* @param null $member
* @return bool
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function canDelete($member = null)
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
if (!$member && $member !== false) {
2017-05-24 15:26:28 +02:00
$member = Member::currentUser();
}
return $this->canEdit($member);
}
/**
2017-06-01 15:10:07 +02:00
* @param null $member
* @return bool
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function canAddChildren($member = null)
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
if (!$member && $member !== false) {
2017-05-24 15:26:28 +02:00
$member = Member::currentUser();
}
return $this->canEdit($member);
}
/**
2017-06-01 15:10:07 +02:00
* @param null $member
* @return bool
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function canPublish($member = null)
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
if (!$member && $member !== false) {
2017-05-24 15:26:28 +02:00
$member = Member::currentUser();
}
return $this->canEdit($member);
}
/**
2017-05-29 13:42:42 +02:00
* Create a duplicate of this page and save it to another subsite
*
* @param int|Subsite $subsiteID The Subsite to copy to, or its ID
* @param bool $includeChildren Recursively copy child Pages.
* @param int $parentID Where to place the Page in the SiteTree's structure.
*
* @return SiteTree duplicated page
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function duplicateToSubsite($subsiteID = null, $includeChildren = false, $parentID = 0)
2017-05-24 15:26:28 +02:00
{
2017-05-29 13:42:42 +02:00
if ($subsiteID instanceof Subsite) {
2017-05-24 15:26:28 +02:00
$subsiteID = $subsiteID->ID;
}
$oldSubsite = Subsite::currentSubsiteID();
2017-05-29 13:42:42 +02:00
2017-05-24 15:26:28 +02:00
if ($subsiteID) {
Subsite::changeSubsite($subsiteID);
} else {
$subsiteID = $oldSubsite;
}
2017-05-29 13:42:42 +02:00
$page = $this->owner->duplicate(false);
$page->CheckedPublicationDifferences = $page->AddedToStage = true;
2017-05-24 15:26:28 +02:00
$subsiteID = ($subsiteID ? $subsiteID : $oldSubsite);
2017-05-29 13:42:42 +02:00
$page->SubsiteID = $subsiteID;
$page->ParentID = $parentID;
2017-05-24 15:26:28 +02:00
// MasterPageID is here for legacy purposes, to satisfy the subsites_relatedpages module
2017-05-29 13:42:42 +02:00
$page->MasterPageID = $this->owner->ID;
$page->write();
2017-05-24 15:26:28 +02:00
2017-05-29 13:42:42 +02:00
Subsite::changeSubsite($oldSubsite);
2017-05-24 15:26:28 +02:00
2017-06-01 15:57:53 +02:00
if ($includeChildren) {
foreach ($this->owner->AllChildren() as $child) {
$child->duplicateToSubsite($subsiteID, $includeChildren, $page->ID);
}
}
2017-05-29 13:42:42 +02:00
return $page;
2017-05-24 15:26:28 +02:00
}
/**
* Called by ContentController::init();
2017-06-01 15:10:07 +02:00
* @param $controller
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public static function contentcontrollerInit($controller)
2017-05-24 15:26:28 +02:00
{
$subsite = Subsite::currentSubsite();
if ($subsite && $subsite->Theme) {
2017-05-29 13:44:19 +02:00
Config::modify()->set(SSViewer::class, 'theme', Subsite::currentSubsite()->Theme);
2017-05-24 15:26:28 +02:00
}
}
2017-05-29 13:42:42 +02:00
public function alternateAbsoluteLink()
2017-05-24 15:26:28 +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());
if ($this->owner->SubsiteID) {
2017-06-01 15:57:53 +02:00
$url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url);
2017-05-24 15:26:28 +02:00
}
return $url;
}
/**
* Use the CMS domain for iframed CMS previews to prevent single-origin violations
* and SSL cert problems.
2017-06-01 15:10:07 +02:00
* @param null $action
* @return string
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function alternatePreviewLink($action = null)
2017-05-24 15:26:28 +02:00
{
$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.
2017-06-01 15:10:07 +02:00
* @param $tags
* @return string
2017-05-24 15:26:28 +02:00
*/
2017-05-29 13:42:42 +02:00
public function MetaTags(&$tags)
2017-05-24 15:26:28 +02:00
{
if ($this->owner->SubsiteID) {
$tags .= '<meta name="x-subsite-id" content="' . $this->owner->SubsiteID . "\" />\n";
2017-05-24 15:26:28 +02:00
}
return $tags;
}
2017-05-29 13:42:42 +02:00
public function augmentSyncLinkTracking()
2017-05-24 15:26:28 +02:00
{
// Set LinkTracking appropriately
$links = HTTP::getLinksIn($this->owner->Content);
2017-06-01 15:57:53 +02:00
$linkedPages = [];
2017-05-24 15:26:28 +02: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);
$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
2017-06-01 15:57:53 +02:00
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
2017-05-24 15:26:28 +02:00
Subsite::disable_subsite_filter(true);
2017-06-01 15:57:53 +02:00
$candidatePage = DataObject::get_one(SiteTree::class,
"\"URLSegment\" = '" . Convert::raw2sql(urldecode($rest)) . "' AND \"SubsiteID\" = " . $subsiteID,
false);
2017-05-24 15:26:28 +02:00
Subsite::disable_subsite_filter($origDisableSubsiteFilter);
if ($candidatePage) {
$linkedPages[] = $candidatePage->ID;
} else {
$this->owner->HasBrokenLink = true;
}
}
}
}
}
$this->owner->CrossSubsiteLinkTracking()->setByIDList($linkedPages);
}
2017-05-29 13:42:42 +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;
}
2017-05-24 15:26:28 +02:00
/**
* Return a piece of text to keep DataObject cache keys appropriately specific
*/
2017-05-29 13:42:42 +02:00
public function cacheKeyComponent()
2017-05-24 15:26:28 +02:00
{
2017-06-01 15:57:53 +02:00
return 'subsite-' . Subsite::currentSubsiteID();
2017-05-24 15:26:28 +02:00
}
/**
* @param Member
* @return boolean|null
*/
2017-05-29 13:42:42 +02:00
public function canCreate($member = null)
2017-05-24 15:26:28 +02:00
{
// 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;
}
}
}
}