1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\Forms\CheckboxField; |
7
|
|
|
use SilverStripe\Forms\CurrencyField; |
8
|
|
|
use SilverStripe\Forms\DropdownField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\HeaderField; |
11
|
|
|
use SilverStripe\Forms\ReadonlyField; |
12
|
|
|
use SilverStripe\Forms\TextField; |
13
|
|
|
use SilverStripe\ORM\DataObject; |
14
|
|
|
use SilverStripe\ORM\FieldType\DBBoolean; |
15
|
|
|
use SilverStripe\Security\Permission; |
16
|
|
|
use SilverStripe\Security\Security; |
17
|
|
|
|
18
|
|
|
class ProductOption extends DataObject |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $db = [ |
24
|
|
|
'Title' => 'Varchar(255)', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private static $table_name = 'ProductOption'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private static $singular_name = 'Option'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private static $plural_name = 'Options'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private static $defaults = [ |
46
|
|
|
'ManyMany[Available]' => true, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private static $summary_fields = [ |
53
|
|
|
'Title' => 'Title', |
54
|
|
|
'IsAvailable' => 'Available', |
55
|
|
|
'OptionType' => 'Type', |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return FieldList |
60
|
|
|
*/ |
61
|
|
|
public function getCMSFields() |
62
|
|
|
{ |
63
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
64
|
|
|
if ($this->exists()) { |
65
|
|
|
$fields->addFieldToTab( |
66
|
|
|
'Root.Main', |
67
|
|
|
ReadonlyField::create('ManyMany[OptionModifierKey]') |
68
|
|
|
->setTitle(_t('OptionItem.ModifierKey', 'Modifier Key')) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$fields->addFieldsToTab('Root.Main', [ |
73
|
|
|
CheckboxField::create('ManyMany[Available]', 'Available for purchase'), |
74
|
|
|
|
75
|
|
|
DropdownField::create('ManyMany[Type]', 'Option Type', OptionType::get()->map()) |
76
|
|
|
->setEmptyString(''), |
77
|
|
|
|
78
|
|
|
// Weight Modifier Fields |
79
|
|
|
HeaderField::create('WeightHD', _t('OptionItem.WeightHD', 'Modify Weight'), 4), |
80
|
|
|
TextField::create("ManyMany[WeightModifier]") |
81
|
|
|
->setTitle(_t('OptionItem.WeightModifier', 'Weight')), |
82
|
|
|
DropdownField::create( |
83
|
|
|
'ManyMany[WeightModifierAction]', |
84
|
|
|
_t('OptionItem.WeightModifierAction', 'Weight Modification'), |
85
|
|
|
[ |
86
|
|
|
'Add' => _t( |
87
|
|
|
'OptionItem.WeightAdd', |
88
|
|
|
'Add to Base Weight', |
89
|
|
|
'Add to weight' |
90
|
|
|
), |
91
|
|
|
'Subtract' => _t( |
92
|
|
|
'OptionItem.WeightSubtract', |
93
|
|
|
'Subtract from Base Weight', |
94
|
|
|
'Subtract from weight' |
95
|
|
|
), |
96
|
|
|
'Set' => _t('OptionItem.WeightSet', 'Set as a new Weight'), |
97
|
|
|
] |
98
|
|
|
) |
99
|
|
|
->setEmptyString('') |
100
|
|
|
->setDescription(_t( |
101
|
|
|
'OptionItem.WeightDescription', |
102
|
|
|
'Does weight modify or replace base weight?' |
103
|
|
|
)), |
104
|
|
|
|
105
|
|
|
// Price Modifier FIelds |
106
|
|
|
HeaderField::create('PriceHD', _t('OptionItem.PriceHD', 'Modify Price'), 4), |
107
|
|
|
CurrencyField::create('ManyMany[PriceModifier]') |
108
|
|
|
->setTitle(_t('OptionItem.PriceModifier', 'Price')), |
109
|
|
|
DropdownField::create( |
110
|
|
|
'ManyMany[PriceModifierAction]', |
111
|
|
|
_t('OptionItem.PriceModifierAction', 'Price Modification'), |
112
|
|
|
[ |
113
|
|
|
'Add' => _t( |
114
|
|
|
'OptionItem.PriceAdd', |
115
|
|
|
'Add to Base Price', |
116
|
|
|
'Add to price' |
117
|
|
|
), |
118
|
|
|
'Subtract' => _t( |
119
|
|
|
'OptionItem.PriceSubtract', |
120
|
|
|
'Subtract from Base Price', |
121
|
|
|
'Subtract from price' |
122
|
|
|
), |
123
|
|
|
'Set' => _t('OptionItem.PriceSet', 'Set as a new Price'), |
124
|
|
|
] |
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'), 4), |
131
|
|
|
TextField::create('ManyMany[CodeModifier]') |
132
|
|
|
->setTitle(_t('OptionItem.CodeModifier', 'Code')), |
133
|
|
|
DropdownField::create( |
134
|
|
|
'ManyMany[CodeModifierAction]', |
135
|
|
|
_t('OptionItem.CodeModifierAction', 'Code Modification'), |
136
|
|
|
[ |
137
|
|
|
'Add' => _t( |
138
|
|
|
'OptionItem.CodeAdd', |
139
|
|
|
'Add to Base Code', |
140
|
|
|
'Add to code' |
141
|
|
|
), |
142
|
|
|
'Subtract' => _t( |
143
|
|
|
'OptionItem.CodeSubtract', |
144
|
|
|
'Subtract from Base Code', |
145
|
|
|
'Subtract from code' |
146
|
|
|
), |
147
|
|
|
'Set' => _t('OptionItem.CodeSet', 'Set as a new Code'), |
148
|
|
|
] |
149
|
|
|
) |
150
|
|
|
->setEmptyString('') |
151
|
|
|
->setDescription(_t('OptionItem.CodeDescription', 'Does code modify or replace base code?')), |
152
|
|
|
]); |
153
|
|
|
}); |
154
|
|
|
|
155
|
|
|
return parent::getCMSFields(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
*/ |
161
|
|
|
public function onBeforeWrite() |
162
|
|
|
{ |
163
|
|
|
parent::onBeforeWrite(); |
164
|
|
|
|
165
|
|
|
$field = 'ManyMany[OptionModifierKey]'; |
166
|
|
|
$this->{$field} = $this->getGeneratedValue(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $oma |
171
|
|
|
* @param bool $returnWithOnlyPlusMinus |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public static function getOptionModifierActionSymbol($oma, $returnWithOnlyPlusMinus = false) |
176
|
|
|
{ |
177
|
|
|
switch ($oma) { |
178
|
|
|
case 'Subtract': |
179
|
|
|
$symbol = '-'; |
180
|
|
|
break; |
181
|
|
|
case 'Set': |
182
|
|
|
$symbol = ($returnWithOnlyPlusMinus) ? '' : ':'; |
183
|
|
|
break; |
184
|
|
|
default: |
185
|
|
|
$symbol = '+'; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $symbol; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function getWeightModifierWithSymbol() |
195
|
|
|
{ |
196
|
|
|
return self::getOptionModifierActionSymbol($this->WeightModifierAction) . $this->WeightModifier; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
|
|
public function getPriceModifierWithSymbol() |
203
|
|
|
{ |
204
|
|
|
return self::getOptionModifierActionSymbol($this->PriceModifierAction) . $this->PriceModifier; |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function getCodeModifierWithSymbol() |
211
|
|
|
{ |
212
|
|
|
return self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier; |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function getGeneratedValue() |
219
|
|
|
{ |
220
|
|
|
$modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
|
|
|
221
|
|
|
$modPriceWithSymbol = self::getOptionModifierActionSymbol($this->PriceModifierAction) . $modPrice; |
|
|
|
|
222
|
|
|
$modWeight = ($this->WeightModifier) ? (string)$this->WeightModifier : '0'; |
|
|
|
|
223
|
|
|
$modWeight = self::getOptionModifierActionSymbol($this->WeightModifierAction) . $modWeight; |
|
|
|
|
224
|
|
|
$modCode = self::getOptionModifierActionSymbol($this->CodeModifierAction) . $this->CodeModifier; |
|
|
|
|
225
|
|
|
|
226
|
|
|
return $this->Title . '{p' . $modPriceWithSymbol . '|w' . $modWeight . '|c' . $modCode . '}'; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return mixed|string |
231
|
|
|
*/ |
232
|
|
|
public function getGeneratedTitle() |
233
|
|
|
{ |
234
|
|
|
$modPrice = ($this->PriceModifier) ? (string)$this->PriceModifier : '0'; |
|
|
|
|
235
|
|
|
$title = $this->Title; |
236
|
|
|
$title .= ($this->PriceModifier != 0) ? |
|
|
|
|
237
|
|
|
': (' . self::getOptionModifierActionSymbol( |
238
|
|
|
$this->PriceModifierAction, |
|
|
|
|
239
|
|
|
$returnWithOnlyPlusMinus = true |
240
|
|
|
) . '$' . $modPrice . ')' : |
241
|
|
|
''; |
242
|
|
|
|
243
|
|
|
return $title; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @return bool |
248
|
|
|
*/ |
249
|
|
|
public function getAvailability() |
250
|
|
|
{ |
251
|
|
|
$available = ($this->Available == 1) ? true : false; |
|
|
|
|
252
|
|
|
$this->extend('updateOptionAvailability', $available); |
253
|
|
|
|
254
|
|
|
return $available; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return string |
259
|
|
|
*/ |
260
|
|
|
public function getIsAvailable() |
261
|
|
|
{ |
262
|
|
|
$available = DBBoolean::create(); |
263
|
|
|
$available->setValue($this->getAvailability()); |
264
|
|
|
|
265
|
|
|
return $available->Nice(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
|
|
public function getOptionType() |
272
|
|
|
{ |
273
|
|
|
if ($this->Type) { |
|
|
|
|
274
|
|
|
$type = OptionType::get()->byID($this->Type); |
275
|
|
|
if ($type) { |
|
|
|
|
276
|
|
|
return $type->Title; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param $member |
283
|
|
|
* @return bool|int|void |
284
|
|
|
*/ |
285
|
|
|
public function canCreate($member = null, $context = []) |
286
|
|
|
{ |
287
|
|
|
if (!$member) { |
288
|
|
|
$member = Security::getCurrentUser(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param $member |
296
|
|
|
* @return bool|int|void|null |
297
|
|
|
*/ |
298
|
|
|
public function canEdit($member = null, $context = []) |
|
|
|
|
299
|
|
|
{ |
300
|
|
|
if (!$member) { |
301
|
|
|
$member = Security::getCurrentUser(); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param $member |
309
|
|
|
* @return bool|int|void |
310
|
|
|
*/ |
311
|
|
|
public function canDelete($member = null, $context = []) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
if (!$member) { |
314
|
|
|
$member = Security::getCurrentUser(); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return Permission::checkMember($member, 'MANAGE_FOXY_PRODUCTS'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param $product |
322
|
|
|
* @return mixed |
323
|
|
|
*/ |
324
|
|
|
public function getPrice($product) |
325
|
|
|
{ |
326
|
|
|
switch ($this->PriceModifierAction) { |
|
|
|
|
327
|
|
|
case 'Subtract': |
328
|
|
|
return $product->Price - $this->PriceModifier; |
|
|
|
|
329
|
|
|
break; |
|
|
|
|
330
|
|
|
case 'Set': |
331
|
|
|
return $this->PriceModifier; |
332
|
|
|
break; |
333
|
|
|
case 'Add': |
334
|
|
|
return $product->Price + $this->PriceModifier; |
335
|
|
|
break; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $product->Price; |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|