Completed
Push — master ( cfc87e...ecfb42 )
by Laurent
03:10
created

GroupController::updateAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 15
nc 2
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 AppBundle\Controller\AbstractController;
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
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
25
26
/**
27
 * Group controller.
28
 *
29
 * @category Controller
30
 *
31
 * @Route("/admin/group")
32
 */
33
class GroupController extends AbstractController
34
{
35
    /**
36
     * Lists all Group entities.
37
     *
38
     * @Route("/", name="group")
39
     * @Method("GET")
40
     * @Template()
41
     *
42
     * @return array
43
     */
44
    public function indexAction()
45
    {
46
        $return = $this->abstractIndexAction('Group', null);
47
48
        return $return;
49
    }
50
51
    /**
52
     * Finds and displays a Group entity.
53
     *
54
     * @Route("/{id}/show", name="group_show", requirements={"id"="\d+"})
55
     * @Method("GET")
56
     * @Template()
57
     *
58
     * @param \AppBundle\Entity\Group $group Group to display
59
     * @return array
60
     */
61
    public function showAction(Group $group)
62
    {
63
        $return = $this->abstractShowAction($group, 'group');
64
65
        return $return;
66
    }
67
68
    /**
69
     * Displays a form to create a new Group entity.
70
     *
71
     * @Route("/new", name="group_new")
72
     * @Method("GET")
73
     * @Template()
74
     *
75
     * @return array
76
     */
77
    public function newAction()
78
    {
79
        $group = new Group();
80
        $form = $this->createForm(GroupType::class, $group);
81
        $this->addRoles($form, $group);
82
83
        return array(
84
            'group' => $group,
85
            'form'   => $form->createView(),
86
        );
87
    }
88
89
    /**
90
     * Creates a new Group entity.
91
     *
92
     * @Route("/create", name="group_create")
93
     * @Method("POST")
94
     * @Template("AppBundle:Group:new.html.twig")
95
     *
96
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
97
     * @return array
98
     */
99
    public function createAction(Request $request)
100
    {
101
        $group = new Group();
102
        $form = $this->createForm(GroupType::class, $group);
103
        $this->addRoles($form, $group);
104
        $return = ['group' => $group, 'form' => $form->createView(),];
105
106
        if ($form->handleRequest($request)->isValid()) {
107
            $etm = $this->getDoctrine()->getManager();
108
            $etm->persist($group);
109
            $etm->flush();
110
            $this->addFlash('info', 'gestock.create.ok');
111
112
            $return = $this->redirectToRoute('group_show', array('id' => $group->getId()));
113
        }
114
115
        return $return;
116
    }
117
118
    /**
119
     * Displays a form to edit an existing Group entity.
120
     *
121
     * @Route("/{id}/edit", name="group_edit", requirements={"id"="\d+"})
122
     * @Method("GET")
123
     * @Template()
124
     *
125
     * @param \AppBundle\Entity\Group $group Group item to edit
126
     * @return array
127
     */
128
    public function editAction(Group $group)
129
    {
130
        $editForm = $this->createForm(GroupType::class, $group, array(
131
            'action' => $this->generateUrl('group_update', array('id' => $group->getId())),
132
            'method' => 'PUT',
133
        ));
134
        $this->addRoles($editForm, $group);
135
136
        $deleteForm = $this->createDeleteForm($group->getId(), 'group_delete');
137
138
        return array(
139
            'group' => $group,
140
            'edit_form'   => $editForm->createView(),
141
            'delete_form' => $deleteForm->createView(),
142
        );
143
    }
144
145
    /**
146
     * Edits an existing Group entity.
147
     *
148
     * @Route("/{id}/update", name="group_update", requirements={"id"="\d+"})
149
     * @Method("PUT")
150
     * @Template("AppBundle:Group:edit.html.twig")
151
     *
152
     * @param \AppBundle\Entity\Group                   $group   Group item to update
153
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
154
     * @return array
155
     */
156
    public function updateAction(Group $group, Request $request)
157
    {
158
        $editForm = $this->createForm(GroupType::class, $group, array(
159
            'action' => $this->generateUrl('group_update', array('id' => $group->getId())),
160
            'method' => 'PUT',
161
        ));
162
        $this->addRoles($editForm, $group);
163
164
        $deleteForm = $this->createDeleteForm($group->getId(), 'group_delete');
165
166
        $return = array(
167
            'group' => $group,
168
            'edit_form'   => $editForm->createView(),
169
            'delete_form' => $deleteForm->createView(),
170
        );
171
172
        if ($editForm->handleRequest($request)->isValid()) {
173
            $this->getDoctrine()->getManager()->flush();
174
            $this->addFlash('info', 'gestock.edit.ok');
175
176
            $return = $this->redirectToRoute('group_edit', array('id' => $group->getId()));
177
        }
178
179
        return $return;
180
    }
181
182
    /**
183
     * Deletes a Group entity.
184
     *
185
     * @Route("/{id}/delete", name="group_delete", requirements={"id"="\d+"})
186
     * @Method("DELETE")
187
     *
188
     * @param \AppBundle\Entity\Group                   $group   Group item to delete
189
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
190
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
191
     */
192
    public function deleteAction(Group $group, Request $request)
193
    {
194
        $form = $this->createDeleteForm($group->getId(), 'group_delete');
195
        
196
        $etm = $this->getDoctrine()->getManager();
197
        $users = $group->getUsers();
198
        foreach ($users as $user) {
199
            $user->getGroups()->removeElement($group);
200
        }
201
        $etm->flush();
202
203
        $this->get('fos_user.group_manager')->deleteGroup($group);
204
205
        if ($form->handleRequest($request)->isValid()) {
206
            $etm = $this->getDoctrine()->getManager();
207
            $etm->remove($group);
208
            $etm->flush();
209
        }
210
211
        return $this->redirectToRoute('group');
212
    }
213
214
    /**
215
     * Get the existing roles
216
     *
217
     * @return array Array of roles
218
     */
219
    private function getExistingRoles()
220
    {
221
        $roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
222
        $roles = array_keys($roleHierarchy);
223
        $theRoles = array();
224
225
        foreach ($roles as $role) {
226
            $theRoles[$role] = $role;
227
        }
228
        return $theRoles;
229
    }
230
231
    /**
232
     * Add roles to form
233
     *
234
     * @param \Symfony\Component\Form\Form  $form  The form in which to insert the roles
235
     * @param \AppBundle\Entity\Group       $group The entity to deal
236
     * @return \Symfony\Component\Form\Form The form
237
     */
238
    private function addRoles($form, $group)
239
    {
240
        $form->add('roles', ChoiceType::class, array(
241
            'choices' => $this->getExistingRoles(),
242
            'choices_as_values' => true,
243
            'data' => $group->getRoles(),
244
            'label' => 'Roles',
245
            'expanded' => true,
246
            'multiple' => true,
247
            'mapped' => true,
248
        ));
249
250
        return $form;
251
    }
252
}
253