Completed
Pull Request — 5.0 (#2066)
by Jeroen
31:48 queued 20:01
created

Controller/GroupsController.php (1 issue)

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\UserManagementBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
7
use Kunstmaan\AdminBundle\Controller\BaseSettingsController;
8
use Kunstmaan\AdminBundle\Entity\Group;
9
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
10
use Kunstmaan\AdminBundle\Form\GroupType;
11
use Kunstmaan\AdminListBundle\AdminList\AdminList;
12
13
use Kunstmaan\UserManagementBundle\AdminList\GroupAdminListConfigurator;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
17
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
21
22
/**
23
 * Settings controller handling everything related to creating, editing, deleting and listing groups in an admin list
24
 */
25 View Code Duplication
class GroupsController extends BaseSettingsController
0 ignored issues
show
This class seems to be duplicated in 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...
26
{
27
    /**
28
     * List groups
29
     *
30
     * @Route("/", name="KunstmaanUserManagementBundle_settings_groups")
31
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
32
     *
33
     * @throws AccessDeniedException
34
     * @return array
35
     */
36
    public function listAction(Request $request)
37
    {
38
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
39
40
        /* @var $em EntityManager */
41
        $em = $this->getDoctrine()->getManager();
42
        /* @var AdminList $adminlist */
43
        $adminlist = $this->get("kunstmaan_adminlist.factory")->createList(new GroupAdminListConfigurator($em));
44
        $adminlist->bindRequest($request);
45
46
        return array(
47
            'adminlist' => $adminlist,
48
        );
49
    }
50
51
    /**
52
     * Add a group
53
     *
54
     * @Route("/add", name="KunstmaanUserManagementBundle_settings_groups_add")
55
     * @Method({"GET", "POST"})
56
     * @Template()
57
     *
58
     * @throws AccessDeniedException
59
     * @return array
60
     */
61
    public function addAction(Request $request)
62
    {
63
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
64
65
        /* @var $em EntityManager */
66
        $em = $this->getDoctrine()->getManager();
67
        $group = new Group();
68
        $form = $this->createForm(GroupType::class, $group);
69
70
        if ($request->isMethod('POST')) {
71
            $form->handleRequest($request);
72
            if ($form->isSubmitted() && $form->isValid()) {
73
                $em->persist($group);
74
                $em->flush();
75
76
                $this->addFlash(
77
                    FlashTypes::SUCCESS,
78
                    $this->get('translator')->trans('kuma_user.group.add.flash.success', array(
79
                        '%groupname%' => $group->getName()
80
                    ))
81
                );
82
83
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
84
            }
85
        }
86
87
        return array(
88
            'form' => $form->createView(),
89
        );
90
    }
91
92
    /**
93
     * Edit a group
94
     *
95
     * @param int $id
96
     *
97
     * @Route("/{id}/edit", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_groups_edit")
98
     * @Method({"GET", "POST"})
99
     * @Template()
100
     *
101
     * @throws AccessDeniedException
102
     * @return array
103
     */
104
    public function editAction(Request $request, $id)
105
    {
106
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
107
108
        /* @var $em EntityManager */
109
        $em = $this->getDoctrine()->getManager();
110
        /* @var Group $group */
111
        $group = $em->getRepository('KunstmaanAdminBundle:Group')->find($id);
112
        $form = $this->createForm(GroupType::class, $group);
113
114
        if ($request->isMethod('POST')) {
115
            $form->handleRequest($request);
116
            if ($form->isSubmitted() && $form->isValid()) {
117
                $em->persist($group);
118
                $em->flush();
119
120
                $this->addFlash(
121
                    FlashTypes::SUCCESS,
122
                    $this->get('translator')->trans('kuma_user.group.edit.flash.success', array(
123
                        '%groupname%' => $group->getName()
124
                    ))
125
                );
126
127
                return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
128
            }
129
        }
130
131
        return array(
132
            'form'  => $form->createView(),
133
            'group' => $group
134
        );
135
    }
136
137
    /**
138
     * Delete a group
139
     *
140
     * @param int $id
141
     *
142
     * @Route("/{id}/delete", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_groups_delete")
143
     * @Method({"GET", "POST"})
144
     * @Template()
145
     *
146
     * @throws AccessDeniedException
147
     * @return RedirectResponse
148
     */
149
    public function deleteAction($id)
150
    {
151
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
152
153
        /* @var $em EntityManager */
154
        $em = $this->getDoctrine()->getManager();
155
        $group = $em->getRepository('KunstmaanAdminBundle:Group')->find($id);
156
        if (!is_null($group)) {
157
            $em->remove($group);
158
            $em->flush();
159
160
            $this->addFlash(
161
                FlashTypes::SUCCESS,
162
                $this->get('translator')->trans('kuma_user.group.delete.flash.success', array(
163
                    '%groupname%' => $group->getName()
164
                ))
165
            );
166
        }
167
168
        return new RedirectResponse($this->generateUrl('KunstmaanUserManagementBundle_settings_groups'));
169
    }
170
171
}
172