Merge pull request #255 from dnadesign/copy_children_subsites

NEW: Add IncludeChildren option for copying pages between subsites.
This commit is contained in:
Daniel Hensby 2016-07-18 11:40:23 +01:00 committed by GitHub
commit 65bf5c732b
4 changed files with 64 additions and 26 deletions

View File

@ -30,7 +30,7 @@ class LeftAndMainSubsites extends Extension
$subsite = Subsite::currentSubSite();
return $subsite ? Convert::raw2xml($subsite->Title) : _t('LeftAndMain.SITECONTENTLEFT');
}
public function updatePageOptions(&$fields)
{
$fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
@ -108,7 +108,7 @@ class LeftAndMainSubsites extends Extension
}
/*
* Generates a list of subsites with the data needed to
* Generates a list of subsites with the data needed to
* produce a dropdown site switcher
* @return ArrayList
*/
@ -128,7 +128,7 @@ class LeftAndMainSubsites extends Extension
foreach ($list as $subsite) {
$CurrentState = $subsite->ID == $currentSubsiteID ? 'selected' : '';
$output->push(new ArrayData(array(
'CurrentState' => $CurrentState,
'ID' => $subsite->ID,
@ -316,13 +316,23 @@ class LeftAndMainSubsites extends Extension
}
}
/**
* @param array $data
* @param Form $form
*/
public function copytosubsite($data, $form)
{
$page = DataObject::get_by_id('SiteTree', $data['ID']);
$subsite = DataObject::get_by_id('Subsite', $data['CopyToSubsiteID']);
$newPage = $page->duplicateToSubsite($subsite->ID, true);
$includeChildren = (isset($data['CopyToSubsiteWithChildren'])) ? $data['CopyToSubsiteWithChildren'] : false;
$newPage = $page->duplicateToSubsite($subsite->ID, $includeChildren);
$response = $this->owner->getResponse();
$response->addHeader('X-Reload', true);
return $this->owner->redirect(Controller::join_links($this->owner->Link('show'), $newPage->ID));
return $this->owner->redirect(Controller::join_links(
$this->owner->Link('show'),
$newPage->ID
));
}
}

View File

@ -81,27 +81,23 @@ class SiteTreeSubsites extends DataExtension
// 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(
$fields->addFieldsToTab(
'Root.Main',
$copyField = new DropdownField(
"CopyToSubsiteID",
_t('SiteTreeSubsites.CopyToSubsite', "Copy page to subsite"),
$subsitesMap,
''
)
);
$copyField->setDescription(_t(
'SiteTreeSubsites.CopyToSubsiteDescription',
"Note that this page will be copied to the top level and should be re-organised if necessary."
));
$fields->addFieldToTab(
'Root.Main',
$copyAction = new InlineFormAction(
"copytosubsite",
_t('SiteTreeSubsites.CopyAction', "Copy")
)
ToggleCompositeField::create('SubsiteOperations',
_t('SiteTreeSubsites.SubsiteOperations', 'Subsite Operations'),
array(
new DropdownField("CopyToSubsiteID", _t('SiteTreeSubsites.CopyToSubsite', "Copy page to subsite"), $subsitesMap),
new CheckboxField("CopyToSubsiteWithChildren", _t('SiteTreeSubsites.CopyToSubsiteWithChildren', 'Include children pages?')),
$copyAction = new InlineFormAction(
"copytosubsite",
_t('SiteTreeSubsites.CopyAction', "Copy")
)
)
)->setHeadingLevel(4)
);
$copyAction->includeDefaultJS(false);
}
@ -125,6 +121,9 @@ class SiteTreeSubsites extends DataExtension
}
}
/**
* @return SiteConfig
*/
public function alternateSiteConfig()
{
if (!$this->owner->SubsiteID) {
@ -214,15 +213,17 @@ class SiteTreeSubsites extends DataExtension
* Create a duplicate of this page and save it to another subsite
*
* @param int|Subsite $subsiteID The Subsite to copy to, or its ID
*
* @return SiteTree duplicated page
*/
public function duplicateToSubsite($subsiteID = null)
public function duplicateToSubsite($subsiteID = null, $includeChildren = false)
{
if ($subsiteID instanceof Subsite) {
$subsiteID = $subsiteID->ID;
}
$oldSubsite = Subsite::currentSubsiteID();
if ($subsiteID) {
Subsite::changeSubsite($subsiteID);
} else {
@ -242,6 +243,12 @@ class SiteTreeSubsites extends DataExtension
$page->MasterPageID = $this->owner->ID;
$page->write();
if($includeChildren) {
foreach($this->owner->AllChildren() as $child) {
$child->duplicateToSubsite($subsiteID, $includeChildren);
}
}
Subsite::changeSubsite($oldSubsite);
return $page;

View File

@ -1,10 +1,13 @@
<?php
/**
* Handy alternative to copying pages when creating a subsite through the UI.
* Can be used to batch-add new pages after subsite creation,
* or simply to process a large site outside of the UI.
*
* Can be used to batch-add new pages after subsite creation, or simply to
* process a large site outside of the UI.
*
* Example: sake dev/tasks/SubsiteCopyPagesTask from=<subsite-source> to=<subsite-target>
*
* @package subsites
*/
class SubsiteCopyPagesTask extends BuildTask
{

View File

@ -285,6 +285,24 @@ class SiteTreeSubsitesTest extends BaseSubsiteTest
$mainSubsiteImportantPage->write();
$this->assertEquals('important-page', $mainSubsiteImportantPage->URLSegment);
}
function testCopySubsiteWithChildren() {
$page = $this->objFromFixture('Page', 'about');
$newSubsite = $this->objFromFixture('Subsite', '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');
}
function testCopySubsiteWithoutChildren() {
$page = $this->objFromFixture('Page', 'about');
$newSubsite = $this->objFromFixture('Subsite', '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');
}
}
class SiteTreeSubsitesTest_ClassA extends SiteTree implements TestOnly