Completed
Push — master ( fe7869...b5829e )
by Laurent
02:59
created

GroupController::updateAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
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 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
25
/**
26
 * Group controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/admin/group")
31
 */
32
class GroupController extends AbstractController
33
{
34
    /**
35
     * Lists all Group entities.
36
     *
37
     * @Route("/", name="group")
38
     * @Method("GET")
39
     * @Template()
40
     *
41
     * @return array
42
     */
43
    public function indexAction()
44
    {
45
        $return = $this->abstractIndexAction('Group', null);
46
47
        return $return;
48
    }
49
50
    /**
51
     * Finds and displays a Group entity.
52
     *
53
     * @Route("/{id}/show", name="group_show", requirements={"id"="\d+"})
54
     * @Method("GET")
55
     * @Template()
56
     *
57
     * @param \AppBundle\Entity\Group $group Group to display
58
     * @return array
59
     */
60
    public function showAction(Group $group)
61
    {
62
        $return = $this->abstractShowAction($group, 'group');
63
64
        return $return;
65
    }
66
67
    /**
68
     * Displays a form to create a new Group entity.
69
     *
70
     * @Route("/new", name="group_new")
71
     * @Method("GET")
72
     * @Template()
73
     *
74
     * @return array
75
     */
76
    public function newAction()
77
    {
78
        $group = new Group();
79
        $form = $this->createForm(GroupType::class, $group);
80
        $this->addRoles($form, $group);
0 ignored issues
show
Bug introduced by
The method addRoles() cannot be called from this context as it is declared private in class AppBundle\Controller\AbstractController.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
81
82
        return array(
83
            'group' => $group,
84
            'form'   => $form->createView(),
85
        );
86
    }
87
88
    /**
89
     * Creates a new Group entity.
90
     *
91
     * @Route("/create", name="group_create")
92
     * @Method("POST")
93
     * @Template("AppBundle:Group:new.html.twig")
94
     *
95
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
96
     * @return array
97
     */
98
    public function createAction(Request $request)
99
    {
100
        $group = new Group();
101
        $form = $this->createForm(GroupType::class, $group);
102
        $this->addRoles($form, $group);
0 ignored issues
show
Bug introduced by
The method addRoles() cannot be called from this context as it is declared private in class AppBundle\Controller\AbstractController.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
103
        $form->handleRequest($request);
104
        $return = ['group' => $group, 'form' => $form->createView(),];
105
106
        if ($form->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
        $return = $this->abstractEditAction($group, 'group', GroupType::class);
131
132
        return $return;
133
    }
134
135
    /**
136
     * Edits an existing Group entity.
137
     *
138
     * @Route("/{id}/update", name="group_update", requirements={"id"="\d+"})
139
     * @Method("PUT")
140
     * @Template("AppBundle:Group:edit.html.twig")
141
     *
142
     * @param \AppBundle\Entity\Group                   $group   Group item to update
143
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
144
     * @return array
145
     */
146
    public function updateAction(Group $group, Request $request)
147
    {
148
        $return = $this->abstractUpdateAction(
149
            $group,
150
            $request,
151
            'group',
152
            GroupType::class
153
        );
154
155
        return $return;
156
    }
157
158
    /**
159
     * Deletes a Group entity.
160
     *
161
     * @Route("/{id}/delete", name="group_delete", requirements={"id"="\d+"})
162
     * @Method("DELETE")
163
     *
164
     * @param \AppBundle\Entity\Group                   $group   Group item to delete
165
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
166
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
167
     */
168
    public function deleteAction(Group $group, Request $request)
169
    {
170
        $form = $this->createDeleteForm($group->getId(), 'group_delete');
171
        
172
        $etm = $this->getDoctrine()->getManager();
173
        $users = $group->getUsers();
174
        foreach ($users as $user) {
175
            $user->getGroups()->removeElement($group);
176
        }
177
        $etm->flush();
178
179
        $this->get('fos_user.group_manager')->deleteGroup($group);
180
181
        if ($form->handleRequest($request)->isValid()) {
182
            $etm = $this->getDoctrine()->getManager();
183
            $etm->remove($group);
184
            $etm->flush();
185
        }
186
187
        return $this->redirectToRoute('group');
188
    }
189
}
190