Passed
Pull Request — master (#309)
by Jason
13:47
created

OptionGroup::requireDefaultRecords()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 17
nc 16
nop 0
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use SilverStripe\Forms\RequiredFields;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\ValidationResult;
8
use SilverStripe\Security\Permission;
9
10
/**
11
 *
12
 * @package FoxyStripe
13
 *
14
 */
15
16
class OptionGroup extends DataObject
17
{
18
    /**
19
     * @var array
20
     */
21
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
22
        'Title' => 'Varchar(100)'
23
    );
24
25
    /**
26
     * @var array
27
     */
28
    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...
29
        'Options' => OptionItem::class,
30
    );
31
32
    /**
33
     * @var string
34
     */
35
    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...
36
37
    /**
38
     * @var string
39
     */
40
    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...
41
42
    /**
43
     * @var string
44
     */
45
    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...
46
47
    /**
48
     * @var string
49
     */
50
    private static $table_name = 'FS_OptionGroup';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
51
52
    /**
53
     *
54
     */
55
    public function requireDefaultRecords()
56
    {
57
        parent::requireDefaultRecords();
58
        // create a catch-all group
59
        if (!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) {
60
            $do = new OptionGroup();
61
            $do->Title = "Options";
0 ignored issues
show
Bug Best Practice introduced by
The property Title does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
            $do->write();
63
        }
64
        if (!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) {
65
            $do = new OptionGroup();
66
            $do->Title = "Size";
67
            $do->write();
68
        }
69
        if (!OptionGroup::get()->filter(array('Title' => 'Color'))->first()) {
70
            $do = new OptionGroup();
71
            $do->Title = "Color";
72
            $do->write();
73
        }
74
        if (!OptionGroup::get()->filter(array('Title' => 'Type'))->first()) {
75
            $do = new OptionGroup();
76
            $do->Title = "Type";
77
            $do->write();
78
        }
79
    }
80
81
    /**
82
     * @return RequiredFields
83
     */
84
    public function getCMSValidator()
85
    {
86
        return new RequiredFields(array('Title'));
87
    }
88
89
    /**
90
     * @return ValidationResult
91
     */
92
    public function validate()
93
    {
94
        $result = parent::validate();
95
96
        $title = $this->Title;
97
        $firstChar = substr($title, 0, 1);
98
        if (preg_match('/[^a-zA-Z]/', $firstChar)) {
99
            $result->addError('The first character of the Title can only be a letter', 'bad');
100
        }
101
        if (preg_match('/[^a-zA-Z]\s/', $title)) {
102
            $result->addError('Please only use letters, numbers and spaces in the title', 'bad');
103
        }
104
105
        return $result;
106
    }
107
108
    /**
109
     *
110
     */
111
    public function onBeforeDelete()
112
    {
113
        parent::onBeforeDelete();
114
115
        //make sure that if we delete this option group, we reassign the group's option items to the 'None' group.
116
        $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID));
117
118
        if (isset($items)) {
119
            if ($noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first()) {
120
                foreach ($items as $item) {
121
                    $item->ProductOptionGroupID = $noneGroup->ID;
122
                    $item->write();
123
                }
124
            }
125
        }
126
    }
127
128
    /**
129
     * @param bool $member
130
     * @return bool
131
     */
132
    public function canView($member = false)
133
    {
134
        return true;
135
    }
136
137
    /**
138
     * @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...
139
     * @return bool|int
140
     */
141
    public function canEdit($member = null)
142
    {
143
        switch ($this->Title) {
144
            case 'Options':
145
                return false;
146
                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...
147
            default:
148
                return Permission::check('Product_CANCRUD', 'any', $member);
149
                break;
150
        }
151
    }
152
153
    /**
154
     * @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...
155
     * @return bool|int
156
     */
157
    public function canDelete($member = null)
158
    {
159
        return $this->canEdit($member);
160
    }
161
162
    /**
163
     * @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...
164
     * @return bool|int
165
     */
166
    public function canCreate($member = null, $context = [])
167
    {
168
        return Permission::check('Product_CANCRUD', 'any', $member);
169
    }
170
}
171