1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Admin; |
4
|
|
|
|
5
|
|
|
use Sonata\AdminBundle\Admin\AbstractAdmin; |
6
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
7
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
8
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
9
|
|
|
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType; |
10
|
|
|
use Sonata\BlockBundle\Meta\Metadata; |
11
|
|
|
use Sonata\Form\Type\CollectionType; |
12
|
|
|
use Sonata\Form\Type\DateTimePickerType; |
13
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\DateTimeType; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
18
|
|
|
|
19
|
|
|
class PageAdmin extends AbstractAdmin |
20
|
|
|
{ |
21
|
|
|
use AdminTrait; |
22
|
|
|
|
23
|
|
|
protected $datagridValues = [ |
24
|
|
|
'_page' => 1, |
25
|
|
|
'_sort_order' => 'DESC', |
26
|
|
|
'_sort_by' => 'updatedAt', |
27
|
|
|
'_per_page' => 256, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
protected $perPageOptions = [16, 250, 1000]; |
31
|
|
|
|
32
|
|
|
protected $maxPerPage = 1000; |
33
|
|
|
|
34
|
|
|
protected $liipImage; |
35
|
|
|
|
36
|
|
|
protected $defaultLocale; |
37
|
|
|
|
38
|
|
|
public function __construct($code, $class, $baseControllerName) |
39
|
|
|
{ |
40
|
|
|
parent::__construct($code, $class, $baseControllerName); |
41
|
|
|
$this->listModes['tree'] = [ |
42
|
|
|
'class' => 'fa fa-sitemap', |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setDefaultLocale($defaultLocale) |
47
|
|
|
{ |
48
|
|
|
$this->defaultLocale = $defaultLocale; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setLiipImage($liipImage) |
52
|
|
|
{ |
53
|
|
|
$this->liipImage = $liipImage; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function configure() |
57
|
|
|
{ |
58
|
|
|
$this->setTemplate('edit', '@PiedWebCMS/admin/page_edit.html.twig'); |
59
|
|
|
$this->setTemplate('show', '@PiedWebCMS/admin/page_show.html.twig'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if page entity's item $name exist. |
64
|
|
|
*/ |
65
|
|
|
protected function exists(string $name): bool |
66
|
|
|
{ |
67
|
|
|
return method_exists($this->getContainer()->getParameter('pwc.entity_page'), 'get'.$name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function configureFormFieldsBlockDetails(FormMapper $formMapper): FormMapper |
71
|
|
|
{ |
72
|
|
|
$formMapper->with('admin.details', ['class' => 'col-md-5']); |
73
|
|
|
|
74
|
|
|
if ($this->exists('parentPage')) { |
75
|
|
|
$formMapper->add('parentPage', EntityType::class, [ |
76
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_page'), |
77
|
|
|
'label' => 'admin.page.parentPage.label', |
78
|
|
|
'required' => false, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->configueFormFieldTranslations($formMapper); |
83
|
|
|
|
84
|
|
|
$formMapper->add('createdAt', DateTimePickerType::class, [ |
85
|
|
|
'format' => DateTimeType::HTML5_FORMAT, |
86
|
|
|
'dp_side_by_side' => true, |
87
|
|
|
'dp_use_current' => true, |
88
|
|
|
'dp_use_seconds' => false, |
89
|
|
|
'dp_collapse' => true, |
90
|
|
|
'dp_calendar_weeks' => false, |
91
|
|
|
'dp_view_mode' => 'days', |
92
|
|
|
'dp_min_view_mode' => 'days', |
93
|
|
|
'label' => 'admin.page.createdAt.label', |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
if ($this->exists('name')) { |
97
|
|
|
$formMapper->add('name', TextType::class, [ |
98
|
|
|
'label' => 'admin.page.name.label', |
99
|
|
|
'required' => false, |
100
|
|
|
'help_html' => true, |
101
|
|
|
'help' => 'admin.page.name.help', |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($this->exists('excrept')) { |
106
|
|
|
$formMapper->add('excrept', TextareaType::class, [ |
107
|
|
|
'required' => false, |
108
|
|
|
'label' => 'admin.page.excrept.label', |
109
|
|
|
'help_html' => true, |
110
|
|
|
'help' => 'admin.page.excrept.help', |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->exists('relatedPages')) { |
115
|
|
|
$formMapper->add('relatedPages', ModelAutocompleteType::class, [ |
116
|
|
|
'required' => false, |
117
|
|
|
'multiple' => true, |
118
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_page'), |
119
|
|
|
'property' => 'title', // or any field in your media entity |
120
|
|
|
'label' => 'admin.page.relatedPage.label', |
121
|
|
|
'btn_add' => false, |
122
|
|
|
'to_string_callback' => function ($entity) { |
123
|
|
|
return $entity->getTitle(); |
124
|
|
|
}, |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->configureFormFieldOtherProperties($formMapper); |
129
|
|
|
|
130
|
|
|
$formMapper->end(); |
131
|
|
|
|
132
|
|
|
return $formMapper; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function configureFormFieldsBlockContent(FormMapper $formMapper): FormMapper |
136
|
|
|
{ |
137
|
|
|
$formMapper->with('admin.page.mainContent.label', ['class' => 'col-md-7']); |
138
|
|
|
$formMapper->add('mainContent', TextareaType::class, [ |
139
|
|
|
'attr' => [ |
140
|
|
|
'style' => 'min-height: 80vh;font-size:125%; max-width:900px', |
141
|
|
|
'data-editor' => 'markdown', |
142
|
|
|
'data-gutter' => 0, |
143
|
|
|
], |
144
|
|
|
'required' => false, |
145
|
|
|
'label' => ' ', |
146
|
|
|
'help_html' => true, |
147
|
|
|
'help' => 'admin.page.mainContent.help', |
148
|
|
|
]); |
149
|
|
|
$formMapper->add('mainContentIsMarkdown', null, [ |
150
|
|
|
'required' => false, |
151
|
|
|
'label' => 'admin.page.markdown.label', |
152
|
|
|
'help_html' => true, |
153
|
|
|
'help' => 'admin.page.markdown.help', |
154
|
|
|
]); |
155
|
|
|
$formMapper->end(); |
156
|
|
|
|
157
|
|
|
return $formMapper; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function configureFormFieldOtherProperties(FormMapper $formMapper): FormMapper |
161
|
|
|
{ |
162
|
|
|
return !$this->exists('otherProperties') ? $formMapper : $formMapper->add('otherProperties', null, [ |
163
|
|
|
'required' => false, |
164
|
|
|
'attr' => [ |
165
|
|
|
'style' => 'min-height:15vh', |
166
|
|
|
'data-editor' => 'yaml', |
167
|
|
|
], |
168
|
|
|
'label' => 'admin.page.otherProperties.label', |
169
|
|
|
'help_html' => true, |
170
|
|
|
'help' => 'admin.page.otherProperties.help', |
171
|
|
|
]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
protected function getHosts() |
175
|
|
|
{ |
176
|
|
|
return array_keys($this->getContainer()->getParameter('pwc.apps')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function configureFormFieldHost(FormMapper $formMapper): FormMapper |
180
|
|
|
{ |
181
|
|
|
if (null === $this->getSubject()->getHost()) { |
182
|
|
|
$this->getSubject()->setHost($this->getHosts()[0]); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $formMapper->add('host', ChoiceType::class, [ |
186
|
|
|
'choices' => array_combine($this->getHosts(), $this->getHosts()), |
187
|
|
|
'required' => false, |
188
|
|
|
'label' => 'admin.page.host.label', |
189
|
|
|
'empty_data' => $this->getHosts()[0], |
190
|
|
|
]); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function configueFormFieldTranslations(FormMapper $formMapper): FormMapper |
194
|
|
|
{ |
195
|
|
|
return $formMapper->add('translations', ModelAutocompleteType::class, [ |
196
|
|
|
'required' => false, |
197
|
|
|
'multiple' => true, |
198
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_page'), |
199
|
|
|
'property' => 'slug', |
200
|
|
|
'label' => 'admin.page.translations.label', |
201
|
|
|
'help_html' => true, |
202
|
|
|
'help' => 'admin.page.translations.help', |
203
|
|
|
'btn_add' => false, |
204
|
|
|
'to_string_callback' => function ($entity) { |
205
|
|
|
return $entity->getLocale() |
206
|
|
|
? $entity->getLocale().' ('.$entity->getSlug().')' |
207
|
|
|
: $entity->getSlug(); // switch for getLocale |
208
|
|
|
// todo : remove it in next release and leave only get locale |
209
|
|
|
// todo : add a clickable link to the other admin |
210
|
|
|
}, |
211
|
|
|
]); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
protected function configureFormFieldsBlockTitle(FormMapper $formMapper): FormMapper |
215
|
|
|
{ |
216
|
|
|
$formMapper |
217
|
|
|
->with('admin.page.title.label', ['class' => 'col-md-7']); |
218
|
|
|
|
219
|
|
|
$formMapper->add('title', TextType::class, [ |
220
|
|
|
'label' => 'admin.page.title.label', |
221
|
|
|
'help_html' => true, |
222
|
|
|
'help' => 'admin.page.title.help', |
223
|
|
|
'attr' => ['class' => 'titleToMeasure'], |
224
|
|
|
]); |
225
|
|
|
|
226
|
|
|
// Method existance is checked on each element to permit to use admin without all page Trait. |
227
|
|
|
if ($this->exists('H1')) { |
228
|
|
|
$formMapper->add('h1', TextType::class, [ |
229
|
|
|
'required' => false, |
230
|
|
|
'attr' => ['class' => 'input-lg'], |
231
|
|
|
'label' => 'admin.page.h1.label', |
232
|
|
|
'help_html' => true, |
233
|
|
|
'help' => 'admin.page.h1.help', |
234
|
|
|
]); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if ($this->exists('MainImage')) { |
238
|
|
|
$formMapper->add('mainImage', \Sonata\AdminBundle\Form\Type\ModelListType::class, [ |
239
|
|
|
'required' => false, |
240
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_media'), |
241
|
|
|
'label' => 'admin.page.mainImage.label', |
242
|
|
|
'btn_edit' => false, |
243
|
|
|
]); |
244
|
|
|
} |
245
|
|
|
$formMapper->end(); |
246
|
|
|
|
247
|
|
|
return $formMapper; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function configureFormFieldBlockParams(FormMapper $formMapper) |
251
|
|
|
{ |
252
|
|
|
$formMapper->with('admin.page.params.label', ['class' => 'col-md-5']); |
253
|
|
|
|
254
|
|
|
$formMapper->add('slug', TextType::class, [ |
255
|
|
|
'label' => 'admin.page.slug.label', |
256
|
|
|
'help_html' => true, |
257
|
|
|
'help' => $this->getSubject() && $this->getSubject()->getSlug() |
258
|
|
|
? '<span class="btn btn-link" onclick="toggleDisabled()" id="disabledLinkSlug"> |
259
|
|
|
<i class="fa fa-unlock"></i></span> |
260
|
|
|
<script>function toggleDisabled() { |
261
|
|
|
$(".slug_disabled").first().removeAttr("disabled"); |
262
|
|
|
$(".slug_disabled").first().focus(); |
263
|
|
|
$("#disabledLinkSlug").first().remove(); |
264
|
|
|
}</script>' |
265
|
|
|
: 'admin.page.slug.help', |
266
|
|
|
'attr' => [ |
267
|
|
|
'class' => 'slug_disabled', |
268
|
|
|
($this->getSubject() ? ($this->getSubject()->getSlug() ? 'disabled' : 't') : 't') => '', |
269
|
|
|
], |
270
|
|
|
]); |
271
|
|
|
|
272
|
|
|
if ($this->exists('Host') && count($this->getHosts()) > 1) { |
273
|
|
|
$this->configureFormFieldHost($formMapper); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if ($this->exists('Locale')) { |
277
|
|
|
$formMapper->add('locale', TextType::class, [ |
278
|
|
|
'label' => 'admin.page.locale.label', |
279
|
|
|
'help_html' => true, |
280
|
|
|
'help' => 'admin.page.locale.help', |
281
|
|
|
]); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if ($this->exists('metaRobots')) { |
285
|
|
|
$formMapper->add('metaRobots', ChoiceType::class, [ |
286
|
|
|
'choices' => [ |
287
|
|
|
'admin.page.metaRobots.choice.noIndex' => 'noindex', |
288
|
|
|
], |
289
|
|
|
'label' => 'admin.page.metaRobots.label', |
290
|
|
|
'required' => false, |
291
|
|
|
]); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
$formMapper->end(); |
295
|
|
|
|
296
|
|
|
return $formMapper; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
protected function configureFormFieldsBlockImages(FormMapper $formMapper): FormMapper |
300
|
|
|
{ |
301
|
|
|
if ($this->exists('images')) { |
302
|
|
|
$formMapper->with('admin.page.images.label', ['class' => 'col-md-5']); |
303
|
|
|
$formMapper->add( |
304
|
|
|
'pageHasMedias', |
305
|
|
|
CollectionType::class, |
306
|
|
|
[ |
307
|
|
|
'by_reference' => false, |
308
|
|
|
'required' => false, |
309
|
|
|
'label' => ' ', |
310
|
|
|
'type_options' => [ |
311
|
|
|
'delete' => true, |
312
|
|
|
], |
313
|
|
|
], |
314
|
|
|
[ |
315
|
|
|
'allow_add' => false, |
316
|
|
|
'allow_delete' => true, |
317
|
|
|
'btn_add' => false, |
318
|
|
|
'btn_catalogue' => false, |
319
|
|
|
'edit' => 'inline', |
320
|
|
|
'inline' => 'table', |
321
|
|
|
'sortable' => 'position', |
322
|
|
|
//'link_parameters' => ['context' => $context], |
323
|
|
|
'admin_code' => 'piedweb.admin.pagehasmedia', |
324
|
|
|
] |
325
|
|
|
); |
326
|
|
|
$formMapper->end(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $formMapper; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
protected function configureFormFields(FormMapper $formMapper) |
333
|
|
|
{ |
334
|
|
|
$this->configureFormFieldsBlockTitle($formMapper); |
335
|
|
|
$this->configureFormFieldBlockParams($formMapper); |
336
|
|
|
$this->configureFormFieldsBlockContent($formMapper); |
337
|
|
|
$this->configureFormFieldsBlockDetails($formMapper); |
338
|
|
|
$this->configureFormFieldsBlockImages($formMapper); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
protected function configureDatagridFilters(DatagridMapper $formMapper) |
342
|
|
|
{ |
343
|
|
|
$formMapper->add('locale', null, ['label' => 'admin.page.locale.label']); |
344
|
|
|
|
345
|
|
|
if (count($this->getHosts()) > 1) { |
346
|
|
|
$formMapper->add('host', null, ['label' => 'admin.page.host.label']); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
$formMapper->add('slug', null, ['label' => 'admin.page.slug.label']); |
350
|
|
|
|
351
|
|
|
$formMapper->add('title', null, ['label' => 'admin.page.title.label']); |
352
|
|
|
|
353
|
|
|
if ($this->exists('H1')) { |
354
|
|
|
$formMapper->add('h1', null, ['label' => 'admin.page.h1.label']); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$formMapper->add('mainContent', null, ['label' => 'admin.page.mainContent.label']); |
358
|
|
|
|
359
|
|
|
if ($this->exists('name')) { |
360
|
|
|
$formMapper->add('name', null, ['label' => 'admin.page.name.label']); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
if ($this->exists('parentPage')) { |
364
|
|
|
$formMapper->add('parentPage', null, ['label' => 'admin.page.parentPage.label']); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if ($this->exists('metaRobots')) { |
368
|
|
|
$formMapper->add('metaRobots', null, [ |
369
|
|
|
'choices' => [ |
370
|
|
|
'admin.page.metaRobots.choice.noIndex' => 'noindex', |
371
|
|
|
], |
372
|
|
|
'label' => 'admin.page.metaRobots.label', |
373
|
|
|
]); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if ($this->exists('author')) { |
377
|
|
|
$formMapper->add('author', null, [ |
378
|
|
|
'label' => 'admin.page.author.label', |
379
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_user'), |
380
|
|
|
'required' => false, |
381
|
|
|
]); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
public function preUpdate($page) |
386
|
|
|
{ |
387
|
|
|
$page->setUpdatedAt(new \Datetime()); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
protected function configureListFields(ListMapper $listMapper) |
391
|
|
|
{ |
392
|
|
|
$listMapper->addIdentifier('title', 'html', [ |
393
|
|
|
'label' => 'admin.page.title.label', |
394
|
|
|
'template' => '@PiedWebCMS/admin/base_list_field.html.twig', |
395
|
|
|
]); |
396
|
|
|
$listMapper->add('updatedAt', null, [ |
397
|
|
|
'format' => 'd/m à H:m', |
398
|
|
|
'label' => 'admin.page.updatedAt.label', |
399
|
|
|
]); |
400
|
|
|
$listMapper->add('createdAt', null, [ |
401
|
|
|
'format' => 'd/m/y', |
402
|
|
|
'label' => 'admin.page.createdAt.label', |
403
|
|
|
]); |
404
|
|
|
$listMapper->add('metaRobots', null, [ |
405
|
|
|
'label' => 'admin.page.metaRobots.label', |
406
|
|
|
]); |
407
|
|
|
$listMapper->add('_action', null, [ |
408
|
|
|
'actions' => [ |
409
|
|
|
'show' => [], |
410
|
|
|
'delete' => [], |
411
|
|
|
], |
412
|
|
|
'row_align' => 'right', |
413
|
|
|
'header_class' => 'text-right', |
414
|
|
|
'label' => 'admin.action', |
415
|
|
|
]); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
public function getObjectMetadata($page) |
419
|
|
|
{ |
420
|
|
|
$media = $page->getMainImage(); |
421
|
|
|
if (null !== $media && false !== strpos($media->getMimeType(), 'image/')) { |
422
|
|
|
$fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia(); |
423
|
|
|
$thumb = $this->liipImage->getBrowserPath($fullPath, 'thumb'); |
424
|
|
|
} else { |
425
|
|
|
$thumb = self::$thumb; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
return new Metadata($page->getTitle(), null, $thumb); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|