Completed
Push — sf2.7 ( fb6965...00100f )
by Laurent
03:55
created

UserController::getOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * UserController controller des 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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
23
use AppBundle\Entity\User;
24
use AppBundle\Form\Type\UserType;
25
use AppBundle\Form\Type\UserFilterType;
26
use Symfony\Component\Form\FormInterface;
27
28
/**
29
 * User controller.
30
 *
31
 * @category Controller
32
 *
33
 * @Route("/admin/users")
34
 */
35
class UserController extends AbstractController
36
{
37
    /**
38
     * Lists all User entities.
39
     *
40
     * @Route("/", name="admin_users")
41
     * @Method("GET")
42
     * @Template()
43
     */
44
    public function indexAction()
45
    {
46
        $em = $this->getDoctrine()->getManager();
47
        $form = $this->createForm(new UserFilterType());
48
        if (!is_null($response = $this->saveFilter($form, 'user', 'admin_users'))) {
49
            return $response;
50
        }
51
        $qb = $em->getRepository('AppBundle:User')->createQueryBuilder('u');
52
        $paginator = $this->filter($form, $qb, 'user');
53
        
54
        return array(
55
            'form'      => $form->createView(),
56
            'paginator' => $paginator,
57
        );
58
    }
59
60
    /**
61
     * Finds and displays a User entity.
62
     *
63
     * @Route("/{id}/show", name="admin_users_show", requirements={"id"="\d+"})
64
     * @Method("GET")
65
     * @Template()
66
     */
67
    public function showAction(User $user)
68
    {
69
        $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete');
70
71
        return array(
72
            'user' => $user,
73
            'delete_form' => $deleteForm->createView(),
74
        );
75
    }
76
77
    /**
78
     * Displays a form to create a new User entity.
79
     *
80
     * @Route("/new", name="admin_users_new")
81
     * @Method("GET")
82
     * @Template()
83
     */
84
    public function newAction()
85
    {
86
        $user = new User();
87
        $form = $this->createForm(new UserType(), $user);
88
89
        return array(
90
            'user' => $user,
91
            'form'   => $form->createView(),
92
        );
93
    }
94
95
    /**
96
     * Creates a new User entity.
97
     *
98
     * @Route("/create", name="admin_users_create")
99
     * @Method("POST")
100
     * @Template("AppBundle:User:new.html.twig")
101
     */
102
    public function createAction(Request $request)
103
    {
104
        $user = new User();
105
        $form = $this->createForm(new UserType(), $user);
106
        if ($form->handleRequest($request)->isValid()) {
107
            $user->setEnabled(true);
108
            $userManager = $this->get('fos_user.user_manager');
109
            $userManager->updateUser($user);
110
111
            return $this->redirectToRoute('admin_users_show', array('id', $user->getId()));
112
        }
113
114
        return array(
115
            'user' => $user,
116
            'form'   => $form->createView(),
117
        );
118
    }
119
120
    /**
121
     * Displays a form to edit an existing User entity.
122
     *
123
     * @Route("/{id}/edit", name="admin_users_edit", requirements={"id"="\d+"})
124
     * @Method("GET")
125
     * @Template()
126
     */
127
    public function editAction(User $user)
128
    {
129
        $editForm = $this->createForm(new UserType(), $user, array(
130
            'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())),
131
            'method' => 'PUT',
132
            'passwordRequired' => false,
133
            'lockedRequired' => true
134
        ));
135
        $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete');
136
 
137
        return array(
138
            'user' => $user,
139
            'edit_form'   => $editForm->createView(),
140
            'delete_form' => $deleteForm->createView(),
141
        );
142
    }
143
144
    /**
145
     * Edits an existing User entity.
146
     *
147
     * @Route("/{id}/update", name="admin_users_update", requirements={"id"="\d+"})
148
     * @Method("PUT")
149
     * @Template("AppBundle:User:edit.html.twig")
150
     */
151 View Code Duplication
    public function updateAction(User $user, 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...
152
    {
153
        $editForm = $this->createForm(new UserType(), $user, array(
154
            'action' => $this->generateUrl('admin_users_update', array('id' => $user->getId())),
155
            'method' => 'PUT',
156
            'passwordRequired' => false,
157
            'lockedRequired' => true
158
        ));
159
        if ($editForm->handleRequest($request)->isValid()) {
160
            $userManager = $this->get('fos_user.user_manager');
161
            $userManager->updateUser($user);
162
163
            return $this->redirectToRoute('admin_users_edit', array('id' => $user->getId()));
164
        }
165
        $deleteForm = $this->createDeleteForm($user->getId(), 'admin_users_delete');
166
167
        return array(
168
            'user' => $user,
169
            'edit_form'   => $editForm->createView(),
170
            'delete_form' => $deleteForm->createView(),
171
        );
172
    }
173
174
175
    /**
176
     * Save order.
177
     *
178
     * @Route("/order/{field}/{type}", name="admin_users_sort")
179
     */
180
    public function sortAction($field, $type)
181
    {
182
        $this->setOrder('user', $field, $type);
183
184
        return $this->redirectToRoute('admin_users');
185
    }
186
187
    /**
188
     * Save filters
189
     *
190
     * @param  FormInterface $form
191
     * @param  string        $name    route/entity name
192
     * @param  string        $route   route name, if different from entity name
193
     * @param  Request       $request Request
194
     * @param  array         $params  possible route parameters
195
     * @return Response
196
     */
197
    protected function saveFilter(
198
        FormInterface $form,
199
        $name,
200
        $route = null,
201
        Request $request = null,
202
        array $params = null
203
    ) {
204
        $url = $this->generateUrl($route ?: $name, is_null($params) ? array() : $params);
205
        if (isset($request)) {
206
            if ($request->query->has('submit-filter') && $form->handleRequest($request)->isValid()) {
207
                $request->getSession()->set('filter.' . $name, $request->query->get($form->getName()));
208
209
                return $this->redirect($url);
210
            } elseif ($request->query->has('reset-filter')) {
211
                $request->getSession()->set('filter.' . $name, null);
212
213
                return $this->redirect($url);
214
            }
215
        }
216
    }
217
218
    /**
219
     * Filter form
220
     *
221
     * @param  FormInterface                                       $form
222
     * @param  QueryBuilder                                        $qb
223
     * @param  string                                              $name
224
     * @return \Knp\Component\Pager\Pagination\PaginationInterface
225
     */
226
    protected function filter(FormInterface $form, QueryBuilder $qb, $name)
227
    {
228
        if (!is_null($values = $this->getFilter($name))) {
229
            if ($form->submit($values)->isValid()) {
230
                $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb);
231
            }
232
        }
233
234
        // possible sorting
235
        $this->addQueryBuilderSort($qb, $name);
236
        return $this->get('knp_paginator')->paginate($qb, $this->getRequest()->query->get('page', 1), 20);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
237
    }
238
239
    /**
240
     * Get filters from session
241
     *
242
     * @param  string $name
243
     * @return array
244
     */
245
    protected function getFilter($name)
246
    {
247
        return $this->getRequest()->getSession()->get('filter.' . $name);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
248
    }
249
250
    /**
251
     * Deletes a User entity.
252
     *
253
     * @Security("has_role('ROLE_SUPER_ADMIN')")
254
     * @Route("/{id}/delete", name="admin_users_delete", requirements={"id"="\d+"})
255
     * @Method("DELETE")
256
     */
257
    public function deleteAction(User $user, Request $request)
258
    {
259
        $form = $this->createDeleteForm($user->getId(), 'admin_users_delete');
260
        if ($form->handleRequest($request)->isValid()) {
261
            $em = $this->getDoctrine()->getManager();
262
            $em->remove($user);
263
            $em->flush();
264
        }
265
266
        return $this->redirectToRoute('admin_users');
267
    }
268
}
269