silverstripe-subsites/code/extensions/SiteTreeSubsites.php

478 lines
16 KiB
PHP
Raw Normal View History

<?php
2017-05-29 13:44:19 +02:00
namespace SilverStripe\Subsites\Extensions;
use Page;
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\FormAction;
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\Security;
use SilverStripe\SiteConfig\SiteConfig;
2017-05-29 13:44:19 +02:00
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState;
use SilverStripe\View\SSViewer;
2017-05-29 13:44:19 +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;
}
$subsiteID = null;
2017-05-24 15:26:28 +02:00
if (Subsite::$force_subsite) {
$subsiteID = Subsite::$force_subsite;
} else {
$subsiteID = SubsiteState::singleton()->getSubsiteId();
}
if ($subsiteID === null) {
return;
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 = SubsiteState::singleton()->getSubsiteId();
2017-05-24 15:26:28 +02:00
}
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) {
$fields->addFieldToTab(
2017-05-24 15:26:28 +02:00
'Root.Main',
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),
new CheckboxField(
'CopyToSubsiteWithChildren',
_t('SiteTreeSubsites.CopyToSubsiteWithChildren', 'Include children pages?')
),
$copyAction = new FormAction(
'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
// @todo check if this needs re-implementation
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);
}
}
/**
* Does the basic duplication, but doesn't write anything
* this means we can subclass this easier and do more complex
* relation duplication.
*/
public function duplicateToSubsitePrep($subsiteID)
{
if (is_object($subsiteID)) {
$subsiteID = $subsiteID->ID;
}
$oldSubsite = SubsiteState::singleton()->getSubsiteId();
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;
$subsiteID = ($subsiteID ? $subsiteID : $oldSubsite);
$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;
}
}
// MasterPageID is here for legacy purposes, to satisfy the subsites_relatedpages module
$clone->MasterPageID = $this->owner->ID;
return $clone;
}
/**
* 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;
}
/**
* 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();
}
}
}
}
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 = Security::getCurrentUser();
2017-05-24 15:26:28 +02:00
}
// 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 = SubsiteState::singleton()->getSubsiteId();
2017-05-24 15:26:28 +02:00
}
// 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) {
$member = Security::getCurrentUser();
2017-05-24 15:26:28 +02:00
}
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) {
$member = Security::getCurrentUser();
2017-05-24 15:26:28 +02:00
}
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) {
$member = Security::getCurrentUser();
2017-05-24 15:26:28 +02:00
}
return $this->canEdit($member);
}
/**
* 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) {
SSViewer::set_themes(array_merge([$subsite->Theme], SSViewer::get_themes()));
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);
$candidatePage = DataObject::get_one(
SiteTree::class,
2017-06-01 15:57:53 +02:00
"\"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 ?: SubsiteState::singleton()->getSubsiteId();
2017-05-29 13:42:42 +02:00
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
{
return 'subsite-' . SubsiteState::singleton()->getSubsiteId();
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(get_class($this->owner), $blacklisted)) {
2017-05-24 15:26:28 +02:00
return false;
}
}
}
}