Completed
Push — master ( 6caeae...d0891d )
by Will
02:19
created

ElementPublishChildren   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 2
dl 0
loc 28
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B onBeforeVersionedPublish() 0 24 5
1
<?php
2
3
/**
4
 * @package elemental
5
 */
6
class ElementPublishChildren extends DataExtension
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
    public function onBeforeVersionedPublish()
10
    {
11
        $staged = array();
12
13
        foreach ($this->owner->Elements() as $widget) {
14
            $staged[] = $widget->ID;
15
16
            $widget->publish('Stage', 'Live');
17
        }
18
19
        if($this->owner->ID) {
20
21
            // remove any elements that are on live but not in draft.
22
            $widgets = Versioned::get_by_stage('BaseElement', 'Live', sprintf(
23
                "ListID = '%s'", $this->owner->ID
24
            ));
25
26
            foreach ($widgets as $widget) {
27
                if (!in_array($widget->ID, $staged)) {
28
                    $widget->deleteFromStage('Live');
29
                }
30
            }
31
        }
32
    }
33
}
34