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( |
|
|
|
|
22
|
|
|
'Title' => 'Varchar(100)' |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $has_many = array( |
|
|
|
|
29
|
|
|
'Options' => OptionItem::class, |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private static $singular_name = 'Product Option Group'; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $plural_name = 'Product Option Groups'; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private static $table_name = 'FS_OptionGroup'; |
|
|
|
|
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"; |
|
|
|
|
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 |
|
|
|
|
139
|
|
|
* @return bool|int |
140
|
|
|
*/ |
141
|
|
|
public function canEdit($member = null) |
142
|
|
|
{ |
143
|
|
|
switch ($this->Title) { |
144
|
|
|
case 'Options': |
145
|
|
|
return false; |
146
|
|
|
break; |
|
|
|
|
147
|
|
|
default: |
148
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param null $member |
|
|
|
|
155
|
|
|
* @return bool|int |
156
|
|
|
*/ |
157
|
|
|
public function canDelete($member = null) |
158
|
|
|
{ |
159
|
|
|
return $this->canEdit($member); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param null $member |
|
|
|
|
164
|
|
|
* @return bool|int |
165
|
|
|
*/ |
166
|
|
|
public function canCreate($member = null, $context = []) |
167
|
|
|
{ |
168
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|