1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\ProductBundle\Controller; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\View\View; |
15
|
|
|
use Sylius\Bundle\ProductBundle\Form\Type\ProductAttributeChoiceType; |
16
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
17
|
|
|
use Sylius\Component\Attribute\Model\AttributeInterface; |
18
|
|
|
use Symfony\Component\Form\FormView; |
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Mateusz Zalewski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ProductAttributeController extends ResourceController |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param Request $request |
31
|
|
|
* @param string $template |
32
|
|
|
* |
33
|
|
|
* @return Response |
34
|
|
|
*/ |
35
|
|
|
public function getAttributeTypesAction(Request $request, $template) |
36
|
|
|
{ |
37
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
38
|
|
|
|
39
|
|
|
$view = View::create() |
40
|
|
|
->setTemplate($template) |
41
|
|
|
->setTemplateVar($this->metadata->getPluralName()) |
42
|
|
|
->setData([ |
43
|
|
|
'types' => $this->get('sylius.registry.attribute_type')->all(), |
44
|
|
|
'metadata' => $this->metadata, |
45
|
|
|
]) |
46
|
|
|
; |
47
|
|
|
|
48
|
|
|
return $this->viewHandler->handle($configuration, $view); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Response |
53
|
|
|
*/ |
54
|
|
|
public function renderAttributesAction(Request $request) |
55
|
|
|
{ |
56
|
|
|
$template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeChoice.html.twig'); |
57
|
|
|
|
58
|
|
|
$form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [ |
59
|
|
|
'multiple' => true, |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
return $this->render($template, ['form' => $form->createView()]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Request $request |
67
|
|
|
* |
68
|
|
|
* @return Response |
69
|
|
|
*/ |
70
|
|
|
public function renderAttributeValueFormsAction(Request $request) |
71
|
|
|
{ |
72
|
|
|
$template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeValueForms.html.twig'); |
73
|
|
|
|
74
|
|
|
$form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [ |
75
|
|
|
'multiple' => true, |
76
|
|
|
]); |
77
|
|
|
$form->handleRequest($request); |
78
|
|
|
|
79
|
|
|
$attributes = $form->getData(); |
80
|
|
|
if (null === $attributes) { |
81
|
|
|
throw new BadRequestHttpException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$localeCodes = $this->get('sylius.translation_locale_provider')->getDefinedLocalesCodes(); |
85
|
|
|
|
86
|
|
|
foreach ($attributes as $attribute) { |
87
|
|
|
$forms[$attribute->getId()] = $this->getAttributeFormsInAllLocales($attribute, $localeCodes); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->render($template, [ |
91
|
|
|
'forms' => $forms, |
92
|
|
|
'count' => $request->query->get('count'), |
93
|
|
|
'metadata' => $this->metadata, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param AttributeInterface $attribute |
99
|
|
|
* @param string[] $localeCodes |
100
|
|
|
* |
101
|
|
|
* @return FormView[] |
102
|
|
|
*/ |
103
|
|
|
protected function getAttributeFormsInAllLocales(AttributeInterface $attribute, array $localeCodes) |
104
|
|
|
{ |
105
|
|
|
$attributeForm = $this->get('sylius.form_registry.attribute_type')->get($attribute->getType(), 'default'); |
106
|
|
|
|
107
|
|
|
$forms = []; |
108
|
|
|
foreach ($localeCodes as $localeCode) { |
109
|
|
|
$forms[$localeCode] = $this |
110
|
|
|
->get('form.factory') |
111
|
|
|
->createNamed('value', $attributeForm, null, ['label' => $attribute->getName()]) |
112
|
|
|
->createView() |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $forms; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|