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