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