1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Form; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Extension\Purchasable; |
6
|
|
|
use Dynamic\Foxy\Extension\Shippable; |
7
|
|
|
use Dynamic\Foxy\Model\Foxy; |
8
|
|
|
use Dynamic\Foxy\Model\FoxyHelper; |
9
|
|
|
use Dynamic\Foxy\Model\Setting; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\Form; |
12
|
|
|
use SilverStripe\Forms\FormAction; |
13
|
|
|
use SilverStripe\Forms\HeaderField; |
14
|
|
|
use SilverStripe\Forms\HiddenField; |
15
|
|
|
use SilverStripe\Forms\RequiredFields; |
16
|
|
|
|
17
|
|
|
class AddToCartForm extends Form |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var |
21
|
|
|
*/ |
22
|
|
|
protected $foxy_setting; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var |
26
|
|
|
*/ |
27
|
|
|
private $product; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $foxySetting |
31
|
|
|
* |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function setFoxySetting($foxySetting) |
35
|
|
|
{ |
36
|
|
|
$foxySetting = $foxySetting === null ? Setting::current_foxy_setting() : $foxySetting; |
37
|
|
|
if ($foxySetting instanceof Setting) { |
38
|
|
|
$this->foxy_setting = $foxySetting; |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
throw new \InvalidArgumentException('$foxySetting needs to be an instance of Foxy Setting.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return FoxyStripeSetting |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function getFoxySetting() |
48
|
|
|
{ |
49
|
|
|
if (!$this->foxy_setting) { |
50
|
|
|
$this->setFoxySetting(Setting::current_foxy_setting()); |
51
|
|
|
} |
52
|
|
|
return $this->foxy_setting; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $product |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setProduct($product) |
61
|
|
|
{ |
62
|
|
|
if ($product->isProduct()) { |
63
|
|
|
$this->product = $product; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
throw new \InvalidArgumentException('$product needs to implement a Foxy DataExtension.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return ProductPage |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function getProduct() |
73
|
|
|
{ |
74
|
|
|
return $this->product; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* AddToCartForm constructor. |
79
|
|
|
* |
80
|
|
|
* @param ContentController $controller |
|
|
|
|
81
|
|
|
* @param string $name |
82
|
|
|
* @param FieldList|null $fields |
83
|
|
|
* @param FieldList|null $actions |
84
|
|
|
* @param null $validator |
|
|
|
|
85
|
|
|
* @param null $product |
|
|
|
|
86
|
|
|
* @param null $foxySetting |
|
|
|
|
87
|
|
|
* |
88
|
|
|
*/ |
89
|
|
|
public function __construct( |
90
|
|
|
$controller, |
91
|
|
|
$name, |
92
|
|
|
FieldList $fields = null, |
93
|
|
|
FieldList $actions = null, |
94
|
|
|
$validator = null, |
95
|
|
|
$product = null, |
96
|
|
|
$foxySetting = null |
97
|
|
|
) { |
98
|
|
|
$this->setProduct($product); |
99
|
|
|
$this->setFoxySetting($foxySetting); |
100
|
|
|
|
101
|
|
|
$fields = ($fields != null && $fields->exists()) ? |
102
|
|
|
$this->getProductFields($fields) : |
103
|
|
|
$this->getProductFields(FieldList::create()); |
104
|
|
|
|
105
|
|
|
$actions = ($actions != null && $actions->exists()) ? |
106
|
|
|
$this->getProductActions($actions) : |
107
|
|
|
$this->getProductActions(FieldList::create()); |
108
|
|
|
|
109
|
|
|
$validator = (!empty($validator) || $validator != null) ? $validator : RequiredFields::create(); |
110
|
|
|
|
111
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
112
|
|
|
|
113
|
|
|
//have to call after parent::__construct() |
114
|
|
|
$this->setAttribute('action', FoxyHelper::FormActionURL()); |
115
|
|
|
$this->disableSecurityToken(); |
116
|
|
|
$this->setHTMLID($this->getTemplateHelper()->generateFormID($this) . "_{$product->ID}"); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param FieldList $fields |
121
|
|
|
* |
122
|
|
|
* @return FieldList |
123
|
|
|
*/ |
124
|
|
|
protected function getProductFields(FieldList $fields) |
125
|
|
|
{ |
126
|
|
|
$hiddenTitle = ($this->product->ReceiptTitle) ? |
127
|
|
|
htmlspecialchars($this->product->ReceiptTitle) : |
128
|
|
|
htmlspecialchars($this->product->Title); |
129
|
|
|
$code = $this->product->Code; |
130
|
|
|
|
131
|
|
|
if ($this->product->isAvailable()) { |
132
|
|
|
$fields->push( |
133
|
|
|
HiddenField::create('name') |
134
|
|
|
->setValue( |
135
|
|
|
self::getGeneratedValue($code, 'name', $hiddenTitle, 'value') |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
$fields->push( |
140
|
|
|
HiddenField::create('category') |
141
|
|
|
->setValue( |
142
|
|
|
self::getGeneratedValue($code, 'category', $this->product->FoxyCategory()->Code, 'value') |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$fields->push( |
147
|
|
|
HiddenField::create('code') |
148
|
|
|
->setValue( |
149
|
|
|
self::getGeneratedValue($code, 'code', $this->product->Code, 'value') |
150
|
|
|
) |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$fields->push( |
154
|
|
|
HiddenField::create('product_id') |
155
|
|
|
->setValue( |
156
|
|
|
self::getGeneratedValue($code, 'product_id', $this->product->ID, 'value') |
157
|
|
|
) |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$fields->push( |
161
|
|
|
HiddenField::create('price') |
162
|
|
|
->setValue( |
163
|
|
|
self::getGeneratedValue($code, 'price', $this->product->Price, 'value') |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
if ($this->product->hasExtension(Shippable::class)) { |
168
|
|
|
if ($this->product->Weight > 0) { |
169
|
|
|
$fields->push( |
170
|
|
|
HiddenField::create('weight') |
171
|
|
|
->setValue( |
172
|
|
|
self::getGeneratedValue($code, 'weight', $this->product->Weight, 'value') |
173
|
|
|
) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$image = null; |
179
|
|
|
if ($this->product->hasMethod('getImage')) { |
180
|
|
|
if ($this->product->getImage()) { |
181
|
|
|
$image = $this->product->getImage()->CMSThumbnail()->absoluteURL; |
182
|
|
|
} |
183
|
|
|
if ($image) { |
184
|
|
|
$fields->push( |
185
|
|
|
HiddenField::create('image') |
186
|
|
|
->setValue( |
187
|
|
|
self::getGeneratedValue($code, 'image', $image, 'value') |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/* |
195
|
|
|
// TODO: revisit after product options are implemented |
196
|
|
|
$optionsSet = $this->getProductOptionSet(); |
197
|
|
|
$fields->push($optionsSet); |
198
|
|
|
$quantityMax = ($this->site_config->MaxQuantity) ? $this->site_config->MaxQuantity : 10; |
199
|
|
|
$fields->push(QuantityField::create('x:visibleQuantity')->setTitle('Quantity')->setValue(1)); |
200
|
|
|
$fields->push( |
201
|
|
|
HiddenField::create('quantity') |
202
|
|
|
->setValue( |
203
|
|
|
self::getGeneratedValue($code, 'quantity', 1, 'value') |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
*/ |
207
|
|
|
|
208
|
|
|
$fields->push( |
209
|
|
|
HeaderField::create('submitPrice', '$' . $this->product->Price, 4) |
210
|
|
|
->addExtraClass('submit-price') |
211
|
|
|
); |
212
|
|
|
$fields->push( |
213
|
|
|
$unavailable = HeaderField::create('unavailableText', 'Selection unavailable', 4) |
214
|
|
|
->addExtraClass('unavailable-text') |
215
|
|
|
); |
216
|
|
|
if (!empty(trim($this->foxy_setting->StoreDomain)) && $this->product->isAvailable()) { |
217
|
|
|
$unavailable->addExtraClass('hidden'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
} else { |
221
|
|
|
$fields->push(HeaderField::create('unavailableText', 'currently unavaiable', 4)); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$this->extend('updateProductFields', $fields); |
225
|
|
|
|
226
|
|
|
return $fields; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param FieldList $actions |
231
|
|
|
* |
232
|
|
|
* @return FieldList |
233
|
|
|
*/ |
234
|
|
|
protected function getProductActions(FieldList $actions) |
235
|
|
|
{ |
236
|
|
|
if (!empty(trim($this->foxy_setting->StoreDomain)) && $this->product->isAvailable()) { |
237
|
|
|
$actions->push( |
238
|
|
|
FormAction::create( |
239
|
|
|
'x:submit', |
240
|
|
|
_t(__CLASS__ . '.AddToCart', 'Add to Cart') |
241
|
|
|
) |
242
|
|
|
->addExtraClass('fs-add-to-cart-button') |
243
|
|
|
->setName('x:submit') |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$this->extend('updateProductActions', $actions); |
248
|
|
|
|
249
|
|
|
return $actions; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param null $productCode |
|
|
|
|
254
|
|
|
* @param null $optionName |
|
|
|
|
255
|
|
|
* @param null $optionValue |
|
|
|
|
256
|
|
|
* @param string $method |
257
|
|
|
* @param bool $output |
258
|
|
|
* @param bool $urlEncode |
259
|
|
|
* |
260
|
|
|
* @return null|string |
261
|
|
|
*/ |
262
|
|
|
// todo - Purchasable Extension or AddToCartForm? protected in Form |
263
|
|
|
public static function getGeneratedValue( |
264
|
|
|
$productCode = null, |
265
|
|
|
$optionName = null, |
266
|
|
|
$optionValue = null, |
267
|
|
|
$method = 'name', |
268
|
|
|
$output = false, |
269
|
|
|
$urlEncode = false |
270
|
|
|
) { |
271
|
|
|
$optionName = ($optionName !== null) ? preg_replace('/\s/', '_', $optionName) : $optionName; |
|
|
|
|
272
|
|
|
$helper = FoxyHelper::create(); |
273
|
|
|
|
274
|
|
|
return $helper::fc_hash_value($productCode, $optionName, $optionValue, $method, $output, $urlEncode); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
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