Completed
Pull Request — master (#33)
by Benjamin
02:30
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 Knp\Menu\ItemInterface as MenuItemInterface;
8
use Sonata\AdminBundle\Route\RouteCollection;
9
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
10
11
class AdminNode extends BaseAdmin
12
{
13
    protected $baseRouteName = 'alpixel_admin_cms_node';
14
    protected $baseRoutePattern = 'node';
15
    protected $classnameLabel = "pages";
16
17
    protected $datagridValues = [
18
        '_page' => 1,
19
        '_sort_order' => 'DESC',
20
        '_sort_by' => 'dateUpdated',
21
    ];
22
23
    protected function configureRoutes(RouteCollection $collection)
24
    {
25
        $collection->clearExcept(['list']);
26
    }
27
28
    /**
29
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
30
     *
31
     * @return void
32
     */
33
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
34
    {
35
        $container = $this->getConfigurationPool()->getContainer();
36
        $entityManager = $container->get('doctrine.orm.default_entity_manager');
37
        $datagridMapper
38
            ->add('locale', 'doctrine_orm_callback', [
39
                'label' => 'Langue',
40
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) {
41
                    if (!$value['value']) {
42
                        return false;
43
                    }
44
                    $queryBuilder
45
                        ->andWhere($alias . '.locale = :locale')
46
                        ->setParameter('locale', $value['value']);
47
                    return true;
48
                },
49
            ],
50
                'choice',
51
                [
52
                    'choices' => $this->getRealLocales(),
53
                ])
54
            ->add('title', null, [
55
                'label' => 'Page',
56
            ])
57
            ->add('published', null, [
58
                'label' => 'Publié',
59
            ])
60
            ->add('node', 'doctrine_orm_callback', [
61
                'label' => 'Type de contenu',
62
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) use ($entityManager) {
63
                    if (!$value['value']) {
64
                        return false;
65
                    }
66
                    // We can't query the type from the AlpixelCMSBundle:Node repository (InheritanceType) because of that
67
                    // we try to get the repository in AppBundle with the value which is the class name of entity. :pig:
68
                    try {
69
                        $repository = $entityManager->getRepository(sprintf('AppBundle:%s', ucfirst($value['value'])));
70
                    } catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) {
71
                        return false;
72
                    }
73
                    $data = $repository->findAll();
74
                    if (empty($data)) {
75
                        return false;
76
                    }
77
                    $queryBuilder
78
                        ->andWhere($alias . '.id IN (:ids)')
79
                        ->setParameter('ids', $data);
80
                    return true;
81
                },
82
            ],
83
                'choice',
84
                [
85
                    'choices' => $this->getCMSEntityTypes(),
86
                ]
87
            );
88
    }
89
90
91
    /**
92
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
93
     *
94
     * @return void
95
     */
96
    protected function configureListFields(ListMapper $listMapper)
97
    {
98
        $listMapper
99
            ->add('id')
100
            ->add('locale', null, [
101
                'label' => 'Langue',
102
            ])
103
            ->add('title', null, [
104
                'label' => 'Page',
105
            ])
106
            ->add('type', null, [
107
                'label' => 'Type',
108
                'template' => 'AlpixelCMSBundle:admin:fields/list__field_type.html.twig',
109
            ])
110
            ->add('dateCreated', null, [
111
                'label' => 'Date de création',
112
            ])
113
            ->add('dateUpdated', null, [
114
                'label' => 'Date d\'édition',
115
            ])
116
            ->add('published', null, [
117
                'label' => 'Publié',
118
            ])
119
            ->add('_action', 'actions', [
120
                'actions' => [
121
                    'see' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_see.html.twig'],
122
                    'edit' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_edit.html.twig'],
123
                    'delete' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_delete.html.twig'],
124
                ],
125
            ]);
126
    }
127
128
    public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
129
    {
130
        if (isset($this->breadcrumbs[$action])) {
131
            return $this->breadcrumbs[$action];
132
        }
133
134
        $menu = $this->menuFactory->createItem('root');
135
136
        $menu = $menu->addChild('Dashboard',
137
            array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
138
        );
139
140
        $menu = $menu->addChild('Gestion des pages',
141
            array('uri' => $this->routeGenerator->generate('alpixel_admin_cms_node_list'))
142
        );
143
144
        return $this->breadcrumbs[$action] = $menu;
145
    }
146
147
}