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

ElementPublishChildren::onBeforeVersionedPublish()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 11
nc 8
nop 0
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