diff --git a/code/extensions/LeftAndMainSubsites.php b/code/extensions/LeftAndMainSubsites.php index 6d16314..bd7fb73 100644 --- a/code/extensions/LeftAndMainSubsites.php +++ b/code/extensions/LeftAndMainSubsites.php @@ -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 + )); } } diff --git a/code/extensions/SiteTreeSubsites.php b/code/extensions/SiteTreeSubsites.php index 60afa20..5a8c979 100644 --- a/code/extensions/SiteTreeSubsites.php +++ b/code/extensions/SiteTreeSubsites.php @@ -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; diff --git a/code/tasks/SubsiteCopyPagesTask.php b/code/tasks/SubsiteCopyPagesTask.php index ceac417..f48eb60 100644 --- a/code/tasks/SubsiteCopyPagesTask.php +++ b/code/tasks/SubsiteCopyPagesTask.php @@ -1,10 +1,13 @@ to= + * + * @package subsites */ class SubsiteCopyPagesTask extends BuildTask { diff --git a/tests/SiteTreeSubsitesTest.php b/tests/SiteTreeSubsitesTest.php index 1b1b825..5457a29 100644 --- a/tests/SiteTreeSubsitesTest.php +++ b/tests/SiteTreeSubsitesTest.php @@ -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