Passed
Push — master ( cbbc27...86f200 )
by Dev
11:12
created

PageAdmin::configureFormFieldsBlockImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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