1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the thealternativezurich/feedback project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Controller\Administration\Semester; |
13
|
|
|
|
14
|
|
|
use App\Controller\Administration\Base\BaseController; |
15
|
|
|
use App\Entity\Event; |
16
|
|
|
use App\Entity\Semester; |
17
|
|
|
use App\Model\Breadcrumb; |
18
|
|
|
use App\Security\Voter\Base\BaseVoter; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @Route("/event") |
25
|
|
|
*/ |
26
|
|
|
class EventController extends BaseController |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @Route("/new", name="administration_semester_event_new") |
30
|
|
|
* |
31
|
|
|
* @return Response |
32
|
|
|
*/ |
33
|
|
|
public function newAction(Request $request, Semester $semester) |
34
|
|
|
{ |
35
|
|
|
//create the event |
36
|
|
|
$event = new Event(); |
37
|
|
|
$event->setSemester($semester); |
38
|
|
|
$event->setName(''); |
39
|
|
|
|
40
|
|
|
//fill out default values as smart as possible |
41
|
|
|
/** @var Event|null $lastEvent */ |
42
|
|
|
$lastEvent = $semester->getEvents()->first(); |
43
|
|
|
if ($lastEvent) { |
44
|
|
|
$event->setFeedbackStartTime($lastEvent->getFeedbackStartTime()); |
45
|
|
|
$event->setFeedbackEndTime($lastEvent->getFeedbackEndTime()); |
46
|
|
|
$event->setTemplateName($lastEvent->getTemplateName()); |
47
|
|
|
} else { |
48
|
|
|
$event->setFeedbackStartTime('19:00:00'); |
49
|
|
|
$event->setFeedbackEndTime('21:00:00'); |
50
|
|
|
$event->setTemplateName('default.json'); |
51
|
|
|
} |
52
|
|
|
$event->setDate((new \DateTime())->modify('+20 day')->format('Y-m-d')); |
53
|
|
|
|
54
|
|
|
//check if can indeed create the event |
55
|
|
|
$this->ensureAccessGranted($event); |
56
|
|
|
|
57
|
|
|
//process form |
58
|
|
|
$myForm = $this->handleCreateForm( |
59
|
|
|
$request, |
60
|
|
|
$event, |
61
|
|
|
function () use ($event) { |
62
|
|
|
return $this->prePersistActions($event); |
63
|
|
|
} |
64
|
|
|
); |
65
|
|
|
if ($myForm instanceof Response) { |
66
|
|
|
return $myForm; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->renderWithSemester('administration/semester/event/new.html.twig', $semester, ['form' => $myForm->createView()]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @Route("/{event}/edit", name="administration_semester_event_edit") |
74
|
|
|
* |
75
|
|
|
* @return Response |
76
|
|
|
*/ |
77
|
|
|
public function editAction(Request $request, Semester $semester, Event $event) |
78
|
|
|
{ |
79
|
|
|
$this->ensureAccessGranted($event); |
80
|
|
|
|
81
|
|
|
//process form |
82
|
|
|
$myForm = $this->handleUpdateForm( |
83
|
|
|
$request, |
84
|
|
|
$event, |
85
|
|
|
function () use ($event) { |
86
|
|
|
return $this->prePersistActions($event); |
87
|
|
|
} |
88
|
|
|
); |
89
|
|
|
if ($myForm instanceof Response) { |
90
|
|
|
return $myForm; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->renderWithSemester('administration/semester/event/edit.html.twig', $semester, ['form' => $myForm->createView(), 'event' => $event]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Route("/{event}/view/", name="administration_semester_event_view") |
98
|
|
|
* |
99
|
|
|
* @return Response |
100
|
|
|
*/ |
101
|
|
|
public function viewAction(Semester $semester, Event $event) |
102
|
|
|
{ |
103
|
|
|
return $this->renderWithSemester('administration/semester/event/view.html.twig', $semester, ['event' => $event]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @Route("/{event}/result/", name="administration_semester_event_result") |
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
|
|
public function resultAction(Semester $semester, Event $event) |
112
|
|
|
{ |
113
|
|
|
return $this->renderWithSemester('administration/semester/event/result.html.twig', $semester, ['event' => $event]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** * |
117
|
|
|
* @Route("/{event}/remove", name="administration_semester_event_remove") |
118
|
|
|
* |
119
|
|
|
* @return Response |
120
|
|
|
*/ |
121
|
|
|
public function removeAction(Request $request, Semester $semester, Event $event) |
122
|
|
|
{ |
123
|
|
|
$this->ensureAccessGranted($event); |
124
|
|
|
|
125
|
|
|
//process form |
126
|
|
|
$form = $this->handleDeleteForm($request, $event); |
127
|
|
|
if (null === $form) { |
128
|
|
|
return $this->redirectToRoute('administration_semesters'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->renderWithSemester('administration/semester/event/remove.html.twig', $semester, ['event' => $event, 'form' => $form->createView()]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* get the breadcrumbs leading to this controller. |
136
|
|
|
* |
137
|
|
|
* @return Breadcrumb[] |
138
|
|
|
*/ |
139
|
|
|
protected function getIndexBreadcrumbs() |
140
|
|
|
{ |
141
|
|
|
return array_merge(parent::getIndexBreadcrumbs(), [ |
142
|
|
|
new Breadcrumb( |
143
|
|
|
$this->generateUrl('administration_semesters'), |
144
|
|
|
$this->getTranslator()->trans('index.title', [], 'administration_semester') |
145
|
|
|
), |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function ensureAccessGranted(Event $event) |
150
|
|
|
{ |
151
|
|
|
$this->denyAccessUnlessGranted(BaseVoter::VIEW, $event); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
protected function prePersistActions(Event $event) |
158
|
|
|
{ |
159
|
|
|
$event->loadTemplateIfSafe($this->getParameter('PUBLIC_DIR')); |
160
|
|
|
|
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* renders the view & prepares views which need the active election. |
166
|
|
|
* |
167
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
168
|
|
|
*/ |
169
|
|
|
protected function renderWithSemester(string $view, Semester $semester, array $parameters = []) |
170
|
|
|
{ |
171
|
|
|
return $this->render($view, $parameters + ['semester' => $semester]); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|