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\Component\Grid\Definition; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Grid\Event\GridDefinitionConverterEvent; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class ArrayToDefinitionConverter implements ArrayToDefinitionConverterInterface |
21
|
|
|
{ |
22
|
|
|
const EVENT_NAME = 'sylius.grid.%s'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var EventDispatcherInterface |
26
|
|
|
*/ |
27
|
|
|
private $eventDispatcher; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
31
|
|
|
*/ |
32
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
33
|
|
|
{ |
34
|
|
|
$this->eventDispatcher = $eventDispatcher; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function convert($code, array $configuration) |
41
|
|
|
{ |
42
|
|
|
$grid = Grid::fromCodeAndDriverConfiguration( |
43
|
|
|
$code, |
44
|
|
|
$configuration['driver']['name'], |
45
|
|
|
$configuration['driver']['options'] |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
if (array_key_exists('sorting', $configuration)) { |
49
|
|
|
$grid->setSorting($configuration['sorting']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (array_key_exists('limits', $configuration)) { |
53
|
|
|
$grid->setLimits($configuration['limits']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($configuration['fields'] as $name => $fieldConfiguration) { |
57
|
|
|
$grid->addField($this->convertField($name, $fieldConfiguration)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach ($configuration['filters'] as $name => $filterConfiguration) { |
61
|
|
|
$grid->addFilter($this->convertFilter($name, $filterConfiguration)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($configuration['actions'] as $name => $actionGroupConfiguration) { |
65
|
|
|
$grid->addActionGroup($this->convertActionGroup($name, $actionGroupConfiguration)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->eventDispatcher->dispatch($this->getEventName($code), new GridDefinitionConverterEvent($grid)); |
69
|
|
|
|
70
|
|
|
return $grid; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* @param array $configuration |
76
|
|
|
* |
77
|
|
|
* @return Field |
78
|
|
|
*/ |
79
|
|
|
private function convertField($name, array $configuration) |
80
|
|
|
{ |
81
|
|
|
$field = Field::fromNameAndType($name, $configuration['type']); |
82
|
|
|
|
83
|
|
|
if (array_key_exists('path', $configuration)) { |
84
|
|
|
$field->setPath($configuration['path']); |
85
|
|
|
} |
86
|
|
|
if (array_key_exists('label', $configuration)) { |
87
|
|
|
$field->setLabel($configuration['label']); |
88
|
|
|
} |
89
|
|
|
if (array_key_exists('enabled', $configuration)) { |
90
|
|
|
$field->setEnabled($configuration['enabled']); |
91
|
|
|
} |
92
|
|
|
if (array_key_exists('sortable', $configuration)) { |
93
|
|
|
$field->setSortable($configuration['sortable']); |
94
|
|
|
} |
95
|
|
|
if (array_key_exists('position', $configuration)) { |
96
|
|
|
$field->setPosition($configuration['position']); |
97
|
|
|
} |
98
|
|
|
if (array_key_exists('options', $configuration)) { |
99
|
|
|
$field->setOptions($configuration['options']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $field; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $name |
107
|
|
|
* @param array $configuration |
108
|
|
|
* |
109
|
|
|
* @return Filter |
110
|
|
|
*/ |
111
|
|
|
private function convertFilter($name, array $configuration) |
112
|
|
|
{ |
113
|
|
|
$filter = Filter::fromNameAndType($name, $configuration['type']); |
114
|
|
|
|
115
|
|
|
if (array_key_exists('label', $configuration)) { |
116
|
|
|
$filter->setLabel($configuration['label']); |
117
|
|
|
} |
118
|
|
|
if (array_key_exists('template', $configuration)) { |
119
|
|
|
$filter->setTemplate($configuration['template']); |
120
|
|
|
} |
121
|
|
|
if (array_key_exists('enabled', $configuration)) { |
122
|
|
|
$filter->setEnabled($configuration['enabled']); |
123
|
|
|
} |
124
|
|
|
if (array_key_exists('position', $configuration)) { |
125
|
|
|
$filter->setPosition($configuration['position']); |
126
|
|
|
} |
127
|
|
|
if (array_key_exists('options', $configuration)) { |
128
|
|
|
$filter->setOptions($configuration['options']); |
129
|
|
|
} |
130
|
|
|
if (array_key_exists('form_options', $configuration)) { |
131
|
|
|
$filter->setFormOptions($configuration['form_options']); |
132
|
|
|
} |
133
|
|
|
if (array_key_exists('default_value', $configuration)) { |
134
|
|
|
$filter->setCriteria($configuration['default_value']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $filter; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @param array $configuration |
143
|
|
|
* |
144
|
|
|
* @return ActionGroup |
145
|
|
|
*/ |
146
|
|
|
private function convertActionGroup($name, array $configuration) |
147
|
|
|
{ |
148
|
|
|
$actionGroup = ActionGroup::named($name); |
149
|
|
|
|
150
|
|
|
foreach ($configuration as $actionName => $actionConfiguration) { |
151
|
|
|
$actionGroup->addAction($this->convertAction($actionName, $actionConfiguration)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $actionGroup; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $name |
159
|
|
|
* @param array $configuration |
160
|
|
|
* |
161
|
|
|
* @return Action |
162
|
|
|
*/ |
163
|
|
|
private function convertAction($name, array $configuration) |
164
|
|
|
{ |
165
|
|
|
$action = Action::fromNameAndType($name, $configuration['type']); |
166
|
|
|
|
167
|
|
|
if (array_key_exists('label', $configuration)) { |
168
|
|
|
$action->setLabel($configuration['label']); |
169
|
|
|
} |
170
|
|
|
if (array_key_exists('icon', $configuration)) { |
171
|
|
|
$action->setIcon($configuration['icon']); |
172
|
|
|
} |
173
|
|
|
if (array_key_exists('enabled', $configuration)) { |
174
|
|
|
$action->setEnabled($configuration['enabled']); |
175
|
|
|
} |
176
|
|
|
if (array_key_exists('position', $configuration)) { |
177
|
|
|
$action->setPosition($configuration['position']); |
178
|
|
|
} |
179
|
|
|
if (array_key_exists('options', $configuration)) { |
180
|
|
|
$action->setOptions($configuration['options']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $action; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $code |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
private function getEventName($code) |
192
|
|
|
{ |
193
|
|
|
return sprintf(self::EVENT_NAME, str_replace('sylius_', '', $code)); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|