Completed
Push — master ( 59c799...149ad0 )
by Will
02:15
created

code/extensions/ElementDuplicationExtension.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @package elemental
5
 */
6
class ElementDuplicationExtension extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
9
    /**
10
     * Duplicate items
11
     *
12
     */
13
    public function onAfterDuplicate($original, $doWrite=true)
0 ignored issues
show
The parameter $doWrite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    {
15
        $thisClass = $this->owner->ClassName;
16
        $duplicateRelations = Config::inst()->get($thisClass, 'duplicate_relations');
17
18
        if ($duplicateRelations && !empty($duplicateRelations)) {
19
            foreach ($duplicateRelations as $relation) {
20
                $items = $original->$relation();
21
                foreach ($items as $item) {
22
                    $duplicateItem = $item->duplicate(false);
23
                    $duplicateItem->{$thisClass.'ID'} = $this->owner->ID;
24
                    $duplicateItem->write();
25
                }
26
            }
27
        }
28
    }
29
30
    public function onBeforeDuplicate($original, $doWrite=true)
0 ignored issues
show
The parameter $original is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $doWrite is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        $thisClass = $this->owner->ClassName;
33
        $clearRelations = Config::inst()->get($thisClass, 'duplicate_clear_relations');
34
35
        if ($clearRelations && !empty($clearRelations)) {
36
            foreach ($clearRelations as $clearRelation) {
37
                $clearRelation = $clearRelation . 'ID';
38
                $this->owner->$clearRelation = 0;
39
            }
40
        }
41
    }
42
}
43