1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Zicht\Bundle\PageBundle\Admin; |
7
|
|
|
|
8
|
|
|
use Sonata\AdminBundle\Admin\Admin; |
9
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
10
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
11
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
12
|
|
|
use Sonata\AdminBundle\Show\ShowMapper; |
13
|
|
|
use Symfony\Component\Form\FormEvent; |
14
|
|
|
use Symfony\Component\Form\FormEvents; |
15
|
|
|
use Zicht\Bundle\AdminBundle\Util\AdminUtil; |
16
|
|
|
use Zicht\Bundle\MenuBundle\Entity\MenuItem; |
17
|
|
|
use Zicht\Bundle\MenuBundle\Form\Subscriber\MenuItemPersistenceSubscriber; |
18
|
|
|
use Zicht\Bundle\MenuBundle\Manager\MenuManager; |
19
|
|
|
use Zicht\Bundle\PageBundle\Entity\ContentItem; |
20
|
|
|
use Zicht\Bundle\PageBundle\Manager\PageManager; |
21
|
|
|
use Zicht\Bundle\PageBundle\Model\ContentItemContainer; |
22
|
|
|
use Zicht\Bundle\PageBundle\Model\PageInterface; |
23
|
|
|
use Zicht\Bundle\UrlBundle\Url\Provider; |
24
|
|
|
use Zicht\Util\Str; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Admin for the messages catalogue |
28
|
|
|
*/ |
29
|
|
|
class PageAdmin extends Admin |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $persistFilters = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $templates = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var PageManager |
43
|
|
|
*/ |
44
|
|
|
protected $pageManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var MenuManager |
48
|
|
|
*/ |
49
|
|
|
protected $menuManager = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $contentItemAdminCode; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Provider|null |
58
|
|
|
*/ |
59
|
|
|
private $urlProvider = null; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor, overridden to be able to set the (required) content item admin code. |
63
|
|
|
* |
64
|
|
|
* @param string $code |
65
|
|
|
* @param string $class |
66
|
|
|
* @param string $baseControllerName |
67
|
|
|
* @param string $contentItemAdminCode |
68
|
|
|
*/ |
69
|
|
|
public function __construct($code, $class, $baseControllerName, $contentItemAdminCode) |
70
|
|
|
{ |
71
|
|
|
parent::__construct($code, $class, $baseControllerName); |
72
|
|
|
|
73
|
|
|
$this->contentItemAdminCode = $contentItemAdminCode; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the page manager |
78
|
|
|
* |
79
|
|
|
* @param \Zicht\Bundle\PageBundle\Manager\PageManager $pageManager |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function setPageManager(PageManager $pageManager) |
83
|
|
|
{ |
84
|
|
|
$this->pageManager = $pageManager; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the menumanager, which is needed for flushing the menu items to the persistence layer whenever a page |
90
|
|
|
* is updated. |
91
|
|
|
* |
92
|
|
|
* @param \Zicht\Bundle\MenuBundle\Manager\MenuManager $manager |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function setMenuManager(MenuManager $manager) |
96
|
|
|
{ |
97
|
|
|
$this->menuManager = $manager; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Sets the url provider |
103
|
|
|
* |
104
|
|
|
* @param Provider $urlProvider |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setUrlProvider(Provider $urlProvider) |
108
|
|
|
{ |
109
|
|
|
$this->urlProvider = $urlProvider; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @{inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function configureShowFields(ShowMapper $showMapper) |
116
|
|
|
{ |
117
|
|
|
return $showMapper->add('title'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @{inheritDoc} |
122
|
|
|
*/ |
123
|
|
|
public function configureListFields(ListMapper $listMapper) |
124
|
|
|
{ |
125
|
|
|
return $listMapper |
126
|
|
|
->addIdentifier('title') |
127
|
|
|
->add('displayType') |
128
|
|
|
->add('date_updated') |
129
|
|
|
->add( |
130
|
|
|
'_action', |
131
|
|
|
'actions', |
132
|
|
|
array( |
133
|
|
|
'actions' => array( |
134
|
|
|
'view' => array(), |
135
|
|
|
'edit' => array(), |
136
|
|
|
'delete' => array() |
137
|
|
|
) |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @{inheritDoc} |
144
|
|
|
*/ |
145
|
|
|
public function configureFormFields(FormMapper $formMapper) |
146
|
|
|
{ |
147
|
|
|
$formMapper |
148
|
|
|
->tab('admin.tab.general') |
149
|
|
|
->add('title', null, array('required' => true)) |
150
|
|
|
->end()->end(); |
151
|
|
|
|
152
|
|
|
if (($subject = $this->getSubject()) && $subject->getId()) { |
153
|
|
|
if ($subject->getContentItemMatrix() && $subject->getContentItemMatrix()->getTypes()) { |
154
|
|
|
if (!($subject instanceof ContentItemContainer)) { |
155
|
|
|
throw new \RuntimeException(sprintf('The zicht/page-bundle assumes that entity %s implements the ContentItemContainer interface.', get_class($subject))); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$formMapper |
159
|
|
|
->tab('admin.tab.content') |
160
|
|
|
->add( |
161
|
|
|
'contentItems', |
162
|
|
|
'sonata_type_collection', |
163
|
|
|
array( |
164
|
|
|
'btn_add' => 'content_item.add' |
165
|
|
|
), |
166
|
|
|
array( |
167
|
|
|
'edit' => 'inline', |
168
|
|
|
'inline' => 'table', |
169
|
|
|
'sortable' => 'weight', |
170
|
|
|
'admin_code' => $this->code . '|' . $this->contentItemAdminCode |
171
|
|
|
) |
172
|
|
|
) |
173
|
|
|
->end()->end(); |
174
|
|
|
|
175
|
|
|
$formMapper->getFormBuilder()->addEventListener( |
176
|
|
|
FormEvents::SUBMIT, |
177
|
|
|
function (FormEvent $e) { |
178
|
|
|
/** @var PageInterface $pageData */ |
179
|
|
|
$pageData = $e->getData(); |
180
|
|
|
|
181
|
|
|
$contentItems = $pageData->getContentItems(); |
182
|
|
|
|
183
|
|
|
foreach ($contentItems as $data) { |
184
|
|
|
if (null === $data) { |
185
|
|
|
continue; |
186
|
|
|
} |
187
|
|
|
$type = $data->getConvertToType(); |
188
|
|
|
|
189
|
|
|
if (!$data->getId() && $type !== get_class($data)) { |
190
|
|
|
$item = new $type; |
191
|
|
|
|
192
|
|
|
ContentItem::convert($data, $item); |
193
|
|
|
|
194
|
|
|
$pageData->removeContentItem($data); |
195
|
|
|
$pageData->addContentItem($item); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
$e->setData($pageData); |
199
|
|
|
}, |
200
|
|
|
64 |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
$formMapper |
204
|
|
|
->tab('admin.tab.menu') |
205
|
|
|
->add('menu_item', 'zicht_menu_item', array('translation_domain' => $this->getTranslationDomain())) |
206
|
|
|
->end() |
207
|
|
|
->end(); |
208
|
|
|
|
209
|
|
|
$formMapper |
210
|
|
|
->getFormBuilder() |
211
|
|
|
->addEventSubscriber( |
212
|
|
|
new MenuItemPersistenceSubscriber( |
213
|
|
|
$this->menuManager, |
214
|
|
|
$this->urlProvider, |
|
|
|
|
215
|
|
|
'menu_item' |
216
|
|
|
) |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @{inheritDoc} |
224
|
|
|
*/ |
225
|
|
|
protected function configureDatagridFilters(DatagridMapper $filter) |
226
|
|
|
{ |
227
|
|
|
$filter->add('title') |
228
|
|
|
->add('id'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @{inheritDoc} |
233
|
|
|
*/ |
234
|
|
|
public function preUpdate($object) |
235
|
|
|
{ |
236
|
|
|
$this->fixOneToMany($object); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @{inheritDoc} |
241
|
|
|
*/ |
242
|
|
|
public function prePersist($object) |
243
|
|
|
{ |
244
|
|
|
$this->fixOneToMany($object); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Fixes the many-to-one side of the one-to-many content items and flushes the menu manager. |
249
|
|
|
* |
250
|
|
|
* @param \Zicht\Bundle\PageBundle\Model\PageInterface $object |
251
|
|
|
* @return void |
252
|
|
|
*/ |
253
|
|
|
protected function fixOneToMany(PageInterface $object) |
254
|
|
|
{ |
255
|
|
|
$items = $object->getContentItems(); |
256
|
|
|
if ($items) { |
257
|
|
|
foreach ($object->getContentItems() as $item) { |
258
|
|
|
$item->setPage($object); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
if ($this->menuManager) { |
262
|
|
|
$this->menuManager->flush(); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Pre remove function |
268
|
|
|
* |
269
|
|
|
* @param mixed $object |
270
|
|
|
*/ |
271
|
|
|
public function preRemove($object) |
272
|
|
|
{ |
273
|
|
|
if (!is_null($this->urlProvider) && !is_null($this->menuManager)) { |
274
|
|
|
$url = $this->urlProvider->url($object); |
275
|
|
|
$menuItem = $this->menuManager->getItem($url); |
|
|
|
|
276
|
|
|
|
277
|
|
|
if ($menuItem instanceof MenuItem) { |
|
|
|
|
278
|
|
|
$this->menuManager->removeItem($menuItem); |
279
|
|
|
$this->menuManager->flush(); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Reorder tabs |
286
|
|
|
* |
287
|
|
|
* @param FormMapper $formMapper |
288
|
|
|
* @param array $tabOrder |
289
|
|
|
* |
290
|
|
|
* @deprecated See Zicht\Bundle\AdminBundle\Util\AdminUtil::reorderTabs |
291
|
|
|
* |
292
|
|
|
*/ |
293
|
|
|
public function reorderTabs(FormMapper $formMapper, array $tabOrder) |
294
|
|
|
{ |
295
|
|
|
AdminUtil::reorderTabs($formMapper, $tabOrder); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Removes field (and also removes the tab when the tab/group is empty) |
300
|
|
|
* |
301
|
|
|
* @param string|array $fieldNames one fieldname or array of fieldnames |
302
|
|
|
* @param FormMapper $formMapper |
303
|
|
|
* @return self |
304
|
|
|
*/ |
305
|
|
|
public function removeFields($fieldNames, FormMapper $formMapper) |
306
|
|
|
{ |
307
|
|
|
if (!is_array($fieldNames)) { |
308
|
|
|
$fieldNames = array($fieldNames); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
foreach ($fieldNames as $fieldName) { |
312
|
|
|
$formMapper->remove($fieldName); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$this->removeEmptyGroups(); |
316
|
|
|
|
317
|
|
|
return $this; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Removes tab and all it's fields in it |
322
|
|
|
* |
323
|
|
|
* @param string $tabName |
324
|
|
|
* @param FormMapper $formMapper |
325
|
|
|
*/ |
326
|
|
|
public function removeTab($tabName, FormMapper $formMapper) |
327
|
|
|
{ |
328
|
|
|
$tabs = $this->getFormTabs(); |
329
|
|
|
|
330
|
|
|
if (array_key_exists($tabName, $tabs)) { |
|
|
|
|
331
|
|
|
$groups = $this->getFormGroups(); |
332
|
|
|
|
333
|
|
|
if (!is_array($groups)) { |
|
|
|
|
334
|
|
|
return; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
foreach ($tabs[$tabName]['groups'] as $group) { |
338
|
|
|
if (isset($groups[$group])) { |
339
|
|
|
foreach ($groups[$group]['fields'] as $field) { |
340
|
|
|
$formMapper->remove($field); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
unset($groups[$group]); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$this->setFormGroups($groups); |
347
|
|
|
$this->removeEmptyGroups(); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Removes the empty tabs from the groups |
353
|
|
|
*/ |
354
|
|
|
public function removeEmptyGroups() |
355
|
|
|
{ |
356
|
|
|
$tabs = $this->getFormTabs(); |
357
|
|
|
|
358
|
|
|
if (!is_array($tabs)) { |
359
|
|
|
return; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
$groups = $this->getFormGroups(); |
363
|
|
|
|
364
|
|
|
foreach ($tabs as $tabKey => $tab) { |
365
|
|
|
foreach ($tab['groups'] as $tabGroup) { |
366
|
|
|
if (!array_key_exists($tabGroup, $groups)) { |
367
|
|
|
unset($tabs[$tabKey]); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$this->setFormTabs($tabs); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @{inheritDoc} |
377
|
|
|
*/ |
378
|
|
|
public function getLabel() |
379
|
|
|
{ |
380
|
|
|
return sprintf('admin.label.%s', Str::infix(lcfirst(Str::classname(get_class($this))), '_')); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|