Completed
Push — master ( 020990...9dfb7f )
by Benjamin
02:17
created

BaseNodeEntityAdmin::configureListFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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