Passed
Push — master ( 542bba...4c863a )
by Dev
13:56
created

PageAdmin::getNewInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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