Completed
Pull Request — master (#33)
by Benjamin
02:29
created

AdminNode::configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Admin;
4
5
use Sonata\AdminBundle\Datagrid\DatagridMapper;
6
use Sonata\AdminBundle\Datagrid\ListMapper;
7
use Sonata\AdminBundle\Route\RouteCollection;
8
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
9
10
class AdminNode extends BaseAdmin
11
{
12
    protected $baseRouteName = 'alpixel_admin_cms_node';
13
    protected $baseRoutePattern = 'node';
14
    protected $classnameLabel = "pages";
15
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->clearExcept(['list']);
25
    }
26
27
    /**
28
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
29
     *
30
     * @return void
31
     */
32
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
33
    {
34
        $container = $this->getConfigurationPool()->getContainer();
35
        $entityManager = $container->get('doctrine.orm.default_entity_manager');
36
        $datagridMapper
37
            ->add('locale', 'doctrine_orm_callback', [
38
                'label' => 'Langue',
39
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) {
40
                    if (!$value['value']) {
41
                        return false;
42
                    }
43
                    $queryBuilder
44
                        ->andWhere($alias . '.locale = :locale')
45
                        ->setParameter('locale', $value['value']);
46
                    return true;
47
                },
48
            ],
49
                'choice',
50
                [
51
                    'choices' => $this->getRealLocales(),
52
                ])
53
            ->add('title', null, [
54
                'label' => 'Page',
55
            ])
56
            ->add('published', null, [
57
                'label' => 'Publié',
58
            ])
59
            ->add('node', 'doctrine_orm_callback', [
60
                'label' => 'Type de contenu',
61
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) use ($entityManager) {
62
                    if (!$value['value']) {
63
                        return false;
64
                    }
65
                    // We can't query the type from the AlpixelCMSBundle:Node repository (InheritanceType) because of that
66
                    // we try to get the repository in AppBundle with the value which is the class name of entity. :pig:
67
                    try {
68
                        $repository = $entityManager->getRepository(sprintf('AppBundle:%s', ucfirst($value['value'])));
69
                    } catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) {
70
                        return false;
71
                    }
72
                    $data = $repository->findAll();
73
                    if (empty($data)) {
74
                        return false;
75
                    }
76
                    $queryBuilder
77
                        ->andWhere($alias . '.id IN (:ids)')
78
                        ->setParameter('ids', $data);
79
                    return true;
80
                },
81
            ],
82
                'choice',
83
                [
84
                    'choices' => $this->getCMSEntityTypes(),
85
                ]
86
            );
87
    }
88
89
90
    /**
91
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
92
     *
93
     * @return void
94
     */
95
    protected function configureListFields(ListMapper $listMapper)
96
    {
97
        $listMapper
98
            ->add('id')
99
            ->add('locale', null, [
100
                'label' => 'Langue',
101
            ])
102
            ->add('title', null, [
103
                'label' => 'Page',
104
            ])
105
            ->add('type', null, [
106
                'label' => 'Type',
107
                'template' => 'AlpixelCMSBundle:admin:fields/list__field_type.html.twig',
108
            ])
109
            ->add('dateCreated', null, [
110
                'label' => 'Date de création',
111
            ])
112
            ->add('dateUpdated', null, [
113
                'label' => 'Date d\'édition',
114
            ])
115
            ->add('published', null, [
116
                'label' => 'Publié',
117
            ])
118
            ->add('_action', 'actions', [
119
                'actions' => [
120
                    'see' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_see.html.twig'],
121
                    'edit' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_edit.html.twig'],
122
                    'delete' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_delete.html.twig'],
123
                ],
124
            ]);
125
    }
126
127
    public function setContentTypes($contentTypes)
128
    {
129
        $this->cmsTypes = $contentTypes;
130
    }
131
132
    public function getContentTypes()
133
    {
134
        return $this->cmsTypes;
135
    }
136
}