1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Model; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
6
|
|
|
use SilverStripe\Control\Session; |
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
8
|
|
|
use SilverStripe\Forms\CurrencyField; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\HeaderField; |
12
|
|
|
use SilverStripe\Forms\NumericField; |
13
|
|
|
use SilverStripe\Forms\TextField; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
use SilverStripe\ORM\ValidationResult; |
16
|
|
|
use SilverStripe\Security\Permission; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class OptionItem |
20
|
|
|
*/ |
21
|
|
|
class OptionItem extends DataObject |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $db = array( |
|
|
|
|
27
|
|
|
'Title' => 'Text', |
28
|
|
|
'WeightModifier' => 'Int', |
29
|
|
|
'CodeModifier' => 'Text', |
30
|
|
|
'PriceModifier' => 'Currency', |
31
|
|
|
'WeightModifierAction' => "Enum('Add,Subtract,Set','Add')", |
32
|
|
|
'CodeModifierAction' => "Enum('Add,Subtract,Set','Add')", |
33
|
|
|
'PriceModifierAction' => "Enum('Add,Subtract,Set','Add')", |
34
|
|
|
'Available' => 'Boolean', |
35
|
|
|
'SortOrder' => 'Int' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private static $has_one = array( |
|
|
|
|
42
|
|
|
'Product' => ProductPage::class, |
43
|
|
|
'ProductOptionGroup' => OptionGroup::class, |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
50
|
|
|
'OrderDetails' => OrderDetail::class |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private static $defaults = array( |
|
|
|
|
57
|
|
|
'Available' => true |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private static $summary_fields = array( |
|
|
|
|
64
|
|
|
'Title' => 'Title', |
65
|
|
|
'ProductOptionGroup.Title' => 'Group', |
66
|
|
|
'IsAvailable' => 'Available' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private static $searchable_fields = [ |
|
|
|
|
73
|
|
|
'Title' => [ |
74
|
|
|
'title' => 'Title', |
75
|
|
|
], |
76
|
|
|
'ProductOptionGroup.Title' => [ |
77
|
|
|
'title' => 'Group' |
78
|
|
|
], |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
private static $default_sort = 'SortOrder'; |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
private static $table_name = 'FS_OptionItem'; |
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return FieldList |
93
|
|
|
*/ |
94
|
|
|
public function getCMSFields() |
95
|
|
|
{ |
96
|
|
|
$fields = parent::getCMSFields(); |
97
|
|
|
|
98
|
|
|
$fields->removeByName([ |
99
|
|
|
'OrderDetails', |
100
|
|
|
'SortOrder', |
101
|
|
|
'ProductID', |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
// set variables from Product |
105
|
|
|
$productID = ($this->ProductID != 0) ? $this->ProductID : Session::get('CMSMain.currentPage'); |
|
|
|
|
106
|
|
|
$product = ProductPage::get()->byID($productID); |
107
|
|
|
|
108
|
|
|
$parentPrice = $product->obj('Price')->Nice(); |
109
|
|
|
$parentWeight = $product->Weight; |
110
|
|
|
$parentCode = $product->Code; |
111
|
|
|
|
112
|
|
|
// ProductOptionGroup Dropdown field w/ add new |
113
|
|
|
$groups = function () { |
114
|
|
|
return OptionGroup::get()->map()->toArray(); |
115
|
|
|
}; |
116
|
|
|
$groupFields = singleton(OptionGroup::class)->getCMSFields(); |
117
|
|
|
$groupField = DropdownField::create('ProductOptionGroupID', _t("OptionItem.Group", "Group"), $groups()) |
|
|
|
|
118
|
|
|
->setEmptyString('') |
119
|
|
|
->setDescription(_t('OptionItem.GroupDescription', 'Name of this group of options. Managed in <a href="admin/settings">Settings > FoxyStripe > Option Groups</a>')); |
120
|
|
|
if (class_exists('QuickAddNewExtension')) { |
121
|
|
|
$groupField->useAddNew('OptionGroup', $groups, $groupFields); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
125
|
|
|
TextField::create('Title') |
126
|
|
|
->setTitle(_t("OptionItem.Title", "Product Option Name")), |
127
|
|
|
CheckboxField::create('Available') |
128
|
|
|
->setTitle(_t("OptionItem.Available", "Available for purchase")) |
129
|
|
|
->setDescription(_t('OptionItem.AvailableDescription', "If unchecked, will disable this option in the drop down menu")), |
130
|
|
|
$groupField |
131
|
|
|
)); |
132
|
|
|
|
133
|
|
|
$fields->addFieldsToTab('Root.Modifiers', array( |
134
|
|
|
HeaderField::create('ModifyHD', _t('OptionItem.ModifyHD', 'Product Option Modifiers'), 2), |
|
|
|
|
135
|
|
|
|
136
|
|
|
// Weight Modifier Fields |
137
|
|
|
HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 3), |
138
|
|
|
NumericField::create('WeightModifier') |
139
|
|
|
->setTitle(_t('OptionItem.WeightModifier', 'Weight')), |
140
|
|
|
DropdownField::create('WeightModifierAction', _t('OptionItem.WeightModifierAction', 'Weight Modification'), |
141
|
|
|
array( |
142
|
|
|
'Add' => _t( |
143
|
|
|
'OptionItem.WeightAdd', |
144
|
|
|
"Add to Base Weight ({weight})", |
145
|
|
|
'Add to weight', |
146
|
|
|
array('weight' => $parentWeight) |
147
|
|
|
), |
148
|
|
|
'Subtract' => _t( |
149
|
|
|
'OptionItem.WeightSubtract', |
150
|
|
|
"Subtract from Base Weight ({weight})", |
151
|
|
|
'Subtract from weight', |
152
|
|
|
array('weight' => $parentWeight) |
153
|
|
|
), |
154
|
|
|
'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight') |
155
|
|
|
) |
156
|
|
|
)->setEmptyString('') |
157
|
|
|
->setDescription(_t('OptionItem.WeightDescription', 'Does weight modify or replace base weight?')), |
158
|
|
|
|
159
|
|
|
// Price Modifier FIelds |
160
|
|
|
HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 3), |
161
|
|
|
CurrencyField::create('PriceModifier') |
162
|
|
|
->setTitle(_t('OptionItem.PriceModifier', 'Price')), |
163
|
|
|
DropdownField::create('PriceModifierAction', _t('OptionItem.PriceModifierAction', 'Price Modification'), |
164
|
|
|
array( |
165
|
|
|
'Add' => _t( |
166
|
|
|
'OptionItem.PriceAdd', |
167
|
|
|
"Add to Base Price ({price})", |
168
|
|
|
'Add to price', |
169
|
|
|
array('price' => $parentPrice) |
170
|
|
|
), |
171
|
|
|
'Subtract' => _t( |
172
|
|
|
'OptionItem.PriceSubtract', |
173
|
|
|
"Subtract from Base Price ({price})", |
174
|
|
|
'Subtract from price', |
175
|
|
|
array('price' => $parentPrice) |
176
|
|
|
), |
177
|
|
|
'Set' => _t('OptionItem.PriceSet', 'Set as a new Price') |
178
|
|
|
) |
179
|
|
|
)->setEmptyString('') |
180
|
|
|
->setDescription(_t('OptionItem.PriceDescription', 'Does price modify or replace base price?')), |
181
|
|
|
|
182
|
|
|
// Code Modifier Fields |
183
|
|
|
HeaderField::create('CodeHD', _t('OptionItem.CodeHD', 'Modify Code'), 3), |
184
|
|
|
TextField::create('CodeModifier') |
185
|
|
|
->setTitle(_t('OptionItem.CodeModifier', 'Code')), |
186
|
|
|
DropdownField::create('CodeModifierAction', _t('OptionItem.CodeModifierAction', 'Code Modification'), |
187
|
|
|
array( |
188
|
|
|
'Add' => _t( |
189
|
|
|
'OptionItem.CodeAdd', |
190
|
|
|
"Add to Base Code ({code})", |
191
|
|
|
'Add to code', |
192
|
|
|
array('code' => $parentCode) |
193
|
|
|
), |
194
|
|
|
'Subtract' => _t( |
195
|
|
|
'OptionItem.CodeSubtract', |
196
|
|
|
'Subtract from Base Code ({code})', |
197
|
|
|
'Subtract from code', |
198
|
|
|
array('code' => $parentCode) |
199
|
|
|
), |
200
|
|
|
'Set' => _t('OptionItem.CodeSet', 'Set as a new Code') |
201
|
|
|
) |
202
|
|
|
)->setEmptyString('') |
203
|
|
|
->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')) |
204
|
|
|
)); |
205
|
|
|
|
206
|
|
|
/* |
|
|
|
|
207
|
|
|
// Cateogry Dropdown field w/ add new |
208
|
|
|
// removed until relevance determined |
209
|
|
|
$categories = function(){ |
210
|
|
|
return ProductCategory::get()->map()->toArray(); |
211
|
|
|
}; |
212
|
|
|
|
213
|
|
|
// to do - have OptionItem category override set ProductPage category if selected: issue #155 |
214
|
|
|
$categoryField = DropdownField::create('CategoryID', 'Category', $categories()) |
215
|
|
|
->setEmptyString('') |
216
|
|
|
->setDescription('Categories can be managed in <a href="admin/settings">Settings > FoxyStripe > Categories</a>'); |
217
|
|
|
if (class_exists('QuickAddNewExtension')) $categoryField->useAddNew('ProductCategory', $categories); |
218
|
|
|
|
219
|
|
|
$fields->insertAfter($categoryField, 'ProductOptionGroupID'); |
220
|
|
|
*/ |
221
|
|
|
|
222
|
|
|
return $fields; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return ValidationResult |
227
|
|
|
*/ |
228
|
|
|
public function validate() |
229
|
|
|
{ |
230
|
|
|
$result = parent::validate(); |
231
|
|
|
|
232
|
|
|
if ($this->ProductOptionGroupID == 0) { |
|
|
|
|
233
|
|
|
$result->addError('Must set a Group prior to saving'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $result; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param $oma |
241
|
|
|
* @param bool $returnWithOnlyPlusMinus |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus=false) |
245
|
|
|
{ |
246
|
|
|
switch ($oma) { |
247
|
|
|
case 'Subtract': |
248
|
|
|
$symbol = '-'; |
249
|
|
|
break; |
250
|
|
|
case 'Set': |
251
|
|
|
$symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
252
|
|
|
break; |
253
|
|
|
default: |
254
|
|
|
$symbol = '+'; |
255
|
|
|
} |
256
|
|
|
return $symbol; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function getWeightModifierWithSymbol() |
263
|
|
|
{ |
264
|
|
|
return self::getOptionModifierActionSymbol($this->WeightModifierAction).$this->WeightModifier; |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
public function getPriceModifierWithSymbol() |
271
|
|
|
{ |
272
|
|
|
return self::getOptionModifierActionSymbol($this->PriceModifierAction).$this->PriceModifier; |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
public function getCodeModifierWithSymbol() |
279
|
|
|
{ |
280
|
|
|
return self::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return mixed |
285
|
|
|
*/ |
286
|
|
|
public function getProductOptionGroupTitle() |
287
|
|
|
{ |
288
|
|
|
return $this->ProductOptionGroup()->Title; |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return string |
293
|
|
|
*/ |
294
|
|
|
public function getGeneratedValue() |
295
|
|
|
{ |
296
|
|
|
$modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
|
|
|
297
|
|
|
$modPriceWithSymbol = OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction).$modPrice; |
|
|
|
|
298
|
|
|
$modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
|
|
|
|
299
|
|
|
$modWeight = OptionItem::getOptionModifierActionSymbol($this->WeightModifierAction).$modWeight; |
|
|
|
|
300
|
|
|
$modCode = OptionItem::getOptionModifierActionSymbol($this->CodeModifierAction).$this->CodeModifier; |
|
|
|
|
301
|
|
|
return $this->Title.'{p'.$modPriceWithSymbol.'|w'.$modWeight.'|c'.$modCode.'}'; |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return mixed|string |
306
|
|
|
*/ |
307
|
|
|
public function getGeneratedTitle() |
308
|
|
|
{ |
309
|
|
|
$modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
|
|
|
310
|
|
|
$title = $this->Title; |
|
|
|
|
311
|
|
|
$title .= ($this->PriceModifier != 0) ? ': ('.OptionItem::getOptionModifierActionSymbol($this->PriceModifierAction, $returnWithOnlyPlusMinus=true).'$'.$modPrice.')' : ''; |
|
|
|
|
312
|
|
|
return $title; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
public function getAvailability() |
319
|
|
|
{ |
320
|
|
|
$available = ($this->Available == 1) ? true : false ; |
|
|
|
|
321
|
|
|
|
322
|
|
|
$this->extend('updateOptionAvailability', $available); |
323
|
|
|
|
324
|
|
|
return $available; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
public function getIsAvailable() |
331
|
|
|
{ |
332
|
|
|
if ($this->getAvailability()) { |
333
|
|
|
return "yes"; |
334
|
|
|
} |
335
|
|
|
return "no"; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param bool $member |
340
|
|
|
* @return bool |
341
|
|
|
*/ |
342
|
|
|
public function canView($member = false) |
343
|
|
|
{ |
344
|
|
|
return true; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param null $member |
|
|
|
|
349
|
|
|
* @return bool|int |
350
|
|
|
*/ |
351
|
|
|
public function canEdit($member = null) |
352
|
|
|
{ |
353
|
|
|
return Permission::check('Product_CANCRUD'); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param null $member |
|
|
|
|
358
|
|
|
* @return bool|int |
359
|
|
|
*/ |
360
|
|
|
public function canDelete($member = null) |
361
|
|
|
{ |
362
|
|
|
return Permission::check('Product_CANCRUD'); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param null $member |
|
|
|
|
367
|
|
|
* @return bool|int |
368
|
|
|
*/ |
369
|
|
|
public function canCreate($member = null, $context = []) |
370
|
|
|
{ |
371
|
|
|
return Permission::check('Product_CANCRUD'); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|