2007-08-16 08:38:29 +02:00
|
|
|
<?php
|
|
|
|
|
2017-05-29 13:44:19 +02:00
|
|
|
namespace SilverStripe\Subsites\Extensions;
|
|
|
|
|
2017-08-30 01:06:07 +02:00
|
|
|
use Page;
|
2019-05-31 00:28:36 +02:00
|
|
|
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField;
|
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;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-06-04 05:05:59 +02:00
|
|
|
use SilverStripe\Forms\FormAction;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\Forms\ToggleCompositeField;
|
2017-09-01 02:33:16 +02:00
|
|
|
use SilverStripe\i18n\i18n;
|
2018-03-26 12:23:19 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\ORM\DataExtension;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DataQuery;
|
2018-03-26 12:23:19 +02:00
|
|
|
use SilverStripe\ORM\Map;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
2018-03-15 04:34:13 +01:00
|
|
|
use SilverStripe\Security\Member;
|
2017-08-30 02:04:43 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\SiteConfig\SiteConfig;
|
2017-05-29 13:44:19 +02:00
|
|
|
use SilverStripe\Subsites\Model\Subsite;
|
2019-05-31 06:18:52 +02:00
|
|
|
use SilverStripe\Subsites\Service\ThemeResolver;
|
2017-08-30 01:06:07 +02:00
|
|
|
use SilverStripe\Subsites\State\SubsiteState;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2022-12-13 22:43:53 +01:00
|
|
|
use SilverStripe\VersionedAdmin\Controllers\HistoryViewerController;
|
2017-05-29 13:44:19 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
/**
|
|
|
|
* Extension for the SiteTree object to add subsites support
|
2023-12-14 03:21:15 +01:00
|
|
|
* @method SilverStripe\ORM\ManyManyList<SiteTree> CrossSubsiteLinkTracking()
|
|
|
|
* @method Subsite Subsite()
|
2007-08-16 08:38:29 +02:00
|
|
|
*/
|
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
|
|
|
{
|
2017-06-01 15:48:01 +02:00
|
|
|
return $this->owner->SubsiteID == 0;
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +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...
|
2018-02-01 01:19:02 +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)) {
|
2017-05-24 15:26:28 +02:00
|
|
|
if ($query->filtersOnID()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-26 21:55:04 +01:00
|
|
|
$subsiteID = SubsiteState::singleton()->getSubsiteId();
|
2017-08-30 01:06:07 +02:00
|
|
|
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);
|
2022-04-13 03:49:48 +02:00
|
|
|
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) {
|
2017-08-30 01:06:07 +02:00
|
|
|
$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
|
|
|
{
|
2017-06-01 14:49:55 +02:00
|
|
|
$subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain');
|
2017-06-01 14:44:09 +02:00
|
|
|
if ($subsites && $subsites->count()) {
|
2017-05-29 13:42:42 +02:00
|
|
|
$subsitesToMap = $subsites->exclude('ID', $this->owner->SubsiteID);
|
2018-02-01 03:41:54 +01:00
|
|
|
$subsitesMap = $subsitesToMap->map('ID', 'Title');
|
2018-03-26 12:23:19 +02:00
|
|
|
} else {
|
|
|
|
$subsitesMap = new Map(ArrayList::create());
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2022-12-13 22:43:53 +01:00
|
|
|
$viewingPageHistory = Controller::has_curr() && Controller::curr() instanceof HistoryViewerController;
|
|
|
|
|
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
|
|
|
|
2022-12-13 22:43:53 +01:00
|
|
|
if ($isDefaultSubsite && $subsitesMap->count() && !$viewingPageHistory) {
|
2017-06-04 05:05:59 +02:00
|
|
|
$fields->addFieldToTab(
|
2017-05-24 15:26:28 +02:00
|
|
|
'Root.Main',
|
2017-08-29 07:07:24 +02:00
|
|
|
ToggleCompositeField::create(
|
|
|
|
'SubsiteOperations',
|
2017-09-07 06:38:20 +02:00
|
|
|
_t(__CLASS__ . '.SubsiteOperations', 'Subsite Operations'),
|
2017-06-01 15:57:53 +02:00
|
|
|
[
|
2018-01-30 04:21:45 +01:00
|
|
|
DropdownField::create('CopyToSubsiteID', _t(
|
2017-09-07 06:38:20 +02:00
|
|
|
__CLASS__ . '.CopyToSubsite',
|
2017-08-29 07:07:24 +02:00
|
|
|
'Copy page to subsite'
|
2018-02-01 03:41:54 +01:00
|
|
|
), $subsitesMap),
|
2018-01-30 04:21:45 +01:00
|
|
|
CheckboxField::create(
|
2017-08-29 07:07:24 +02:00
|
|
|
'CopyToSubsiteWithChildren',
|
2017-09-07 06:38:20 +02:00
|
|
|
_t(__CLASS__ . '.CopyToSubsiteWithChildren', 'Include children pages?')
|
2017-08-29 07:07:24 +02:00
|
|
|
),
|
2018-01-30 04:21:45 +01:00
|
|
|
$copyAction = FormAction::create(
|
2017-06-01 14:49:55 +02:00
|
|
|
'copytosubsite',
|
2017-09-07 06:38:20 +02:00
|
|
|
_t(__CLASS__ . '.CopyAction', 'Copy')
|
2017-04-23 22:23:34 +02:00
|
|
|
)
|
2017-06-01 15:57:53 +02:00
|
|
|
]
|
2017-04-23 22:23:34 +02:00
|
|
|
)->setHeadingLevel(4)
|
2017-05-24 15:26:28 +02:00
|
|
|
);
|
2017-05-29 13:42:42 +02:00
|
|
|
|
2018-01-30 04:21:45 +01:00
|
|
|
$copyAction->addExtraClass('btn btn-primary font-icon-save ml-3');
|
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');
|
2019-05-31 00:28:36 +02:00
|
|
|
/** @var Subsite $subsite */
|
2017-05-29 13:42:42 +02:00
|
|
|
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');
|
2019-05-31 00:28:36 +02:00
|
|
|
if ($urlsegment && $urlsegment instanceof SiteTreeURLSegmentField) {
|
2017-12-14 02:17:30 +01:00
|
|
|
$urlsegment->setURLPrefix($baseLink);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 06:39:32 +02:00
|
|
|
/**
|
2018-08-24 00:12:05 +02:00
|
|
|
* Does the basic duplication, but doesn't write anything this means we can subclass this easier and do more
|
|
|
|
* complex relation duplication.
|
|
|
|
*
|
|
|
|
* Note that when duplicating including children, everything is written.
|
|
|
|
*
|
|
|
|
* @param Subsite|int $subsiteID
|
|
|
|
* @param bool $includeChildren
|
|
|
|
* @return SiteTree
|
2017-08-02 06:39:32 +02:00
|
|
|
*/
|
2018-08-24 00:12:05 +02:00
|
|
|
public function duplicateToSubsitePrep($subsiteID, $includeChildren)
|
2017-08-02 06:39:32 +02:00
|
|
|
{
|
|
|
|
if (is_object($subsiteID)) {
|
|
|
|
$subsiteID = $subsiteID->ID;
|
|
|
|
}
|
|
|
|
|
2018-08-24 00:12:05 +02:00
|
|
|
return SubsiteState::singleton()
|
|
|
|
->withState(function (SubsiteState $newState) use ($subsiteID, $includeChildren) {
|
|
|
|
$newState->setSubsiteId($subsiteID);
|
|
|
|
|
|
|
|
/** @var SiteTree $page */
|
|
|
|
$page = $this->owner;
|
|
|
|
|
|
|
|
try {
|
2018-08-24 00:30:27 +02:00
|
|
|
// We have no idea what the ParentID should be, but it shouldn't be the same as it was since
|
|
|
|
// we're now in a different subsite. As a workaround use the url-segment and subsite ID.
|
|
|
|
if ($page->Parent()) {
|
|
|
|
$parentSeg = $page->Parent()->URLSegment;
|
|
|
|
$newParentPage = Page::get()->filter('URLSegment', $parentSeg)->first();
|
|
|
|
$originalParentID = $page->ParentID;
|
|
|
|
if ($newParentPage) {
|
|
|
|
$page->ParentID = (int) $newParentPage->ID;
|
|
|
|
} else {
|
|
|
|
// reset it to the top level, so the user can decide where to put it
|
|
|
|
$page->ParentID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disable query filtering by subsite during actual duplication
|
2018-08-24 00:12:05 +02:00
|
|
|
$originalFilter = Subsite::$disable_subsite_filter;
|
|
|
|
Subsite::disable_subsite_filter(true);
|
|
|
|
|
|
|
|
return $includeChildren ? $page->duplicateWithChildren() : $page->duplicate(false);
|
|
|
|
} finally {
|
|
|
|
Subsite::disable_subsite_filter($originalFilter);
|
2018-08-24 00:30:27 +02:00
|
|
|
|
|
|
|
// Re-set the original parent ID for the current page
|
|
|
|
$page->ParentID = $originalParentID;
|
2018-08-24 00:12:05 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When duplicating a page, assign the current subsite ID from the state
|
|
|
|
*/
|
|
|
|
public function onBeforeDuplicate()
|
|
|
|
{
|
|
|
|
$subsiteId = SubsiteState::singleton()->getSubsiteId();
|
|
|
|
if ($subsiteId !== null) {
|
|
|
|
$this->owner->SubsiteID = $subsiteId;
|
2017-08-02 06:39:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a duplicate of this page and save it to another subsite
|
2018-08-24 00:12:05 +02:00
|
|
|
*
|
|
|
|
* @param Subsite|int $subsiteID The Subsite to copy to, or its ID
|
|
|
|
* @param boolean $includeChildren Whether to duplicate child pages too
|
|
|
|
* @return SiteTree The duplicated page
|
2017-08-02 06:39:32 +02:00
|
|
|
*/
|
2018-08-24 00:12:05 +02:00
|
|
|
public function duplicateToSubsite($subsiteID = null, $includeChildren = false)
|
2017-08-02 06:39:32 +02:00
|
|
|
{
|
2022-11-28 05:49:50 +01:00
|
|
|
/** @var SiteTree|SiteTreeSubsites */
|
2018-08-24 00:12:05 +02:00
|
|
|
$clone = $this->owner->duplicateToSubsitePrep($subsiteID, $includeChildren);
|
2017-08-02 06:39:32 +02:00
|
|
|
$clone->invokeWithExtensions('onBeforeDuplicateToSubsite', $this->owner);
|
2018-08-24 00:12:05 +02:00
|
|
|
|
|
|
|
if (!$includeChildren) {
|
|
|
|
// Write the new page if "include children" is false, because it is written by default when it's true.
|
|
|
|
$clone->write();
|
|
|
|
}
|
|
|
|
// Deprecated: manually duplicate any configured relationships
|
2017-08-02 06:39:32 +02:00
|
|
|
$clone->duplicateSubsiteRelations($this->owner);
|
2018-08-24 00:12:05 +02:00
|
|
|
|
2017-08-02 06:39:32 +02:00
|
|
|
$clone->invokeWithExtensions('onAfterDuplicateToSubsite', $this->owner);
|
2018-08-24 00:12:05 +02:00
|
|
|
|
2017-08-02 06:39:32 +02:00
|
|
|
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
|
|
|
|
*
|
2022-11-28 05:49:50 +01:00
|
|
|
* This was originally deprecated - Use the "cascade_duplicates" config API instead
|
|
|
|
* Ideally this would be re-deprecated
|
|
|
|
*
|
2018-08-24 00:13:22 +02:00
|
|
|
* @param SiteTree $originalPage
|
2017-08-02 06:39:32 +02:00
|
|
|
*/
|
|
|
|
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;
|
2017-09-07 06:38:20 +02:00
|
|
|
$sc->Title = _t('SilverStripe\\Subsites\\Model\\Subsite.SiteConfigTitle', 'Your Site Name');
|
|
|
|
$sc->Tagline = _t('SilverStripe\\Subsites\\Model\\Subsite.SiteConfigSubtitle', 'Your tagline here');
|
2017-05-24 15:26:28 +02:00
|
|
|
$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"
|
|
|
|
*
|
2018-06-19 05:43:09 +02:00
|
|
|
* If there are no subsites configured yet, this logic is skipped.
|
|
|
|
*
|
|
|
|
* @param Member|null $member
|
|
|
|
* @return bool|null
|
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) {
|
2017-08-30 02:04:43 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 05:43:09 +02:00
|
|
|
// Do not provide any input if there are no subsites configured
|
2018-06-19 23:27:04 +02:00
|
|
|
if (!Subsite::get()->exists()) {
|
2018-06-19 05:43:09 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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.
|
2017-08-30 01:06:07 +02:00
|
|
|
$subsiteID = SubsiteState::singleton()->getSubsiteId();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if they have access to this object's site
|
2022-04-13 03:49:48 +02:00
|
|
|
if (!(in_array(0, $goodSites ?? []) || in_array($subsiteID, $goodSites ?? []))) {
|
2017-05-24 15:26:28 +02:00
|
|
|
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-08-30 02:04:43 +02:00
|
|
|
$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) {
|
2017-08-30 02:04:43 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-19 05:43:09 +02:00
|
|
|
* @param Member|null $member
|
|
|
|
* @return bool|null
|
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-08-30 02:04:43 +02:00
|
|
|
$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
|
|
|
{
|
2019-05-31 06:18:52 +02:00
|
|
|
/** @var Subsite $subsite */
|
2017-05-24 15:26:28 +02:00
|
|
|
$subsite = Subsite::currentSubsite();
|
|
|
|
|
|
|
|
if ($subsite && $subsite->Theme) {
|
2019-05-31 06:18:52 +02:00
|
|
|
SSViewer::set_themes(ThemeResolver::singleton()->getThemeList($subsite));
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
2017-08-10 20:14:59 +02:00
|
|
|
|
2022-07-28 02:19:33 +02:00
|
|
|
$ignore_subsite_locale = Config::inst()->get(self::class, 'ignore_subsite_locale');
|
|
|
|
|
|
|
|
if (!$ignore_subsite_locale
|
|
|
|
&& $subsite
|
|
|
|
&& $subsite->Language
|
|
|
|
&& i18n::getData()->validate($subsite->Language)
|
|
|
|
) {
|
2017-08-10 20:14:59 +02:00
|
|
|
i18n::set_locale($subsite->Language);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 15:56:42 +02:00
|
|
|
/**
|
|
|
|
* @param null $action
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function alternateAbsoluteLink($action = null)
|
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.
|
2017-09-07 15:56:42 +02:00
|
|
|
$url = Director::absoluteURL($this->owner->Link($action));
|
2017-05-24 15:26:28 +02:00
|
|
|
if ($this->owner->SubsiteID) {
|
2023-01-26 21:55:04 +01: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
|
2018-07-04 04:43:01 +02:00
|
|
|
* and SSL cert problems. Always set SubsiteID to avoid errors because a page doesn't
|
|
|
|
* exist on the CMS domain.
|
|
|
|
*
|
|
|
|
* @param string &$link
|
|
|
|
* @param string|null $action
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function updatePreviewLink(&$link, $action = null)
|
|
|
|
{
|
|
|
|
$url = Director::absoluteURL($this->owner->Link($action));
|
|
|
|
$link = HTTP::setGetVar('SubsiteID', $this->owner->SubsiteID, $url);
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-06-01 14:49:55 +02:00
|
|
|
$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) {
|
2022-04-13 03:49:48 +02:00
|
|
|
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);
|
2017-05-24 15:26:28 +02: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
|
|
|
|
|
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);
|
2020-01-09 01:14:07 +01:00
|
|
|
$candidatePage = SiteTree::get()->filter([
|
|
|
|
'URLSegment' => urldecode($rest),
|
|
|
|
'SubsiteID' => $subsiteID,
|
|
|
|
])->first();
|
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.
|
2023-01-26 21:55:04 +01:00
|
|
|
$subsiteID = SubsiteState::singleton()->getSubsiteId();
|
|
|
|
if (empty($this->owner->SubsiteID) || $subsiteID == $this->owner->SubsiteID) {
|
2017-05-29 13:42:42 +02:00
|
|
|
return null;
|
|
|
|
}
|
2023-01-26 21:55:04 +01:00
|
|
|
return SubsiteState::singleton()->withState(function (SubsiteState $newState) {
|
|
|
|
$newState->setSubsiteId($this->owner->SubsiteID);
|
|
|
|
return (bool) $this->owner->validURLSegment();
|
|
|
|
});
|
2017-05-29 13:42:42 +02:00
|
|
|
}
|
|
|
|
|
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-08-30 01:06:07 +02:00
|
|
|
return 'subsite-' . SubsiteState::singleton()->getSubsiteId();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 04:34:13 +01:00
|
|
|
* @param Member $member
|
2017-05-24 15:26:28 +02:00
|
|
|
* @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) {
|
2018-03-15 05:15:19 +01:00
|
|
|
// SS 4.1: JSON encoded. SS 4.0, comma delimited
|
2022-04-13 03:49:48 +02:00
|
|
|
$blacklist = json_decode($subsite->PageTypeBlacklist ?? '', true);
|
2018-03-15 05:15:19 +01:00
|
|
|
if ($blacklist === false) {
|
2022-04-13 03:49:48 +02:00
|
|
|
$blacklist = explode(',', $subsite->PageTypeBlacklist ?? '');
|
2018-03-15 05:15:19 +01:00
|
|
|
}
|
2017-09-11 22:06:22 +02:00
|
|
|
|
2018-03-15 05:15:19 +01:00
|
|
|
if (in_array(get_class($this->owner), (array) $blacklist)) {
|
2017-05-24 15:26:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-01 02:35:17 +02:00
|
|
|
}
|