Completed
Push — master ( 8bc180...8527e5 )
by Benjamin
03:52 queued 01:36
created

ItemAdmin::setPositionService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
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
use Symfony\Component\HttpFoundation\Request;
12
13
class ItemAdmin extends Admin
14
{
15
    public $last_position = 0;
16
17
    protected $datagridValues = [
18
        '_page'       => 1,
19
        '_sort_order' => 'ASC',
20
        '_sort_by'    => 'position',
21
    ];
22
23
    public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
24
    {
25
        $this->positionService = $positionHandler;
26
    }
27
28
    protected function configureRoutes(RouteCollection $collection)
29
    {
30
        $collection->add('move', $this->getRouterIdParameter().'/move/{position}');
31
        $collection->add('item', $this->getRouterIdParameter().'/item');
32
    }
33
34
    public function getPersistentParameters()
35
    {
36
        $parameters = [];
37
        if ($this->getRequest() instanceof Request) {
38
            $query = $this->getRequest()->query;
39
            if ($query->has('menu')) {
40
                $parameters['menu'] = $query->getInt('menu');
41
            }
42
43
            if ($query->has('item')) {
44
                $parameters['item'] = $query->getInt('item');
45
            }
46
        }
47
48
        return $parameters;
49
    }
50
51
    public function createQuery($context = 'list')
52
    {
53
        $query = parent::createQuery($context);
54
        if ($this->getRequest() instanceof Request) {
55
            $requestQuery = $this->getRequest()->query;
56
            if ($requestQuery->has('menu')) {
57
                $menuId = $requestQuery->getInt('menu');
58
                $query
59
                    ->join($query->getRootAlias().'.menu', 'm')
60
                    ->leftJoin($query->getRootAlias().'.parent', 'p')
61
                    ->where('m.id = :menuId')
62
                    ->andwhere('p.id IS NULL')
63
                    ->setParameters([
64
                        'menuId' => $menuId,
65
                    ]);
66
            }
67
68
            if ($requestQuery->has('item')) {
69
                $parentId = $requestQuery->getInt('item');
70
                $query->join($query->getRootAlias() . '.parent', 'p')
71
                    ->andWhere('p.id = :parentId')
72
                    ->setParameters([
73
                        'parentId' => $parentId,
74
                    ]);
75
            }
76
        }
77
78
        return $query;
79
    }
80
81
    protected function configureFormFields(FormMapper $formMapper)
82
    {
83
        $id = $this->getRequest()->query->getInt('menu');
84
        $isNew = ($this->id($this->getSubject()) === null) ? false : true;
85
86
        if ($isNew === false) {
87
            $formMapper
88
                ->add('menu', null, [
89
                    'label'         => 'Menu',
90
                    'required'      => true,
91
                    'property'      => 'name',
92 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...
93
                        $query = $entityRepository->createQuerybuilder('m');
94
                        if ($id === null) {
95
                            return $query;
96
                        }
97
98
                        return $query
99
                            ->where('m.id = :id')
100
                            ->setParameter('id', $id);
101
                    },
102
                ]);
103
        }
104
105
        $formMapper
106
            ->add('menu', null, [
107
                'label'         => 'Menu',
108
                'required'      => true,
109
                'property'      => 'name',
110 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...
111
                    $query = $entityRepository->createQuerybuilder('m');
112
                    if ($id === null) {
113
                        return $query;
114
                    }
115
116
                    return $query
117
                        ->where('m.id = :id')
118
                        ->setParameter('id', $id);
119
                },
120
            ])
121
            ->add('parent', null, [
122
                'label'    => 'Item parent',
123
                'required' => false,
124
                'property' => 'name',
125
            ])
126
            ->add('name', null, [
127
                'label'    => 'Nom du menu à afficher',
128
                'required' => true,
129
            ])
130
            ->add('uri', 'text', [
131
                'label'    => 'URI',
132
                'required' => true,
133
            ]);
134
    }
135
136
    protected function configureListFields(ListMapper $listMapper)
137
    {
138
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
139
140
        $listMapper
141
            ->add('name', null, [
142
                'label'    => 'Label',
143
                'required' => true,
144
            ])
145
            ->add('uri', null, [
146
                'label'    => 'URI',
147
                'required' => true,
148
            ])
149
            ->add('position', null, [
150
                'label'    => 'Position',
151
                'required' => true,
152
            ])
153
            ->add('_action', 'actions', [
154
                'actions' => [
155
                    'edit' => [],
156
                    'move' => [
157
                        'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig',
158
                    ],
159
                    'item' => [
160
                        'template' => 'AlpixelMenuBundle:CRUD:list__action_item.html.twig',
161
                    ],
162
                ],
163
            ]);
164
    }
165
166
    public function postUpdate($object = null)
167
    {
168
        if ($object !== null && $object->getUri() !== null) {
169
            $container  = $this->getConfigurationPool()->getContainer();
170
            $checker    = $container->get('alpixel_menu.utils.url_checker');
171
            $url        = $object->getUri();
172
            if ($checker->check($url) === URLChecker::URL_PROBLEM) {
173
                $session = $container->get('session');
174
                $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.');
175
            }
176
        }
177
    }
178
}
179