1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Form; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\OptionGroup; |
6
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
7
|
|
|
use SilverStripe\Forms\CompositeField; |
8
|
|
|
use SilverStripe\Forms\DropdownField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\Form; |
11
|
|
|
use SilverStripe\Forms\FormAction; |
12
|
|
|
use SilverStripe\Forms\HeaderField; |
13
|
|
|
use SilverStripe\Forms\HiddenField; |
14
|
|
|
use SilverStripe\ORM\GroupedList; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class FoxyStripePurchaseForm. |
18
|
|
|
* |
19
|
|
|
* @property SiteConfig $site_config |
20
|
|
|
* @property ProductPage $product |
21
|
|
|
*/ |
22
|
|
|
class FoxyStripePurchaseForm extends Form |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
protected $site_config; |
28
|
|
|
/** |
29
|
|
|
* @var |
30
|
|
|
*/ |
31
|
|
|
private $product; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $siteConfig |
35
|
|
|
* |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function setSiteConfig($siteConfig) |
39
|
|
|
{ |
40
|
|
|
$siteConfig = $siteConfig === null ? SiteConfig::current_site_config() : $siteConfig; |
|
|
|
|
41
|
|
|
if ($siteConfig instanceof SiteConfig) { |
42
|
|
|
$this->site_config = $siteConfig; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
throw new InvalidArgumentException('$siteConfig needs to be an instance of SiteConfig.'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return SiteConfig |
51
|
|
|
*/ |
52
|
|
|
public function getSiteConfig() |
53
|
|
|
{ |
54
|
|
|
if (!$this->site_config) { |
55
|
|
|
$this->setSiteConfig(SiteConfig::current_site_config()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->site_config; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $product |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setProduct($product) |
67
|
|
|
{ |
68
|
|
|
if ($product instanceof ProductPage) { |
69
|
|
|
$this->product = $product; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
throw new InvalidArgumentException('$product needs to be an instance of ProductPage.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return ProductPage |
78
|
|
|
*/ |
79
|
|
|
public function getProduct() |
80
|
|
|
{ |
81
|
|
|
return $this->product; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* FoxyStripePurchaseForm constructor. |
86
|
|
|
* |
87
|
|
|
* @param Controller $controller |
|
|
|
|
88
|
|
|
* @param string $name |
89
|
|
|
* @param FieldList|null $fields |
|
|
|
|
90
|
|
|
* @param FieldList|null $actions |
91
|
|
|
* @param null $validator |
92
|
|
|
* @param null $product |
93
|
|
|
* @param null $siteConfig |
94
|
|
|
*/ |
95
|
|
|
public function __construct( |
96
|
|
|
$controller, |
97
|
|
|
$name, |
98
|
|
|
FieldList $fields = null, |
99
|
|
|
FieldList $actions = null, |
100
|
|
|
$validator = null, |
101
|
|
|
$product = null, |
102
|
|
|
$siteConfig = null |
103
|
|
|
) { |
104
|
|
|
$this->setProduct($product); |
105
|
|
|
$this->setSiteConfig($siteConfig); |
106
|
|
|
|
107
|
|
|
$fields = ($fields != null && $fields->exists()) ? |
108
|
|
|
$this->getProductFields($fields) : |
109
|
|
|
$this->getProductFields(FieldList::create()); |
110
|
|
|
|
111
|
|
|
$actions = ($actions != null && $actions->exists()) ? |
112
|
|
|
$this->getProductActions($actions) : |
113
|
|
|
$this->getProductActions(FieldList::create()); |
114
|
|
|
$validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
|
|
|
|
115
|
|
|
|
116
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
117
|
|
|
|
118
|
|
|
//have to call after parent::__construct() |
119
|
|
|
$this->setAttribute('action', FoxyCart::FormActionURL()); |
|
|
|
|
120
|
|
|
$this->disableSecurityToken(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param FieldList $fields |
125
|
|
|
* |
126
|
|
|
* @return FieldList |
127
|
|
|
*/ |
128
|
|
|
protected function getProductFields(FieldList $fields) |
129
|
|
|
{ |
130
|
|
|
$hiddenTitle = ($this->product->ReceiptTitle) ? |
|
|
|
|
131
|
|
|
htmlspecialchars($this->product->ReceiptTitle) : |
132
|
|
|
htmlspecialchars($this->product->Title); |
133
|
|
|
$code = $this->product->Code; |
|
|
|
|
134
|
|
|
|
135
|
|
|
if ($this->product->Available) { |
|
|
|
|
136
|
|
|
$fields->push(HiddenField::create(ProductPage::getGeneratedValue( |
|
|
|
|
137
|
|
|
$code, |
138
|
|
|
'name', |
139
|
|
|
$hiddenTitle |
140
|
|
|
))->setValue($hiddenTitle)); |
141
|
|
|
$fields->push(HiddenField::create(ProductPage::getGeneratedValue( |
142
|
|
|
$code, |
143
|
|
|
'category', |
144
|
|
|
$this->product->Category()->Code |
|
|
|
|
145
|
|
|
))->setValue($this->product->Category()->Code)); |
146
|
|
|
$fields->push(HiddenField::create(ProductPage::getGeneratedValue( |
147
|
|
|
$code, |
148
|
|
|
'code', |
149
|
|
|
$this->product->Code |
150
|
|
|
))->setValue($this->product->Code)); |
151
|
|
|
$fields->push(HiddenField::create(ProductPage::getGeneratedValue( |
|
|
|
|
152
|
|
|
$code, |
153
|
|
|
'product_id', |
154
|
|
|
$this->product->ID |
155
|
|
|
))->setValue($this->product->ID)); |
156
|
|
|
$fields->push(HiddenField::create(ProductPage::getGeneratedValue( |
157
|
|
|
$code, |
158
|
|
|
'price', |
159
|
|
|
$this->product->Price |
|
|
|
|
160
|
|
|
))->setValue($this->product->Price));//can't override id |
161
|
|
|
$fields->push(HiddenField::create(ProductPage::getGeneratedValue( |
162
|
|
|
$code, |
163
|
|
|
'weight', |
164
|
|
|
$this->product->Weight |
|
|
|
|
165
|
|
|
))->setValue($this->product->Weight)); |
166
|
|
|
|
167
|
|
|
if ($this->product->PreviewImage()->exists()) { |
|
|
|
|
168
|
|
|
$fields->push( |
169
|
|
|
HiddenField::create(ProductPage::getGeneratedValue( |
170
|
|
|
$code, |
171
|
|
|
'image', |
172
|
|
|
$this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL |
173
|
|
|
)) |
174
|
|
|
->setValue($this->product->PreviewImage()->PaddedImage(80, 80)->absoluteURL) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$optionsSet = $this->getProductOptionSet(); |
179
|
|
|
$fields->push($optionsSet); |
180
|
|
|
|
181
|
|
|
$quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10; |
182
|
|
|
$count = 1; |
183
|
|
|
$quantity = array(); |
184
|
|
|
while ($count <= $quantityMax) { |
185
|
|
|
$countVal = ProductPage::getGeneratedValue( |
186
|
|
|
$this->product->Code, |
187
|
|
|
'quantity', |
188
|
|
|
$count, |
189
|
|
|
'value' |
190
|
|
|
); |
191
|
|
|
$quantity[$countVal] = $count; |
192
|
|
|
++$count; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$fields->push(DropdownField::create('quantity', 'Quantity', $quantity)); |
196
|
|
|
|
197
|
|
|
$fields->push(HeaderField::create('submitPrice', '$'.$this->product->Price, 4) |
|
|
|
|
198
|
|
|
->addExtraClass('submit-price')); |
199
|
|
|
$fields->push(HeaderField::create('unavailableText', 'Selection unavailable', 4) |
200
|
|
|
->addExtraClass('hidden unavailable-text')); |
201
|
|
|
|
202
|
|
|
$this->extend('updatePurchaseFormFields', $fields); |
203
|
|
|
} else { |
204
|
|
|
$fields->push(HeaderField::create('submitPrice', 'Currently Out of Stock'), 4); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->extend('updateFoxyStripePurchaseFormFields', $fields); |
208
|
|
|
|
209
|
|
|
return $fields; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param FieldList $actions |
214
|
|
|
* |
215
|
|
|
* @return FieldList |
216
|
|
|
*/ |
217
|
|
|
protected function getProductActions(FieldList $actions) |
218
|
|
|
{ |
219
|
|
|
$actions->push($submit = FormAction::create( |
220
|
|
|
'', |
|
|
|
|
221
|
|
|
_t('ProductForm.AddToCart', 'Add to Cart') |
222
|
|
|
)); |
223
|
|
|
$submit->setAttribute( |
224
|
|
|
'name', |
225
|
|
|
ProductPage::getGeneratedValue( |
226
|
|
|
$this->product->Code, |
|
|
|
|
227
|
|
|
'Submit', |
228
|
|
|
_t('ProductForm.AddToCart', 'Add to Cart') |
229
|
|
|
) |
230
|
|
|
); |
231
|
|
|
if (!$this->site_config->StoreName || |
232
|
|
|
$this->site_config->StoreName == '' || |
233
|
|
|
!isset($this->site_config->StoreName) || |
234
|
|
|
!$this->product->Available |
|
|
|
|
235
|
|
|
) { |
236
|
|
|
$submit->setAttribute('Disabled', true); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$this->extend('updateFoxyStripePurchaseFormActions', $fields); |
240
|
|
|
|
241
|
|
|
return $actions; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return CompositeField |
246
|
|
|
*/ |
247
|
|
|
protected function getProductOptionSet() |
248
|
|
|
{ |
249
|
|
|
$assignAvailable = function ($self) { |
250
|
|
|
$this->extend('updateFoxyStripePurchaseForm', $form); |
251
|
|
|
$self->Available = ($self->getAvailability()) ? true : false; |
252
|
|
|
}; |
253
|
|
|
|
254
|
|
|
$options = $this->product->ProductOptions(); |
|
|
|
|
255
|
|
|
$groupedOptions = new GroupedList($options); |
256
|
|
|
$groupedBy = $groupedOptions->groupBy('ProductOptionGroupID'); |
257
|
|
|
|
258
|
|
|
$optionsSet = CompositeField::create(); |
259
|
|
|
|
260
|
|
|
foreach ($groupedBy as $id => $set) { |
261
|
|
|
$group = OptionGroup::get()->byID($id); |
262
|
|
|
$title = $group->Title; |
263
|
|
|
$name = preg_replace('/\s/', '_', $title); |
264
|
|
|
$set->each($assignAvailable); |
265
|
|
|
$disabled = array(); |
266
|
|
|
$fullOptions = array(); |
267
|
|
|
foreach ($set as $item) { |
268
|
|
|
$fullOptions[ProductPage::getGeneratedValue( |
269
|
|
|
$this->product->Code, |
|
|
|
|
270
|
|
|
$group->Title, |
271
|
|
|
$item->getGeneratedValue(), |
272
|
|
|
'value' |
273
|
|
|
)] = $item->getGeneratedTitle(); |
274
|
|
|
if (!$item->Availability) { |
275
|
|
|
array_push( |
276
|
|
|
$disabled, |
277
|
|
|
ProductPage::getGeneratedValue( |
278
|
|
|
$this->product->Code, |
279
|
|
|
$group->Title, |
280
|
|
|
$item->getGeneratedValue(), |
281
|
|
|
'value' |
282
|
|
|
) |
283
|
|
|
); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
$optionsSet->push( |
287
|
|
|
$dropdown = DropdownField::create($name, $title, $fullOptions)->setTitle($title) |
288
|
|
|
); |
289
|
|
|
$dropdown->setDisabledItems($disabled); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$optionsSet->addExtraClass('foxycartOptionsContainer'); |
293
|
|
|
|
294
|
|
|
return $optionsSet; |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths