Completed
Pull Request — master (#71)
by John
02:15
created

ElementalWidgetExtension::doUnpublish()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 0
1
<?php
2
3
/**
4
 * @package elemental
5
 */
6
class ElementalWidgetExtension 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
    /**
10
     * @param array $fields
11
     */
12
    public function updateSummaryFields(&$fields)
13
    {
14
        unset($fields['CMSPublishedState']);
15
    }
16
17
    /**
18
     * Basic permissions, defaults to page perms where possible
19
     */
20 View Code Duplication
    public function canView($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        if ($page = $this->owner->getPage()) {
23
            return $page->canView($member);
24
        }
25
26
        if(Director::is_cli()) return true;
27
28
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
29
    }
30
31
    /**
32
     * Basic permissions, defaults to page perms where possible
33
     */
34 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        if ($page = $this->owner->getPage()) {
37
            return $page->canEdit($member);
38
        }
39
40
        if(Director::is_cli()) return true;
41
42
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
43
    }
44
45
    /**
46
     * Basic permissions, defaults to page perms where possible
47
     * Uses archive not delete so that current stage is respected
48
     * i.e if a widget is not published, then it can be deleted by someone who
49
     * doesn't have publishing permissions
50
     */
51 View Code Duplication
    public function canDelete($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        if ($page = $this->owner->getPage()) {
54
            return $page->canArchive($member);
55
        }
56
57
        if(Director::is_cli()) return true;
58
59
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
60
    }
61
62
    /**
63
     * Basic permissions, defaults to page perms where possible
64
     */
65
    public function canCreate($member = null)
66
    {
67
        if(Director::is_cli()) return true;
68
69
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
70
    }
71
72
    /**
73
     * Handles unpublishing as VersionedDataObjects doesn't
74
     * Modelled on SiteTree::doUnpublish
75
     * Has to be applied here, rather than BaseElement so that goes against Widget
76
     */
77
    public function doUnpublish() {
78
        if(!$this->owner->ID) return false;
79
80
        $this->owner->extend('onBeforeUnpublish');
81
82
        $origStage = Versioned::get_reading_mode();
83
        Versioned::set_reading_mode('Stage.Live');
84
85
        // This way our ID won't be unset
86
        $clone = clone $this->owner;
87
        $clone->delete();
88
89
        Versioned::set_reading_mode($origStage);
90
91
        $virtualLinkedElements = $this->owner->getPublishedVirtualLinkedElements();
92
        if ($virtualLinkedElements) foreach($virtualLinkedElements as $vle) $vle->doUnpublish();
93
94
        $this->owner->extend('onAfterUnpublish');
95
96
        return true;
97
    }
98
99
}
100