Completed
Push — sf2.7 ( 2fae47...cc13d7 )
by Laurent
04:45
created

GroupController::createDeleteForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * GroupController controller des groupes d'utilisateurs.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version   since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\Group;
23
use AppBundle\Form\Type\GroupType;
24
25
/**
26
 * Group controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/groups")
31
 */
32
class GroupController extends Controller
33
{
34
    /**
35
     * Lists all Group entities.
36
     *
37
     * @Route("/", name="admin_groups")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction()
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $entities = $em->getRepository('AppBundle:Group')->findAll();
45
        
46
        return array('entities'  => $entities);
47
    }
48
49
    /**
50
     * Finds and displays a Group entity.
51
     *
52
     * @Route("/{id}/show", name="admin_groups_show", requirements={"id"="\d+"})
53
     * @Method("GET")
54
     * @Template()
55
     */
56
    public function showAction(Group $group)
57
    {
58
        $deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
59
60
        return array(
61
            'group' => $group,
62
            'delete_form' => $deleteForm->createView(),
63
        );
64
    }
65
66
    /**
67
     * Displays a form to create a new Group entity.
68
     *
69
     * @Route("/new", name="admin_groups_new")
70
     * @Method("GET")
71
     * @Template()
72
     */
73
    public function newAction()
74
    {
75
        $group = new Group();
76
        $form = $this->createForm(new GroupType(), $group);
77
        $form->add('roles', 'choice', array(
78
            'choices' => $this->getExistingRoles(),
79
            'data' => $group->getRoles(),
80
            'label' => 'Roles',
81
            'translation_domain' => 'admin',
82
            'expanded' => true,
83
            'multiple' => true,
84
            'mapped' => true,
85
        ));
86
87
88
        return array(
89
            'group' => $group,
90
            'form'   => $form->createView(),
91
        );
92
    }
93
94
    /**
95
     * Creates a new Group entity.
96
     *
97
     * @Route("/create", name="admin_groups_create")
98
     * @Method("POST")
99
     * @Template("AppBundle:Group:new.html.twig")
100
     */
101 View Code Duplication
    public function createAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method 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...
102
    {
103
        $group = new Group();
104
        $form = $this->createForm(new GroupType(), $group);
105
        $form->add('roles', 'choice', array(
106
            'choices' => $this->getExistingRoles(),
107
            'data' => $group->getRoles(),
108
            'label' => 'Roles',
109
            'expanded' => true,
110
            'multiple' => true,
111
            'mapped' => true,
112
        ));
113
        if ($form->handleRequest($request)->isValid()) {
114
            $em = $this->getDoctrine()->getManager();
115
            $em->persist($group);
116
            $em->flush();
117
118
            return $this->redirectToRoute('admin_groups_show', array('id', $group->getId()));
119
        }
120
121
        return array(
122
            'group' => $group,
123
            'form'   => $form->createView(),
124
        );
125
    }
126
127
    /**
128
     * Displays a form to edit an existing Group entity.
129
     *
130
     * @Route("/{id}/edit", name="admin_groups_edit", requirements={"id"="\d+"})
131
     * @Method("GET")
132
     * @Template()
133
     */
134
    public function editAction(Group $group)
135
    {
136
        $editForm = $this->createForm(
137
            new GroupType(),
138
            $group,
139
            array(
140
                'action' => $this->generateUrl(
141
                    'admin_groups_update',
142
                    array('id' => $group->getId())
143
                ),
144
                'method' => 'PUT',
145
            )
146
        );
147
        $editForm->add('roles', 'choice', array(
148
            'choices' => $this->getExistingRoles(),
149
            'data' => $group->getRoles(),
150
            'label' => 'Roles',
151
            'expanded' => true,
152
            'multiple' => true,
153
            'mapped' => true,
154
        ));
155
        $deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
156
157
        return array(
158
            'group' => $group,
159
            'edit_form'   => $editForm->createView(),
160
            'delete_form' => $deleteForm->createView(),
161
        );
162
    }
163
164
    /**
165
     * Edits an existing Group entity.
166
     *
167
     * @Route("/{id}/update", name="admin_groups_update", requirements={"id"="\d+"})
168
     * @Method("PUT")
169
     * @Template("AppBundle:Group:edit.html.twig")
170
     */
171
    public function updateAction(Group $group, Request $request)
172
    {
173
        $editForm = $this->createForm(new GroupType(), $group, array(
174
            'action' => $this->generateUrl('admin_groups_update', array('id' => $group->getId())),
175
            'method' => 'PUT',
176
        ));
177
        $editForm->add('roles', 'choice', array(
178
            'choices' => $this->getExistingRoles(),
179
            'data' => $group->getRoles(),
180
            'label' => 'Roles',
181
            'expanded' => true,
182
            'multiple' => true,
183
            'mapped' => true,
184
        ));
185
        if ($editForm->handleRequest($request)->isValid()) {
186
            $this->getDoctrine()->getManager()->flush();
187
188
            return $this->redirectToRoute('admin_groups_edit', array('id' => $group->getId()));
189
        }
190
        $deleteForm = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
191
192
        return array(
193
            'group' => $group,
194
            'edit_form'   => $editForm->createView(),
195
            'delete_form' => $deleteForm->createView(),
196
        );
197
    }
198
199
    /**
200
     * Deletes a Group entity.
201
     *
202
     * @Route("/{id}/delete", name="admin_groups_delete", requirements={"id"="\d+"})
203
     * @Method("DELETE")
204
     */
205
    public function deleteAction(Group $group, Request $request)
206
    {
207
        $form = $this->createDeleteForm($group->getId(), 'admin_groups_delete');
208
        
209
        $em = $this->getDoctrine()->getManager();
210
        $users = $group->getUsers();
211
        foreach ($users as $user) {
212
            $user->getGroups()->removeElement($group);
213
        }
214
        $em->flush();
215
216
        $this->get('fos_user.group_manager')->deleteGroup($group);
217
218
        if ($form->handleRequest($request)->isValid()) {
219
            $em = $this->getDoctrine()->getManager();
220
            $em->remove($group);
221
            $em->flush();
222
        }
223
224
        return $this->redirectToRoute('admin_groups');
225
    }
226
227
    /**
228
     * Create Delete form
229
     *
230
     * @param integer                       $id
231
     * @param string                        $route
232
     * @return \Symfony\Component\Form\Form
233
     */
234
    protected function createDeleteForm($id, $route)
235
    {
236
        return $this->createFormBuilder(null, array('attr' => array('id' => 'delete')))
237
            ->setAction($this->generateUrl($route, array('id' => $id)))
238
            ->setMethod('DELETE')
239
            ->getForm()
240
        ;
241
    }
242
243
    /**
244
     * Get the existing roles
245
     *
246
     * @return array Array of roles
247
     */
248
    private function getExistingRoles()
249
    {
250
        $roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
251
        $roles = array_keys($roleHierarchy);
252
        $theRoles = array();
253
254
        foreach ($roles as $role) {
255
            $theRoles[$role] = $role;
256
        }
257
        return $theRoles;
258
    }
259
}
260