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 Abienvenu\KyelaBundle\Entity\Choice; |
25
|
|
|
use Abienvenu\KyelaBundle\Entity\Event; |
26
|
|
|
use Abienvenu\KyelaBundle\Entity\Participant; |
27
|
|
|
use Abienvenu\KyelaBundle\Entity\Poll; |
28
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
29
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
30
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
31
|
|
|
use Abienvenu\KyelaBundle\Entity\Participation; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Participation controller. |
35
|
|
|
* |
36
|
|
|
* @Route("/participation") |
37
|
|
|
*/ |
38
|
|
|
class ParticipationController extends Controller |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* Displays interactive participation table |
42
|
|
|
* |
43
|
|
|
* @Route("", methods="GET") |
44
|
|
|
* @Template() |
45
|
|
|
*/ |
46
|
|
|
public function showAction(Poll $poll, $isFuture) |
47
|
|
|
{ |
48
|
|
|
$em = $this->getDoctrine()->getManager(); |
49
|
|
|
$events = $em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($poll, $isFuture); |
|
|
|
|
50
|
|
|
$choices = $em->getRepository("KyelaBundle:Choice")->getOrderedChoices($poll); |
|
|
|
|
51
|
|
|
$participationsArray = []; |
52
|
|
|
foreach ($events as $event) |
53
|
|
|
{ |
54
|
|
|
foreach ($event->getParticipations() as $participation) |
55
|
|
|
{ |
56
|
|
|
$accessKey = "{$event->getId()}-{$participation->getParticipant()->getId()}"; |
57
|
|
|
$participationsArray[$accessKey] = $participation; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return [ |
61
|
|
|
'poll' => $poll, |
62
|
|
|
'choices' => $choices, |
63
|
|
|
'events' => $events, |
64
|
|
|
'participations' => $participationsArray, |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates a new Participation on the fly. |
70
|
|
|
* |
71
|
|
|
* @Route("/new/{event}/{participant}/{choice}", name="participation_new", methods="GET") |
72
|
|
|
*/ |
73
|
|
|
public function newAction(Event $event, Participant $participant, Choice $choice) |
74
|
|
|
{ |
75
|
|
|
$em = $this->getDoctrine()->getManager(); |
76
|
|
|
|
77
|
|
|
// If participation exists, editAction should have been called, not newAction |
78
|
|
|
// But just in case, we look for an existing participation |
79
|
|
|
$participation = $em->getRepository('KyelaBundle:Participation')->findOneBy(['participant' => $participant->getId(), 'event' => $event->getId()]); |
80
|
|
|
if (!$participation) |
81
|
|
|
{ |
82
|
|
|
$participation = new Participation(); |
83
|
|
|
} |
84
|
|
|
$participation->setEvent($event); |
85
|
|
|
$participation->setParticipant($participant); |
86
|
|
|
$participation->setChoice($choice); |
87
|
|
|
|
88
|
|
|
$em->persist($participation); |
89
|
|
|
$em->flush(); |
90
|
|
|
return $this->render( |
91
|
|
|
'KyelaBundle:participation:_cell.html.twig', |
92
|
|
|
['participation' => $participation, 'choices' => $event->getPoll()->getChoices(), 'event' => $event, 'participant' => $participant]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Edits a Participation on the fly |
97
|
|
|
* |
98
|
|
|
* @Route("/edit/{participation}/{newChoice}", name="participation_edit", methods="GET") |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function editAction(Participation $participation, Choice $newChoice) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$em = $this->getDoctrine()->getManager(); |
103
|
|
|
|
104
|
|
|
$participation->setChoice($newChoice); |
105
|
|
|
$em->flush(); |
106
|
|
|
return $this->render( |
107
|
|
|
'KyelaBundle:participation:_cell.html.twig', |
108
|
|
|
[ |
109
|
|
|
'participation' => $participation, |
110
|
|
|
'choices' => $newChoice->getPoll()->getChoices(), |
111
|
|
|
'event' => $participation->getEvent(), |
112
|
|
|
'participant' => $participation->getParticipant() |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Removes a Participation on the fly |
119
|
|
|
* |
120
|
|
|
* @Route("/delete/{participation}", name="participation_delete", methods="GET") |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
public function deleteAction(Participation $participation) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$em = $this->getDoctrine()->getManager(); |
125
|
|
|
$em->remove($participation); |
126
|
|
|
$em->flush(); |
127
|
|
|
return $this->render( |
128
|
|
|
'KyelaBundle:participation:_cell.html.twig', |
129
|
|
|
[ |
130
|
|
|
'participation' => null, |
131
|
|
|
'choices' => $participation->getEvent()->getPoll()->getChoices(), |
132
|
|
|
'event' => $participation->getEvent(), |
133
|
|
|
'participant' => $participation->getParticipant() |
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: