onAfterDuplicateToSubsite()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 22
rs 9.8333
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
use Page;
0 ignored issues
show
Bug introduced by
The type 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...
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\ORM\DataObject;
10
use SilverStripe\ORM\DB;
11
12
/**
13
 * @package elemental
14
 */
15
class ElementalSubsitePageExtension extends DataExtension
16
{
17
    /**
18
     * If the page is duplicated across subsites, copy the elements across too.
19
     *
20
     * @return Page The duplicated page
21
     */
22
    public function onAfterDuplicateToSubsite($originalPage)
23
    {
24
        /** @var ElementalArea $originalElementalArea */
25
        $originalElementalArea = $originalPage->getComponent('ElementalArea');
26
27
        $duplicateElementalArea = $originalElementalArea->duplicate(false);
28
        $duplicateElementalArea->write();
29
30
        $this->owner->ElementalAreaID = $duplicateElementalArea->ID;
31
        $this->owner->write();
32
33
        foreach ($originalElementalArea->Items() as $originalElement) {
34
            /** @var BaseElement $originalElement */
35
            $duplicateElement = $originalElement->duplicate(true);
36
37
            // manually set the ParentID of each element, so we don't get versioning issues
38
            DB::query(
39
                sprintf(
40
                    "UPDATE %s SET ParentID = %d WHERE ID = %d",
41
                    DataObject::getSchema()->tableName(BaseElement::class),
42
                    $duplicateElementalArea->ID,
43
                    $duplicateElement->ID
44
                )
45
            );
46
        }
47
    }
48
}
49