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