Completed
Pull Request — master (#71)
by John
03:00
created

ElementalWidgetExtension::canCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 3
nop 1
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
     * Basic permissions, defaults to page perms where possible
10
     */
11 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...
12
    {
13
        if ($page = $this->owner->getPage()) {
14
            return $page->canView($member);
15
        }
16
17
        if(Director::is_cli()) return true;
18
19
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
20
    }
21
22
    /**
23
     * Basic permissions, defaults to page perms where possible
24
     */
25 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...
26
    {
27
        if ($page = $this->owner->getPage()) {
28
            return $page->canEdit($member);
29
        }
30
31
        if(Director::is_cli()) return true;
32
33
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
34
    }
35
36
    /**
37
     * Basic permissions, defaults to page perms where possible
38
     * Uses archive not delete so that current stage is respected
39
     * i.e if a widget is not published, then it can be deleted by someone who
40
     * doesn't have publishing permissions
41
     */
42 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...
43
    {
44
        if ($page = $this->owner->getPage()) {
45
            return $page->canArchive($member);
46
        }
47
48
        if(Director::is_cli()) return true;
49
50
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
51
    }
52
53
    /**
54
     * Basic permissions, defaults to page perms where possible
55
     */
56
    public function canCreate($member = null)
57
    {
58
        if(Director::is_cli()) return true;
59
60
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
61
    }
62
63
    /**
64
     * Handles unpublishing as VersionedDataObjects doesn't
65
     * Modelled on SiteTree::doUnpublish
66
     * Has to be applied here, rather than BaseElement so that goes against Widget
67
     */
68
    public function doUnpublish() {
69
        if(!$this->owner->ID) return false;
70
71
        $this->owner->extend('onBeforeUnpublish');
72
73
        $origStage = Versioned::get_reading_mode();
74
        Versioned::set_reading_mode('Stage.Live');
75
76
        // This way our ID won't be unset
77
        $clone = clone $this->owner;
78
        $clone->delete();
79
80
        Versioned::set_reading_mode($origStage);
81
82
        $virtualLinkedElements = $this->owner->getPublishedVirtualLinkedElements();
83
        if ($virtualLinkedElements) foreach($virtualLinkedElements as $vle) $vle->doUnpublish();
84
85
        $this->owner->extend('onAfterUnpublish');
86
87
        return true;
88
    }
89
90
}
91