|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @package FoxyStripe |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
class OptionItem extends DataObject{ |
|
9
|
|
|
|
|
10
|
|
|
private static $db = array( |
|
11
|
|
|
'Title' => 'Text', |
|
12
|
|
|
'WeightModifier' => 'Int', |
|
13
|
|
|
'CodeModifier' => 'Text', |
|
14
|
|
|
'PriceModifier' => 'Currency', |
|
15
|
|
|
'WeightModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
16
|
|
|
'CodeModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
17
|
|
|
'PriceModifierAction' => "Enum('Add,Subtract,Set','Add')", |
|
18
|
|
|
'Available' => 'Boolean', |
|
19
|
|
|
'SortOrder' => 'Int' |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
private static $has_one = array( |
|
23
|
|
|
'Product' => 'ProductPage', |
|
24
|
|
|
'ProductOptionGroup' => 'OptionGroup', |
|
25
|
|
|
//'Category' => 'ProductCategory' |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
private static $belongs_many_many = array( |
|
29
|
|
|
'OrderDetails' => 'OrderDetail' |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
private static $defaults = array( |
|
33
|
|
|
'Available' => true |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
private static $summary_fields = array( |
|
37
|
|
|
'Title' => 'Title', |
|
38
|
|
|
'ProductOptionGroup.Title' => 'Group', |
|
39
|
|
|
'Available.Nice' => 'Available' |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
private static $default_sort = 'SortOrder'; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
public function getCMSFields(){ |
|
45
|
|
|
$fields = FieldList::create( |
|
46
|
|
|
new Tabset('Root', |
|
47
|
|
|
new Tab('Main'), |
|
48
|
|
|
new Tab('Modifiers') |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
// set variables from Product |
|
53
|
|
|
$productID = ($this->ProductID != 0) ? $this->ProductID : Session::get('CMSMain.currentPage'); |
|
|
|
|
|
|
54
|
|
|
$product = ProductPage::get()->byID($productID); |
|
55
|
|
|
|
|
56
|
|
|
$parentPrice = $product->obj('Price')->Nice(); |
|
57
|
|
|
$parentWeight = $product->Weight; |
|
58
|
|
|
$parentCode = $product->Code; |
|
59
|
|
|
|
|
60
|
|
|
// ProductOptionGroup Dropdown field w/ add new |
|
61
|
|
|
$groups = function(){ |
|
62
|
|
|
return OptionGroup::get()->map()->toArray(); |
|
63
|
|
|
}; |
|
64
|
|
|
$groupFields = singleton('OptionGroup')->getCMSFields(); |
|
65
|
|
|
$groupField = DropdownField::create('ProductOptionGroupID', _t("OptionItem.Group", "Group"), $groups()) |
|
66
|
|
|
->setEmptyString('') |
|
67
|
|
|
->setDescription(_t('OptionItem.GroupDescription', 'Name of this group of options. Managed in <a href="admin/settings">Settings > FoxyStripe > Option Groups</a>')); |
|
68
|
|
|
if (class_exists('QuickAddNewExtension')) $groupField->useAddNew('OptionGroup', $groups, $groupFields); |
|
69
|
|
|
|
|
70
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
|
71
|
|
|
HeaderField::create('DetailsHD', _t("OptionItem.DetailsHD", "Product Option Details"), 2), |
|
72
|
|
|
Textfield::create('Title') |
|
73
|
|
|
->setTitle(_t("OptionItem.Title", "Product Option Name")), |
|
74
|
|
|
CheckboxField::create('Available') |
|
75
|
|
|
->setTitle( _t("OptionItem.Available", "Available for purchase")) |
|
76
|
|
|
->setDescription(_t('OptionItem.AvailableDescription', "If unchecked, will disable this option in the drop down menu")), |
|
77
|
|
|
$groupField |
|
78
|
|
|
)); |
|
79
|
|
|
|
|
80
|
|
|
$fields->addFieldsToTab('Root.Modifiers', array( |
|
81
|
|
|
HeaderField::create('ModifyHD', _t('OptionItem.ModifyHD', 'Product Option Modifiers'), 2), |
|
82
|
|
|
|
|
83
|
|
|
// Weight Modifier Fields |
|
84
|
|
|
HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 3), |
|
85
|
|
|
NumericField::create('WeightModifier') |
|
86
|
|
|
->setTitle(_t('OptionItem.WeightModifier', 'Weight')), |
|
87
|
|
|
DropdownField::create('WeightModifierAction', _t('OptionItem.WeightModifierAction', 'Weight Modification'), |
|
88
|
|
|
array( |
|
89
|
|
|
'Add' => _t( |
|
90
|
|
|
'OptionItem.WeightAdd', |
|
91
|
|
|
"Add to Base Weight ({weight})", |
|
92
|
|
|
'Add to weight', |
|
93
|
|
|
array('weight' => $parentWeight) |
|
|
|
|
|
|
94
|
|
|
), |
|
95
|
|
|
'Subtract' => _t( |
|
96
|
|
|
'OptionItem.WeightSubtract', |
|
97
|
|
|
"Subtract from Base Weight ({weight})", |
|
98
|
|
|
'Subtract from weight', |
|
99
|
|
|
array('weight' => $parentWeight) |
|
|
|
|
|
|
100
|
|
|
), |
|
101
|
|
|
'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight') |
|
102
|
|
|
) |
|
103
|
|
|
)->setEmptyString('') |
|
104
|
|
|
->setDescription(_t('OptionItem.WeightDescription', 'Does weight modify or replace base weight?')), |
|
105
|
|
|
|
|
106
|
|
|
// Price Modifier FIelds |
|
107
|
|
|
HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 3), |
|
108
|
|
|
CurrencyField::create('PriceModifier') |
|
109
|
|
|
->setTitle(_t('OptionItem.PriceModifier', 'Price')), |
|
110
|
|
|
DropdownField::create('PriceModifierAction', _t('OptionItem.PriceModifierAction', 'Price Modification'), |
|
111
|
|
|
array( |
|
112
|
|
|
'Add' => _t( |
|
113
|
|
|
'OptionItem.PriceAdd', |
|
114
|
|
|
"Add to Base Price ({price})", |
|
115
|
|
|
'Add to price', |
|
116
|
|
|
array('price' => $parentPrice) |
|
|
|
|
|
|
117
|
|
|
), |
|
118
|
|
|
'Subtract' => _t( |
|
119
|
|
|
'OptionItem.PriceSubtract', |
|
120
|
|
|
"Subtract from Base Price ({price})", |
|
121
|
|
|
'Subtract from price', |
|
122
|
|
|
array('price' => $parentPrice) |
|
|
|
|
|
|
123
|
|
|
), |
|
124
|
|
|
'Set' => _t('OptionItem.PriceSet', 'Set as a new Price') |
|
125
|
|
|
) |
|
126
|
|
|
)->setEmptyString('') |
|
127
|
|
|
->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')), |
|
128
|
|
|
|
|
129
|
|
|
// Code Modifier Fields |
|
130
|
|
|
HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 3), |
|
131
|
|
|
TextField::create('CodeModifier') |
|
132
|
|
|
->setTitle(_t('OptionItem.CodeModifier', 'Code')), |
|
133
|
|
|
DropdownField::create('CodeModifierAction', _t('OptionItem.CodeModifierAction', 'Code Modification'), |
|
134
|
|
|
array( |
|
135
|
|
|
'Add' => _t( |
|
136
|
|
|
'OptionItem.CodeAdd', |
|
137
|
|
|
"Add to Base Code ({code})", |
|
138
|
|
|
'Add to code', |
|
139
|
|
|
array('code' => $parentCode) |
|
|
|
|
|
|
140
|
|
|
), |
|
141
|
|
|
'Subtract' => _t( |
|
142
|
|
|
'OptionItem.CodeSubtract', |
|
143
|
|
|
'Subtract from Base Code ({code})', |
|
144
|
|
|
'Subtract from code', |
|
145
|
|
|
array('code' => $parentCode) |
|
|
|
|
|
|
146
|
|
|
), |
|
147
|
|
|
'Set' => _t('OptionItem.CodeSet', 'Set as a new Code') |
|
148
|
|
|
) |
|
149
|
|
|
)->setEmptyString('') |
|
150
|
|
|
->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')) |
|
151
|
|
|
)); |
|
152
|
|
|
|
|
153
|
|
|
/* |
|
|
|
|
|
|
154
|
|
|
// Cateogry Dropdown field w/ add new |
|
155
|
|
|
// removed until relevance determined |
|
156
|
|
|
$categories = function(){ |
|
157
|
|
|
return ProductCategory::get()->map()->toArray(); |
|
158
|
|
|
}; |
|
159
|
|
|
|
|
160
|
|
|
// to do - have OptionItem category override set ProductPage category if selected: issue #155 |
|
161
|
|
|
$categoryField = DropdownField::create('CategoryID', 'Category', $categories()) |
|
162
|
|
|
->setEmptyString('') |
|
163
|
|
|
->setDescription('Categories can be managed in <a href="admin/settings">Settings > FoxyStripe > Categories</a>'); |
|
164
|
|
|
if (class_exists('QuickAddNewExtension')) $categoryField->useAddNew('ProductCategory', $categories); |
|
165
|
|
|
|
|
166
|
|
|
$fields->insertAfter($categoryField, 'ProductOptionGroupID'); |
|
167
|
|
|
*/ |
|
168
|
|
|
|
|
169
|
|
|
// allow CMS fields to be extended |
|
170
|
|
|
$this->extend('getCMSFields', $fields); |
|
171
|
|
|
|
|
172
|
|
|
return $fields; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function validate(){ |
|
176
|
|
|
$result = parent::validate(); |
|
177
|
|
|
|
|
178
|
|
|
if($this->ProductOptionGroupID == 0){ |
|
|
|
|
|
|
179
|
|
|
$result->error('Must set a Group prior to saving'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $result; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus=false){ |
|
186
|
|
|
switch($oma){ |
|
187
|
|
|
case 'Subtract': |
|
188
|
|
|
$symbol = '-'; |
|
189
|
|
|
break; |
|
190
|
|
|
case 'Set': |
|
191
|
|
|
$symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
|
192
|
|
|
break; |
|
193
|
|
|
default: |
|
194
|
|
|
$symbol = '+'; |
|
195
|
|
|
} |
|
196
|
|
|
return $symbol; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getWeightModifierWithSymbol(){ |
|
200
|
|
|
return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function getPriceModifierWithSymbol(){ |
|
204
|
|
|
return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function getCodeModifierWithSymbol(){ |
|
208
|
|
|
return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
|
|
|
|
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function getProductOptionGroupTitle(){ |
|
212
|
|
|
return $this->ProductOptionGroup()->Title; |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function getGeneratedValue(){ |
|
216
|
|
|
$modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
|
|
|
|
|
217
|
|
|
$modPriceWithSymbol = OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
|
|
|
|
|
|
218
|
|
|
$modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
|
|
|
|
|
|
219
|
|
|
$modWeight = OptionItem::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
|
|
|
|
|
|
220
|
|
|
$modCode = OptionItem::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
|
|
|
|
|
221
|
|
|
return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function getGeneratedTitle(){ |
|
225
|
|
|
$modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
|
|
|
|
|
226
|
|
|
$title = $this->Title; |
|
|
|
|
|
|
227
|
|
|
$title .= ($this->PriceModifier != 0) ? ': ('.OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction, $returnWithOnlyPlusMinus=true).'$'.$modPrice.')' : ''; |
|
|
|
|
|
|
228
|
|
|
return $title; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function getAvailability(){ |
|
232
|
|
|
return ($this->Available == 0) ? true : false ; |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function canView($member = false) { |
|
236
|
|
|
return true; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function canEdit($member = null) { |
|
240
|
|
|
return Permission::check('Product_CANCRUD'); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function canDelete($member = null) { |
|
244
|
|
|
return Permission::check('Product_CANCRUD'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function canCreate($member = null) { |
|
248
|
|
|
return Permission::check('Product_CANCRUD'); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
} |
|
252
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.