]> Raphaël G. Git Repositories - airbundle/blobdiff - Controller/ApplicationController.php
Allow phone field to be optional
[airbundle] / Controller / ApplicationController.php
index cf7e7ceeece7b3f4345599d86dbea8c6910d14e6..a6091cd0f1ba9ee2e60d3ad351662ffb351d4622 100644 (file)
@@ -3,6 +3,11 @@
 namespace Rapsys\AirBundle\Controller;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\RequestContext;
+use Symfony\Component\Form\FormError;
+use Symfony\Component\Routing\Exception\MethodNotAllowedException;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
 use Rapsys\AirBundle\Entity\Slot;
 use Rapsys\AirBundle\Entity\User;
 use Rapsys\AirBundle\Entity\Session;
@@ -39,24 +44,19 @@ class ApplicationController extends DefaultController {
                        'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
                ]);
 
-               //Reject non post requests
-               if (!$request->isMethod('POST')) {
-                       throw new \RuntimeException('Request method MUST be POST');
-               }
-
                //Refill the fields in case of invalid form
                $form->handleRequest($request);
 
                //Handle invalid form
-               if (!$form->isValid()) {
+               if (!$form->isSubmitted() || !$form->isValid()) {
                        //Set section
-                       $section = $this->translator->trans('Application Add');
+                       $section = $this->translator->trans('Application add');
 
                        //Set title
-                       $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
+                       $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
 
                        //Render the view
-                       return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form]+$this->context);
+                       return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
                }
 
                //Get doctrine
@@ -82,6 +82,241 @@ class ApplicationController extends DefaultController {
                        $session->setCreated(new \DateTime('now'));
                        $session->setUpdated(new \DateTime('now'));
 
+                       //Get short location
+                       $short = $data['location']->getShort();
+
+                       //Get slot
+                       $slot = $data['slot']->getTitle();
+
+                       //Set premium
+                       $session->setPremium($premium = false);
+
+                       //Check if slot is afternoon
+                       //XXX: premium is stored only for Afternoon and Evening
+                       if ($slot == 'Afternoon') {
+                               //Compute premium
+                               //XXX: a session is considered premium a day off
+                               $session->setPremium($premium = $this->isPremium($data['date']));
+                       //Check if slot is evening
+                       //XXX: premium is stored only for Afternoon and Evening
+                       } elseif ($slot == 'Evening') {
+                               //Compute premium
+                               //XXX: a session is considered premium the eve of a day off
+                               $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \DateInterval('P1D'))));
+                       //Check if slot is after
+                       } elseif ($slot == 'After') {
+                               //Compute premium
+                               //XXX: a session is considered premium the eve of a day off
+                               $premium = $this->isPremium((clone $data['date'])->add(new \DateInterval('P1D')));
+                       }
+
+                       //Set default length at 6h
+                       //XXX: date part will be truncated on save
+                       $session->setLength(new \DateTime('06:00:00'));
+
+                       //Check if admin
+                       if ($this->isGranted('ROLE_ADMIN')) {
+                               //Check if morning
+                               if ($slot == 'Morning') {
+                                       //Set begin at 9h
+                                       $session->setBegin(new \DateTime('09:00:00'));
+
+                                       //Set length at 5h
+                                       $session->setLength(new \DateTime('05:00:00'));
+                               //Check if afternoon
+                               } elseif ($slot == 'Afternoon') {
+                                       //Set begin at 16h
+                                       $session->setBegin(new \DateTime('16:00:00'));
+
+                                       //Set length at 3h
+                                       $session->setLength(new \DateTime('03:00:00'));
+                               //Check if evening
+                               } elseif ($slot == 'Evening') {
+                                       //Set begin at 19h
+                                       $session->setBegin(new \DateTime('19:30:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set length at 7h
+                                               $session->setLength(new \DateTime('07:00:00'));
+                                       }
+
+                                       //Set length at 4h
+                                       $session->setLength(new \DateTime('04:30:00'));
+                               //Check if after
+                               } else {
+                                       //Set begin at 1h
+                                       $session->setBegin(new \DateTime('01:00:00'));
+
+                                       //Set length at 4h
+                                       $session->setLength(new \DateTime('04:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set begin at 2h
+                                               $session->setBegin(new \DateTime('02:00:00'));
+
+                                               //Set length at 3h
+                                               $session->setLength(new \DateTime('03:00:00'));
+                                       }
+                               }
+                       //Docks => 14h -> 19h | 19h -> 01/02h
+                       //XXX: remove Garnier from here to switch back to 21h
+                       } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
+                               //Check if afternoon
+                               if ($slot == 'Afternoon') {
+                                       //Set begin at 14h
+                                       $session->setBegin(new \DateTime('14:00:00'));
+
+                                       //Set length at 5h
+                                       $session->setLength(new \DateTime('05:00:00'));
+                               //Check if evening
+                               } elseif ($slot == 'Evening') {
+                                       //Set begin at 19h
+                                       $session->setBegin(new \DateTime('19:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set length at 7h
+                                               $session->setLength(new \DateTime('07:00:00'));
+                                       }
+                               //Check if after
+                               } else {
+                                       //Set begin at 1h
+                                       $session->setBegin(new \DateTime('01:00:00'));
+
+                                       //Set length at 4h
+                                       $session->setLength(new \DateTime('04:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set begin at 2h
+                                               $session->setBegin(new \DateTime('02:00:00'));
+
+                                               //Set length at 3h
+                                               $session->setLength(new \DateTime('03:00:00'));
+                                       }
+                               }
+                       //Garnier => 21h -> 01/02h
+                       } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) {
+                               //Check if evening
+                               if ($slot == 'Evening') {
+                                       //Set begin at 21h
+                                       $session->setBegin(new \DateTime('21:00:00'));
+
+                                       //Set length at 5h
+                                       $session->setLength(new \DateTime('05:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set length at 6h
+                                               $session->setLength(new \DateTime('06:00:00'));
+                                       }
+                               //Check if after
+                               } else {
+                                       //Set begin at 1h
+                                       $session->setBegin(new \DateTime('01:00:00'));
+
+                                       //Set length at 4h
+                                       $session->setLength(new \DateTime('04:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set begin at 2h
+                                               $session->setBegin(new \DateTime('02:00:00'));
+
+                                               //Set length at 3h
+                                               $session->setLength(new \DateTime('03:00:00'));
+                                       }
+                               }
+                       //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h
+                       } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) {
+                               //Check if evening
+                               if ($slot == 'Evening') {
+                                       //Set begin at 19h
+                                       $session->setBegin(new \DateTime('19:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set length at 7h
+                                               $session->setLength(new \DateTime('07:00:00'));
+                                       }
+                               //Check if after
+                               } else {
+                                       //Set begin at 1h
+                                       $session->setBegin(new \DateTime('01:00:00'));
+
+                                       //Set length at 4h
+                                       $session->setLength(new \DateTime('04:00:00'));
+
+                                       //Check if next day is premium
+                                       if ($premium) {
+                                               //Set begin at 2h
+                                               $session->setBegin(new \DateTime('02:00:00'));
+
+                                               //Set length at 3h
+                                               $session->setLength(new \DateTime('03:00:00'));
+                                       }
+                               }
+                       //La Villette => 14h -> 19h
+                       } elseif ($short == 'Villette' && $slot == 'Afternoon') {
+                               //Set begin at 14h
+                               $session->setBegin(new \DateTime('14:00:00'));
+
+                               //Set length at 5h
+                               $session->setLength(new \DateTime('05:00:00'));
+                       //Place Colette => 14h -> 21h
+                       //TODO: add check here that it's a millegaux account ?
+                       } elseif ($short == 'Colette' && $slot == 'Afternoon') {
+                               //Set begin at 14h
+                               $session->setBegin(new \DateTime('14:00:00'));
+
+                               //Set length at 7h
+                               $session->setLength(new \DateTime('07:00:00'));
+                       //Galerie d'Orléans => 14h -> 18h
+                       } elseif ($short == 'Orleans' && $slot == 'Afternoon') {
+                               //Set begin at 14h
+                               $session->setBegin(new \DateTime('14:00:00'));
+
+                               //Set length at 4h
+                               $session->setLength(new \DateTime('04:00:00'));
+                       //Jardin du Monde => 14h -> 15h
+                       } elseif ($short == 'Monde' && $slot == 'Morning') {
+                               //Set begin at 14h
+                               $session->setBegin(new \DateTime('14:00:00'));
+
+                               //Set length at 4h
+                               $session->setLength(new \DateTime('01:00:00'));
+                       //Combination not supported
+                       } else {
+                               //Add error in flash message
+                               $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')]));
+
+                               //Set section
+                               $section = $this->translator->trans('Application add');
+
+                               //Set title
+                               $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
+
+                               //Render the view
+                               return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
+                       }
+
+                       //Check if admin
+                       if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \DateTime('00:00:00')) {
+                               //Add error in flash message
+                               $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')]));
+
+                               //Set section
+                               $section = $this->translator->trans('Application add');
+
+                               //Set title
+                               $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
+
+                               //Render the view
+                               return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context);
+                       }
+
                        //Queue session save
                        $manager->persist($session);
 
@@ -104,14 +339,8 @@ class ApplicationController extends DefaultController {
                        //Retrieve application
                        $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
 
-                       //Add notice in flash message
-                       //TODO: set warning about application already exists bla bla bla...
-                       #$this->addFlash('notice', $this->translator->trans('Application request the %date% for %location% on the slot %slot% saved', ['%location%' => $data['location']->getTitle(), '%slot%' => $data['slot']->getTitle(), '%date%' => $data['date']->format('Y-m-d')]));
-
-                       //Add error message to mail field
-                       #$form->get('slot')->addError(new FormError($this->translator->trans('Application already exists')));
-
-                       //TODO: redirect anyway on uri with application highlighted
+                       //Add warning in flash message
+                       $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')]));
                //Catch no application and session without identifier (not persisted&flushed) cases
                } catch (\Doctrine\ORM\NoResultException|\Doctrine\ORM\ORMInvalidArgumentException $e) {
                        //Create the application
@@ -131,23 +360,11 @@ class ApplicationController extends DefaultController {
                        $manager->persist($application);
 
                        //Flush to get the ids
-                       #$manager->flush();
+                       $manager->flush();
 
                        //Add notice in flash message
                        $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')]));
                }
-               
-               //Try unshort return field
-               if (
-                       !empty($data['return']) &&
-                       ($unshort = $this->slugger->unshort($data['return'])) &&
-                       ($route = json_decode($unshort, true)) !== null
-               ) {
-                       $return = $this->generateUrl($route['_route'], ['session' => $session->getId()?:1]+$route['_route_params']);
-               }
-
-               //XXX: Debug
-               header('Content-Type: text/plain');
 
                //Extract and process referer
                if ($referer = $request->headers->get('referer')) {
@@ -165,174 +382,152 @@ class ApplicationController extends DefaultController {
 
                        //Try with referer path
                        try {
-                               var_dump($this->router);
-                               exit;
-                               var_dump($path = '/location');
-                               var_dump($this->router->match($path));
-                               var_dump($path = '/fr/emplacement');
-                               exit;
-                               var_dump($this->router->match());
-                               exit;
-                               var_dump($path);
-                               var_dump($query);
-                               exit;
+                               //Save old context
+                               $oldContext = $this->router->getContext();
+
+                               //Force clean context
+                               //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
+                               $this->router->setContext(new RequestContext());
+
                                //Retrieve route matching path
                                $route = $this->router->match($path);
-                               var_dump($route);
-                               exit;
 
-                               //Verify that it differ from current one
-                               if (($name = $route['_route']) == $logout) {
-                                       throw new ResourceNotFoundException('Identical referer and logout route');
-                               }
+                               //Reset context
+                               $this->router->setContext($oldContext);
+
+                               //Clear old context
+                               unset($oldContext);
+
+                               //Extract name
+                               $name = $route['_route'];
 
                                //Remove route and controller from route defaults
                                unset($route['_route'], $route['_controller']);
 
+                               //Check if session view route
+                               if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
+                                       //Replace id
+                                       $route['id'] = $session->getId();
+                               //Other routes
+                               } else {
+                                       //Set session
+                                       $route['session'] = $session->getId();
+                               }
+
                                //Generate url
-                               $url = $this->router->generate($name, $route);
+                               return $this->redirectToRoute($name, $route);
                        //No route matched
-                       } catch(ResourceNotFoundException $e) {
+                       } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
                                //Unset referer to fallback to default route
                                unset($referer);
                        }
                }
-               var_dump($request->headers->get('referer'));
-               #var_dump($request->get('_route'));
-
-               var_dump($return);
-               exit;
 
-               //Fetch slugger helper
-               $slugger = $this->get('rapsys.slugger');
-
-               var_dump($short = $slugger->short(json_encode(['_route' => $request->get('_route'), '_route_params' => $request->get('_route_params')])));
-               $short[12] = 'T';
-               var_dump($ret = json_decode($slugger->unshort($short), true));
-               var_dump($ret);
-               var_dump($this->generateUrl($ret['_route'], $ret['_route_params']));
-               #var_dump(json_decode($slugger->unshort($data['return'])));
-               #var_dump($application->getId());
-               exit;
+               //Redirect to cleanup the form
+               return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
+       }
 
-               //Init application
-               $application = false;
+       /**
+        * Compute eastern for selected year
+        *
+        * @param int $year The eastern year
+        *
+        * @return DateTime The eastern date
+        */
+       function getEastern($year) {
+               //Set static
+               static $data = null;
+               //Check if already computed
+               if (isset($data[$year])) {
+                       //Return computed eastern
+                       return $data[$year];
+               //Check if data is null
+               } elseif (is_null($data)) {
+                       //Init data array
+                       $data = [];
+               }
+               $d = (19 * ($year % 19) + 24) % 30;
+               $e = (2 * ($year % 4) + 4 * ($year % 7) + 6 * $d + 5) % 7;
+
+               $day = 22 + $d + $e;
+               $month = 3;
+
+               if ($day > 31) {
+                       $day = $d + $e - 9;
+                       $month = 4;
+               } elseif ($d == 29 && $e == 6) {
+                       $day = 10;
+                       $month = 4;
+               } elseif ($d == 28 && $e == 6) {
+                       $day = 18;
+                       $month = 4;
+               }
 
-               //Protect application fetching
-               try {
-                       //TODO: handle admin case where we provide a user in extra
-                       $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $this->getUser());
+               //Store eastern in data
+               return ($data[$year] = new \DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day)));
+       }
 
-                       //Add error message to mail field
-                       $form->get('slot')->addError(new FormError($this->translator->trans('Application already exists')));
-               //Catch no application cases
-               //XXX: combine these catch when php 7.1 is available
-               } catch (\Doctrine\ORM\NoResultException $e) {
-               //Catch invalid argument because session is not already persisted
-               } catch(\Doctrine\ORM\ORMInvalidArgumentException $e) {
+       /**
+        * Check if date is a premium day
+        *
+        * @desc Consider as premium a day off
+        *
+        * @param DateTime $date The date to check
+        * @return bool Whether the date is off or not
+        */
+       function isPremium($date) {
+               //Get day number
+               $w = $date->format('w');
+
+               //Check if weekend day
+               if ($w == 0 || $w == 6) {
+                       //Date is weekend day
+                       return true;
                }
 
-               //Create new application if none found
-               if (!$application) {
-                       //Create the application
-                       $application = new Application();
-                       $application->setSession($session);
-                       //TODO: handle admin case where we provide a user in extra
-                       $application->setUser($this->getUser());
-                       $application->setCreated(new \DateTime('now'));
-                       $application->setUpdated(new \DateTime('now'));
-                       $manager->persist($application);
-
-                       //Flush to get the ids
-                       $manager->flush();
+               //Get date day
+               $d = $date->format('d');
 
-                       //Add notice in flash message
-                       $this->addFlash('notice', $this->translator->trans('Application request the %date% for %location% on the slot %slot% saved', ['%location%' => $data['location']->getTitle(), '%slot%' => $data['slot']->getTitle(), '%date%' => $data['date']->format('Y-m-d')]));
+               //Get date month
+               $m = $date->format('m');
 
-                       //Redirect to cleanup the form
-                       return $this->redirectToRoute('rapsys_air_admin');
+               //Check if fixed holiday
+               if (
+                       //Check if 1st january
+                       ($d == 1 && $m == 1) ||
+                       //Check if 1st may
+                       ($d == 1 && $m == 5) ||
+                       //Check if 8st may
+                       ($d == 8 && $m == 5) ||
+                       //Check if 14st july
+                       ($d == 14 && $m == 7) ||
+                       //Check if 15st august
+                       ($d == 15 && $m == 8) ||
+                       //Check if 1st november
+                       ($d == 1 && $m == 11) ||
+                       //Check if 11st november
+                       ($d == 11 && $m == 11) ||
+                       //Check if 25st december
+                       ($d == 25 && $m == 12)
+               ) {
+                       //Date is a fixed holiday
+                       return true;
                }
-       }
 
-       function test(Request $request) {
-
-               //Compute period
-               $period = new \DatePeriod(
-                       //Start from first monday of week
-                       new \DateTime('Monday this week'),
-                       //Iterate on each day
-                       new \DateInterval('P1D'),
-                       //End with next sunday and 4 weeks
-                       new \DateTime('Monday this week + 5 week')
-               );
-
-               //Fetch sessions
-               $sessions = $doctrine->getRepository(Session::class)->findAllByDatePeriod($period);
-
-               //Init calendar
-               $calendar = [];
-               
-               //Init month
-               $month = null;
-
-               //Iterate on each day
-               foreach($period as $date) {
-                       //Init day in calendar
-                       $calendar[$Ymd = $date->format('Ymd')] = [
-                               'title' => $date->format('d'),
-                               'class' => [],
-                               'sessions' => []
-                       ];
-                       //Append month for first day of month
-                       if ($month != $date->format('m')) {
-                               $month = $date->format('m');
-                               $calendar[$Ymd]['title'] .= '/'.$month;
-                       }
-                       //Deal with today
-                       if ($date->format('U') == ($today = strtotime('today'))) {
-                               $calendar[$Ymd]['title'] .= '/'.$month;
-                               $calendar[$Ymd]['current'] = true;
-                               $calendar[$Ymd]['class'][] =  'current';
-                       }
-                       //Disable passed days
-                       if ($date->format('U') < $today) {
-                               $calendar[$Ymd]['disabled'] = true;
-                               $calendar[$Ymd]['class'][] =  'disabled';
-                       }
-                       //Set next month days
-                       if ($date->format('m') > date('m')) {
-                               $calendar[$Ymd]['next'] = true;
-                               $calendar[$Ymd]['class'][] =  'next';
-                       }
-                       //Iterate on each session to find the one of the day
-                       foreach($sessions as $session) {
-                               if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
-                                       //Count number of application
-                                       $count = count($session->getApplications());
-
-                                       //Compute classes
-                                       $class = [];
-                                       if ($session->getApplication()) {
-                                               $class[] = 'granted';
-                                       } elseif ($count == 0) {
-                                               $class[] = 'orphaned';
-                                       } elseif ($count > 1) {
-                                               $class[] = 'disputed';
-                                       } else {
-                                               $class[] = 'pending';
-                                       }
+               //Get eastern
+               $eastern = $this->getEastern($date->format('Y'));
 
-                                       //Add the session
-                                       $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
-                                               'id' => $session->getId(),
-                                               'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
-                                               'class' => $class
-                                       ];
-                               }
-                       }
-
-                       //Sort sessions
-                       ksort($calendar[$Ymd]['sessions']);
+               //Check dynamic holidays
+               if (
+                       (clone $eastern)->add(new \DateInterval('P1D')) == $date ||
+                       (clone $eastern)->add(new \DateInterval('P39D')) == $date ||
+                       (clone $eastern)->add(new \DateInterval('P50D')) == $date
+               ) {
+                       //Date is a dynamic holiday
+                       return true;
                }
+
+               //Date is not a holiday and week day
+               return false;
        }
 }