Completed
Push — master ( 630912...8bc180 )
by Benjamin
05:02 queued 02:18
created

ItemAdmin::configureListFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 1
Metric Value
c 5
b 2
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\Controller\Admin\CRUD;
4
5
use Alpixel\Bundle\MenuBundle\Utils\URLChecker;
6
use Doctrine\ORM\EntityRepository;
7
use Sonata\AdminBundle\Admin\Admin;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Sonata\AdminBundle\Form\FormMapper;
10
use Sonata\AdminBundle\Route\RouteCollection;
11
12
class ItemAdmin extends Admin
13
{
14
    public $last_position = 0;
15
16
    protected $datagridValues = [
17
        '_page'       => 1,
18
        '_sort_order' => 'ASC',
19
        '_sort_by'    => 'position',
20
    ];
21
22
    public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
23
    {
24
        $this->positionService = $positionHandler;
25
    }
26
27
    protected function configureRoutes(RouteCollection $collection)
28
    {
29
        $collection->add('move', $this->getRouterIdParameter().'/move/{position}');
30
    }
31
32
    public function getPersistentParameters()
33
    {
34
        if (!$this->getRequest()) {
35
            return [];
36
        }
37
38
        return [
39
            'menu' => $this->getRequest()->query->getInt('menu'),
40
        ];
41
    }
42
43
    public function createQuery($context = 'list')
44
    {
45
        $id = $this->getRequest()->query->getInt('menu');
46
        $query = parent::createQuery($context);
47
        $query->join($query->getRootAlias().'.menu', 'm')
48
            ->where('m.id = :id')
49
            ->setParameters([
50
                'id' => $id,
51
            ]);
52
53
        return $query;
54
    }
55
56
    protected function configureFormFields(FormMapper $formMapper)
57
    {
58
        $id = $this->getRequest()->query->getInt('menu');
59
        $isNew = ($this->id($this->getSubject()) === null) ? false : true;
60
61
        if ($isNew === false) {
62
            $formMapper
63
                ->add('menu', null, [
64
                    'label'         => 'Menu',
65
                    'required'      => true,
66
                    'property'      => 'name',
67 View Code Duplication
                    'query_builder' => function (EntityRepository $entityRepository) use ($id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                        $query = $entityRepository->createQuerybuilder('m');
69
                        if ($id === null) {
70
                            return $query;
71
                        }
72
73
                        return $query
74
                            ->where('m.id = :id')
75
                            ->setParameter('id', $id);
76
                    },
77
                ]);
78
        }
79
80
        $formMapper
81
            ->add('menu', null, [
82
                'label'         => 'Menu',
83
                'required'      => true,
84
                'property'      => 'name',
85 View Code Duplication
                'query_builder' => function (EntityRepository $entityRepository) use ($id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
                    $query = $entityRepository->createQuerybuilder('m');
87
                    if ($id === null) {
88
                        return $query;
89
                    }
90
91
                    return $query
92
                        ->where('m.id = :id')
93
                        ->setParameter('id', $id);
94
                },
95
            ])
96
            ->add('parent', null, [
97
                'label'    => 'Item parent',
98
                'required' => false,
99
                'property' => 'name',
100
            ])
101
            ->add('name', null, [
102
                'label'    => 'Nom du menu à afficher',
103
                'required' => true,
104
            ])
105
            ->add('uri', 'text', [
106
                'label'    => 'URI',
107
                'required' => true,
108
            ]);
109
    }
110
111
    protected function configureListFields(ListMapper $listMapper)
112
    {
113
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
114
115
        $listMapper
116
            ->add('name', null, [
117
                'label'    => 'Label',
118
                'required' => true,
119
            ])
120
            ->add('uri', null, [
121
                'label'    => 'URI',
122
                'required' => true,
123
            ])
124
            ->add('position', null, [
125
                'label'    => 'Position',
126
                'required' => true,
127
            ])
128
            ->add('_action', 'actions', [
129
                'actions' => [
130
                    'edit' => [],
131
                    'move' => [
132
                        'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig',
133
                    ],
134
                ],
135
            ]);
136
    }
137
138
    public function postUpdate($object = null)
139
    {
140
        if ($object !== null && $object->getUri() !== null) {
141
            $container  = $this->getConfigurationPool()->getContainer();
142
            $checker    = $container->get('alpixel_menu.utils.url_checker');
143
            $url        = $object->getUri();
144
            if ($checker->check($url) === URLChecker::URL_PROBLEM) {
145
                $session = $container->get('session');
146
                $session->getFlashBag()->add('warning', 'Cependant une erreur semble être apparue quand nous avons tenté d\'analyser la page "'.$url.'". Vous devriez vérifier que le lien spécifié n\'affiche aucune erreur.');
147
            }
148
        }
149
    }
150
}
151