Completed
Push — master ( 8fc13e...c19aa3 )
by Axel
06:21
created

MenuController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
B editAction() 0 45 8
A viewAction() 0 25 2
A listAction() 0 5 1
A deleteAction() 0 21 6
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\MenuModule\Controller;
15
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
17
use Symfony\Component\Finder\Exception\AccessDeniedException;
18
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Annotation\Route;
23
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
24
use Zikula\MenuModule\Entity\MenuItemEntity;
25
use Zikula\MenuModule\Entity\Repository\MenuItemRepository;
26
use Zikula\MenuModule\Form\Type\DeleteMenuItemType;
27
use Zikula\MenuModule\Form\Type\MenuItemType;
28
use Zikula\PermissionsModule\Annotation\PermissionCheck;
29
use Zikula\ThemeModule\Engine\Annotation\Theme;
30
31
/**
32
 * Class MenuController
33
 *
34
 * @Route("/admin")
35
 * @PermissionCheck("admin")
36
 */
37
class MenuController extends AbstractController
38
{
39
    /**
40
     * @var string
41
     */
42
    private $domTreeNodePrefix = 'node_';
43
44
    /**
45
     * @Route("/list")
46
     * @Template("@ZikulaMenuModule/Menu/list.html.twig")
47
     * @Theme("admin")
48
     */
49
    public function listAction(
50
        MenuItemRepository $menuItemRepository
51
    ): array {
52
        return [
53
            'rootNodes' => $menuItemRepository->getRootNodes()
54
        ];
55
    }
56
57
    /**
58
     * @Route("/view/{id}")
59
     * @Template("@ZikulaMenuModule/Menu/view.html.twig")
60
     * @Theme("admin")
61
     *
62
     * @see https://jstree.com/
63
     * @see https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md
64
     * @throws AccessDeniedException Thrown if the menu item is not a root item
65
     */
66
    public function viewAction(
67
        MenuItemRepository $menuItemRepository,
68
        MenuItemEntity $menuItemEntity
69
    ): array {
70
        if (null !== $menuItemEntity->getParent()) {
71
            throw new AccessDeniedException();
72
        }
73
        $htmlTree = $menuItemRepository->childrenHierarchy(
74
            $menuItemEntity, /* node to start from */
75
            false, /* false: load all children, true: only direct */
76
            [
77
                'decorate' => true,
78
                'html' => true,
79
                'childOpen' => function($node) {
80
                    return '<li class="jstree-open" id="' . $this->domTreeNodePrefix . $node['id'] . '">';
81
                },
82
                'nodeDecorator' => static function($node) {
83
                    return '<a href="#">' . $node['title'] . ' (' . $node['id'] . ')</a>';
84
                }
85
            ]
86
        );
87
88
        return [
89
            'menu' => $menuItemEntity,
90
            'tree' => $htmlTree
91
        ];
92
    }
93
94
    /**
95
     * @Route("/edit/{id}", defaults={"id" = null})
96
     * @Theme("admin")
97
     */
98
    public function editAction(
99
        Request $request,
100
        MenuItemRepository $menuItemRepository,
101
        MenuItemEntity $menuItemEntity = null
102
    ): Response {
103
        if (!isset($menuItemEntity)) {
104
            $menuItemEntity = new MenuItemEntity();
105
        }
106
        $form = $this->createForm(MenuItemType::class, $menuItemEntity);
107
        $form->add('save', SubmitType::class, [
108
            'label' => $this->trans('Save'),
109
            'icon' => 'fa-check',
110
            'attr' => [
111
                'class' => 'btn btn-success'
112
            ]
113
        ])
114
        ->add('cancel', SubmitType::class, [
115
            'label' => $this->trans('Cancel'),
116
            'icon' => 'fa-times',
117
            'attr' => [
118
                'class' => 'btn btn-secondary'
119
            ]
120
        ]);
121
        $form->handleRequest($request);
122
        if ($form->isSubmitted() && $form->isValid() && $form->get('save')->isClicked()) {
0 ignored issues
show
Bug introduced by
The method isClicked() does not exist on Symfony\Component\Form\FormInterface. It seems like you code against a sub-type of Symfony\Component\Form\FormInterface such as Symfony\Component\Form\SubmitButton. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        if ($form->isSubmitted() && $form->isValid() && $form->get('save')->/** @scrutinizer ignore-call */ isClicked()) {
Loading history...
123
            $menuItemEntity = $form->getData();
124
            $menuItemRepository->persistAsFirstChild($menuItemEntity);
125
            if (null === $menuItemEntity->getId()) {
126
                // create dummy child
127
                $dummy = new MenuItemEntity();
128
                $dummy->setTitle('dummy child');
129
                $menuItemRepository->persistAsFirstChildOf($dummy, $menuItemEntity);
130
            }
131
            $this->getDoctrine()->getManager()->flush();
132
133
            return $this->redirectToRoute('zikulamenumodule_menu_list');
134
        }
135
        if ($form->isSubmitted() && $form->get('cancel')->isClicked()) {
136
            $this->addFlash('status', 'Operation cancelled.');
137
138
            return $this->redirectToRoute('zikulamenumodule_menu_list');
139
        }
140
141
        return $this->render('@ZikulaMenuModule/Menu/editRootNode.html.twig', [
142
            'form' => $form->createView()
143
        ]);
144
    }
145
146
    /**
147
     * @Route("/delete/{id}")
148
     * @Template("@ZikulaMenuModule/Menu/delete.html.twig")
149
     * @Theme("admin")
150
     *
151
     * @return array|RedirectResponse
152
     */
153
    public function deleteAction(Request $request, MenuItemEntity $menuItemEntity)
154
    {
155
        $form = $this->createForm(DeleteMenuItemType::class, [
156
            'entity' => $menuItemEntity
157
        ]);
158
        $form->handleRequest($request);
159
        if ($form->isSubmitted()) {
160
            if ($form->isSubmitted() && $form->get('delete')->isClicked()) {
161
                $menuItemEntity = $form->get('entity')->getData();
162
                $this->getDoctrine()->getManager()->remove($menuItemEntity);
163
                $this->getDoctrine()->getManager()->flush();
164
                $this->addFlash('status', 'Done! Menu removed.');
165
            } elseif ($form->isSubmitted() && $form->get('cancel')->isClicked()) {
166
                $this->addFlash('status', 'Operation cancelled.');
167
            }
168
169
            return $this->redirectToRoute('zikulamenumodule_menu_list');
170
        }
171
172
        return [
173
            'form' => $form->createView()
174
        ];
175
    }
176
}
177