1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\PagePartBundle\Helper\FormWidgets; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Kunstmaan\AdminBundle\Helper\FormWidgets\FormWidget; |
8
|
|
|
use Kunstmaan\NodeBundle\Entity\PageInterface; |
9
|
|
|
use Kunstmaan\PagePartBundle\Entity\PageTemplateConfiguration; |
10
|
|
|
use Kunstmaan\PagePartBundle\Helper\HasPageTemplateInterface; |
11
|
|
|
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdmin; |
12
|
|
|
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminConfiguratorInterface; |
13
|
|
|
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdminFactory; |
14
|
|
|
use Kunstmaan\PagePartBundle\PagePartConfigurationReader\PagePartConfigurationReaderInterface; |
15
|
|
|
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationReaderInterface; |
16
|
|
|
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateConfigurationService; |
17
|
|
|
use Kunstmaan\PagePartBundle\PageTemplate\PageTemplateInterface; |
18
|
|
|
use Kunstmaan\PagePartBundle\PageTemplate\Region; |
19
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
20
|
|
|
use Symfony\Component\Form\FormView; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PageTemplateWidget |
25
|
|
|
*/ |
26
|
|
|
class PageTemplateWidget extends FormWidget |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var EntityManagerInterface |
30
|
|
|
*/ |
31
|
|
|
private $em; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PagePartAdminFactory |
35
|
|
|
*/ |
36
|
|
|
private $pagePartAdminFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var PageInterface |
40
|
|
|
*/ |
41
|
|
|
private $page; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Request |
45
|
|
|
*/ |
46
|
|
|
private $request; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var PagePartWidget[] |
50
|
|
|
*/ |
51
|
|
|
private $widgets = array(); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var PageTemplateInterface[] |
55
|
|
|
*/ |
56
|
|
|
private $pageTemplates = array(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var PagePartAdminConfiguratorInterface[] |
60
|
|
|
*/ |
61
|
|
|
private $pagePartAdminConfigurations = array(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var PageTemplateConfiguration |
65
|
|
|
*/ |
66
|
|
|
protected $pageTemplateConfiguration; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param HasPageTemplateInterface $page |
70
|
|
|
* @param Request $request |
71
|
|
|
* @param EntityManagerInterface $em |
72
|
|
|
* @param PagePartAdminFactory $pagePartAdminFactory |
73
|
|
|
* @param PageTemplateConfigurationReaderInterface $templateReader |
74
|
|
|
* @param PagePartConfigurationReaderInterface $pagePartReader |
75
|
|
|
* @param PageTemplateConfigurationService $pageTemplateConfigurationService |
76
|
|
|
*/ |
77
|
|
|
public function __construct( |
78
|
|
|
HasPageTemplateInterface $page, |
79
|
|
|
Request $request, |
|
|
|
|
80
|
|
|
EntityManagerInterface $em, |
81
|
|
|
PagePartAdminFactory $pagePartAdminFactory, |
82
|
|
|
PageTemplateConfigurationReaderInterface $templateReader, |
83
|
|
|
PagePartConfigurationReaderInterface $pagePartReader, |
84
|
|
|
PageTemplateConfigurationService $pageTemplateConfigurationService |
85
|
|
|
) { |
86
|
|
|
parent::__construct(); |
87
|
|
|
|
88
|
|
|
$this->page = $page; |
|
|
|
|
89
|
|
|
$this->em = $em; |
90
|
|
|
$this->request = $request; |
91
|
|
|
$this->pagePartAdminFactory = $pagePartAdminFactory; |
92
|
|
|
|
93
|
|
|
$this->pageTemplates = $templateReader->getPageTemplates($page); |
94
|
|
|
$this->pagePartAdminConfigurations = $pagePartReader->getPagePartAdminConfigurators($page); |
95
|
|
|
$this->pageTemplateConfiguration = $pageTemplateConfigurationService->findOrCreateFor($page); |
96
|
|
|
|
97
|
|
|
foreach ($this->getPageTemplate()->getRows() as $row) { |
98
|
|
|
foreach ($row->getRegions() as $region) { |
99
|
|
|
$this->processRegion($region); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Region $region The region |
106
|
|
|
*/ |
107
|
|
|
private function processRegion($region) |
108
|
|
|
{ |
109
|
|
|
if (\count($region->getChildren())) { |
110
|
|
|
foreach ($region->getChildren() as $child) { |
111
|
|
|
$this->processRegion($child); |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$this->loadWidgets($region); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Region $region The region |
120
|
|
|
*/ |
121
|
|
|
private function loadWidgets($region) |
122
|
|
|
{ |
123
|
|
|
$pagePartAdminConfiguration = null; |
124
|
|
|
foreach ($this->pagePartAdminConfigurations as $ppac) { |
125
|
|
|
if ($ppac->getContext() == $region->getName()) { |
126
|
|
|
$pagePartAdminConfiguration = $ppac; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($pagePartAdminConfiguration !== null) { |
131
|
|
|
$pagePartWidget = new PagePartWidget($this->page, $this->request, $this->em, $pagePartAdminConfiguration, $this->pagePartAdminFactory); |
|
|
|
|
132
|
|
|
$this->widgets[$region->getName()] = $pagePartWidget; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return PageTemplateInterface |
138
|
|
|
*/ |
139
|
|
|
public function getPageTemplate() |
140
|
|
|
{ |
141
|
|
|
return $this->pageTemplates[$this->pageTemplateConfiguration->getPageTemplate()]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return PageTemplateInterface[] |
146
|
|
|
*/ |
147
|
|
|
public function getPageTemplates() |
148
|
|
|
{ |
149
|
|
|
return $this->pageTemplates; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return PageInterface|HasPageTemplateInterface |
154
|
|
|
*/ |
155
|
|
|
public function getPage() |
156
|
|
|
{ |
157
|
|
|
return $this->page; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param FormBuilderInterface $builder The form builder |
162
|
|
|
*/ |
163
|
|
|
public function buildForm(FormBuilderInterface $builder) |
164
|
|
|
{ |
165
|
|
|
foreach ($this->widgets as $widget) { |
166
|
|
|
$widget->buildForm($builder); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param Request $request |
172
|
|
|
*/ |
173
|
|
|
public function bindRequest(Request $request) |
174
|
|
|
{ |
175
|
|
|
$configurationname = $request->get('pagetemplate_template'); |
176
|
|
|
$this->pageTemplateConfiguration->setPageTemplate($configurationname); |
177
|
|
|
foreach ($this->widgets as $widget) { |
178
|
|
|
$widget->bindRequest($request); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param EntityManager $em The entity manager |
184
|
|
|
*/ |
185
|
|
|
public function persist(EntityManager $em) |
186
|
|
|
{ |
187
|
|
|
$em->persist($this->pageTemplateConfiguration); |
188
|
|
|
foreach ($this->widgets as $widget) { |
189
|
|
|
$widget->persist($em); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param FormView $formView |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function getFormErrors(FormView $formView) |
199
|
|
|
{ |
200
|
|
|
$errors = array(); |
201
|
|
|
|
202
|
|
|
foreach ($this->widgets as $widget) { |
203
|
|
|
$errors = array_merge($errors, $widget->getFormErrors($formView)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $errors; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function getTemplate() |
213
|
|
|
{ |
214
|
|
|
return '@KunstmaanPagePart/FormWidgets/PageTemplateWidget/widget.html.twig'; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $name |
219
|
|
|
* |
220
|
|
|
* @return PagePartAdmin |
|
|
|
|
221
|
|
|
*/ |
222
|
|
|
public function getFormWidget($name) |
223
|
|
|
{ |
224
|
|
|
if (\array_key_exists($name, $this->widgets)) { |
225
|
|
|
return $this->widgets[$name]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return null; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param Request $request |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public function getExtraParams(Request $request) |
237
|
|
|
{ |
238
|
|
|
return []; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|