Completed
Push — master ( d4a13f...650739 )
by Nic
9s
created

AccordionPanel::canCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Elements\Accordion\Model;
4
5
use DNADesign\Elemental\Forms\TextCheckboxGroupField;
6
use Dynamic\Elements\Accordion\Elements\ElementAccordion;
7
use Sheadawson\Linkable\Models\Link;
8
use SilverStripe\Assets\Image;
9
use SilverStripe\Forms\CheckboxField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Permission;
15
16
class AccordionPanel extends DataObject
17
{
18
    /**
19
     * @var string
20
     */
21
    private static $singular_name = 'Accordion Panel';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
22
23
    /**
24
     * @var string
25
     */
26
    private static $plural_name = 'Accordion Panels';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @var string
30
     */
31
    private static $description = 'A panel for a Accordion widget';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
32
33
    /**
34
     * @var array
35
     */
36
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
37
        'Title' => 'Varchar(255)',
38
        'ShowTitle' => 'Boolean',
39
        'Content' => 'HTMLText',
40
        'Sort' => 'Int',
41
    ];
42
    /**
43
     * @var array
44
     */
45
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
46
        'Accordion' => ElementAccordion::class,
47
        'Image' => Image::class,
48
        'ElementLink' => Link::class,
49
    ];
50
51
    /**
52
     * @var array Related objects to be published recursively on AccordionPanel::publishRecursively()
53
     */
54
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
55
        'Image',
56
    ];
57
58
    /**
59
     * @var array Show the panel $Title by default
60
     */
61
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
62
        'ShowTitle' => true,
63
    ];
64
65
    /**
66
     * @var string
67
     */
68
    private static $default_sort = 'Sort';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
69
70
    /**
71
     * @var string Database table name, default's to the fully qualified name
72
     */
73
    private static $table_name = 'AccordionPanel';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
74
75
    /**
76
     * @return FieldList
77
     *
78
     * @throws \Exception
79
     */
80
    public function getCMSFields()
81
    {
82
        $this->beforeUpdateCMSFields(function ($fields) {
83
            $fields->removeByName([
84
                'Sort',
85
                'AccordionID',
86
            ]);
87
88
            $fields->removeByName('ShowTitle');
89
            $fields->replaceField(
90
                'Title',
91
                TextCheckboxGroupField::create(
92
                    TextField::create('Title', _t(__CLASS__ . '.TitleLabel', 'Title (displayed if checked)')),
93
                    CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed'))
94
                )
95
                    ->setName('TitleAndDisplayed')
96
            );
97
98
            $fields->dataFieldByName('Image')
99
                ->setFolderName('Uploads/Elements/Accordions');
100
        });
101
102
        return parent::getCMSFields();
103
    }
104
105
    /**
106
     * @return null
107
     */
108
    public function getPage()
109
    {
110
        $page = null;
111
112
        if ($this->AccordionID) {
0 ignored issues
show
Bug Best Practice introduced by
The property AccordionID does not exist on Dynamic\Elements\Accordion\Model\AccordionPanel. Since you implemented __get, consider adding a @property annotation.
Loading history...
113
            if ($this->Accordion()->hasMethod('getPage')) {
0 ignored issues
show
Bug introduced by
The method Accordion() does not exist on Dynamic\Elements\Accordion\Model\AccordionPanel. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            if ($this->/** @scrutinizer ignore-call */ Accordion()->hasMethod('getPage')) {
Loading history...
114
                $page = $this->Accordion()->getPage();
115
            }
116
        }
117
118
        return $page;
119
    }
120
121
    /**
122
     * Basic permissions, defaults to page perms where possible.
123
     *
124
     * @param Member $member
125
     * @return boolean
126
     */
127
    public function canView($member = null)
128
    {
129
        $extended = $this->extendedCan(__FUNCTION__, $member);
130
        if ($extended !== null) {
131
            return $extended;
132
        }
133
134
        if ($page = $this->getPage()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $page is correct as $this->getPage() targeting Dynamic\Elements\Accordi...cordionPanel::getPage() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
135
            return $page->canView($member);
136
        }
137
138
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
139
    }
140
141
    /**
142
     * Basic permissions, defaults to page perms where possible.
143
     *
144
     * @param Member $member
145
     *
146
     * @return boolean
147
     */
148
    public function canEdit($member = null)
149
    {
150
        $extended = $this->extendedCan(__FUNCTION__, $member);
151
        if ($extended !== null) {
152
            return $extended;
153
        }
154
155
        if ($page = $this->getPage()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $page is correct as $this->getPage() targeting Dynamic\Elements\Accordi...cordionPanel::getPage() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
156
            return $page->canEdit($member);
157
        }
158
159
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
160
    }
161
162
    /**
163
     * Basic permissions, defaults to page perms where possible.
164
     *
165
     * Uses archive not delete so that current stage is respected i.e if a
166
     * element is not published, then it can be deleted by someone who doesn't
167
     * have publishing permissions.
168
     *
169
     * @param Member $member
170
     *
171
     * @return boolean
172
     */
173
    public function canDelete($member = null)
174
    {
175
        $extended = $this->extendedCan(__FUNCTION__, $member);
176
        if ($extended !== null) {
177
            return $extended;
178
        }
179
180
        if ($page = $this->getPage()) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $page is correct as $this->getPage() targeting Dynamic\Elements\Accordi...cordionPanel::getPage() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
181
            return $page->canArchive($member);
182
        }
183
184
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
185
    }
186
187
    /**
188
     * Basic permissions, defaults to page perms where possible.
189
     *
190
     * @param Member $member
191
     * @param array $context
192
     *
193
     * @return boolean
194
     */
195
    public function canCreate($member = null, $context = array())
196
    {
197
        $extended = $this->extendedCan(__FUNCTION__, $member);
198
        if ($extended !== null) {
199
            return $extended;
200
        }
201
202
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
203
    }
204
}
205