|
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->with('admin.page.title.label', ['class' => 'col-md-7']); |
|
217
|
|
|
$formMapper->add('title', TextType::class, [ |
|
218
|
|
|
'label' => 'admin.page.title.label', |
|
219
|
|
|
'help_html' => true, |
|
220
|
|
|
'help' => 'admin.page.title.help', |
|
221
|
|
|
'attr' => ['class' => 'titleToMeasure'], |
|
222
|
|
|
]); |
|
223
|
|
|
|
|
224
|
|
|
// Method existance is checked on each element to permit to use admin without all page Trait. |
|
225
|
|
|
if ($this->exists('H1')) { |
|
226
|
|
|
$formMapper->add('h1', TextType::class, [ |
|
227
|
|
|
'required' => false, |
|
228
|
|
|
'attr' => ['class' => 'input-lg'], |
|
229
|
|
|
'label' => 'admin.page.h1.label', |
|
230
|
|
|
'help_html' => true, |
|
231
|
|
|
'help' => 'admin.page.h1.help', |
|
232
|
|
|
]); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
if ($this->exists('MainImage')) { |
|
236
|
|
|
$formMapper->add('mainImage', \Sonata\AdminBundle\Form\Type\ModelListType::class, [ |
|
237
|
|
|
'required' => false, |
|
238
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_media'), |
|
239
|
|
|
'label' => 'admin.page.mainImage.label', |
|
240
|
|
|
'btn_edit' => false, |
|
241
|
|
|
]); |
|
242
|
|
|
} |
|
243
|
|
|
$formMapper->end(); |
|
244
|
|
|
|
|
245
|
|
|
$formMapper->with('admin.page.params.label', ['class' => 'col-md-5']); |
|
246
|
|
|
$formMapper->add('slug', TextType::class, [ |
|
247
|
|
|
'label' => 'admin.page.slug.label', |
|
248
|
|
|
'help_html' => true, |
|
249
|
|
|
'help' => $this->getSubject() && $this->getSubject()->getSlug() |
|
250
|
|
|
? '<span class="btn btn-link" onclick="toggleDisabled()" id="disabledLinkSlug"> |
|
251
|
|
|
<i class="fa fa-unlock"></i></span> |
|
252
|
|
|
<script>function toggleDisabled() { |
|
253
|
|
|
$(".slug_disabled").first().removeAttr("disabled"); |
|
254
|
|
|
$(".slug_disabled").first().focus(); |
|
255
|
|
|
$("#disabledLinkSlug").first().remove(); |
|
256
|
|
|
}</script>' |
|
257
|
|
|
: 'admin.page.slug.help', |
|
258
|
|
|
'attr' => [ |
|
259
|
|
|
'class' => 'slug_disabled', |
|
260
|
|
|
($this->getSubject() ? ($this->getSubject()->getSlug() ? 'disabled' : 't') : 't') => '', |
|
261
|
|
|
], |
|
262
|
|
|
]); |
|
263
|
|
|
|
|
264
|
|
|
if ($this->exists('Host') && count($this->getHosts()) > 1) { |
|
265
|
|
|
$this->configureFormFieldHost($formMapper); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
if ($this->exists('Locale')) { |
|
269
|
|
|
$formMapper->add('locale', TextType::class, [ |
|
270
|
|
|
'label' => 'admin.page.locale.label', |
|
271
|
|
|
'help_html' => true, |
|
272
|
|
|
'help' => 'admin.page.locale.help', |
|
273
|
|
|
]); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
if ($this->exists('metaRobots')) { |
|
277
|
|
|
$formMapper->add('metaRobots', ChoiceType::class, [ |
|
278
|
|
|
'choices' => [ |
|
279
|
|
|
'admin.page.metaRobots.choice.noIndex' => 'noindex', |
|
280
|
|
|
], |
|
281
|
|
|
'label' => 'admin.page.metaRobots.label', |
|
282
|
|
|
'required' => false, |
|
283
|
|
|
]); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
$formMapper->end(); |
|
287
|
|
|
|
|
288
|
|
|
return $formMapper; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
protected function configureFormFieldsBlockImages(FormMapper $formMapper): FormMapper |
|
292
|
|
|
{ |
|
293
|
|
|
if ($this->exists('images')) { |
|
294
|
|
|
$formMapper->with('admin.page.images.label', ['class' => 'col-md-5']); |
|
295
|
|
|
$formMapper->add( |
|
296
|
|
|
'pageHasMedias', |
|
297
|
|
|
CollectionType::class, |
|
298
|
|
|
[ |
|
299
|
|
|
'by_reference' => false, |
|
300
|
|
|
'required' => false, |
|
301
|
|
|
'label' => ' ', |
|
302
|
|
|
'type_options' => [ |
|
303
|
|
|
'delete' => true, |
|
304
|
|
|
], |
|
305
|
|
|
], |
|
306
|
|
|
[ |
|
307
|
|
|
'allow_add' => false, |
|
308
|
|
|
'allow_delete' => true, |
|
309
|
|
|
'btn_add' => false, |
|
310
|
|
|
'btn_catalogue' => false, |
|
311
|
|
|
'edit' => 'inline', |
|
312
|
|
|
'inline' => 'table', |
|
313
|
|
|
'sortable' => 'position', |
|
314
|
|
|
//'link_parameters' => ['context' => $context], |
|
315
|
|
|
'admin_code' => 'piedweb.admin.pagehasmedia', |
|
316
|
|
|
] |
|
317
|
|
|
); |
|
318
|
|
|
$formMapper->end(); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
return $formMapper; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
protected function configureFormFields(FormMapper $formMapper) |
|
325
|
|
|
{ |
|
326
|
|
|
$this->configureFormFieldsBlockTitle($formMapper); |
|
327
|
|
|
$this->configureFormFieldsBlockContent($formMapper); |
|
328
|
|
|
$this->configureFormFieldsBlockDetails($formMapper); |
|
329
|
|
|
$this->configureFormFieldsBlockImages($formMapper); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
protected function configureDatagridFilters(DatagridMapper $formMapper) |
|
333
|
|
|
{ |
|
334
|
|
|
$formMapper->add('locale', null, ['label' => 'admin.page.locale.label']); |
|
335
|
|
|
$formMapper->add('slug', null, ['label' => 'admin.page.slug.label']); |
|
336
|
|
|
|
|
337
|
|
|
$formMapper->add('title', null, ['label' => 'admin.page.title.label']); |
|
338
|
|
|
|
|
339
|
|
|
if ($this->exists('H1')) { |
|
340
|
|
|
$formMapper->add('h1', null, ['label' => 'admin.page.h1.label']); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
$formMapper->add('mainContent', null, ['label' => 'admin.page.mainContent.label']); |
|
344
|
|
|
|
|
345
|
|
|
if ($this->exists('name')) { |
|
346
|
|
|
$formMapper->add('name', null, ['label' => 'admin.page.name.label']); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
if ($this->exists('parentPage')) { |
|
350
|
|
|
$formMapper->add('parentPage', null, ['label' => 'admin.page.parentPage.label']); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
if ($this->exists('metaRobots')) { |
|
354
|
|
|
$formMapper->add('metaRobots', null, [ |
|
355
|
|
|
'choices' => [ |
|
356
|
|
|
'admin.page.metaRobots.choice.noIndex' => 'noindex', |
|
357
|
|
|
], |
|
358
|
|
|
'label' => 'admin.page.metaRobots.label', |
|
359
|
|
|
]); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
if ($this->exists('author')) { |
|
363
|
|
|
$formMapper->add('author', null, [ |
|
364
|
|
|
'label' => 'admin.page.author.label', |
|
365
|
|
|
'class' => $this->getContainer()->getParameter('pwc.entity_user'), |
|
366
|
|
|
'required' => false, |
|
367
|
|
|
]); |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
public function preUpdate($page) |
|
372
|
|
|
{ |
|
373
|
|
|
$page->setUpdatedAt(new \Datetime()); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
protected function configureListFields(ListMapper $listMapper) |
|
377
|
|
|
{ |
|
378
|
|
|
$listMapper->add('slug', null, [ |
|
379
|
|
|
'label' => 'admin.page.slug.label', |
|
380
|
|
|
]); |
|
381
|
|
|
$listMapper->addIdentifier('title', 'html', [ |
|
382
|
|
|
'label' => 'admin.page.title.label', |
|
383
|
|
|
]); |
|
384
|
|
|
$listMapper->add('updatedAt', null, [ |
|
385
|
|
|
'format' => 'd/m à H:m', |
|
386
|
|
|
'label' => 'admin.page.updatedAt.label', |
|
387
|
|
|
]); |
|
388
|
|
|
$listMapper->add('createdAt', null, [ |
|
389
|
|
|
'format' => 'd/m/y', |
|
390
|
|
|
'label' => 'admin.page.createdAt.label', |
|
391
|
|
|
]); |
|
392
|
|
|
$listMapper->add('metaRobots', null, [ |
|
393
|
|
|
'label' => 'admin.page.metaRobots.label', |
|
394
|
|
|
]); |
|
395
|
|
|
$listMapper->add('_action', null, [ |
|
396
|
|
|
'actions' => [ |
|
397
|
|
|
'show' => [], |
|
398
|
|
|
'delete' => [], |
|
399
|
|
|
], |
|
400
|
|
|
'row_align' => 'right', |
|
401
|
|
|
'header_class' => 'text-right', |
|
402
|
|
|
'label' => 'admin.action', |
|
403
|
|
|
]); |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
public function getObjectMetadata($page) |
|
407
|
|
|
{ |
|
408
|
|
|
$media = $page->getMainImage(); |
|
409
|
|
|
if (null !== $media && false !== strpos($media->getMimeType(), 'image/')) { |
|
410
|
|
|
$fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia(); |
|
411
|
|
|
$thumb = $this->liipImage->getBrowserPath($fullPath, 'thumb'); |
|
412
|
|
|
} else { |
|
413
|
|
|
$thumb = self::$thumb; |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
return new Metadata($page->getTitle(), null, $thumb); |
|
417
|
|
|
} |
|
418
|
|
|
} |
|
419
|
|
|
|