3 namespace Rapsys\AirBundle\Controller
;
5 use Symfony\Component\HttpFoundation\Request
;
6 use Symfony\Component\HttpFoundation\Response
;
7 use Symfony\Component\Routing\RequestContext
;
8 use Symfony\Component\Form\FormError
;
9 use Symfony\Component\Routing\Exception\MethodNotAllowedException
;
10 use Symfony\Component\Routing\Exception\ResourceNotFoundException
;
11 use Rapsys\AirBundle\Entity\Slot
;
12 use Rapsys\AirBundle\Entity\User
;
13 use Rapsys\AirBundle\Entity\Session
;
14 use Rapsys\AirBundle\Entity\Application
;
16 class ApplicationController
extends DefaultController
{
20 * @desc Persist application and all required dependencies in database
22 * @param Request $request The request instance
24 * @return Response The rendered view or redirection
26 * @throws \RuntimeException When user has not at least guest role
28 public function add(Request
$request) {
29 //Prevent non-guest to access here
30 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')]));
32 //Create ApplicationType form
33 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
35 'action' => $this->generateUrl('rapsys_air_application_add'),
36 //Set the form attribute
37 #'attr' => [ 'class' => 'col' ],
39 'admin' => $this->isGranted('ROLE_ADMIN'),
40 //Set default user to current
41 'user' => $this->getUser()->getId(),
42 //Set default slot to evening
43 //XXX: default to Evening (3)
44 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3)
47 //Refill the fields in case of invalid form
48 $form->handleRequest($request);
51 if (!$form->isSubmitted() || !$form->isValid()) {
53 $section = $this->translator
->trans('Application add');
56 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
59 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
63 $doctrine = $this->getDoctrine();
66 $manager = $doctrine->getManager();
69 $data = $form->getData();
71 //Protect session fetching
74 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
75 //Catch no session case
76 } catch (\Doctrine\ORM\NoResultException
$e) {
78 $session = new Session();
79 $session->setLocation($data['location']);
80 $session->setDate($data['date']);
81 $session->setSlot($data['slot']);
82 $session->setCreated(new \
DateTime('now'));
83 $session->setUpdated(new \
DateTime('now'));
86 $short = $data['location']->getShort();
89 $slot = $data['slot']->getTitle();
92 $session->setPremium($premium = false);
94 //Check if slot is afternoon
95 //XXX: premium is stored only for Afternoon and Evening
96 if ($slot == 'Afternoon') {
98 //XXX: a session is considered premium a day off
99 $session->setPremium($premium = $this->isPremium($data['date']));
100 //Check if slot is evening
101 //XXX: premium is stored only for Afternoon and Evening
102 } elseif ($slot == 'Evening') {
104 //XXX: a session is considered premium the eve of a day off
105 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D'))));
106 //Check if slot is after
107 } elseif ($slot == 'After') {
109 //XXX: a session is considered premium the eve of a day off
110 $premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D')));
113 //Set default length at 6h
114 //XXX: date part will be truncated on save
115 $session->setLength(new \
DateTime('06:00:00'));
118 if ($this->isGranted('ROLE_ADMIN')) {
120 if ($slot == 'Morning') {
122 $session->setBegin(new \
DateTime('09:00:00'));
125 $session->setLength(new \
DateTime('05:00:00'));
127 } elseif ($slot == 'Afternoon') {
129 $session->setBegin(new \
DateTime('16:00:00'));
132 $session->setLength(new \
DateTime('03:00:00'));
134 } elseif ($slot == 'Evening') {
136 $session->setBegin(new \
DateTime('19:30:00'));
138 //Check if next day is premium
141 $session->setLength(new \
DateTime('07:00:00'));
145 $session->setLength(new \
DateTime('04:30:00'));
149 $session->setBegin(new \
DateTime('01:00:00'));
152 $session->setLength(new \
DateTime('04:00:00'));
154 //Check if next day is premium
157 $session->setBegin(new \
DateTime('02:00:00'));
160 $session->setLength(new \
DateTime('03:00:00'));
163 //Docks => 14h -> 19h | 19h -> 01/02h
164 //XXX: remove Garnier from here to switch back to 21h
165 } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
167 if ($slot == 'Afternoon') {
169 $session->setBegin(new \
DateTime('14:00:00'));
172 $session->setLength(new \
DateTime('05:00:00'));
174 } elseif ($slot == 'Evening') {
176 $session->setBegin(new \
DateTime('19:00:00'));
178 //Check if next day is premium
181 $session->setLength(new \
DateTime('07:00:00'));
186 $session->setBegin(new \
DateTime('01:00:00'));
189 $session->setLength(new \
DateTime('04:00:00'));
191 //Check if next day is premium
194 $session->setBegin(new \
DateTime('02:00:00'));
197 $session->setLength(new \
DateTime('03:00:00'));
200 //Garnier => 21h -> 01/02h
201 } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
203 if ($slot == 'Evening') {
205 $session->setBegin(new \
DateTime('21:00:00'));
208 $session->setLength(new \
DateTime('05:00:00'));
210 //Check if next day is premium
213 $session->setLength(new \
DateTime('06:00:00'));
218 $session->setBegin(new \
DateTime('01:00:00'));
221 $session->setLength(new \
DateTime('04:00:00'));
223 //Check if next day is premium
226 $session->setBegin(new \
DateTime('02:00:00'));
229 $session->setLength(new \
DateTime('03:00:00'));
232 //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
233 } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
235 if ($slot == 'Evening') {
237 $session->setBegin(new \
DateTime('19:00:00'));
239 //Check if next day is premium
242 $session->setLength(new \
DateTime('07:00:00'));
247 $session->setBegin(new \
DateTime('01:00:00'));
250 $session->setLength(new \
DateTime('04:00:00'));
252 //Check if next day is premium
255 $session->setBegin(new \
DateTime('02:00:00'));
258 $session->setLength(new \
DateTime('03:00:00'));
261 //La Villette => 14h -> 19h
262 } elseif ($short == 'Villette' && $slot == 'Afternoon') {
264 $session->setBegin(new \
DateTime('14:00:00'));
267 $session->setLength(new \
DateTime('05:00:00'));
268 //Place Colette => 14h -> 21h
269 //TODO: add check here that it's a millegaux account ?
270 } elseif ($short == 'Colette' && $slot == 'Afternoon') {
272 $session->setBegin(new \
DateTime('14:00:00'));
275 $session->setLength(new \
DateTime('07:00:00'));
276 //Galerie d'OrlƩans => 14h -> 18h
277 } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
279 $session->setBegin(new \
DateTime('14:00:00'));
282 $session->setLength(new \
DateTime('04:00:00'));
283 //Jardin du Monde => 14h -> 15h
284 } elseif ($short == 'Monde' && $slot == 'Morning') {
286 $session->setBegin(new \
DateTime('14:00:00'));
289 $session->setLength(new \
DateTime('01:00:00'));
290 //Combination not supported
292 //Add error in flash message
293 $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')]));
296 $section = $this->translator
->trans('Application add');
299 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
302 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
306 if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \
DateTime('00:00:00')) {
307 //Add error in flash message
308 $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')]));
311 $section = $this->translator
->trans('Application add');
314 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
317 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
321 $manager->persist($session);
323 //Flush to get the ids
326 $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')]));
330 $user = $this->getUser();
332 //Replace with requested user for admin
333 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
334 $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']);
337 //Protect application fetching
339 //Retrieve application
340 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user);
342 //Add warning in flash message
343 $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')]));
344 //Catch no application and session without identifier (not persisted&flushed) cases
345 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException
$e) {
346 //Create the application
347 $application = new Application();
348 $application->setSession($session);
349 $application->setUser($user);
350 $application->setCreated(new \
DateTime('now'));
351 $application->setUpdated(new \
DateTime('now'));
353 //Refresh session updated field
354 $session->setUpdated(new \
DateTime('now'));
357 $manager->persist($session);
359 //Queue application save
360 $manager->persist($application);
362 //Flush to get the ids
365 //Add notice in flash message
366 $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')]));
369 //Extract and process referer
370 if ($referer = $request->headers
->get('referer')) {
371 //Create referer request instance
372 $req = Request
::create($referer);
375 $path = $req->getPathInfo();
377 //Get referer query string
378 $query = $req->getQueryString();
381 $path = str_replace($request->getScriptName(), '', $path);
383 //Try with referer path
386 $oldContext = $this->router
->getContext();
388 //Force clean context
389 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
390 $this->router
->setContext(new RequestContext());
392 //Retrieve route matching path
393 $route = $this->router
->match($path);
396 $this->router
->setContext($oldContext);
402 $name = $route['_route'];
404 //Remove route and controller from route defaults
405 unset($route['_route'], $route['_controller']);
407 //Check if session view route
408 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
410 $route['id'] = $session->getId();
414 $route['session'] = $session->getId();
418 return $this->redirectToRoute($name, $route);
420 } catch(MethodNotAllowedException
|ResourceNotFoundException
$e) {
421 //Unset referer to fallback to default route
426 //Redirect to cleanup the form
427 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
431 * Compute eastern for selected year
433 * @param int $year The eastern year
435 * @return DateTime The eastern date
437 function getEastern($year) {
440 //Check if already computed
441 if (isset($data[$year])) {
442 //Return computed eastern
444 //Check if data is null
445 } elseif (is_null($data)) {
449 $d = (19 * ($year %
19) +
24) %
30;
450 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
458 } elseif ($d == 29 && $e == 6) {
461 } elseif ($d == 28 && $e == 6) {
466 //Store eastern in data
467 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
471 * Check if date is a premium day
473 * @desc Consider as premium a day off
475 * @param DateTime $date The date to check
476 * @return bool Whether the date is off or not
478 function isPremium($date) {
480 $w = $date->format('w');
482 //Check if weekend day
483 if ($w == 0 || $w == 6) {
484 //Date is weekend day
489 $d = $date->format('d');
492 $m = $date->format('m');
494 //Check if fixed holiday
496 //Check if 1st january
497 ($d == 1 && $m == 1) ||
499 ($d == 1 && $m == 5) ||
501 ($d == 8 && $m == 5) ||
503 ($d == 14 && $m == 7) ||
504 //Check if 15st august
505 ($d == 15 && $m == 8) ||
506 //Check if 1st november
507 ($d == 1 && $m == 11) ||
508 //Check if 11st november
509 ($d == 11 && $m == 11) ||
510 //Check if 25st december
511 ($d == 25 && $m == 12)
513 //Date is a fixed holiday
518 $eastern = $this->getEastern($date->format('Y'));
520 //Check dynamic holidays
522 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
523 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
524 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
526 //Date is a dynamic holiday
530 //Date is not a holiday and week day