Completed
Push — master ( 31b335...9a25ab )
by
unknown
04:15
created

onAfterDuplicateToSubsite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\ElementalSubsites\Extensions;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use DNADesign\Elemental\Models\ElementalArea;
7
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\DB;
11
use SilverStripe\Versioned\Versioned;
12
13
14
/**
15
 * @package elemental
16
 */
17
class ElementalSubsitePageExtension extends DataExtension
18
{
19
20
    /**
21
     * If the page is duplicated across subsites, copy the elements across too.
22
     *
23
     * @return Page The duplicated page
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalSubsites\Extensions\Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     */
25
    public function onAfterDuplicateToSubsite($originalPage)
26
    {
27
        $originalElementalArea = $originalPage->getComponent('ElementalArea');
28
        $duplicateElementalArea = $originalElementalArea->duplicate(false);
29
        $duplicateElementalArea->write();
30
        $this->owner->ElementalAreaID = $duplicateElementalArea->ID;
31
        $this->owner->write();
32
33
        foreach ($originalElementalArea->Items() as $originalElement) {
34
            $duplicateElement = $originalElement->duplicate(true);
35
36
            // manually set the ParentID of each element, so we don't get versioning issues
37
            DB::query(sprintf("UPDATE Element SET ParentID = %d WHERE ID = %d", $duplicateElementalArea->ID, $duplicateElement->ID));
38
        }
39
    }
40
}
41