]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/ApplicationController.php
Contextualize buttons
[airbundle] / Controller / ApplicationController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\Routing\RequestContext;
7 use Symfony\Component\Form\FormError;
8 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
9 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
10 use Rapsys\AirBundle\Entity\Slot;
11 use Rapsys\AirBundle\Entity\User;
12 use Rapsys\AirBundle\Entity\Session;
13 use Rapsys\AirBundle\Entity\Application;
14
15 class ApplicationController extends DefaultController {
16 /**
17 * Add application
18 *
19 * @desc Persist application and all required dependencies in database
20 *
21 * @param Request $request The request instance
22 *
23 * @return Response The rendered view or redirection
24 *
25 * @throws \RuntimeException When user has not at least guest role
26 */
27 public function add(Request $request) {
28 //Prevent non-guest to access here
29 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
30
31 //Reject non post requests
32 if (!$request->isMethod('POST')) {
33 throw new \RuntimeException('Request method MUST be POST');
34 }
35
36 //Create ApplicationType form
37 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
38 //Set the action
39 'action' => $this->generateUrl('rapsys_air_application_add'),
40 //Set the form attribute
41 #'attr' => [ 'class' => 'col' ],
42 //Set admin
43 'admin' => $this->isGranted('ROLE_ADMIN'),
44 //Set default user to current
45 'user' => $this->getUser()->getId(),
46 //Set default slot to evening
47 //XXX: default to Evening (3)
48 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
49 ]);
50
51 //Refill the fields in case of invalid form
52 $form->handleRequest($request);
53
54 //Handle invalid form
55 if (!$form->isValid()) {
56 //Set section
57 $section = $this->translator->trans('Application add');
58
59 //Set title
60 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
61
62 //Render the view
63 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
64 }
65
66 //Get doctrine
67 $doctrine = $this->getDoctrine();
68
69 //Get manager
70 $manager = $doctrine->getManager();
71
72 //Get data
73 $data = $form->getData();
74
75 //Protect session fetching
76 try {
77 //Fetch session
78 $session = $doctrine->getRepository(Session::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
79 //Catch no session case
80 } catch (\Doctrine\ORM\NoResultException $e) {
81 //Create the session
82 $session = new Session();
83 $session->setLocation($data['location']);
84 $session->setDate($data['date']);
85 $session->setSlot($data['slot']);
86 $session->setCreated(new \DateTime('now'));
87 $session->setUpdated(new \DateTime('now'));
88
89 //Get short location
90 $short = $data['location']->getShort();
91
92 //Get slot
93 $slot = $data['slot']->getTitle();
94
95 //Set premium
96 $session->setPremium($premium = false);
97
98 //Check if slot is afternoon
99 //XXX: premium is stored only for Afternoon and Evening
100 if ($slot == 'Afternoon') {
101 //Compute premium
102 //XXX: a session is considered premium a day off
103 $session->setPremium($premium = $this->isPremium($data['date']));
104 //Check if slot is evening
105 //XXX: premium is stored only for Afternoon and Evening
106 } elseif ($slot == 'Evening') {
107 //Compute premium
108 //XXX: a session is considered premium the eve of a day off
109 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \DateInterval('P1D'))));
110 //Check if slot is after
111 } elseif ($slot == 'After') {
112 //Compute premium
113 //XXX: a session is considered premium the eve of a day off
114 $premium = $this->isPremium((clone $data['date'])->add(new \DateInterval('P1D')));
115 }
116
117 //Set default length at 6h
118 //XXX: date part will be truncated on save
119 $session->setLength(new \DateTime('06:00:00'));
120
121 //Check if admin
122 if ($this->isGranted('ROLE_ADMIN')) {
123 //Check if morning
124 if ($slot == 'Morning') {
125 //Set begin at 9h
126 $session->setBegin(new \DateTime('09:00:00'));
127
128 //Set length at 5h
129 $session->setLength(new \DateTime('05:00:00'));
130 //Check if afternoon
131 } elseif ($slot == 'Afternoon') {
132 //Set begin at 14h
133 $session->setBegin(new \DateTime('14:00:00'));
134
135 //Set length at 5h
136 $session->setLength(new \DateTime('05:00:00'));
137 //Check if evening
138 } elseif ($slot == 'Evening') {
139 //Set begin at 19h
140 $session->setBegin(new \DateTime('19:00:00'));
141
142 //Check if next day is premium
143 if ($premium) {
144 //Set length at 7h
145 $session->setLength(new \DateTime('07:00:00'));
146 }
147 //Check if after
148 } else {
149 //Set begin at 1h
150 $session->setBegin(new \DateTime('01:00:00'));
151
152 //Set length at 4h
153 $session->setLength(new \DateTime('04:00:00'));
154
155 //Check if next day is premium
156 if ($premium) {
157 //Set begin at 2h
158 $session->setBegin(new \DateTime('02:00:00'));
159
160 //Set length at 3h
161 $session->setLength(new \DateTime('03:00:00'));
162 }
163 }
164 //Docks => 14h -> 19h | 19h -> 01/02h
165 //XXX: remove Garnier from here to switch back to 21h
166 } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
167 //Check if afternoon
168 if ($slot == 'Afternoon') {
169 //Set begin at 14h
170 $session->setBegin(new \DateTime('14:00:00'));
171
172 //Set length at 5h
173 $session->setLength(new \DateTime('05:00:00'));
174 //Check if evening
175 } elseif ($slot == 'Evening') {
176 //Set begin at 19h
177 $session->setBegin(new \DateTime('19:00:00'));
178
179 //Check if next day is premium
180 if ($premium) {
181 //Set length at 7h
182 $session->setLength(new \DateTime('07:00:00'));
183 }
184 //Check if after
185 } else {
186 //Set begin at 1h
187 $session->setBegin(new \DateTime('01:00:00'));
188
189 //Set length at 4h
190 $session->setLength(new \DateTime('04:00:00'));
191
192 //Check if next day is premium
193 if ($premium) {
194 //Set begin at 2h
195 $session->setBegin(new \DateTime('02:00:00'));
196
197 //Set length at 3h
198 $session->setLength(new \DateTime('03:00:00'));
199 }
200 }
201 //Garnier => 21h -> 01/02h
202 } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
203 //Check if evening
204 if ($slot == 'Evening') {
205 //Set begin at 21h
206 $session->setBegin(new \DateTime('21:00:00'));
207
208 //Set length at 5h
209 $session->setLength(new \DateTime('05:00:00'));
210
211 //Check if next day is premium
212 if ($premium) {
213 //Set length at 6h
214 $session->setLength(new \DateTime('06:00:00'));
215 }
216 //Check if after
217 } else {
218 //Set begin at 1h
219 $session->setBegin(new \DateTime('01:00:00'));
220
221 //Set length at 4h
222 $session->setLength(new \DateTime('04:00:00'));
223
224 //Check if next day is premium
225 if ($premium) {
226 //Set begin at 2h
227 $session->setBegin(new \DateTime('02:00:00'));
228
229 //Set length at 3h
230 $session->setLength(new \DateTime('03:00:00'));
231 }
232 }
233 //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
234 } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
235 //Check if evening
236 if ($slot == 'Evening') {
237 //Set begin at 19h
238 $session->setBegin(new \DateTime('19:00:00'));
239
240 //Check if next day is premium
241 if ($premium) {
242 //Set length at 7h
243 $session->setLength(new \DateTime('07:00:00'));
244 }
245 //Check if after
246 } else {
247 //Set begin at 1h
248 $session->setBegin(new \DateTime('01:00:00'));
249
250 //Set length at 4h
251 $session->setLength(new \DateTime('04:00:00'));
252
253 //Check if next day is premium
254 if ($premium) {
255 //Set begin at 2h
256 $session->setBegin(new \DateTime('02:00:00'));
257
258 //Set length at 3h
259 $session->setLength(new \DateTime('03:00:00'));
260 }
261 }
262 //La Villette => 14h -> 19h
263 } elseif ($short == 'Villette' && $slot == 'Afternoon') {
264 //Set begin at 14h
265 $session->setBegin(new \DateTime('14:00:00'));
266
267 //Set length at 5h
268 $session->setLength(new \DateTime('05:00:00'));
269 //Place Colette => 14h -> 21h
270 //TODO: add check here that it's a millegaux account ?
271 } elseif ($short == 'Colette' && $slot == 'Afternoon') {
272 //Set begin at 14h
273 $session->setBegin(new \DateTime('14:00:00'));
274
275 //Set length at 7h
276 $session->setLength(new \DateTime('07:00:00'));
277 //Galerie d'OrlƩans => 14h -> 18h
278 } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
279 //Set begin at 14h
280 $session->setBegin(new \DateTime('14:00:00'));
281
282 //Set length at 4h
283 $session->setLength(new \DateTime('04:00:00'));
284 //Combination not supported
285 } else {
286 //Add error in flash message
287 $this->addFlash('error', $this->translator->trans('Session on %date% %location% %slot% not yet supported', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
288
289 //Set section
290 $section = $this->translator->trans('Application add');
291
292 //Set title
293 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
294
295 //Render the view
296 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
297 }
298
299 //Check if admin
300 if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \DateTime('00:00:00')) {
301 //Add error in flash message
302 $this->addFlash('error', $this->translator->trans('Session in the past on %date% %location% %slot% not yet supported', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
303
304 //Set section
305 $section = $this->translator->trans('Application add');
306
307 //Set title
308 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
309
310 //Render the view
311 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
312 }
313
314 //Queue session save
315 $manager->persist($session);
316
317 //Flush to get the ids
318 #$manager->flush();
319
320 $this->addFlash('notice', $this->translator->trans('Session on %date% %location% %slot% created', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
321 }
322
323 //Set user
324 $user = $this->getUser();
325
326 //Replace with requested user for admin
327 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
328 $user = $this->getDoctrine()->getRepository(User::class)->findOneById($data['user']);
329 }
330
331 //Protect application fetching
332 try {
333 //Retrieve application
334 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
335
336 //Add warning in flash message
337 $this->addFlash('warning', $this->translator->trans('Application on %date% %location% %slot% already exists', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
338 //Catch no application and session without identifier (not persisted&flushed) cases
339 } catch (\Doctrine\ORM\NoResultException|\Doctrine\ORM\ORMInvalidArgumentException $e) {
340 //Create the application
341 $application = new Application();
342 $application->setSession($session);
343 $application->setUser($user);
344 $application->setCreated(new \DateTime('now'));
345 $application->setUpdated(new \DateTime('now'));
346
347 //Refresh session updated field
348 $session->setUpdated(new \DateTime('now'));
349
350 //Queue session save
351 $manager->persist($session);
352
353 //Queue application save
354 $manager->persist($application);
355
356 //Flush to get the ids
357 $manager->flush();
358
359 //Add notice in flash message
360 $this->addFlash('notice', $this->translator->trans('Application on %date% %location% %slot% created', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
361 }
362
363 //Extract and process referer
364 if ($referer = $request->headers->get('referer')) {
365 //Create referer request instance
366 $req = Request::create($referer);
367
368 //Get referer path
369 $path = $req->getPathInfo();
370
371 //Get referer query string
372 $query = $req->getQueryString();
373
374 //Remove script name
375 $path = str_replace($request->getScriptName(), '', $path);
376
377 //Try with referer path
378 try {
379 //Save old context
380 $oldContext = $this->router->getContext();
381
382 //Force clean context
383 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
384 $this->router->setContext(new RequestContext());
385
386 //Retrieve route matching path
387 $route = $this->router->match($path);
388
389 //Reset context
390 $this->router->setContext($oldContext);
391
392 //Clear old context
393 unset($oldContext);
394
395 //Extract name
396 $name = $route['_route'];
397
398 //Remove route and controller from route defaults
399 unset($route['_route'], $route['_controller']);
400
401 //Check if session view route
402 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
403 //Replace id
404 $route['id'] = $session->getId();
405 //Other routes
406 } else {
407 //Set session
408 $route['session'] = $session->getId();
409 }
410
411 //Generate url
412 return $this->redirectToRoute($name, $route);
413 //No route matched
414 } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
415 //Unset referer to fallback to default route
416 unset($referer);
417 }
418 }
419
420 //Redirect to cleanup the form
421 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
422 }
423
424 /**
425 * Compute eastern for selected year
426 *
427 * @param int $year The eastern year
428 *
429 * @return DateTime The eastern date
430 */
431 function getEastern($year) {
432 //Set static
433 static $data = null;
434 //Check if already computed
435 if (isset($data[$year])) {
436 //Return computed eastern
437 return $data[$year];
438 //Check if data is null
439 } elseif (is_null($data)) {
440 //Init data array
441 $data = [];
442 }
443 $d = (19 * ($year % 19) + 24) % 30;
444 $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7;
445
446 $day = 22 + $d + $e;
447 $month = 3;
448
449 if ($day > 31) {
450 $day = $d + $e - 9;
451 $month = 4;
452 } elseif ($d == 29 && $e == 6) {
453 $day = 10;
454 $month = 4;
455 } elseif ($d == 28 && $e == 6) {
456 $day = 18;
457 $month = 4;
458 }
459
460 //Store eastern in data
461 return ($data[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
462 }
463
464 /**
465 * Check if date is a premium day
466 *
467 * @desc Consider as premium a day off
468 *
469 * @param DateTime $date The date to check
470 * @return bool Whether the date is off or not
471 */
472 function isPremium($date) {
473 //Get day number
474 $w = $date->format('w');
475
476 //Check if weekend day
477 if ($w == 0 || $w == 6) {
478 //Date is weekend day
479 return true;
480 }
481
482 //Get date day
483 $d = $date->format('d');
484
485 //Get date month
486 $m = $date->format('m');
487
488 //Check if fixed holiday
489 if (
490 //Check if 1st january
491 ($d == 1 && $m == 1) ||
492 //Check if 1st may
493 ($d == 1 && $m == 5) ||
494 //Check if 8st may
495 ($d == 8 && $m == 5) ||
496 //Check if 14st july
497 ($d == 14 && $m == 7) ||
498 //Check if 15st august
499 ($d == 15 && $m == 8) ||
500 //Check if 1st november
501 ($d == 1 && $m == 11) ||
502 //Check if 11st november
503 ($d == 11 && $m == 11) ||
504 //Check if 25st december
505 ($d == 25 && $m == 12)
506 ) {
507 //Date is a fixed holiday
508 return true;
509 }
510
511 //Get eastern
512 $eastern = $this->getEastern($date->format('Y'));
513
514 //Check dynamic holidays
515 if (
516 (clone $eastern)->add(new \DateInterval('P1D')) == $date ||
517 (clone $eastern)->add(new \DateInterval('P39D')) == $date ||
518 (clone $eastern)->add(new \DateInterval('P50D')) == $date
519 ) {
520 //Date is a dynamic holiday
521 return true;
522 }
523
524 //Date is not a holiday and week day
525 return false;
526 }
527 }