-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductController.php
279 lines (228 loc) · 9.79 KB
/
ProductController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\ProductBundle\Controller;
use Sonata\Component\Form\Type\VariationChoiceType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\Response;
use Sonata\Component\Basket\BasketElementInterface;
use Sonata\Component\Basket\BasketInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
class ProductController extends Controller
{
/**
* @param $productId
* @param $slug
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function viewAction($productId, $slug)
{
$product = $this->get('sonata.product.set.manager')->findEnabledFromIdAndSlug($productId, $slug);
if (!$product) {
throw new NotFoundHttpException(sprintf('Unable to find the product with id=%d', $productId));
}
/** @var \Sonata\Component\Product\Pool $productPool */
$productPool = $this->get('sonata.product.pool');
$provider = $productPool->getProvider($product);
if ($provider->hasVariations($product)) {
if (!$provider->hasEnabledVariations($product)) {
throw new NotFoundHttpException('Product has no activated variation');
}
// We display the cheapest variation
$product = $provider->getCheapestEnabledVariation($product);
}
$action = sprintf('%s:view', $provider->getBaseControllerName());
$response = $this->forward($action, array(
'provider' => $provider,
'product' => $product
));
if ($this->get('kernel')->isDebug()) {
$response->setContent(sprintf("\n<!-- [Sonata] Product code: %s, id: %s, action: %s -->\n%s\n<!-- [Sonata] end product -->\n",
$this->get('sonata.product.pool')->getProductCode($product),
$product->getId(),
$action,
$response->getContent()
));
}
return $response;
}
/**
* @param \Symfony\Component\Form\FormView $formView
* @param \Sonata\Component\Basket\BasketElementInterface $basketElement
* @param \Sonata\Component\Basket\BasketInterface $basket
*
* @return Response
*/
public function renderFormBasketElementAction(FormView $formView, BasketElementInterface $basketElement, BasketInterface $basket)
{
$action = sprintf('%s:renderFormBasketElement', $basketElement->getProductProvider()->getBaseControllerName()) ;
$response = $this->forward($action, array(
'formView' => $formView,
'basketElement' => $basketElement,
'basket' => $basket
));
if ($this->get('kernel')->isDebug()) {
$response->setContent(sprintf("\n<!-- [Sonata] Product code: %s, id: %s, action: %s -->\n%s\n<!-- [Sonata] end product -->\n",
$basketElement->getProductCode(),
$basketElement->getProductId(),
$action,
$response->getContent()
));
}
return $response;
}
/**
* Returns price for $productId in given quantity and stock information
* as JSON.
*
* @param $productId
*
* @return JsonResponse
*/
public function getPriceStockForQuantityAction($productId)
{
if (!$this->getRequest()->isXmlHttpRequest() || !($quantity = (int) $this->getRequest()->query->get('quantity'))) {
throw new BadRequestHttpException("Request needs to be XHR and have a quantity parameter");
}
$product = $this->get('sonata.product.set.manager')->findOneBy(array('id' => $productId, 'enabled' => true));
if (!$product) {
throw new NotFoundHttpException(sprintf('Unable to find the product with id=%d', $productId));
}
$errors = array();
/** @var \Sonata\Component\Product\Pool $productPool */
$productPool = $this->get('sonata.product.pool');
$provider = $productPool->getProvider($product);
if ($provider->hasVariations($product)) {
$errors['variations'] = "This is a master product, it has no price";
}
$stock = $provider->getStockAvailable($product) ?: 0;
if ($quantity > $stock) {
$errors['stock'] = $this->get('translator')->trans('product_out_of_stock_quantity', array(), "SonataProductBundle");
}
$currency = $this->get('sonata.basket')->getCurrency();
$price = $provider->calculatePrice($product, $currency, true, $quantity);
return new JsonResponse(array(
'stock' => $stock,
'price' => $price,
'price_text' => $this->get('sonata.intl.templating.helper.number')->formatCurrency($price, $currency),
'errors' => $errors
));
}
/**
* @param \Sonata\Component\Basket\BasketElementInterface $basketElement
* @param \Sonata\Component\Basket\BasketInterface $basket
*
* @return Response
*/
public function renderFinalReviewBasketElementAction(BasketElementInterface $basketElement, BasketInterface $basket)
{
$action = sprintf('%s:renderFinalReviewBasketElement', $basketElement->getProductProvider()->getBaseControllerName()) ;
$response = $this->forward($action, array(
'basketElement' => $basketElement,
'basket' => $basket
));
if ($this->get('kernel')->isDebug()) {
$response->setContent(sprintf("\n<!-- [Sonata] Product code: %s, id: %s, action: %s -->\n%s\n<!-- [Sonata] end product -->\n",
$basketElement->getProductCode(),
$basketElement->getProductId(),
$action,
$response->getContent()
));
}
return $response;
}
/**
* @param $productId
* @param $slug
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function viewVariationsAction($productId, $slug)
{
$product = is_object($productId) ? $productId : $this->get('sonata.product.set.manager')->findEnabledFromIdAndSlug($productId, $slug);
if (!$product) {
throw new NotFoundHttpException(sprintf('Unable to find the product with id=%d', $productId));
}
$provider = $this->get('sonata.product.pool')->getProvider($product);
$action = sprintf('%s:viewVariations', $provider->getBaseControllerName());
$response = $this->forward($action, array(
'product' => $product
));
if ($this->get('kernel')->isDebug()) {
$response->setContent(sprintf("\n<!-- [Sonata] Product code: %s, id: %s, action: %s -->\n%s\n<!-- [Sonata] end product -->\n",
$this->get('sonata.product.pool')->getProductCode($product),
$product->getId(),
$action,
$response->getContent()
));
}
return $response;
}
/**
* @param $productId
* @param $slug
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function variationToProductAction($productId, $slug)
{
$product = is_object($productId) ? $productId : $this->get('sonata.product.set.manager')->findEnabledFromIdAndSlug($productId, $slug);
if (!$product) {
throw new NotFoundHttpException(sprintf('Unable to find the product with id=%d', $productId));
}
if (null !== $product->getParent()) {
// We need the master product
$product = $product->getParent();
}
$provider = $this->get('sonata.product.pool')->getProvider($product);
$data = $this->getRequest()->query->get('sonata_product_variation_choices');
$choices = $provider->getVariationsChoices($product, array_keys($data));
$accessor = PropertyAccess::createPropertyAccessor();
$currentValues = array();
foreach ($choices as $field => $values) {
$currentValues[$field] = array_search($accessor->getValue($product, $field), $values);
}
$form = $this->createForm('sonata_product_variation_choices', $currentValues, array(
'product' => $product,
'fields' => array_keys($data)
));
$form->handleRequest($this->getRequest());
$selectedVariation = $form->getData();
// Retrieving correct values
foreach ($selectedVariation as $key => $value) {
$selectedVariation[$key] = $choices[$key][$value];
}
$variation = $provider->getVariation($product, $selectedVariation);
$action = sprintf('%s:variationToProduct', $provider->getBaseControllerName());
$response = $this->forward($action, array(
'product' => $product,
'variation' => $variation
));
if ($this->get('kernel')->isDebug() && !($response instanceof JsonResponse)) {
$response->setContent(sprintf("\n<!-- [Sonata] Product code: %s, id: %s, action: %s -->\n%s\n<!-- [Sonata] end product -->\n",
$this->get('sonata.product.pool')->getProductCode($product),
$product->getId(),
$action,
$response->getContent()
));
}
return $response;
}
}