FoxyCategory::validate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

150
    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...
151
    {
152
        if (!$member) {
153
            $member = Security::getCurrentUser();
154
        }
155
156
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
157
    }
158
159
    /**
160
     * @param $member
161
     * @return bool|int|void
162
     */
163
    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

163
    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...
164
    {
165
        if (!$member) {
166
            $member = Security::getCurrentUser();
167
        }
168
169
        return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS');
170
    }
171
}
172