1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Configurable; |
6
|
|
|
use SilverStripe\Core\Extensible; |
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
8
|
|
|
|
9
|
|
|
class FoxyHelper extends \FoxyCart_Helper |
10
|
|
|
{ |
11
|
|
|
use Configurable; |
12
|
|
|
use Injectable; |
13
|
|
|
use Extensible; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
private static $secret; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
protected static $cart_url; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
private static $custom_ssl; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
private static $max_quantity = 10; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private static $product_classes = []; |
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var |
42
|
|
|
*/ |
43
|
|
|
private $foxy_secret; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var |
47
|
|
|
*/ |
48
|
|
|
private $foxy_cart_url; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var |
52
|
|
|
*/ |
53
|
|
|
private $foxy_custom_ssl; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var |
57
|
|
|
*/ |
58
|
|
|
private $foxy_max_quantity; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var |
62
|
|
|
*/ |
63
|
|
|
private $foxy_product_classes; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return mixed |
67
|
|
|
*/ |
68
|
|
|
public function getStoreSecret() |
69
|
|
|
{ |
70
|
|
|
if (!$this->foxy_secret) { |
71
|
|
|
$this->setStoreSecret(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->foxy_secret; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setStoreSecret() |
81
|
|
|
{ |
82
|
|
|
$this->foxy_secret = $this->config()->get('secret'); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function getStoreCartURL() |
91
|
|
|
{ |
92
|
|
|
if (!$this->foxy_cart_url) { |
93
|
|
|
$this->setStoreCartURL(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->foxy_cart_url; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setStoreCartURL() |
103
|
|
|
{ |
104
|
|
|
$this->foxy_cart_url = $this->config()->get('cart_url'); |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function getCustomSSL() |
113
|
|
|
{ |
114
|
|
|
if (!$this->foxy_custom_ssl) { |
115
|
|
|
$this->setCustomSSL(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->foxy_custom_ssl; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setCustomSSL() |
125
|
|
|
{ |
126
|
|
|
$this->foxy_custom_ssl = $this->config()->get('custom_ssl'); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function getMaxQuantity() |
135
|
|
|
{ |
136
|
|
|
if (!$this->foxy_max_quantity) { |
137
|
|
|
$this->setMaxQuantity(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->foxy_max_quantity; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function setMaxQuantity() |
147
|
|
|
{ |
148
|
|
|
$this->foxy_max_quantity = $this->config()->get('max_quantity'); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public function getProuductClasses() |
157
|
|
|
{ |
158
|
|
|
if (!$this->foxy_product_classes) { |
159
|
|
|
$this->setProductClasses(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->foxy_product_classes; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function setProductClasses() |
169
|
|
|
{ |
170
|
|
|
$productClasses = $this->config()->get('product_classes'); |
171
|
|
|
|
172
|
|
|
if (empty($productClasses)) { |
173
|
|
|
$this->foxy_product_classes = []; |
174
|
|
|
return $this; |
175
|
|
|
} elseif (!is_array($productClasses)) { |
176
|
|
|
$productClasses = [$productClasses]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($this->config()->get('include_product_subclasses')) { |
180
|
|
|
$productClasses = array_reduce($productClasses, function (array $list, $productClass) { |
181
|
|
|
foreach (ClassInfo::subclassesFor($productClass) as $key => $class) { |
|
|
|
|
182
|
|
|
$list[$key] = $class; |
183
|
|
|
} |
184
|
|
|
$this->foxy_product_classes = $list; |
185
|
|
|
return $this; |
186
|
|
|
}, []); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->foxy_product_classes = $productClasses; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return |null |
|
|
|
|
195
|
|
|
*/ |
196
|
|
|
public function getProducts() |
197
|
|
|
{ |
198
|
|
|
$productClasses = $this->getProuductClasses(); |
199
|
|
|
$products = null; |
200
|
|
|
|
201
|
|
|
if (!empty($productClasses)) { |
202
|
|
|
$products = SiteTree::get()->filter('ClassName', $productClasses); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->owner->extend('updateProducts', $products); |
|
|
|
|
206
|
|
|
|
207
|
|
|
return $products; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* FoxyHelper constructor. |
212
|
|
|
* |
213
|
|
|
* Set the private statics $secret and $cart_url in FoxyCart_Helper |
214
|
|
|
* |
215
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
216
|
|
|
*/ |
217
|
|
|
public function __construct() |
218
|
|
|
{ |
219
|
|
|
self::setCartURL($this->getStoreCartURL()); |
220
|
|
|
self::setSecret($this->getStoreSecret()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public static function StoreURL() |
227
|
|
|
{ |
228
|
|
|
$helper = FoxyHelper::create(); |
229
|
|
|
if ($helper->getCustomSSL()) { |
230
|
|
|
return sprintf('https://%s/', $helper->getStoreCartURL()); |
231
|
|
|
} else { |
232
|
|
|
return sprintf('https://%s.foxycart.com/', $helper->getStoreCartURL()); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
239
|
|
|
*/ |
240
|
|
|
public static function FormActionURL() |
241
|
|
|
{ |
242
|
|
|
return self::StoreURL() . 'cart'; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|