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