Passed
Push — master ( 4017d9...9757db )
by Dev
10:15
created

PageAdmin::getObjectMetadata()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 10
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
96
        return $instance;
97
    }
98
99
    protected function configureDatagridFilters(DatagridMapper $formMapper)
100
    {
101
        $formMapper->add('locale', null, ['label' => 'admin.page.locale.label']);
102
103
        if (\count($this->getHosts()) > 1) {
104
            $formMapper->add('host', null, ['label' => 'admin.page.host.label']);
105
        }
106
107
        $formMapper->add('h1', null, ['label' => 'admin.page.h1.label']);
108
109
        $formMapper->add('mainContent', null, ['label' => 'admin.page.mainContent.label']);
110
111
        $formMapper->add('slug', null, ['label' => 'admin.page.slug.label']);
112
113
        $formMapper->add('title', null, ['label' => 'admin.page.title.label']);
114
115
        if ($this->exists('name')) {
116
            $formMapper->add('name', null, ['label' => 'admin.page.name.label']);
117
        }
118
119
        if ($this->exists('parentPage')) {
120
            $formMapper->add('parentPage', null, ['label' => 'admin.page.parentPage.label']);
121
        }
122
123
        if ($this->exists('metaRobots')) {
124
            $formMapper->add('metaRobots', null, [
125
                'choices' => [
126
                    'admin.page.metaRobots.choice.noIndex' => 'noindex',
127
                ],
128
                'label' => 'admin.page.metaRobots.label',
129
            ]);
130
        }
131
    }
132
133
    public function preUpdate($page)
134
    {
135
        $page->setUpdatedAt(new \Datetime());
136
    }
137
138
    protected function configureListFields(ListMapper $listMapper)
139
    {
140
        $listMapper->addIdentifier('h1', 'html', [
141
            'label' => 'admin.page.title.label',
142
            'template' => '@pwcAdmin/page_list_titleField.html.twig',
143
        ]);
144
        $listMapper->add('updatedAt', null, [
145
            'format' => 'd/m à H:m',
146
            'label' => 'admin.page.updatedAt.label',
147
        ]);
148
        $listMapper->add('_action', null, [
149
            'actions' => [
150
                'show' => [],
151
                'delete' => [],
152
            ],
153
            'row_align' => 'right',
154
            'header_class' => 'text-right',
155
            'label' => 'admin.action',
156
        ]);
157
    }
158
159
    public function getObjectMetadata($page)
160
    {
161
        $media = $page->getMainImage();
162
        if (null !== $media && false !== strpos($media->getMimeType(), 'image/')) {
163
            $fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia();
164
            $thumb = $this->liipImage->getBrowserPath($fullPath, 'thumb');
165
        } else {
166
            $thumb = self::$thumb;
167
        }
168
169
        return new Metadata($page->getTitle(), null, $thumb);
170
    }
171
}
172