Completed
Push — master ( 6130d0...57338d )
by Dev
24:32 queued 11:23
created

PageAdmin::configureFormFieldsBlockImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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