Skip to content

Commit

Permalink
Merge branch '2.1'
Browse files Browse the repository at this point in the history
  # Conflicts:
  #	src/Extensions/SiteTreeSubsites.php
  • Loading branch information
robbieaverill committed Oct 19, 2018
2 parents 5b8a0db + 9199d50 commit bf7dd9c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 54 deletions.
2 changes: 2 additions & 0 deletions _config/middleware.yml
Expand Up @@ -8,3 +8,5 @@ SilverStripe\Core\Injector\Injector:
properties:
Middlewares:
SubsitesStateMiddleware: %$SilverStripe\Subsites\Middleware\InitStateMiddleware
SilverStripe\Dev\Tasks\MigrateFileTask:
class: SilverStripe\Subsites\Tasks\SubsiteMigrateFileTask
102 changes: 67 additions & 35 deletions src/Extensions/SiteTreeSubsites.php
Expand Up @@ -170,57 +170,89 @@ public function updateCMSFields(FieldList $fields)
}

/**
* Does the basic duplication, but doesn't write anything
* this means we can subclass this easier and do more complex
* relation duplication.
* 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
*/
public function duplicateToSubsitePrep($subsiteID)
public function duplicateToSubsitePrep($subsiteID, $includeChildren)
{
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;
}
return SubsiteState::singleton()
->withState(function (SubsiteState $newState) use ($subsiteID, $includeChildren) {
$newState->setSubsiteId($subsiteID);

/** @var SiteTree $page */
$page = $this->owner;

try {
// 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
$originalFilter = Subsite::$disable_subsite_filter;
Subsite::disable_subsite_filter(true);

return $includeChildren ? $page->duplicateWithChildren() : $page->duplicate(false);
} finally {
Subsite::disable_subsite_filter($originalFilter);

// Re-set the original parent ID for the current page
$page->ParentID = $originalParentID;
}
});
}

/**
* 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;
}
// 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
*
* @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
*/
public function duplicateToSubsite($subsiteID = null)
public function duplicateToSubsite($subsiteID = null, $includeChildren = false)
{
$clone = $this->owner->duplicateToSubsitePrep($subsiteID);
$clone = $this->owner->duplicateToSubsitePrep($subsiteID, $includeChildren);
$clone->invokeWithExtensions('onBeforeDuplicateToSubsite', $this->owner);
$clone->write();

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
$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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Forms/GridFieldSubsiteDetailFormItemRequest.php
Expand Up @@ -42,7 +42,7 @@ public function ItemEditForm()
$templateArray
);
$templateDropdown->setEmptyString('(' . _t('Subsite.NOTEMPLATE', 'No template') . ')');
$form->Fields()->addFieldToTab('Root.Configuration', $templateDropdown);
$form->Fields()->addFieldToTab('Root.Main', $templateDropdown);
}

return $form;
Expand Down
10 changes: 7 additions & 3 deletions src/Middleware/InitStateMiddleware.php
Expand Up @@ -43,8 +43,12 @@ public function process(HTTPRequest $request, callable $delegate)

return $delegate($request);
} catch (DatabaseException $ex) {
// Database is not ready
return $delegate($request);
$message = $ex->getMessage();
if (strpos($message, 'No database selected') !== false) {
// Database is not ready, ignore and continue
return $delegate($request);
}
throw $ex;
} finally {
// Persist to the session if using the CMS
if ($state->getUseSessions()) {
Expand All @@ -62,7 +66,7 @@ public function process(HTTPRequest $request, callable $delegate)
public function getIsAdmin(HTTPRequest $request)
{
$adminPaths = static::config()->get('admin_url_paths');
$adminPaths[] = AdminRootController::config()->get('url_base') . '/';
$adminPaths[] = AdminRootController::admin_url();
$currentPath = rtrim($request->getURL(), '/') . '/';
foreach ($adminPaths as $adminPath) {
if (substr($currentPath, 0, strlen($adminPath)) === $adminPath) {
Expand Down
19 changes: 19 additions & 0 deletions src/Tasks/SubsiteMigrateFileTask.php
@@ -0,0 +1,19 @@
<?php

namespace SilverStripe\Subsites\Tasks;

use SilverStripe\Dev\Tasks\MigrateFileTask;
use SilverStripe\Subsites\Model\Subsite;

class SubsiteMigrateFileTask extends MigrateFileTask
{
public function run($request)
{
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
Subsite::disable_subsite_filter(true);

parent::run($request);

Subsite::disable_subsite_filter($origDisableSubsiteFilter);
}
}
43 changes: 28 additions & 15 deletions tests/php/SiteTreeSubsitesTest.php
Expand Up @@ -359,28 +359,41 @@ public function testValidateURLSegment()
$this->assertEquals('important-page', $mainSubsiteImportantPage->URLSegment);
}

public function testCopySubsiteWithChildren()
/**
* @param bool $withChildren
* @param int $expectedChildren
* @dataProvider duplicateToSubsiteProvider
*/
public function testDuplicateToSubsite($withChildren, $expectedChildren)
{
$page = $this->objFromFixture('Page', 'about');
/** @var SiteTree $page */
$page = $this->objFromFixture(Page::class, 'about');
/** @var Subsite $newSubsite */
$newSubsite = $this->objFromFixture(Subsite::class, 'subsite1');

$moved = $page->duplicateToSubsite($newSubsite->ID, true);
$this->assertEquals($moved->SubsiteID, $newSubsite->ID, 'Ensure returned records are on new subsite');
$this->assertEquals(
$moved->AllChildren()->count(),
$page->AllChildren()->count(),
'All pages are copied across'
/** @var SiteTree $duplicatedPage */
$duplicatedPage = $page->duplicateToSubsite($newSubsite->ID, $withChildren);
$this->assertInstanceOf(SiteTree::class, $duplicatedPage, 'A new page is returned');

$this->assertEquals($newSubsite->ID, $duplicatedPage->SubsiteID, 'Ensure returned records are on new subsite');

$this->assertCount(1, $page->AllChildren());
$this->assertCount(
$expectedChildren,
$duplicatedPage->AllChildren(),
'Duplicated page also duplicates children'
);
}

public function testCopySubsiteWithoutChildren()
/**
* @return array[]
*/
public function duplicateToSubsiteProvider()
{
$page = $this->objFromFixture('Page', 'about');
$newSubsite = $this->objFromFixture(Subsite::class, 'subsite2');

$moved = $page->duplicateToSubsite($newSubsite->ID, false);
$this->assertEquals($moved->SubsiteID, $newSubsite->ID, 'Ensure returned records are on new subsite');
$this->assertEquals($moved->AllChildren()->count(), 0, 'All pages are copied across');
return [
[true, 1],
[false, 0],
];
}

public function testIfSubsiteThemeIsSetToThemeList()
Expand Down

0 comments on commit bf7dd9c

Please sign in to comment.