Completed
Pull Request — master (#3)
by Alexis
06:28 queued 03:56
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 Doctrine\ORM\EntityRepository;
6
use Sonata\AdminBundle\Admin\Admin;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Form\FormMapper;
9
use Sonata\AdminBundle\Route\RouteCollection;
10
11
class ItemAdmin extends Admin
12
{
13
    public $last_position = 0;
14
15
    protected $datagridValues = [
16
        '_page' => 1,
17
        '_sort_order' => 'ASC',
18
        '_sort_by' => 'position',
19
    ];
20
21
    public function setPositionService(\Pix\SortableBehaviorBundle\Services\PositionHandler $positionHandler)
22
    {
23
        $this->positionService = $positionHandler;
24
    }
25
26
    protected function configureRoutes(RouteCollection $collection)
27
    {
28
        $collection->add('move', $this->getRouterIdParameter().'/move/{position}');
29
    }
30
31
    public function getPersistentParameters()
32
    {
33
        if (!$this->getRequest()) {
34
            return [];
35
        }
36
37
        return [
38
            'menu' => $this->getRequest()->query->getInt('menu'),
39
        ];
40
    }
41
42
    public function createQuery($context = 'list')
43
    {
44
        $id = $this->getRequest()->query->getInt('menu');
45
        $query = parent::createQuery($context);
46
        $query->join($query->getRootAlias().'.menu', 'm')
47
            ->where('m.id = :id')
48
            ->setParameters([
49
                'id' => $id,
50
            ]);
51
52
        return $query;
53
    }
54
55
    protected function configureFormFields(FormMapper $formMapper)
56
    {
57
        $id = $this->getRequest()->query->getInt('menu');
58
59
        $formMapper
60
            ->add('menu', null, [
61
                'label'         => 'Menu',
62
                'required'      => true,
63
                'property'      => 'name',
64
                'query_builder' => function(EntityRepository $entityRepository) use ($id) {
65
                    $query = $entityRepository->createQuerybuilder('m');
66
                    if ($id == null) {
67
                        return $query;
68
                    }
69
70
                    return $query
71
                        ->where('m.id = :id')
72
                        ->setParameter('id', $id);
73
                }
74
            ])
75
            ->add('parent', null, [
76
                'label'    => 'Item parent',
77
                'required' => false,
78
                'property' => 'name'
79
            ])
80
            ->add('children', null, [
81
                'label'    => 'Item enfant',
82
                'required' => false,
83
                'property' => 'name'
84
            ])
85
            ->add('name', null, [
86
                'label'    => 'Nom du menu à afficher',
87
                'required' => true,
88
            ])
89
            ->add('uri', 'text', [
90
                'label'    => 'URI',
91
                'required' => true,
92
            ])
93
        ;
94
    }
95
96
    protected function configureListFields(ListMapper $listMapper)
97
    {
98
        $this->last_position = $this->positionService->getLastPosition($this->getRoot()->getClass());
99
100
        $listMapper
101
            ->add('name', null, [
102
                'label'    => 'Label',
103
                'required' => true,
104
            ])
105
            ->add('uri', null, [
106
                'label'    => 'URI',
107
                'required' => true,
108
            ])
109
            ->add('position', null, [
110
                'label'    => 'Position',
111
                'required' => true,
112
            ])
113
            ->add('_action', 'actions', array(
114
                'actions' => [
115
                    'edit' => [],
116
                    'move' => [
117
                        'template' => 'PixSortableBehaviorBundle:Default:_sort.html.twig'
118
                    ],
119
                ]
120
            ))
121
        ;
122
    }
123
}
124