|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\GridBundle\Renderer; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Bundle\GridBundle\Form\Registry\FormTypeRegistryInterface; |
|
17
|
|
|
use Sylius\Component\Grid\Definition\Action; |
|
18
|
|
|
use Sylius\Component\Grid\Definition\Field; |
|
19
|
|
|
use Sylius\Component\Grid\Definition\Filter; |
|
20
|
|
|
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface; |
|
21
|
|
|
use Sylius\Component\Grid\Renderer\GridRendererInterface; |
|
22
|
|
|
use Sylius\Component\Grid\View\GridViewInterface; |
|
23
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
|
24
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
|
25
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
26
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
27
|
|
|
|
|
28
|
|
|
final class TwigGridRenderer implements GridRendererInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Twig_Environment |
|
32
|
|
|
*/ |
|
33
|
|
|
private $twig; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var ServiceRegistryInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $fieldsRegistry; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var FormFactoryInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $formFactory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var FormTypeRegistryInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $formTypeRegistry; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private $defaultTemplate; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private $actionTemplates; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
private $filterTemplates; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param \Twig_Environment $twig |
|
67
|
|
|
* @param ServiceRegistryInterface $fieldsRegistry |
|
68
|
|
|
* @param FormFactoryInterface $formFactory |
|
69
|
|
|
* @param FormTypeRegistryInterface $formTypeRegistry |
|
70
|
|
|
* @param string $defaultTemplate |
|
71
|
|
|
* @param array $actionTemplates |
|
72
|
|
|
* @param array $filterTemplates |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct( |
|
75
|
|
|
\Twig_Environment $twig, |
|
76
|
|
|
ServiceRegistryInterface $fieldsRegistry, |
|
77
|
|
|
FormFactoryInterface $formFactory, |
|
78
|
|
|
FormTypeRegistryInterface $formTypeRegistry, |
|
79
|
|
|
string $defaultTemplate, |
|
80
|
|
|
array $actionTemplates = [], |
|
81
|
|
|
array $filterTemplates = [] |
|
82
|
|
|
) { |
|
83
|
|
|
$this->twig = $twig; |
|
84
|
|
|
$this->fieldsRegistry = $fieldsRegistry; |
|
85
|
|
|
$this->formFactory = $formFactory; |
|
86
|
|
|
$this->formTypeRegistry = $formTypeRegistry; |
|
87
|
|
|
$this->defaultTemplate = $defaultTemplate; |
|
88
|
|
|
$this->actionTemplates = $actionTemplates; |
|
89
|
|
|
$this->filterTemplates = $filterTemplates; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* {@inheritdoc} |
|
94
|
|
|
*/ |
|
95
|
|
|
public function render(GridViewInterface $gridView, ?string $template = null) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->twig->render($template ?: $this->defaultTemplate, ['grid' => $gridView]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function renderField(GridViewInterface $gridView, Field $field, $data) |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var FieldTypeInterface $fieldType */ |
|
106
|
|
|
$fieldType = $this->fieldsRegistry->get($field->getType()); |
|
107
|
|
|
$resolver = new OptionsResolver(); |
|
108
|
|
|
$fieldType->configureOptions($resolver); |
|
109
|
|
|
$options = $resolver->resolve($field->getOptions()); |
|
110
|
|
|
|
|
111
|
|
|
return $fieldType->render($field, $data, $options); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function renderAction(GridViewInterface $gridView, Action $action, $data = null) |
|
118
|
|
|
{ |
|
119
|
|
|
$type = $action->getType(); |
|
120
|
|
|
if (!isset($this->actionTemplates[$type])) { |
|
121
|
|
|
throw new \InvalidArgumentException(sprintf('Missing template for action type "%s".', $type)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->twig->render($this->actionTemplates[$type], [ |
|
125
|
|
|
'grid' => $gridView, |
|
126
|
|
|
'action' => $action, |
|
127
|
|
|
'data' => $data, |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* {@inheritdoc} |
|
133
|
|
|
*/ |
|
134
|
|
|
public function renderFilter(GridViewInterface $gridView, Filter $filter) |
|
135
|
|
|
{ |
|
136
|
|
|
$template = $this->getFilterTemplate($filter); |
|
137
|
|
|
|
|
138
|
|
|
$form = $this->formFactory->createNamed('criteria', FormType::class, [], [ |
|
139
|
|
|
'allow_extra_fields' => true, |
|
140
|
|
|
'csrf_protection' => false, |
|
141
|
|
|
'required' => false, |
|
142
|
|
|
]); |
|
143
|
|
|
$form->add( |
|
144
|
|
|
$filter->getName(), |
|
145
|
|
|
$this->formTypeRegistry->get($filter->getType(), 'default'), |
|
146
|
|
|
$filter->getFormOptions() |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$criteria = $gridView->getParameters()->get('criteria', []); |
|
150
|
|
|
$form->submit($criteria); |
|
151
|
|
|
|
|
152
|
|
|
return $this->twig->render($template, [ |
|
153
|
|
|
'grid' => $gridView, |
|
154
|
|
|
'filter' => $filter, |
|
155
|
|
|
'form' => $form->get($filter->getName())->createView(), |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param Filter $filter |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
* |
|
164
|
|
|
* @throws \InvalidArgumentException |
|
165
|
|
|
*/ |
|
166
|
|
|
private function getFilterTemplate(Filter $filter): string |
|
167
|
|
|
{ |
|
168
|
|
|
$template = $filter->getTemplate(); |
|
169
|
|
|
if (null !== $template) { |
|
170
|
|
|
return $template; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$type = $filter->getType(); |
|
174
|
|
|
if (!isset($this->filterTemplates[$type])) { |
|
175
|
|
|
throw new \InvalidArgumentException(sprintf('Missing template for filter type "%s".', $type)); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $this->filterTemplates[$type]; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|