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('14:00:00'));
132 $session->setLength(new \
DateTime('05:00:00'));
134 } elseif ($slot == 'Evening') {
136 $session->setBegin(new \
DateTime('19:00:00'));
138 //Check if next day is premium
141 $session->setLength(new \
DateTime('07:00:00'));
146 $session->setBegin(new \
DateTime('01:00:00'));
149 $session->setLength(new \
DateTime('04:00:00'));
151 //Check if next day is premium
154 $session->setBegin(new \
DateTime('02:00:00'));
157 $session->setLength(new \
DateTime('03:00:00'));
160 //Docks => 14h -> 19h | 19h -> 01/02h
161 //XXX: remove Garnier from here to switch back to 21h
162 } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
164 if ($slot == 'Afternoon') {
166 $session->setBegin(new \
DateTime('14:00:00'));
169 $session->setLength(new \
DateTime('05:00:00'));
171 } elseif ($slot == 'Evening') {
173 $session->setBegin(new \
DateTime('19:00:00'));
175 //Check if next day is premium
178 $session->setLength(new \
DateTime('07:00:00'));
183 $session->setBegin(new \
DateTime('01:00:00'));
186 $session->setLength(new \
DateTime('04:00:00'));
188 //Check if next day is premium
191 $session->setBegin(new \
DateTime('02:00:00'));
194 $session->setLength(new \
DateTime('03:00:00'));
197 //Garnier => 21h -> 01/02h
198 } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
200 if ($slot == 'Evening') {
202 $session->setBegin(new \
DateTime('21:00:00'));
205 $session->setLength(new \
DateTime('05:00:00'));
207 //Check if next day is premium
210 $session->setLength(new \
DateTime('06:00:00'));
215 $session->setBegin(new \
DateTime('01:00:00'));
218 $session->setLength(new \
DateTime('04:00:00'));
220 //Check if next day is premium
223 $session->setBegin(new \
DateTime('02:00:00'));
226 $session->setLength(new \
DateTime('03:00:00'));
229 //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
230 } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
232 if ($slot == 'Evening') {
234 $session->setBegin(new \
DateTime('19:00:00'));
236 //Check if next day is premium
239 $session->setLength(new \
DateTime('07:00:00'));
244 $session->setBegin(new \
DateTime('01:00:00'));
247 $session->setLength(new \
DateTime('04:00:00'));
249 //Check if next day is premium
252 $session->setBegin(new \
DateTime('02:00:00'));
255 $session->setLength(new \
DateTime('03:00:00'));
258 //La Villette => 14h -> 19h
259 } elseif ($short == 'Villette' && $slot == 'Afternoon') {
261 $session->setBegin(new \
DateTime('14:00:00'));
264 $session->setLength(new \
DateTime('05:00:00'));
265 //Place Colette => 14h -> 21h
266 //TODO: add check here that it's a millegaux account ?
267 } elseif ($short == 'Colette' && $slot == 'Afternoon') {
269 $session->setBegin(new \
DateTime('14:00:00'));
272 $session->setLength(new \
DateTime('07:00:00'));
273 //Galerie d'OrlƩans => 14h -> 18h
274 } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
276 $session->setBegin(new \
DateTime('14:00:00'));
279 $session->setLength(new \
DateTime('04:00:00'));
280 //Combination not supported
282 //Add error in flash message
283 $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')]));
286 $section = $this->translator
->trans('Application add');
289 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
292 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
296 if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \
DateTime('00:00:00')) {
297 //Add error in flash message
298 $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')]));
301 $section = $this->translator
->trans('Application add');
304 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
307 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
311 $manager->persist($session);
313 //Flush to get the ids
316 $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')]));
320 $user = $this->getUser();
322 //Replace with requested user for admin
323 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
324 $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']);
327 //Protect application fetching
329 //Retrieve application
330 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user);
332 //Add warning in flash message
333 $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')]));
334 //Catch no application and session without identifier (not persisted&flushed) cases
335 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException
$e) {
336 //Create the application
337 $application = new Application();
338 $application->setSession($session);
339 $application->setUser($user);
340 $application->setCreated(new \
DateTime('now'));
341 $application->setUpdated(new \
DateTime('now'));
343 //Refresh session updated field
344 $session->setUpdated(new \
DateTime('now'));
347 $manager->persist($session);
349 //Queue application save
350 $manager->persist($application);
352 //Flush to get the ids
355 //Add notice in flash message
356 $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')]));
359 //Extract and process referer
360 if ($referer = $request->headers
->get('referer')) {
361 //Create referer request instance
362 $req = Request
::create($referer);
365 $path = $req->getPathInfo();
367 //Get referer query string
368 $query = $req->getQueryString();
371 $path = str_replace($request->getScriptName(), '', $path);
373 //Try with referer path
376 $oldContext = $this->router
->getContext();
378 //Force clean context
379 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
380 $this->router
->setContext(new RequestContext());
382 //Retrieve route matching path
383 $route = $this->router
->match($path);
386 $this->router
->setContext($oldContext);
392 $name = $route['_route'];
394 //Remove route and controller from route defaults
395 unset($route['_route'], $route['_controller']);
397 //Check if session view route
398 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
400 $route['id'] = $session->getId();
404 $route['session'] = $session->getId();
408 return $this->redirectToRoute($name, $route);
410 } catch(MethodNotAllowedException
|ResourceNotFoundException
$e) {
411 //Unset referer to fallback to default route
416 //Redirect to cleanup the form
417 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
421 * Compute eastern for selected year
423 * @param int $year The eastern year
425 * @return DateTime The eastern date
427 function getEastern($year) {
430 //Check if already computed
431 if (isset($data[$year])) {
432 //Return computed eastern
434 //Check if data is null
435 } elseif (is_null($data)) {
439 $d = (19 * ($year %
19) +
24) %
30;
440 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
448 } elseif ($d == 29 && $e == 6) {
451 } elseif ($d == 28 && $e == 6) {
456 //Store eastern in data
457 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
461 * Check if date is a premium day
463 * @desc Consider as premium a day off
465 * @param DateTime $date The date to check
466 * @return bool Whether the date is off or not
468 function isPremium($date) {
470 $w = $date->format('w');
472 //Check if weekend day
473 if ($w == 0 || $w == 6) {
474 //Date is weekend day
479 $d = $date->format('d');
482 $m = $date->format('m');
484 //Check if fixed holiday
486 //Check if 1st january
487 ($d == 1 && $m == 1) ||
489 ($d == 1 && $m == 5) ||
491 ($d == 8 && $m == 5) ||
493 ($d == 14 && $m == 7) ||
494 //Check if 15st august
495 ($d == 15 && $m == 8) ||
496 //Check if 1st november
497 ($d == 1 && $m == 11) ||
498 //Check if 11st november
499 ($d == 11 && $m == 11) ||
500 //Check if 25st december
501 ($d == 25 && $m == 12)
503 //Date is a fixed holiday
508 $eastern = $this->getEastern($date->format('Y'));
510 //Check dynamic holidays
512 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
513 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
514 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
516 //Date is a dynamic holiday
520 //Date is not a holiday and week day