Completed
Push — master ( c1ca11...4ddf1e )
by Benjamin
02:36
created

ItemAdmin::postUpdate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 3
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
        $subject = $this->getSubject();
84
        $isNew   = ($this->id($subject) === null) ? true : false;
85
86
        if ($isNew === true) {
87
            $idMenu = $this->getRequest()->query->getInt('create-menu');
88
            $idItem = $this->getRequest()->query->getInt('create-item');
89
        } else {
90
            $idMenu  = $subject->getMenu()->getId();
91
            $idItem  = ($subject->getParent() !== null) ? $subject->getParent()->getId() : 0;
92
        }
93
94
        $formMapper
95
            ->add('menu', null, [
96
                'label'         => 'Menu',
97
                'required'      => true,
98
                'property'      => 'name',
99 View Code Duplication
                'query_builder' => function (EntityRepository $entityRepository) use ($idMenu) {
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...
100
                    $query = $entityRepository->createQuerybuilder('m');
101
                    if ($idMenu === 0) {
102
                        return $query;
103
                    }
104
105
                    return $query
106
                        ->where('m.id = :id')
107
                        ->setParameter('id', $idMenu);
108
                },
109
            ]);
110
111
        if ($idItem === 0 && $idMenu === 0 || $isNew === false && $subject->getParent() !== null || $isNew === true && $idItem > 0) {
112
            $formMapper->add('parent', null, [
113
                'label'    => 'Item parent',
114
                'required' => true,
115
                'property' => 'name',
116 View Code Duplication
                'query_builder' => function (EntityRepository $entityRepository) use ($idItem) {
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...
117
                    $query = $entityRepository->createQuerybuilder('i');
118
                    if ($idItem === 0) {
119
                        return $query;
120
                    }
121
122
                    return $query
123
                        ->where('i.id = :id')
124
                        ->setParameter('id', $idItem);
125
                }
126
            ]);
127
        }
128
129
        $formMapper
130
            ->add('name', null, [
131
                'label'    => 'Nom du menu à afficher',
132
                'required' => true,
133
            ])
134
            ->add('uri', 'text', [
135
                'label'    => 'URI',
136
                'required' => true,
137
            ]);
138
    }
139
140
    protected function configureListFields(ListMapper $listMapper)
141
    {
142
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
143
144
        $listMapper
145
            ->add('name', null, [
146
                'label'    => 'Label',
147
                'required' => true,
148
            ])
149
            ->add('uri', null, [
150
                'label'    => 'URI',
151
                'required' => true,
152
            ])
153
            ->add('position', null, [
154
                'label'    => 'Position',
155
                'required' => true,
156
            ])
157
            ->add('_action', 'actions', [
158
                'actions' => [
159
                    'edit' => [],
160
                    'add'  => [
161
                        'template' => 'AlpixelMenuBundle:CRUD:add__action_item.html.twig',
162
                    ],
163
                    'move' => [
164
                        'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig',
165
                    ],
166
                    'item' => [
167
                        'template' => 'AlpixelMenuBundle:CRUD:list__action_item.html.twig',
168
                    ],
169
                ],
170
            ]);
171
    }
172
173
    public function postUpdate($object = null)
174
    {
175
        if ($object !== null && $object->getUri() !== null) {
176
            $container = $this->getConfigurationPool()->getContainer();
177
            $checker = $container->get('alpixel_menu.utils.url_checker');
178
            $url = $object->getUri();
179
            if ($checker->check($url) === URLChecker::URL_PROBLEM) {
180
                $session = $container->get('session');
181
                $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.');
182
            }
183
        }
184
    }
185
}
186