Passed
Push — 2024_06_18_noProductSet ( f96c1e )
by Nic
15:15
created

OptionGroup::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\HeaderField;
7
use SilverStripe\Forms\RequiredFields;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\ORM\ValidationResult;
10
use SilverStripe\Security\Permission;
11
12
/**
13
 * Class OptionGroup
14
 * @package Dynamic\FoxyStripe\Model
15
 *
16
 * @property \SilverStripe\ORM\FieldType\DBVarchar Title
17
 *
18
 * @method \SilverStripe\ORM\HasManyList Options
19
 */
20
class OptionGroup extends DataObject
21
{
22
    /**
23
     * @var array
24
     */
25
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
26
        'Title' => 'Varchar(100)',
27
    );
28
29
    /**
30
     * @var array
31
     */
32
    private static $has_many = array(
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
33
        'Options' => OptionItem::class,
34
    );
35
36
    /**
37
     * @var string
38
     */
39
    private static $singular_name = 'Product Option Group';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var string
43
     */
44
    private static $plural_name = 'Product Option Groups';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @var string
48
     */
49
    private static $description = 'Groups of product options, e.g. size, color, etc';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var string
53
     */
54
    private static $table_name = 'OptionGroup';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
55
56
    /**
57
     * @return FieldList
58
     */
59
    public function getCMSFields()
60
    {
61
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
62
            $fields->removeByName('Options');
63
64
            $fields->addFieldToTab(
65
                'Root.Options',
66
                HeaderField::create('OptionsHeader', 'Options can be added on Product Pages.', 3)
67
            );
68
        });
69
70
        return parent::getCMSFields();
71
    }
72
73
    /**
74
     * @throws \SilverStripe\ORM\ValidationException
75
     */
76
    public function requireDefaultRecords()
77
    {
78
        parent::requireDefaultRecords();
79
        // create a catch-all group
80
        if (!self::get()->filter(array('Title' => 'Options'))->first()) {
81
            $do = new self();
82
            $do->Title = 'Options';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'Options' of type string is incompatible with the declared type SilverStripe\ORM\FieldType\DBVarchar of property $Title.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
83
            $do->write();
84
        }
85
        if (!self::get()->filter(array('Title' => 'Size'))->first()) {
86
            $do = new self();
87
            $do->Title = 'Size';
88
            $do->write();
89
        }
90
        if (!self::get()->filter(array('Title' => 'Color'))->first()) {
91
            $do = new self();
92
            $do->Title = 'Color';
93
            $do->write();
94
        }
95
        if (!self::get()->filter(array('Title' => 'Type'))->first()) {
96
            $do = new self();
97
            $do->Title = 'Type';
98
            $do->write();
99
        }
100
    }
101
102
    /**
103
     * @return RequiredFields
104
     */
105
    public function getCMSValidator()
106
    {
107
        return new RequiredFields(array('Title'));
108
    }
109
110
    /**
111
     * @return ValidationResult
112
     */
113
    public function validate()
114
    {
115
        $result = parent::validate();
116
117
        $title = $this->Title;
118
        $firstChar = substr($title, 0, 1);
119
        if (preg_match('/[^a-zA-Z]/', $firstChar)) {
120
            $result->addError('The first character of the Title can only be a letter', 'bad');
121
        }
122
        if (preg_match('/[^a-zA-Z]\s/', $title)) {
123
            $result->addError('Please only use letters, numbers and spaces in the title', 'bad');
124
        }
125
126
        return $result;
127
    }
128
129
    /**
130
     * @throws \SilverStripe\ORM\ValidationException
131
     */
132
    public function onBeforeDelete()
133
    {
134
        parent::onBeforeDelete();
135
136
        //make sure that if we delete this option group, we reassign the group's option items to the 'None' group.
137
        $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID));
138
139
        if (isset($items)) {
140
            if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) {
141
                /** @var OptionItem $item */
142
                foreach ($items as $item) {
143
                    $item->ProductOptionGroupID = $noneGroup->ID;
144
                    $item->write();
145
                }
146
            }
147
        }
148
    }
149
150
    /**
151
     * @param bool $member
152
     *
153
     * @return bool
154
     */
155
    public function canView($member = null)
156
    {
157
        return true;
158
    }
159
160
    /**
161
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
162
     *
163
     * @return bool|int
164
     */
165
    public function canEdit($member = null)
166
    {
167
        switch ($this->Title) {
168
            case 'Options':
169
                return false;
170
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
171
            default:
172
                return Permission::check('Product_CANCRUD', 'any', $member);
173
                break;
174
        }
175
    }
176
177
    /**
178
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
179
     *
180
     * @return bool|int
181
     */
182
    public function canDelete($member = null)
183
    {
184
        return $this->canEdit($member);
185
    }
186
187
    /**
188
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
189
     * @param array $context
190
     *
191
     * @return bool|int
192
     */
193
    public function canCreate($member = null, $context = [])
194
    {
195
        return Permission::check('Product_CANCRUD', 'any', $member);
196
    }
197
}
198