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