Completed
Pull Request — master (#8)
by Matthew
02:34
created

AccordionPanel::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 29
ccs 16
cts 16
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('ElementLinkID',
102 1
                LinkField::create('ElementLinkID', _t(__CLASS__ . 'LinkLabel', 'Link'))
103
            );
104
105 1
            $fields->dataFieldByName('Image')
106 1
                ->setFolderName('Uploads/Elements/Accordions');
107 1
        });
108
109 1
        return parent::getCMSFields();
110
    }
111
112
    /**
113
     * @return null
114
     */
115 8
    public function getPage()
116
    {
117 8
        $page = null;
118
119 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...
120 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

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