PageAdmin::configureListFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 1
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Extension\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\BlockBundle\Meta\Metadata;
10
use Sonata\AdminBundle\Object\Metadata;
11
12
class PageAdmin extends AbstractAdmin
13
{
14
    use AdminTrait;
0 ignored issues
show
Bug introduced by
The trait PiedWeb\CMSBundle\Extension\Admin\AdminTrait requires the property $query which is not provided by PiedWeb\CMSBundle\Extension\Admin\PageAdmin.
Loading history...
15
    use PageAdminFormFieldsTrait;
16
17
    protected $datagridValues = [
18
        '_page' => 1,
19
        '_sort_order' => 'DESC',
20
        '_sort_by' => 'updatedAt',
21
        '_per_page' => 256,
22
    ];
23
24
    protected $perPageOptions = [16, 250, 1000];
25
26
    protected $maxPerPage = 1000;
27
28
    protected $liipImage;
29
30
    protected $defaultLocale;
31
32
    public function __construct($code, $class, $baseControllerName)
33
    {
34
        parent::__construct($code, $class, $baseControllerName);
35
        $this->listModes['tree'] = [
36
            'class' => 'fa fa-sitemap',
37
        ];
38
    }
39
40
    public function setDefaultLocale($defaultLocale)
41
    {
42
        $this->defaultLocale = $defaultLocale;
43
    }
44
45
    public function setLiipImage($liipImage)
46
    {
47
        $this->liipImage = $liipImage;
48
    }
49
50
    /**
51
     * Check if page entity's item $name exist.
52
     */
53
    protected function exists(string $name): bool
54
    {
55
        return method_exists($this->pageClass, 'get'.$name);
56
    }
57
58
    protected function configureFormFields(FormMapper $formMapper)
59
    {
60
        // Next : load this from configuration
61
        $mainFields = ['h1', 'mainContent', 'mainContentIsMarkdown'];
62
        $columnFields = [
63
            'admin.page.state.label' => ['createdAt', 'metaRobots'],
64
            'admin.page.permanlien.label' => ['host', 'slug', 'parentPage'],
65
            'admin.page.extended.label' => ['expand' => true, 'fields' => ['mainImage', 'name', 'title',  'excrept']],
66
            'admin.page.translations.label' => ['locale', 'translations'],
67
            'admin.page.otherProperties.label' => ['expand' => true, 'fields' => ['otherProperties']],
68
            'admin.page.images.label' => ['images'],
69
        ];
70
71
        $formMapper->with('admin.page.mainContent.label', ['class' => 'col-md-9 mainFields']);
72
        foreach ($mainFields as $field) {
73
            $func = 'configureFormField'.ucfirst($field);
74
            $this->$func($formMapper);
75
        }
76
        $formMapper->end();
77
78
        foreach ($columnFields as $k => $block) {
79
            $fields = $block['fields'] ?? $block;
80
            $class = isset($block['expand']) ? 'expand' : '';
81
            $formMapper->with($k, ['class' => 'col-md-3 columnFields '.$class]);
82
            foreach ($fields as $field) {
83
                $func = 'configureFormField'.ucfirst($field);
84
                $this->$func($formMapper);
85
            }
86
87
            $formMapper->end();
88
        }
89
    }
90
91
    public function getNewInstance()
92
    {
93
        $instance = parent::getNewInstance();
94
        $instance->setLocale($this->defaultLocale);
95
        $instance->setMainContentType($this->defaultMainContentType);
96
97
        return $instance;
98
    }
99
100
    protected function configureDatagridFilters(DatagridMapper $formMapper)
101
    {
102
        $formMapper->add('locale', null, ['label' => 'admin.page.locale.label']);
103
104
        if (\count($this->getHosts()) > 1) {
105
            $formMapper->add('host', null, ['label' => 'admin.page.host.label']);
106
        }
107
108
        $formMapper->add('h1', null, ['label' => 'admin.page.h1.label']);
109
110
        $formMapper->add('mainContent', null, ['label' => 'admin.page.mainContent.label']);
111
112
        $formMapper->add('slug', null, ['label' => 'admin.page.slug.label']);
113
114
        $formMapper->add('title', null, ['label' => 'admin.page.title.label']);
115
116
        if ($this->exists('name')) {
117
            $formMapper->add('name', null, ['label' => 'admin.page.name.label']);
118
        }
119
120
        if ($this->exists('parentPage')) {
121
            $formMapper->add('parentPage', null, ['label' => 'admin.page.parentPage.label']);
122
        }
123
124
        if ($this->exists('metaRobots')) {
125
            $formMapper->add('metaRobots', null, [
126
                'choices' => [
127
                    'admin.page.metaRobots.choice.noIndex' => 'noindex',
128
                ],
129
                'label' => 'admin.page.metaRobots.label',
130
            ]);
131
        }
132
    }
133
134
    public function preUpdate($page)
135
    {
136
        $page->setUpdatedAt(new \Datetime());
137
    }
138
139
    protected function configureListFields(ListMapper $listMapper)
140
    {
141
        $listMapper->addIdentifier('h1', 'html', [
142
            'label' => 'admin.page.title.label',
143
            'template' => '@pwcAdmin/page_list_titleField.html.twig',
144
        ]);
145
        $listMapper->add('updatedAt', null, [
146
            'format' => 'd/m à H:m',
147
            'label' => 'admin.page.updatedAt.label',
148
        ]);
149
        $listMapper->add('_action', null, [
150
            'actions' => [
151
                'show' => [],
152
                'delete' => [],
153
            ],
154
            'row_align' => 'right',
155
            'header_class' => 'text-right',
156
            'label' => 'admin.action',
157
        ]);
158
    }
159
160
    public function getObjectMetadata($page)
161
    {
162
        $media = $page->getMainImage();
163
        if (null !== $media && false !== strpos($media->getMimeType(), 'image/')) {
164
            $fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia();
165
            $thumb = $this->liipImage->getBrowserPath($fullPath, 'thumb');
166
        } else {
167
            $thumb = self::$thumb;
168
        }
169
170
        return new Metadata(strip_tags($page->getName(true)), null, $thumb);
171
    }
172
}
173