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 //Create ApplicationType form
32 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
34 'action' => $this->generateUrl('rapsys_air_application_add'),
35 //Set the form attribute
36 #'attr' => [ 'class' => 'col' ],
38 'admin' => $this->isGranted('ROLE_ADMIN'),
39 //Set default user to current
40 'user' => $this->getUser()->getId(),
41 //Set default slot to evening
42 //XXX: default to Evening (3)
43 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3)
46 //Refill the fields in case of invalid form
47 $form->handleRequest($request);
50 if (!$form->isSubmitted() || !$form->isValid()) {
52 $section = $this->translator
->trans('Application add');
55 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
58 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
62 $doctrine = $this->getDoctrine();
65 $manager = $doctrine->getManager();
68 $data = $form->getData();
70 //Protect session fetching
73 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
74 //Catch no session case
75 } catch (\Doctrine\ORM\NoResultException
$e) {
77 $session = new Session();
78 $session->setLocation($data['location']);
79 $session->setDate($data['date']);
80 $session->setSlot($data['slot']);
81 $session->setCreated(new \
DateTime('now'));
82 $session->setUpdated(new \
DateTime('now'));
85 $short = $data['location']->getShort();
88 $slot = $data['slot']->getTitle();
91 $session->setPremium($premium = false);
93 //Check if slot is afternoon
94 //XXX: premium is stored only for Afternoon and Evening
95 if ($slot == 'Afternoon') {
97 //XXX: a session is considered premium a day off
98 $session->setPremium($premium = $this->isPremium($data['date']));
99 //Check if slot is evening
100 //XXX: premium is stored only for Afternoon and Evening
101 } elseif ($slot == 'Evening') {
103 //XXX: a session is considered premium the eve of a day off
104 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D'))));
105 //Check if slot is after
106 } elseif ($slot == 'After') {
108 //XXX: a session is considered premium the eve of a day off
109 $premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D')));
112 //Set default length at 6h
113 //XXX: date part will be truncated on save
114 $session->setLength(new \
DateTime('06:00:00'));
117 if ($this->isGranted('ROLE_ADMIN')) {
119 if ($slot == 'Morning') {
121 $session->setBegin(new \
DateTime('09:00:00'));
124 $session->setLength(new \
DateTime('05:00:00'));
126 } elseif ($slot == 'Afternoon') {
128 $session->setBegin(new \
DateTime('14:00:00'));
131 $session->setLength(new \
DateTime('05:00:00'));
133 } elseif ($slot == 'Evening') {
135 $session->setBegin(new \
DateTime('19:00:00'));
137 //Check if next day is premium
140 $session->setLength(new \
DateTime('07:00:00'));
145 $session->setBegin(new \
DateTime('01:00:00'));
148 $session->setLength(new \
DateTime('04:00:00'));
150 //Check if next day is premium
153 $session->setBegin(new \
DateTime('02:00:00'));
156 $session->setLength(new \
DateTime('03:00:00'));
159 //Docks => 14h -> 19h | 19h -> 01/02h
160 //XXX: remove Garnier from here to switch back to 21h
161 } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
163 if ($slot == 'Afternoon') {
165 $session->setBegin(new \
DateTime('14:00:00'));
168 $session->setLength(new \
DateTime('05:00:00'));
170 } elseif ($slot == 'Evening') {
172 $session->setBegin(new \
DateTime('19:00:00'));
174 //Check if next day is premium
177 $session->setLength(new \
DateTime('07:00:00'));
182 $session->setBegin(new \
DateTime('01:00:00'));
185 $session->setLength(new \
DateTime('04:00:00'));
187 //Check if next day is premium
190 $session->setBegin(new \
DateTime('02:00:00'));
193 $session->setLength(new \
DateTime('03:00:00'));
196 //Garnier => 21h -> 01/02h
197 } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
199 if ($slot == 'Evening') {
201 $session->setBegin(new \
DateTime('21:00:00'));
204 $session->setLength(new \
DateTime('05:00:00'));
206 //Check if next day is premium
209 $session->setLength(new \
DateTime('06:00:00'));
214 $session->setBegin(new \
DateTime('01:00:00'));
217 $session->setLength(new \
DateTime('04:00:00'));
219 //Check if next day is premium
222 $session->setBegin(new \
DateTime('02:00:00'));
225 $session->setLength(new \
DateTime('03:00:00'));
228 //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
229 } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
231 if ($slot == 'Evening') {
233 $session->setBegin(new \
DateTime('19:00:00'));
235 //Check if next day is premium
238 $session->setLength(new \
DateTime('07:00:00'));
243 $session->setBegin(new \
DateTime('01:00:00'));
246 $session->setLength(new \
DateTime('04:00:00'));
248 //Check if next day is premium
251 $session->setBegin(new \
DateTime('02:00:00'));
254 $session->setLength(new \
DateTime('03:00:00'));
257 //La Villette => 14h -> 19h
258 } elseif ($short == 'Villette' && $slot == 'Afternoon') {
260 $session->setBegin(new \
DateTime('14:00:00'));
263 $session->setLength(new \
DateTime('05:00:00'));
264 //Place Colette => 14h -> 21h
265 //TODO: add check here that it's a millegaux account ?
266 } elseif ($short == 'Colette' && $slot == 'Afternoon') {
268 $session->setBegin(new \
DateTime('14:00:00'));
271 $session->setLength(new \
DateTime('07:00:00'));
272 //Galerie d'OrlƩans => 14h -> 18h
273 } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
275 $session->setBegin(new \
DateTime('14:00:00'));
278 $session->setLength(new \
DateTime('04:00:00'));
279 //Combination not supported
281 //Add error in flash message
282 $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')]));
285 $section = $this->translator
->trans('Application add');
288 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
291 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
295 if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \
DateTime('00:00:00')) {
296 //Add error in flash message
297 $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')]));
300 $section = $this->translator
->trans('Application add');
303 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
306 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
);
310 $manager->persist($session);
312 //Flush to get the ids
315 $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')]));
319 $user = $this->getUser();
321 //Replace with requested user for admin
322 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
323 $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']);
326 //Protect application fetching
328 //Retrieve application
329 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user);
331 //Add warning in flash message
332 $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')]));
333 //Catch no application and session without identifier (not persisted&flushed) cases
334 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException
$e) {
335 //Create the application
336 $application = new Application();
337 $application->setSession($session);
338 $application->setUser($user);
339 $application->setCreated(new \
DateTime('now'));
340 $application->setUpdated(new \
DateTime('now'));
342 //Refresh session updated field
343 $session->setUpdated(new \
DateTime('now'));
346 $manager->persist($session);
348 //Queue application save
349 $manager->persist($application);
351 //Flush to get the ids
354 //Add notice in flash message
355 $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')]));
358 //Extract and process referer
359 if ($referer = $request->headers
->get('referer')) {
360 //Create referer request instance
361 $req = Request
::create($referer);
364 $path = $req->getPathInfo();
366 //Get referer query string
367 $query = $req->getQueryString();
370 $path = str_replace($request->getScriptName(), '', $path);
372 //Try with referer path
375 $oldContext = $this->router
->getContext();
377 //Force clean context
378 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
379 $this->router
->setContext(new RequestContext());
381 //Retrieve route matching path
382 $route = $this->router
->match($path);
385 $this->router
->setContext($oldContext);
391 $name = $route['_route'];
393 //Remove route and controller from route defaults
394 unset($route['_route'], $route['_controller']);
396 //Check if session view route
397 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
399 $route['id'] = $session->getId();
403 $route['session'] = $session->getId();
407 return $this->redirectToRoute($name, $route);
409 } catch(MethodNotAllowedException
|ResourceNotFoundException
$e) {
410 //Unset referer to fallback to default route
415 //Redirect to cleanup the form
416 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
420 * Compute eastern for selected year
422 * @param int $year The eastern year
424 * @return DateTime The eastern date
426 function getEastern($year) {
429 //Check if already computed
430 if (isset($data[$year])) {
431 //Return computed eastern
433 //Check if data is null
434 } elseif (is_null($data)) {
438 $d = (19 * ($year %
19) +
24) %
30;
439 $e = (2 * ($year %
4) +
4 * ($year %
7) +
6 * $d +
5) %
7;
447 } elseif ($d == 29 && $e == 6) {
450 } elseif ($d == 28 && $e == 6) {
455 //Store eastern in data
456 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
460 * Check if date is a premium day
462 * @desc Consider as premium a day off
464 * @param DateTime $date The date to check
465 * @return bool Whether the date is off or not
467 function isPremium($date) {
469 $w = $date->format('w');
471 //Check if weekend day
472 if ($w == 0 || $w == 6) {
473 //Date is weekend day
478 $d = $date->format('d');
481 $m = $date->format('m');
483 //Check if fixed holiday
485 //Check if 1st january
486 ($d == 1 && $m == 1) ||
488 ($d == 1 && $m == 5) ||
490 ($d == 8 && $m == 5) ||
492 ($d == 14 && $m == 7) ||
493 //Check if 15st august
494 ($d == 15 && $m == 8) ||
495 //Check if 1st november
496 ($d == 1 && $m == 11) ||
497 //Check if 11st november
498 ($d == 11 && $m == 11) ||
499 //Check if 25st december
500 ($d == 25 && $m == 12)
502 //Date is a fixed holiday
507 $eastern = $this->getEastern($date->format('Y'));
509 //Check dynamic holidays
511 (clone $eastern)->add(new \
DateInterval('P1D')) == $date ||
512 (clone $eastern)->add(new \
DateInterval('P39D')) == $date ||
513 (clone $eastern)->add(new \
DateInterval('P50D')) == $date
515 //Date is a dynamic holiday
519 //Date is not a holiday and week day