|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @package FoxyStripe |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
class OptionGroup extends DataObject{ |
|
9
|
|
|
|
|
10
|
|
|
private static $db = array( |
|
11
|
|
|
'Title' => 'Varchar(100)' |
|
12
|
|
|
); |
|
13
|
|
|
|
|
14
|
|
|
private static $singular_name = 'Product Option Group'; |
|
15
|
|
|
private static $plural_name = 'Product Option Groups'; |
|
16
|
|
|
private static $description = 'Groups of product options, e.g. size, color, etc'; |
|
17
|
|
|
|
|
18
|
|
|
function getCMSFields(){ |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
$fields = parent::getCMSFields(); |
|
21
|
|
|
|
|
22
|
|
|
$this->extend('getCMSFields', $fields); |
|
23
|
|
|
|
|
24
|
|
|
return $fields; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function requireDefaultRecords() { |
|
28
|
|
|
parent::requireDefaultRecords(); |
|
29
|
|
|
// create a catch-all group |
|
30
|
|
View Code Duplication |
if(!OptionGroup::get()->filter(array('Title' => 'Options'))->first()) { |
|
|
|
|
|
|
31
|
|
|
$do = new OptionGroup(); |
|
32
|
|
|
$do->Title = "Options"; |
|
|
|
|
|
|
33
|
|
|
$do->write(); |
|
34
|
|
|
} |
|
35
|
|
View Code Duplication |
if(!OptionGroup::get()->filter(array('Title' => 'Size'))->first()) { |
|
|
|
|
|
|
36
|
|
|
$do = new OptionGroup(); |
|
37
|
|
|
$do->Title = "Size"; |
|
|
|
|
|
|
38
|
|
|
$do->write(); |
|
39
|
|
|
} |
|
40
|
|
View Code Duplication |
if(!OptionGroup::get()->filter(array('Title' => 'Color'))->first()) { |
|
|
|
|
|
|
41
|
|
|
$do = new OptionGroup(); |
|
42
|
|
|
$do->Title = "Color"; |
|
|
|
|
|
|
43
|
|
|
$do->write(); |
|
44
|
|
|
} |
|
45
|
|
View Code Duplication |
if(!OptionGroup::get()->filter(array('Title' => 'Type'))->first()) { |
|
|
|
|
|
|
46
|
|
|
$do = new OptionGroup(); |
|
47
|
|
|
$do->Title = "Type"; |
|
|
|
|
|
|
48
|
|
|
$do->write(); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getCMSValidator() { |
|
53
|
|
|
return new RequiredFields(array('Title')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function validate(){ |
|
57
|
|
|
$result = parent::validate(); |
|
58
|
|
|
|
|
59
|
|
|
$title = $this->Title; |
|
|
|
|
|
|
60
|
|
|
$firstChar = substr($title, 0, 1); |
|
61
|
|
|
if(preg_match('/[^a-zA-Z]/', $firstChar)){ |
|
62
|
|
|
$result->error('The first character of the Title can only be a letter', 'bad'); |
|
63
|
|
|
} |
|
64
|
|
|
if(preg_match('/[^a-zA-Z]\s/', $title)){ |
|
65
|
|
|
$result->error('Please only use letters, numbers and spaces in the title', 'bad'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function onBeforeDelete(){ |
|
72
|
|
|
parent::onBeforeDelete(); |
|
73
|
|
|
|
|
74
|
|
|
//make sure that if we delete this option group, we reassign the group's option items to the 'None' group. |
|
75
|
|
|
$items = OptionItem::get()->filter(array('ProductOptionGroupID' => $this->ID)); |
|
76
|
|
|
|
|
77
|
|
|
if(isset($items)){ |
|
78
|
|
|
$noneGroup = OptionGroup::get()->filter(array('Title' => 'Options'))->first(); |
|
79
|
|
|
foreach($items as $item){ |
|
80
|
|
|
$item->ProductOptionGroupID = $noneGroup->ID; |
|
81
|
|
|
$item->write(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function canView($member = false) { |
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function canEdit($member = null) { |
|
91
|
|
|
switch($this->Title){ |
|
|
|
|
|
|
92
|
|
|
case 'Options': |
|
93
|
|
|
return false; |
|
94
|
|
|
break; |
|
|
|
|
|
|
95
|
|
|
default: |
|
96
|
|
|
return Permission::check('Product_CANCRUD'); |
|
97
|
|
|
break; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function canDelete($member = null) { |
|
103
|
|
|
return $this->canEdit(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function canCreate($member = null) { |
|
107
|
|
|
return Permission::check('Product_CANCRUD'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.