mirror of
https://github.com/silverstripe/silverstripe-subsites
synced 2024-06-29 07:59:27 +02:00
made duplication between subsites more robust
and added some extra methods so it's easier to overwrite or extend
This commit is contained in:
parent
7ee8bdbf72
commit
3587bc666f
|
@ -168,36 +168,81 @@ class SiteTreeSubsites extends DataExtension {
|
||||||
return $this->canEdit($member);
|
return $this->canEdit($member);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 = Subsite::currentSubsiteID();
|
||||||
|
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
|
* 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 $subsiteID int|Subsite The Subsite to copy to, or its ID
|
||||||
*/
|
*/
|
||||||
public function duplicateToSubsite($subsiteID = null) {
|
public function duplicateToSubsite($subsiteID = null) {
|
||||||
if(is_object($subsiteID)) {
|
$clone = $this->owner->duplicateToSubsitePrep($subsiteID);
|
||||||
$subsite = $subsiteID;
|
$clone->invokeWithExtensions('onBeforeDuplicateToSubsite', $this->owner);
|
||||||
$subsiteID = $subsite->ID;
|
$clone->write();
|
||||||
} else $subsite = DataObject::get_by_id('Subsite', $subsiteID);
|
$clone->duplicateSubsiteRelations($this->owner);
|
||||||
|
// new extension hooks which happens after write,
|
||||||
$oldSubsite=Subsite::currentSubsiteID();
|
// onAfterDuplicate isn't reliable due to
|
||||||
if($subsiteID) {
|
// https://github.com/silverstripe/silverstripe-cms/issues/1253
|
||||||
Subsite::changeSubsite($subsiteID);
|
$clone->invokeWithExtensions('onAfterDuplicateToSubsite', $this->owner);
|
||||||
}else {
|
return $clone;
|
||||||
$subsiteID=$oldSubsite;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$page = $this->owner->duplicate(false);
|
/**
|
||||||
|
* 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');
|
||||||
|
|
||||||
$page->CheckedPublicationDifferences = $page->AddedToStage = true;
|
if($relations && !empty($relations)) {
|
||||||
$subsiteID = ($subsiteID ? $subsiteID : $oldSubsite);
|
foreach($relations as $relation) {
|
||||||
$page->SubsiteID = $subsiteID;
|
$items = $originalPage->$relation();
|
||||||
|
foreach($items as $item) {
|
||||||
// MasterPageID is here for legacy purposes, to satisfy the subsites_relatedpages module
|
$duplicateItem = $item->duplicate(false);
|
||||||
$page->MasterPageID = $this->owner->ID;
|
$duplicateItem->{$thisClass.'ID'} = $this->owner->ID;
|
||||||
$page->write();
|
$duplicateItem->write();
|
||||||
|
}
|
||||||
Subsite::changeSubsite($oldSubsite);
|
}
|
||||||
|
}
|
||||||
return $page;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue
Block a user