Passed
Push — master ( 8f6961...722018 )
by Jason
01:37
created

FileListObject::canView()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
namespace Dynamic\Elements\FileList\Model;
4
5
use Dynamic\Elements\FileList\Elements\ElementFileList;
6
use SilverStripe\Assets\File;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Permission;
9
10
class FileListObject extends DataObject
11
{
12
    /**
13
     * @var string
14
     */
15
    private static $singular_name = 'File';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
16
17
    /**
18
     * @var string
19
     */
20
    private static $plural_name = 'Files';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
21
22
    /**
23
     * @var array
24
     */
25
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
26
        'Title' => 'Varchar(255)',
27
        'SortOrder' => "Int",
28
    ];
29
30
    /**
31
     * @var array
32
     */
33
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
34
        'FileList' => ElementFileList::class,
35
        'File' => File::class,
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
42
        'File',
43
    ];
44
45
    /**
46
     * @var array
47
     */
48 1
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
49
        'File.Name',
50 1
        'Title',
51
    ];
52 1
53 1
    /**
54
     * @param bool $includerelations
55
     * @return array
56 1
     */
57
    public function fieldLabels($includerelations = true)
58
    {
59
        $labels = parent::fieldLabels($includerelations);
60
61
        $labels['Title'] = _t(__CLASS__.'.TitleLabel', 'Title');
62
        $labels['File.Name'] = _t(__CLASS__.'.FileNameLabel', 'File');
63
        $labels['File'] = _t(__CLASS__.'.FileLabel', 'File');
64
65
        return $labels;
66
    }
67
68
    /**
69
     * @var string
70
     */
71
    private static $table_name = 'FileListObject';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
72
73
    /**
74
     * @return \SilverStripe\Forms\FieldList
75
     */
76
    public function getCMSFields()
77
    {
78
        $fields = parent::getCMSFields();
79
80
        $fields->dataFieldByName('File')
81
            ->setTitle($this->fieldLabel('File'));
82
83
        $fields->removeByName([
84
            'FileListID',
85
            'SortOrder',
86
        ]);
87
88
        return $fields;
89
    }
90
91
    /**
92
     * Basic permissions, defaults to page perms where possible.
93
     *
94
     * @param \SilverStripe\Security\Member|null $member
95
     * @return boolean
96
     */
97
    public function canView($member = null)
98
    {
99
        $extended = $this->extendedCan(__FUNCTION__, $member);
100
        if ($extended !== null) {
101
            return $extended;
102
        }
103
104
        if ($page = $this->FileList()->getPage()) {
0 ignored issues
show
Bug introduced by
The method FileList() does not exist on Dynamic\Elements\FileList\Model\FileListObject. 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

104
        if ($page = $this->/** @scrutinizer ignore-call */ FileList()->getPage()) {
Loading history...
105
            return $page->canView($member);
106
        }
107
108
        return Permission::check('CMS_ACCESS', 'any', $member);
109
    }
110
111
    /**
112
     * Basic permissions, defaults to page perms where possible.
113
     *
114
     * @param \SilverStripe\Security\Member|null $member
115
     *
116
     * @return boolean
117
     */
118
    public function canEdit($member = null)
119
    {
120
        $extended = $this->extendedCan(__FUNCTION__, $member);
121
        if ($extended !== null) {
122
            return $extended;
123
        }
124
125
        if ($page = $this->FileList()->getPage()) {
126
            return $page->canEdit($member);
127
        }
128
129
        return Permission::check('CMS_ACCESS', 'any', $member);
130
    }
131
132
    /**
133
     * Basic permissions, defaults to page perms where possible.
134
     *
135
     * Uses archive not delete so that current stage is respected i.e if a
136
     * element is not published, then it can be deleted by someone who doesn't
137
     * have publishing permissions.
138
     *
139
     * @param \SilverStripe\Security\Member|null $member
140
     *
141
     * @return boolean
142
     */
143
    public function canDelete($member = null)
144
    {
145
        $extended = $this->extendedCan(__FUNCTION__, $member);
146
        if ($extended !== null) {
147
            return $extended;
148
        }
149
150
        if ($page = $this->FileList()->getPage()) {
151
            return $page->canArchive($member);
152
        }
153
154
        return Permission::check('CMS_ACCESS', 'any', $member);
155
    }
156
157
    /**
158
     * Basic permissions, defaults to page perms where possible.
159
     *
160
     * @param \SilverStripe\Security\Member|null $member
161
     * @param array $context
162
     *
163
     * @return boolean
164
     */
165
    public function canCreate($member = null, $context = array())
166
    {
167
        $extended = $this->extendedCan(__FUNCTION__, $member);
168
        if ($extended !== null) {
169
            return $extended;
170
        }
171
172
        return Permission::check('CMS_ACCESS', 'any', $member);
173
    }
174
}
175