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 | * Class OptionGroup |
||
12 | * @package Dynamic\FoxyStripe\Model |
||
13 | * |
||
14 | * @property \SilverStripe\ORM\FieldType\DBVarchar Title |
||
15 | * |
||
16 | * @method \SilverStripe\ORM\HasManyList Options |
||
17 | */ |
||
18 | class OptionGroup extends DataObject |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private static $db = array( |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
24 | 'Title' => 'Varchar(100)', |
||
25 | ); |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $has_many = array( |
||
0 ignored issues
–
show
|
|||
31 | 'Options' => OptionItem::class, |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private static $singular_name = 'Product Option Group'; |
||
0 ignored issues
–
show
|
|||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private static $plural_name = 'Product Option Groups'; |
||
0 ignored issues
–
show
|
|||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private static $description = 'Groups of product options, e.g. size, color, etc'; |
||
0 ignored issues
–
show
|
|||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private static $table_name = 'OptionGroup'; |
||
0 ignored issues
–
show
|
|||
53 | |||
54 | /** |
||
55 | * @throws \SilverStripe\ORM\ValidationException |
||
56 | */ |
||
57 | public function requireDefaultRecords() |
||
58 | { |
||
59 | parent::requireDefaultRecords(); |
||
60 | // create a catch-all group |
||
61 | if (!self::get()->filter(array('Title' => 'Options'))->first()) { |
||
62 | $do = new self(); |
||
63 | $do->Title = 'Options'; |
||
0 ignored issues
–
show
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.. ![]() |
|||
64 | $do->write(); |
||
65 | } |
||
66 | if (!self::get()->filter(array('Title' => 'Size'))->first()) { |
||
67 | $do = new self(); |
||
68 | $do->Title = 'Size'; |
||
69 | $do->write(); |
||
70 | } |
||
71 | if (!self::get()->filter(array('Title' => 'Color'))->first()) { |
||
72 | $do = new self(); |
||
73 | $do->Title = 'Color'; |
||
74 | $do->write(); |
||
75 | } |
||
76 | if (!self::get()->filter(array('Title' => 'Type'))->first()) { |
||
77 | $do = new self(); |
||
78 | $do->Title = 'Type'; |
||
79 | $do->write(); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return RequiredFields |
||
85 | */ |
||
86 | 1 | public function getCMSValidator() |
|
87 | { |
||
88 | 1 | return new RequiredFields(array('Title')); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return ValidationResult |
||
93 | */ |
||
94 | 13 | public function validate() |
|
95 | { |
||
96 | 13 | $result = parent::validate(); |
|
97 | |||
98 | 13 | $title = $this->Title; |
|
99 | 13 | $firstChar = substr($title, 0, 1); |
|
100 | 13 | if (preg_match('/[^a-zA-Z]/', $firstChar)) { |
|
101 | 1 | $result->addError('The first character of the Title can only be a letter', 'bad'); |
|
102 | } |
||
103 | 13 | if (preg_match('/[^a-zA-Z]\s/', $title)) { |
|
104 | $result->addError('Please only use letters, numbers and spaces in the title', 'bad'); |
||
105 | } |
||
106 | |||
107 | 13 | return $result; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @throws \SilverStripe\ORM\ValidationException |
||
112 | */ |
||
113 | 2 | public function onBeforeDelete() |
|
114 | { |
||
115 | 2 | parent::onBeforeDelete(); |
|
116 | |||
117 | //make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
||
118 | 2 | $items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
|
119 | |||
120 | 2 | if (isset($items)) { |
|
121 | 2 | if ($noneGroup = self::get()->filter(array('Title' => 'Options'))->first()) { |
|
122 | /** @var OptionItem $item */ |
||
123 | 2 | foreach ($items as $item) { |
|
124 | 1 | $item->ProductOptionGroupID = $noneGroup->ID; |
|
125 | 1 | $item->write(); |
|
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param bool $member |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 21 | public function canView($member = null) |
|
137 | { |
||
138 | 21 | return true; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param null $member |
||
0 ignored issues
–
show
|
|||
143 | * |
||
144 | * @return bool|int |
||
145 | */ |
||
146 | 3 | public function canEdit($member = null) |
|
147 | { |
||
148 | 3 | switch ($this->Title) { |
|
149 | 3 | case 'Options': |
|
150 | 2 | return false; |
|
151 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The 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 ![]() |
|||
152 | default: |
||
153 | 3 | return Permission::check('Product_CANCRUD', 'any', $member); |
|
154 | break; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param null $member |
||
0 ignored issues
–
show
|
|||
160 | * |
||
161 | * @return bool|int |
||
162 | */ |
||
163 | 2 | public function canDelete($member = null) |
|
164 | { |
||
165 | 2 | return $this->canEdit($member); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param null $member |
||
0 ignored issues
–
show
|
|||
170 | * @param array $context |
||
171 | * |
||
172 | * @return bool|int |
||
173 | */ |
||
174 | 1 | public function canCreate($member = null, $context = []) |
|
175 | { |
||
176 | 1 | return Permission::check('Product_CANCRUD', 'any', $member); |
|
177 | } |
||
178 | } |
||
179 |