Completed
Push — master ( b7c9ca...dc1345 )
by Benjamin
02:24
created

BaseNodeEntityAdmin::configureFormFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 2 Features 2
Metric Value
c 10
b 2
f 2
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Admin;
4
5
use Alpixel\Bundle\CMSBundle\Form\DateTimeSingleType;
6
use Sonata\AdminBundle\Datagrid\DatagridMapper;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Form\FormMapper;
9
use Sonata\AdminBundle\Route\RouteCollection;
10
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
11
12
class BaseNodeEntityAdmin extends BaseAdmin
13
{
14
    protected $baseRouteName = 'alpixel_admin_cms_node';
15
    protected $baseRoutePattern = 'node';
16
    protected $datagridValues = [
17
        '_page'       => 1,
18
        '_sort_order' => 'DESC',
19
        '_sort_by'    => 'dateUpdated',
20
    ];
21
22
    protected function configureRoutes(RouteCollection $collection)
23
    {
24
        $collection->add('editContent', $this->getRouterIdParameter().'/edit/node');
25
        $collection->add('createTranslation', $this->getRouterIdParameter().'/translate');
26
    }
27
28
    protected function configureMainFields(FormMapper $formMapper)
29
    {
30
        $formMapper
31
            ->add('title', null, [
32
                'label'    => 'Titre',
33
                'required' => true,
34
            ])
35
            ->add('content', 'ckeditor', [
36
                'label' => 'Contenu',
37
                'required' => false,
38
                'config_name' => 'admin',
39
            ]);
40
    }
41
42
    protected function configureFormFields(FormMapper $formMapper)
43
    {
44
        self::configureMainFields($formMapper);
45
        $formMapper
46
            ->add('locale', 'choice', [
47
                'label'    => 'Langue du contenu',
48
                'choices'  => $this->getRealLocales(),
49
                'required' => true,
50
            ])
51
            ->add('dateCreated', DateTimeSingleType::class, [
52
                'label'    => 'Date de création',
53
            ])
54
            ->add('published', 'checkbox', [
55
                'label'    => 'Publié',
56
                'required' => false,
57
            ]);
58
    }
59
60
    /**
61
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
62
     *
63
     * @return void
64
     */
65
    protected function configureListFields(ListMapper $listMapper)
66
    {
67
        $listMapper
68
            ->add('locale', null, [
69
                'label' => 'Langue',
70
            ])
71
            ->add('title', null, [
72
                'label' => 'Page',
73
            ])
74
            ->add('type', null, [
75
                'label'    => 'Type',
76
                'template' => 'AlpixelCMSBundle:admin:fields/list__field_type.html.twig',
77
            ])
78
            ->add('dateCreated', null, [
79
                'label' => 'Date de création',
80
            ])
81
            ->add('dateUpdated', null, [
82
                'label' => 'Date d\'édition',
83
            ])
84
            ->add('published', null, [
85
                'label' => 'Publié',
86
            ])
87
            ->add('_action', 'actions', [
88
                'actions' => [
89
                    'Voir'        => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_see.html.twig'],
90
                    'editContent' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_edit.html.twig'],
91
                    'delete'      => [],
92
                ],
93
            ]);
94
    }
95
96
    /**
97
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
98
     *
99
     * @return void
100
     */
101
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
102
    {
103
        $container     = $this->getConfigurationPool()->getContainer();
104
        $entityManager = $container->get('doctrine.orm.default_entity_manager');
105
106
        $datagridMapper
107
            ->add('locale', 'doctrine_orm_callback', [
108
                'label'   => 'Langue',
109
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) {
110
                    if (!$value['value']) {
111
                        return false;
112
                    }
113
114
                    $queryBuilder
115
                        ->andWhere($alias.'.locale = :locale')
116
                        ->setParameter('locale', $value['value']);
117
118
                    return true;
119
                },
120
            ],
121
            'choice',
122
            [
123
                'choices' => $this->getRealLocales(),
124
            ])
125
            ->add('title', null, [
126
                'label' => 'Page',
127
            ])
128
            ->add('published', null, [
129
                'label' => 'Publié',
130
            ])
131
            ->add('node', 'doctrine_orm_callback', [
132
                'label'    => 'Type de contenu',
133
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) use ($entityManager) {
134
                    if (!$value['value']) {
135
                        return false;
136
                    }
137
138
                    // We can't query the type from the AlpixelCMSBundle:Node repository (InheritanceType) because of that
139
                    // we try to get the repository in AppBundle with the value which is the class name of entity. :pig:
140
                    try {
141
                        $repository = $entityManager->getRepository(sprintf('AppBundle:%s', ucfirst($value['value'])));
142
                    } catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) {
143
                        return false;
144
                    }
145
146
                    $data = $repository->findAll();
147
                    if (empty($data)) {
148
                        return false;
149
                    }
150
151
                    $queryBuilder
152
                        ->andWhere($alias.'.id IN (:ids)')
153
                        ->setParameter('ids', $data);
154
155
                    return true;
156
                },
157
            ],
158
                'choice',
159
                [
160
                    'choices' => $this->getCMSEntityTypes(),
161
                ]
162
            );
163
    }
164
}
165