Completed
Push — master ( 471e58...0520ef )
by Matthew
10s
created

AccordionPanel::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 1
rs 8.8571
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\Forms\LinkField;
8
use Sheadawson\Linkable\Models\Link;
9
use SilverStripe\Assets\Image;
10
use SilverStripe\Forms\CheckboxField;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\Security\Member;
15
use SilverStripe\Security\Permission;
16
17
class AccordionPanel extends DataObject
18
{
19
    /**
20
     * @var string
21
     */
22
    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...
23
24
    /**
25
     * @var string
26
     */
27
    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...
28
29
    /**
30
     * @var string
31
     */
32
    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...
33
34
    /**
35
     * @var array
36
     */
37
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
38
        'Title' => 'Varchar(255)',
39
        'ShowTitle' => 'Boolean',
40
        'Content' => 'HTMLText',
41
        'Sort' => 'Int',
42
    ];
43
    /**
44
     * @var array
45
     */
46
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
47
        'Accordion' => ElementAccordion::class,
48
        'Image' => Image::class,
49
        'ElementLink' => Link::class,
50
    ];
51
52
    /**
53
     * @var array Related objects to be published recursively on AccordionPanel::publishRecursively()
54
     */
55
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
56
        'Image',
57
    ];
58
59
    /**
60
     * @var array Show the panel $Title by default
61
     */
62
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
63
        'ShowTitle' => true,
64
    ];
65
66
    /**
67
     * @var string
68
     */
69
    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...
70
71
    /**
72
     * @var string Database table name, default's to the fully qualified name
73
     */
74
    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...
75
76
    /**
77
     * @return FieldList
78
     *
79
     * @throws \Exception
80
     */
81
    public function getCMSFields()
82
    {
83 1
        $this->beforeUpdateCMSFields(function ($fields) {
84
            /** @var FieldList $fields */
85 1
            $fields->removeByName([
86 1
                'Sort',
87
                'AccordionID',
88
            ]);
89
90 1
            $fields->removeByName('ShowTitle');
91 1
            $fields->replaceField(
92 1
                'Title',
93 1
                TextCheckboxGroupField::create(
94 1
                    TextField::create('Title', _t(__CLASS__ . '.TitleLabel', 'Title (displayed if checked)')),
95 1
                    CheckboxField::create('ShowTitle', _t(__CLASS__ . '.ShowTitleLabel', 'Displayed'))
96
                )
97 1
                    ->setName('TitleAndDisplayed')
98
            );
99
100
            // because it defaults to a dropdown menu
101 1
            $fields->replaceField(
102 1
                'ElementLinkID',
103 1
                LinkField::create('ElementLinkID', _t(__CLASS__ . 'LinkLabel', 'Link'))
104
            );
105
106 1
            $fields->dataFieldByName('Image')
107 1
                ->setFolderName('Uploads/Elements/Accordions');
108 1
        });
109
110 1
        return parent::getCMSFields();
111
    }
112
113
    /**
114
     * @return null
115
     */
116 8
    public function getPage()
117
    {
118 8
        $page = null;
119
120 8
        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...
121 8
            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

121
            if ($this->/** @scrutinizer ignore-call */ Accordion()->hasMethod('getPage')) {
Loading history...
122 8
                $page = $this->Accordion()->getPage();
123
            }
124
        }
125
126 8
        return $page;
127
    }
128
129
    /**
130
     * Basic permissions, defaults to page perms where possible.
131
     *
132
     * @param Member $member
133
     * @return boolean
134
     */
135 8
    public function canView($member = null)
136
    {
137 8
        $extended = $this->extendedCan(__FUNCTION__, $member);
138 8
        if ($extended !== null) {
139
            return $extended;
140
        }
141
142 8
        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...
143
            return $page->canView($member);
144
        }
145
146 8
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
147
    }
148
149
    /**
150
     * Basic permissions, defaults to page perms where possible.
151
     *
152
     * @param Member $member
153
     *
154
     * @return boolean
155
     */
156 1
    public function canEdit($member = null)
157
    {
158 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
159 1
        if ($extended !== null) {
160
            return $extended;
161
        }
162
163 1
        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...
164
            return $page->canEdit($member);
165
        }
166
167 1
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
168
    }
169
170
    /**
171
     * Basic permissions, defaults to page perms where possible.
172
     *
173
     * Uses archive not delete so that current stage is respected i.e if a
174
     * element is not published, then it can be deleted by someone who doesn't
175
     * have publishing permissions.
176
     *
177
     * @param Member $member
178
     *
179
     * @return boolean
180
     */
181 1
    public function canDelete($member = null)
182
    {
183 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
184 1
        if ($extended !== null) {
185
            return $extended;
186
        }
187
188 1
        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...
189
            return $page->canArchive($member);
190
        }
191
192 1
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
193
    }
194
195
    /**
196
     * Basic permissions, defaults to page perms where possible.
197
     *
198
     * @param Member $member
199
     * @param array $context
200
     *
201
     * @return boolean
202
     */
203 1
    public function canCreate($member = null, $context = array())
204
    {
205 1
        $extended = $this->extendedCan(__FUNCTION__, $member);
206 1
        if ($extended !== null) {
207
            return $extended;
208
        }
209
210 1
        return (Permission::check('CMS_ACCESS', 'any', $member)) ? true : null;
211
    }
212
}
213