1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class FoxyStripePurchaseForm |
5
|
|
|
* |
6
|
|
|
* @property SiteConfig $site_config |
7
|
|
|
* @property FoxyStripeProduct $product |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class FoxyStripePurchaseForm extends Form |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var |
15
|
|
|
*/ |
16
|
|
|
protected $site_config; |
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
private $product; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $siteConfig |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
|
|
public function setSiteConfig($siteConfig) |
27
|
|
|
{ |
28
|
|
|
$siteConfig = $siteConfig === null ? SiteConfig::current_site_config() : $siteConfig; |
29
|
|
|
if ($siteConfig instanceof SiteConfig) { |
30
|
|
|
$this->site_config = $siteConfig; |
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
throw new InvalidArgumentException('$siteConfig needs to be an instance of SiteConfig.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return SiteConfig |
38
|
|
|
*/ |
39
|
|
|
public function getSiteConfig() |
40
|
|
|
{ |
41
|
|
|
if (!$this->site_config) { |
42
|
|
|
$this->setSiteConfig(SiteConfig::current_site_config()); |
43
|
|
|
} |
44
|
|
|
return $this->site_config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $product |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function setProduct($product) |
52
|
|
|
{ |
53
|
|
|
if ($product instanceof FoxyStripeProduct) { |
54
|
|
|
$this->product = $product; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
throw new InvalidArgumentException('$product needs to be an instance of FoxyStripeProduct.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return FoxyStripeProduct |
62
|
|
|
*/ |
63
|
|
|
public function getProduct() |
64
|
|
|
{ |
65
|
|
|
return $this->product; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* FoxyStripePurchaseForm constructor. |
70
|
|
|
* @param Controller $controller |
71
|
|
|
* @param string $name |
72
|
|
|
* @param FieldList|null $fields |
73
|
|
|
* @param FieldList|null $actions |
74
|
|
|
* @param null $validator |
75
|
|
|
* @param null $product |
76
|
|
|
* @param null $siteConfig |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
$controller, |
80
|
|
|
$name, |
81
|
|
|
FieldList $fields = null, |
82
|
|
|
FieldList $actions = null, |
83
|
|
|
$validator = null, |
84
|
|
|
$product = null, |
85
|
|
|
$siteConfig = null |
86
|
|
|
) |
87
|
|
|
{ |
88
|
|
|
$this->setProduct($product); |
89
|
|
|
$this->setSiteConfig($siteConfig); |
90
|
|
|
|
91
|
|
|
$fields = ($fields != null && $fields->exists()) ? $this->getProductFields($fields) : $this->getProductFields(FieldList::create()); |
92
|
|
|
|
93
|
|
|
$actions = ($actions != null && $actions->exists()) ? $this->getProductActions($actions) : $this->getProductActions(FieldList::create()); |
94
|
|
|
$validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
95
|
|
|
|
96
|
|
|
Requirements::javascript("framework/thirdparty/jquery/jquery.js"); |
97
|
|
|
if ($product->Available && $product->ProductOptions()->exists()) { |
98
|
|
|
Requirements::javascript("foxystripe/javascript/outOfStock.min.js"); |
99
|
|
|
Requirements::javascript("foxystripe/javascript/ProductOptions.min.js"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
Requirements::customScript(<<<JS |
103
|
|
|
var productID = {$product->ID}; |
104
|
|
|
JS |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
108
|
|
|
|
109
|
|
|
//have to call after parent::__construct() |
110
|
|
|
$this->setAttribute('action', FoxyCart::FormActionURL()); |
111
|
|
|
$this->disableSecurityToken(); |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param FieldList $fields |
117
|
|
|
* @return FieldList |
118
|
|
|
*/ |
119
|
|
|
protected function getProductFields(FieldList $fields) |
120
|
|
|
{ |
121
|
|
|
$hiddenTitle = ($this->product->ReceiptTitle) ? htmlspecialchars($this->product->ReceiptTitle) : htmlspecialchars($this->product->Title); |
|
|
|
|
122
|
|
|
$code = $this->product->Code; |
|
|
|
|
123
|
|
|
|
124
|
|
|
if ($this->product->Available) { |
|
|
|
|
125
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'name', |
126
|
|
|
$hiddenTitle))->setValue($hiddenTitle)); |
127
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'category', |
128
|
|
|
$this->product->Category()->Code))->setValue($this->product->Category()->Code)); |
|
|
|
|
129
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'code', |
130
|
|
|
$this->product->Code))->setValue($this->product->Code)); |
|
|
|
|
131
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'product_id', |
132
|
|
|
$this->product->ID))->setValue($this->product->ID)); |
133
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'price', |
134
|
|
|
$this->product->Price))->setValue($this->product->Price));//can't override id |
|
|
|
|
135
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'weight', |
136
|
|
|
$this->product->Weight))->setValue($this->product->Weight)); |
|
|
|
|
137
|
|
|
if ($this->DiscountTitle && $this->ProductDiscountTiers()->exists()) { |
|
|
|
|
138
|
|
|
$fields->push(HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'discount_quantity_percentage', |
139
|
|
|
$this->product->getDiscountFieldValue()))->setValue($this->product->getDiscountFieldValue())); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
if ($this->product->PreviewImage()->exists()) { |
|
|
|
|
144
|
|
|
$fields->push( |
145
|
|
|
HiddenField::create(FoxyStripeProduct::getGeneratedValue($code, 'image', |
146
|
|
|
$this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL)) |
|
|
|
|
147
|
|
|
->setValue($this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL) |
|
|
|
|
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$optionsSet = $this->getProductOptionSet(); |
152
|
|
|
$fields->push($optionsSet); |
153
|
|
|
|
154
|
|
|
$quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10; |
155
|
|
|
$count = 1; |
156
|
|
|
$quantity = array(); |
157
|
|
|
while ($count <= $quantityMax) { |
158
|
|
|
$countVal = FoxyStripeProduct::getGeneratedValue($this->product->Code, 'quantity', $count, 'value'); |
|
|
|
|
159
|
|
|
$quantity[$countVal] = $count; |
160
|
|
|
$count++; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$fields->push(DropdownField::create('quantity', 'Quantity', $quantity)); |
164
|
|
|
|
165
|
|
|
$fields->push(HeaderField::create('submitPrice', '$' . $this->product->Price, 4)); |
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
$this->extend('updatePurchaseFormFields', $fields); |
169
|
|
|
} else { |
170
|
|
|
$fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->extend('updateFoxyStripePurchaseFormFields', $fields); |
174
|
|
|
|
175
|
|
|
return $fields; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param FieldList $actions |
180
|
|
|
* @return FieldList |
181
|
|
|
*/ |
182
|
|
|
protected function getProductActions(FieldList $actions) |
183
|
|
|
{ |
184
|
|
|
|
185
|
|
|
$actions->push($submit = FormAction::create( |
186
|
|
|
'', |
187
|
|
|
_t('ProductForm.AddToCart', 'Add to Cart') |
188
|
|
|
)); |
189
|
|
|
$submit->setAttribute('name', |
190
|
|
|
FoxyStripeProduct::getGeneratedValue($this->product->Code, 'Submit', _t('ProductForm.AddToCart', 'Add to Cart'))); |
|
|
|
|
191
|
|
|
if (!$this->site_config->StoreName || $this->site_config->StoreName == '' || !isset($this->site_config->StoreName) || !$this->product->Available) { |
|
|
|
|
192
|
|
|
$submit->setAttribute('Disabled', true); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->extend('updateFoxyStripePurchaseFormActions', $fields); |
196
|
|
|
|
197
|
|
|
return $actions; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return CompositeField |
202
|
|
|
*/ |
203
|
|
|
protected function getProductOptionSet() |
204
|
|
|
{ |
205
|
|
|
|
206
|
|
|
$assignAvailable = function ($self) { |
207
|
|
|
$this->extend('updateFoxyStripePurchaseForm', $form); |
208
|
|
|
$self->Available = ($self->getAvailability()) ? true : false; |
209
|
|
|
}; |
210
|
|
|
|
211
|
|
|
$options = $this->product->ProductOptions(); |
|
|
|
|
212
|
|
|
$groupedOptions = new GroupedList($options); |
213
|
|
|
$groupedBy = $groupedOptions->groupBy('ProductOptionGroupID'); |
214
|
|
|
|
215
|
|
|
$optionsSet = CompositeField::create(); |
216
|
|
|
|
217
|
|
|
foreach ($groupedBy as $id => $set) { |
218
|
|
|
$group = OptionGroup::get()->byID($id); |
219
|
|
|
$title = $group->Title; |
220
|
|
|
$name = preg_replace('/\s/', '_', $title); |
221
|
|
|
$set->each($assignAvailable); |
222
|
|
|
$disabled = array(); |
223
|
|
|
$fullOptions = array(); |
224
|
|
|
foreach ($set as $item) { |
225
|
|
|
$fullOptions[FoxyStripeProduct::getGeneratedValue($this->product->Code, $group->Title, |
|
|
|
|
226
|
|
|
$item->getGeneratedValue(), |
227
|
|
|
'value')] = $item->getGeneratedTitle(); |
228
|
|
|
if (!$item->Availability) { |
229
|
|
|
array_push($disabled, |
230
|
|
|
FoxyStripeProduct::getGeneratedValue($this->product->Code, $group->Title, $item->getGeneratedValue(), |
|
|
|
|
231
|
|
|
'value')); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
$optionsSet->push( |
235
|
|
|
$dropdown = DropdownField::create($name, $title, $fullOptions)->setTitle($title) |
236
|
|
|
); |
237
|
|
|
$dropdown->setDisabledItems($disabled); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$optionsSet->addExtraClass('foxycartOptionsContainer'); |
241
|
|
|
|
242
|
|
|
return $optionsSet; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.