Completed
Push — master ( 6c15b7...f5f8be )
by Sander
12:00
created

Controller/MenuItemAdminListController.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MenuBundle\Controller;
4
5
use Kunstmaan\AdminBundle\Entity\EntityInterface;
6
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractAdminListConfigurator;
7
use Kunstmaan\AdminListBundle\AdminList\Configurator\AdminListConfiguratorInterface;
8
use Kunstmaan\AdminListBundle\AdminList\ItemAction\SimpleItemAction;
9
use Kunstmaan\AdminListBundle\Controller\AdminListController;
10
use Kunstmaan\MenuBundle\Entity\BaseMenu;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
class MenuItemAdminListController extends AdminListController
18
{
19
    /**
20
     * @var AdminListConfiguratorInterface
21
     */
22
    private $configurator;
23
24
    /**
25
     * @param Request $request
26
     * @param int     $menuid
27
     * @param int     $entityId
28
     *
29
     * @return AbstractAdminListConfigurator
30
     */
31
    public function getAdminListConfigurator(Request $request, $menuid, $entityId = null)
32
    {
33
        if (!isset($this->configurator)) {
34
            $menu = $this->getDoctrine()->getManager()->getRepository(
35
                $this->container->getParameter('kunstmaan_menu.entity.menu.class')
36
            )->find($menuid);
37
            $rootNode = $this->container->get('kunstmaan_admin.domain_configuration')->getRootNode();
38
39
            $configuratorClass = $this->container->getParameter('kunstmaan_menu.adminlist.menuitem_configurator.class');
40
            $this->configurator = new $configuratorClass($this->getEntityManager(), null, $menu);
41
42
            $adminType = $this->container->getParameter('kunstmaan_menu.form.menuitem_admintype.class');
43
            $menuItemClass = $this->container->getParameter('kunstmaan_menu.entity.menuitem.class');
44
            $this->configurator->setAdminType($adminType);
45
            $this->configurator->setAdminTypeOptions(array('menu' => $menu, 'rootNode' => $rootNode, 'menuItemClass' => $menuItemClass, 'entityId' => $entityId, 'locale' => $request->getLocale()));
46
        }
47
48
        return $this->configurator;
49
    }
50
51
    /**
52
     * The index action
53
     *
54
     * @param Request $request
55
     * @param int     $menuid
56
     *
57
     * @return Response
58
     *
59
     * @Route("/{menuid}/items", name="kunstmaanmenubundle_admin_menuitem")
60
     */
61
    public function indexAction(Request $request, $menuid)
62
    {
63
        $menuRepo = $this->getDoctrine()->getManager()->getRepository(
64
            $this->container->getParameter('kunstmaan_menu.entity.menu.class')
65
        );
66
67
        /** @var BaseMenu $menu */
68
        $menu = $menuRepo->find($menuid);
69
        if ($menu->getLocale() != $request->getLocale()) {
70
            /** @var BaseMenu $translatedMenu */
71
            $translatedMenu = $menuRepo->findOneBy(['locale' => $request->getLocale(), 'name' => $menu->getName()]);
72
            $menuid = $translatedMenu->getId();
73
        }
74
75
        $configurator = $this->getAdminListConfigurator($request, $menuid);
76
        $itemRoute = function (EntityInterface $item) use ($menuid) {
77
            return array(
78
                'path' => 'kunstmaanmenubundle_admin_menuitem_move_up',
79
                'params' => array(
80
                    'menuid' => $menuid,
81
                    'item' => $item->getId(),
82
                ),
83
            );
84
        };
85
        $configurator->addItemAction(new SimpleItemAction($itemRoute, 'arrow-up', 'kuma_admin_list.action.move_up'));
86
87
        $itemRoute = function (EntityInterface $item) use ($menuid) {
88
            return array(
89
                'path' => 'kunstmaanmenubundle_admin_menuitem_move_down',
90
                'params' => array(
91
                    'menuid' => $menuid,
92
                    'item' => $item->getId(),
93
                ),
94
            );
95
        };
96
        $configurator->addItemAction(new SimpleItemAction($itemRoute, 'arrow-down', 'kuma_admin_list.action.move_down'));
97
98
        return parent::doIndexAction($configurator, $request);
99
    }
100
101
    /**
102
     * The add action
103
     *
104
     * @Route("/{menuid}/items/add", name="kunstmaanmenubundle_admin_menuitem_add")
105
     * @Method({"GET", "POST"})
106
     *
107
     * @return array
0 ignored issues
show
Should the return type not be Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
108
     */
109
    public function addAction(Request $request, $menuid)
110
    {
111
        return parent::doAddAction($this->getAdminListConfigurator($request, $menuid), null, $request);
112
    }
113
114
    /**
115
     * The edit action
116
     *
117
     * @param int $id
118
     *
119
     * @Route("{menuid}/items/{id}/edit", requirements={"id" = "\d+"}, name="kunstmaanmenubundle_admin_menuitem_edit")
120
     * @Method({"GET", "POST"})
121
     *
122
     * @return array
0 ignored issues
show
Should the return type not be Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
123
     */
124
    public function editAction(Request $request, $menuid, $id)
125
    {
126
        return parent::doEditAction($this->getAdminListConfigurator($request, $menuid, $id), $id, $request);
127
    }
128
129
    /**
130
     * The delete action
131
     *
132
     * @param int $id
133
     *
134
     * @Route("{menuid}/items/{id}/delete", requirements={"id" = "\d+"}, name="kunstmaanmenubundle_admin_menuitem_delete")
135
     * @Method({"GET", "POST"})
136
     *
137
     * @return array
0 ignored issues
show
Should the return type not be Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
138
     */
139
    public function deleteAction(Request $request, $menuid, $id)
140
    {
141
        return parent::doDeleteAction($this->getAdminListConfigurator($request, $menuid), $id, $request);
142
    }
143
144
    /**
145
     * Move an item up in the list.
146
     *
147
     * @Route("{menuid}/items/{item}/move-up", name="kunstmaanmenubundle_admin_menuitem_move_up")
148
     * @Method({"GET"})
149
     *
150
     * @return RedirectResponse
151
     */
152 View Code Duplication
    public function moveUpAction(Request $request, $menuid, $item)
153
    {
154
        $em = $this->getEntityManager();
155
        $repo = $em->getRepository($this->container->getParameter('kunstmaan_menu.entity.menuitem.class'));
156
        $item = $repo->find($item);
157
158
        if ($item) {
159
            $repo->moveUp($item);
160
        }
161
162
        return new RedirectResponse(
163
            $this->generateUrl('kunstmaanmenubundle_admin_menuitem', array('menuid' => $menuid))
164
        );
165
    }
166
167
    /**
168
     * Move an item down in the list.
169
     *
170
     * @Route("{menuid}/items/{item}/move-down", name="kunstmaanmenubundle_admin_menuitem_move_down")
171
     * @Method({"GET"})
172
     *
173
     * @return RedirectResponse
174
     */
175 View Code Duplication
    public function moveDownAction(Request $request, $menuid, $item)
176
    {
177
        $em = $this->getEntityManager();
178
        $repo = $em->getRepository($this->container->getParameter('kunstmaan_menu.entity.menuitem.class'));
179
        $item = $repo->find($item);
180
181
        if ($item) {
182
            $repo->moveDown($item);
183
        }
184
185
        return new RedirectResponse(
186
            $this->generateUrl('kunstmaanmenubundle_admin_menuitem', array('menuid' => $menuid))
187
        );
188
    }
189
}
190