3 namespace Rapsys\AirBundle\Controller
;
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
;
15 class ApplicationController
extends DefaultController
{
19 * @desc Persist application and all required dependencies in database
21 * @param Request $request The request instance
23 * @return Response The rendered view or redirection
25 * @throws \RuntimeException When user has not at least guest role
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')]));
31 //Reject non post requests
32 if (!$request->isMethod('POST')) {
33 throw new \
RuntimeException('Request method MUST be POST');
36 //Create ApplicationType form
37 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
39 'action' => $this->generateUrl('rapsys_air_application_add'),
40 //Set the form attribute
41 #'attr' => [ 'class' => 'col' ],
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)
51 //Refill the fields in case of invalid form
52 $form->handleRequest($request);
55 if (!$form->isValid()) {
57 $section = $this->translator
->trans('Application add');
60 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
63 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
67 $doctrine = $this->getDoctrine();
70 $manager = $doctrine->getManager();
73 $data = $form->getData();
75 //Protect session fetching
78 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
79 //Catch no session case
80 } catch (\Doctrine\ORM\NoResultException
$e) {
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'));
90 $short = $data['location']->getShort();
93 $slot = $data['slot']->getTitle();
96 $session->setPremium($premium = false);
98 //Check if slot is afternoon
99 //XXX: premium is stored only for Afternoon and Evening
100 if ($slot == 'Afternoon') {
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') {
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') {
113 //XXX: a session is considered premium the eve of a day off
114 $premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D')));
117 //Set default length at 6h
118 //XXX: date part will be truncated on save
119 $session->setLength(new \
DateTime('06:00:00'));
122 if ($this->isGranted('ROLE_ADMIN')) {
124 if ($slot == 'Morning') {
126 $session->setBegin(new \
DateTime('09:00:00'));
129 $session->setLength(new \
DateTime('05:00:00'));
131 } elseif ($slot == 'Afternoon') {
133 $session->setBegin(new \
DateTime('14:00:00'));
136 $session->setLength(new \
DateTime('05:00:00'));
138 } elseif ($slot == 'Evening') {
140 $session->setBegin(new \
DateTime('19:00:00'));
142 //Check if next day is premium
145 $session->setLength(new \
DateTime('07:00:00'));
150 $session->setBegin(new \
DateTime('01:00:00'));
153 $session->setLength(new \
DateTime('04:00:00'));
155 //Check if next day is premium
158 $session->setBegin(new \
DateTime('02:00:00'));
161 $session->setLength(new \
DateTime('03:00:00'));
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'])) {
168 if ($slot == 'Afternoon') {
170 $session->setBegin(new \
DateTime('14:00:00'));
173 $session->setLength(new \
DateTime('05:00:00'));
175 } elseif ($slot == 'Evening') {
177 $session->setBegin(new \
DateTime('19:00:00'));
179 //Check if next day is premium
182 $session->setLength(new \
DateTime('07:00:00'));
187 $session->setBegin(new \
DateTime('01:00:00'));
190 $session->setLength(new \
DateTime('04:00:00'));
192 //Check if next day is premium
195 $session->setBegin(new \
DateTime('02:00:00'));
198 $session->setLength(new \
DateTime('03:00:00'));
201 //Garnier => 21h -> 01/02h
202 } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
204 if ($slot == 'Evening') {
206 $session->setBegin(new \
DateTime('21:00:00'));
209 $session->setLength(new \
DateTime('05:00:00'));
211 //Check if next day is premium
214 $session->setLength(new \
DateTime('06:00:00'));
219 $session->setBegin(new \
DateTime('01:00:00'));
222 $session->setLength(new \
DateTime('04:00:00'));
224 //Check if next day is premium
227 $session->setBegin(new \
DateTime('02:00:00'));
230 $session->setLength(new \
DateTime('03:00:00'));
233 //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
234 } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
236 if ($slot == 'Evening') {
238 $session->setBegin(new \
DateTime('19:00:00'));
240 //Check if next day is premium
243 $session->setLength(new \
DateTime('07:00:00'));
248 $session->setBegin(new \
DateTime('01:00:00'));
251 $session->setLength(new \
DateTime('04:00:00'));
253 //Check if next day is premium
256 $session->setBegin(new \
DateTime('02:00:00'));
259 $session->setLength(new \
DateTime('03:00:00'));
262 //La Villette => 14h -> 19h
263 } elseif ($short == 'Villette' && $slot == 'Afternoon') {
265 $session->setBegin(new \
DateTime('14:00:00'));
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') {
273 $session->setBegin(new \
DateTime('14:00:00'));
276 $session->setLength(new \
DateTime('07:00:00'));
277 //Galerie d'OrlƩans => 14h -> 18h
278 } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
280 $session->setBegin(new \
DateTime('14:00:00'));
283 $session->setLength(new \
DateTime('04:00:00'));
284 //Combination not supported
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')]));
290 $section = $this->translator
->trans('Application add');
293 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
296 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
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')]));
305 $section = $this->translator
->trans('Application add');
308 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
311 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
315 $manager->persist($session);
317 //Flush to get the ids
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')]));
324 $user = $this->getUser();
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']);
331 //Protect application fetching
333 //Retrieve application
334 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user);
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'));
347 //Refresh session updated field
348 $session->setUpdated(new \
DateTime('now'));
351 $manager->persist($session);
353 //Queue application save
354 $manager->persist($application);
356 //Flush to get the ids
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')]));
363 //Extract and process referer
364 if ($referer = $request->headers
->get('referer')) {
365 //Create referer request instance
366 $req = Request
::create($referer);
369 $path = $req->getPathInfo();
371 //Get referer query string
372 $query = $req->getQueryString();
375 $path = str_replace($request->getScriptName(), '', $path);
377 //Try with referer path
380 $oldContext = $this->router
->getContext();
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());
386 //Retrieve route matching path
387 $route = $this->router
->match($path);
390 $this->router
->setContext($oldContext);
396 $name = $route['_route'];
398 //Remove route and controller from route defaults
399 unset($route['_route'], $route['_controller']);
401 //Check if session view route
402 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
404 $route['id'] = $session->getId();
408 $route['session'] = $session->getId();
412 return $this->redirectToRoute($name, $route);
414 } catch(MethodNotAllowedException
|ResourceNotFoundException
$e) {
415 //Unset referer to fallback to default route
420 //Redirect to cleanup the form
421 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
425 * Compute eastern for selected year
427 * @param int $year The eastern year
429 * @return DateTime The eastern date
431 function getEastern($year) {
434 //Check if already computed
435 if (isset($data[$year])) {
436 //Return computed eastern
438 //Check if data is null
439 } elseif (is_null($data)) {
443 $d = (19 * ($year %
19) +
24) %
30;
444 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
452 } elseif ($d == 29 && $e == 6) {
455 } elseif ($d == 28 && $e == 6) {
460 //Store eastern in data
461 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
465 * Check if date is a premium day
467 * @desc Consider as premium a day off
469 * @param DateTime $date The date to check
470 * @return bool Whether the date is off or not
472 function isPremium($date) {
474 $w = $date->format('w');
476 //Check if weekend day
477 if ($w == 0 || $w == 6) {
478 //Date is weekend day
483 $d = $date->format('d');
486 $m = $date->format('m');
488 //Check if fixed holiday
490 //Check if 1st january
491 ($d == 1 && $m == 1) ||
493 ($d == 1 && $m == 5) ||
495 ($d == 8 && $m == 5) ||
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)
507 //Date is a fixed holiday
512 $eastern = $this->getEastern($date->format('Y'));
514 //Check dynamic holidays
516 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
517 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
518 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
520 //Date is a dynamic holiday
524 //Date is not a holiday and week day