Passed
Push — master ( 5540e2...e2d432 )
by Jason
02:58
created

FoxyCategory::canDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Security\Security;
10
11
class FoxyCategory extends DataObject
12
{
13
    /**
14
     * @var array
15
     */
16
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
17
        'Title' => 'Varchar(255)',
18
        'Code' => 'Varchar(50)',
19
    ];
20
21
    /**
22
     * @var array
23
     */
24
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
25
        'Title' => 'Name',
26
        'Code' => 'Code',
27
    ];
28
    /**
29
     * @var array
30
     */
31
    private static $indexes = [
0 ignored issues
show
introduced by
The private property $indexes is not used, and could be removed.
Loading history...
32
        'Code' => [
33
            'type' => 'unique',
34
            'columns' => ['Code'],
35
        ],
36
    ];
37
    /**
38
     * @var string
39
     */
40
    private static $table_name = 'FoxyCategory';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
41
42
    /**
43
     * @param bool $includerelations
44
     * @return array
45
     */
46
    public function fieldLabels($includerelations = true)
47
    {
48
        $labels = parent::fieldLabels($includerelations);
49
50
        $labels['Title'] = _t(__CLASS__ . '.TitleLabel', 'Title');
51
        $labels['Code'] = _t(__CLASS__ . '.CodeLabel', 'Code');
52
53
        return $labels;
54
    }
55
56
    /**
57
     * @return FieldList|void
58
     */
59
    public function getCMSFields()
60
    {
61
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
62
            if ($this->ID) {
63
                if ($this->Code == 'DEFAULT') {
64
                    $fields->replaceField(
65
                        'Title',
66
                        ReadonlyField::create('Title')
67
                    );
68
69
                    $fields->replaceField(
70
                        'Code',
71
                        ReadonlyField::create('Code')
72
                    );
73
                }
74
            }
75
        });
76
77
        return parent::getCMSFields();
78
    }
79
80
    /**
81
     * @return \SilverStripe\ORM\ValidationResult
82
     */
83
    public function validate()
84
    {
85
        $result = parent::validate();
86
87
        if (!$this->Code) {
88
            $result->addError(
89
                _t(__CLASS__ . '.CodeRequired', 'You must set a product price in the Foxy tab')
90
            );
91
        }
92
93
        if (FoxyCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) {
94
            $result->addError(
95
                _t(__CLASS__ . '.CodeUnique', 'Code must be unique for each category.')
96
            );
97
        }
98
99
        return $result;
100
    }
101
102
    /**
103
     * @throws \SilverStripe\ORM\ValidationException
104
     */
105
    public function requireDefaultRecords()
106
    {
107
        parent::requireDefaultRecords();
108
109
        $allCats = self::get();
110
        if (!$allCats->count()) {
111
            $cat = new self();
112
            $cat->Title = 'Default';
113
            $cat->Code = 'DEFAULT';
0 ignored issues
show
Bug Best Practice introduced by
The property Code does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
114
            $cat->write();
115
        }
116
    }
117
118
    /**
119
     * @param $member
120
     * @return bool|int|void
121
     */
122
    public function canCreate($member = null, $context = [])
123
    {
124
        if (!$member) {
125
            $member = Security::getCurrentUser();
126
        }
127
128
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
129
    }
130
131
    /**
132
     * @param $member
133
     * @return bool|int|void|null
134
     */
135
    public function canEdit($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

135
    public function canEdit($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
        if (!$member) {
138
            $member = Security::getCurrentUser();
139
        }
140
141
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
142
    }
143
144
    /**
145
     * @param $member
146
     * @return bool|int|void
147
     */
148
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

148
    public function canDelete($member = null, /** @scrutinizer ignore-unused */ $context = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
        if (!$member) {
151
            $member = Security::getCurrentUser();
152
        }
153
154
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
155
    }
156
}
157