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