1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright 2014-2016 Arnaud Bienvenu |
4
|
|
|
* |
5
|
|
|
* This file is part of Kyela. |
6
|
|
|
|
7
|
|
|
* Kyela is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
|
12
|
|
|
* Kyela is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with Kyela. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Abienvenu\KyelaBundle\Controller; |
23
|
|
|
|
24
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
26
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
27
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
28
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
29
|
|
|
use Abienvenu\KyelaBundle\Entity\Participant; |
30
|
|
|
use Abienvenu\KyelaBundle\Form\Type\ParticipantType; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Participant controller. |
34
|
|
|
* |
35
|
|
|
* @Route("/{pollUrl}/participant") |
36
|
|
|
*/ |
37
|
|
|
class ParticipantController extends CRUDController |
38
|
|
|
{ |
39
|
|
|
protected $entityName = 'KyelaBundle:Participant'; |
40
|
|
|
protected $cancelRoute = 'poll_show'; |
41
|
|
|
protected $successRoute = 'poll_show'; |
42
|
|
|
protected $deleteRoute = 'participant_delete'; |
43
|
|
|
protected $deleteSuccessRoute = 'poll_show'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Displays a form to create a new Participant entity. |
47
|
|
|
* |
48
|
|
|
* @Route("/new", name="participant_new") |
49
|
|
|
* @Method({"GET", "POST"}) |
50
|
|
|
* @Template() |
51
|
|
|
*/ |
52
|
|
|
public function newAction(Request $request) |
53
|
|
|
{ |
54
|
|
|
return $this->doNewAction(ParticipantType::class, new Participant(), $request); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Displays a form to edit an existing Participant entity. |
59
|
|
|
* |
60
|
|
|
* @Route("/{id}/edit", name="participant_edit") |
61
|
|
|
* @Method({"GET", "PUT"}) |
62
|
|
|
* @Template() |
63
|
|
|
*/ |
64
|
|
|
public function editAction(Request $request, $id) |
65
|
|
|
{ |
66
|
|
|
return $this->doEditAction(ParticipantType::class, $id, $request); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Deletes a Participant entity. |
71
|
|
|
* |
72
|
|
|
* @Route("/{id}", name="participant_delete") |
73
|
|
|
* @Method("DELETE") |
74
|
|
|
*/ |
75
|
|
|
public function deleteAction(Request $request, $id) |
76
|
|
|
{ |
77
|
|
|
return $this->doDeleteAction($request, $id); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Reorder participant |
82
|
|
|
* |
83
|
|
|
* @Route("/order", name="participant_order") |
84
|
|
|
* @Method("POST") |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function orderAction(Request $request) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$em = $this->getDoctrine()->getManager(); |
89
|
|
|
$repository = $em->getRepository("KyelaBundle:Participant"); |
90
|
|
|
$order = $request->request->get('participant'); |
91
|
|
|
foreach ($order as $priority => $participantId) |
92
|
|
|
{ |
93
|
|
|
/** @var Participant $choice */ |
94
|
|
|
$participant = $repository->find($participantId); |
95
|
|
|
$participant->setPriority($priority); |
96
|
|
|
} |
97
|
|
|
$em->flush(); |
98
|
|
|
|
99
|
|
|
$response = new JsonResponse(); |
100
|
|
|
$response->setData(array("code" => 100, "success" => true)); |
101
|
|
|
return $response; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.