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