public function doArtisanDuplicateBlocks(ArtisanSection $oldSection, ArtisanSection $newSection, $doWrite = true) { if ($doWrite && $newSection->hasExtension('ArtisanHasBlocksExtension')) { $blocks = $oldSection->ArtisanHasBlocks(); $relationshipIDField = ArtisanBlock::RelationshipName . 'ID'; /** @var ArtisanBlock $block */ foreach ($blocks as $oldBlock) { $newBlock = new ArtisanBlock($oldBlock->toMap(), false, $oldBlock->getModel()); $newBlock->ID = 0; $newBlock->{$relationshipIDField} = $newSection->ID; $newBlock->write(); $newBlock->extend('doArtisanDuplicateFields', $oldBlock, $newBlock, $doWrite); } } }
/** * @param Page $oldPage * @param Page $newPage * @param bool|false $doWrite - set to false to debug and not write records, true otherwise * @throws ValidationException */ public function doArtisanDuplicateSections(Page $oldPage, Page $newPage, $doWrite = true) { // if not write then don't do anything. if ($doWrite && $newPage->hasExtension('ArtisanHasSectionsExtension')) { $sections = $oldPage->ArtisanHasSections(); $relationshipIDField = ArtisanSection::RelationshipName . 'ID'; /** @var ArtisanSection $oldSection */ foreach ($sections as $oldSection) { $newSection = new ArtisanSection($oldSection->toMap(), false, $this()->getModel()); $newSection->ID = 0; $newSection->{$relationshipIDField} = $newPage->ID; $newSection->write(); $newSection->extend('doArtisanDuplicateBlocks', $oldSection, $newSection, $doWrite); $newSection->extend('doArtisanDuplicateFields', $oldSection, $newSection, $doWrite); } } }