Completed
Push — master ( dc1345...98f380 )
by Benjamin
02:22
created

AdminNode::configureListFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 21
nc 1
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\CMSBundle\Admin;
4
5
use Knp\Menu\ItemInterface as MenuItemInterface;
6
use Sonata\AdminBundle\Datagrid\DatagridMapper;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
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
48
                    return true;
49
                },
50
            ],
51
                'choice',
52
                [
53
                    'choices' => $this->getRealLocales(),
54
                ])
55
            ->add('title', null, [
56
                'label' => 'Page',
57
            ])
58
            ->add('published', null, [
59
                'label' => 'Publié',
60
            ])
61
            ->add('node', 'doctrine_orm_callback', [
62
                'label'    => 'Type de contenu',
63
                'callback' => function (ProxyQuery $queryBuilder, $alias, $field, $value) use ($entityManager) {
64
                    if (!$value['value']) {
65
                        return false;
66
                    }
67
                    // We can't query the type from the AlpixelCMSBundle:Node repository (InheritanceType) because of that
68
                    // we try to get the repository in AppBundle with the value which is the class name of entity. :pig:
69
                    try {
70
                        $repository = $entityManager->getRepository(sprintf('AppBundle:%s', ucfirst($value['value'])));
71
                    } catch (\Doctrine\Common\Persistence\Mapping\MappingException $e) {
72
                        return false;
73
                    }
74
                    $data = $repository->findAll();
75
                    if (empty($data)) {
76
                        return false;
77
                    }
78
                    $queryBuilder
79
                        ->andWhere($alias.'.id IN (:ids)')
80
                        ->setParameter('ids', $data);
81
82
                    return true;
83
                },
84
            ],
85
                'choice',
86
                [
87
                    'choices' => $this->getCMSEntityTypes(),
88
                ]
89
            );
90
    }
91
92
    /**
93
     * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
94
     *
95
     * @return void
96
     */
97
    protected function configureListFields(ListMapper $listMapper)
98
    {
99
        $listMapper
100
            ->add('id')
101
            ->add('locale', null, [
102
                'label' => 'Langue',
103
            ])
104
            ->add('title', null, [
105
                'label' => 'Page',
106
            ])
107
            ->add('type', null, [
108
                'label'    => 'Type',
109
                'template' => 'AlpixelCMSBundle:admin:fields/list__field_type.html.twig',
110
            ])
111
            ->add('dateCreated', null, [
112
                'label' => 'Date de création',
113
            ])
114
            ->add('dateUpdated', null, [
115
                'label' => 'Date d\'édition',
116
            ])
117
            ->add('published', null, [
118
                'label' => 'Publié',
119
            ])
120
            ->add('_action', 'actions', [
121
                'actions' => [
122
                    'see'    => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_see.html.twig'],
123
                    'edit'   => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_edit.html.twig'],
124
                    'delete' => ['template' => 'AlpixelCMSBundle:admin:fields/list__action_delete.html.twig'],
125
                ],
126
            ]);
127
    }
128
129
    public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
130
    {
131
        if (isset($this->breadcrumbs[$action])) {
132
            return $this->breadcrumbs[$action];
133
        }
134
135
        $menu = $this->menuFactory->createItem('root');
136
137
        $menu = $menu->addChild('Dashboard',
138
            ['uri' => $this->routeGenerator->generate('sonata_admin_dashboard')]
139
        );
140
141
        $menu = $menu->addChild('Gestion des pages',
142
            ['uri' => $this->routeGenerator->generate('alpixel_admin_cms_node_list')]
143
        );
144
145
        return $this->breadcrumbs[$action] = $menu;
146
    }
147
}
148