|
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( |
|
|
|
|
|
|
26
|
|
|
'Title' => 'Varchar(100)', |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $has_many = array( |
|
|
|
|
|
|
33
|
|
|
'Options' => OptionItem::class, |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $singular_name = 'Product Option Group'; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $plural_name = 'Product Option Groups'; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private static $table_name = 'OptionGroup'; |
|
|
|
|
|
|
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'; |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
171
|
|
|
default: |
|
172
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
|
173
|
|
|
break; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param null $member |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|