]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Controller/ApplicationController.php
Add location default values
[airbundle] / Controller / ApplicationController.php
index 5a787dc4a95af423da5db58084517030d9c386c2..914afa711d79133f26233d19261d5851ba07f01a 100644 (file)
@@ -45,24 +45,26 @@ class ApplicationController extends AbstractController {
         * @throws \RuntimeException When user has not at least guest role
         */
        public function add(Request $request) {
-               //Prevent non-guest to access here
-               $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
+               //Without guest role
+               if (!$this->checker->isGranted('ROLE_GUEST')) {
+                       //Throw 403
+                       throw $this->createAccessDeniedException($this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
+               }
 
                //Get favorites dances
-               $danceFavorites = $this->doctrine->getRepository(Dance::class)->findByUserId($this->getUser()->getId());
+               $danceFavorites = $this->doctrine->getRepository(Dance::class)->findByUserId($this->security->getUser()->getId());
 
                //Set dance default
                $danceDefault = !empty($danceFavorites)?current($danceFavorites):null;
 
-
                //Get favorites locations
-               $locationFavorites = $this->doctrine->getRepository(Location::class)->findByUserId($this->getUser()->getId());
+               $locationFavorites = $this->doctrine->getRepository(Location::class)->findByUserId($this->security->getUser()->getId());
 
                //Set location default
                $locationDefault = !empty($locationFavorites)?current($locationFavorites):null;
 
                //With admin
-               if ($this->isGranted('ROLE_ADMIN')) {
+               if ($this->checker->isGranted('ROLE_ADMIN')) {
                        //Get dances
                        $dances = $this->doctrine->getRepository(Dance::class)->findAll();
 
@@ -84,9 +86,9 @@ class ApplicationController extends AbstractController {
                }
 
                //Create ApplicationType form
-               $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
+               $form = $this->factory->create('Rapsys\AirBundle\Form\ApplicationType', null, [
                        //Set the action
-                       'action' => $this->generateUrl('rapsys_air_application_add'),
+                       'action' => $this->generateUrl('rapsysair_application_add'),
                        //Set the form attribute
                        #'attr' => [ 'class' => 'col' ],
                        //Set dance choices
@@ -102,26 +104,32 @@ class ApplicationController extends AbstractController {
                        //Set location favorites
                        'location_favorites' => $locationFavorites,
                        //With user
-                       'user' => $this->isGranted('ROLE_ADMIN'),
+                       'user' => $this->checker->isGranted('ROLE_ADMIN'),
                        //Set user choices
-                       'user_choices' => $this->doctrine->getRepository(User::class)->findIndexByGroupPseudonym(),
+                       'user_choices' => $this->doctrine->getRepository(User::class)->findChoicesAsArray(),
                        //Set default user to current
-                       'user_default' => $this->getUser()->getId(),
+                       'user_default' => $this->security->getUser()->getId(),
                        //Set default slot to evening
                        //XXX: default to Evening (3)
                        'slot_default' => $this->doctrine->getRepository(Slot::class)->findOneByTitle('Evening')
                ]);
 
+               //Set title
+               $this->context['title']['page'] = $this->translator->trans('Application add');
+
+               //Set section
+               $this->context['title']['section'] = $this->translator->trans('Application');
+
+               //Set description
+               $this->context['description'] = $this->translator->trans('Add an application and session');
+
                //Refill the fields in case of invalid form
                $form->handleRequest($request);
 
                //Handle invalid form
                if (!$form->isSubmitted() || !$form->isValid()) {
-                       //Set title
-                       $title = $this->translator->trans('Application add');
-
                        //Render the view
-                       return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+$this->context);
+                       return $this->render('@RapsysAir/application/add.html.twig', ['form' => $form->createView()]+$this->context);
                }
 
                //Get data
@@ -134,10 +142,7 @@ class ApplicationController extends AbstractController {
                //Catch no session case
                } catch (NoResultException $e) {
                        //Create the session
-                       $session = new Session();
-                       $session->setLocation($data['location']);
-                       $session->setDate($data['date']);
-                       $session->setSlot($data['slot']);
+                       $session = new Session($data['date'], $data['location'], $data['slot']);
 
                        //Get location
                        $location = $data['location']->getTitle();
@@ -154,7 +159,7 @@ class ApplicationController extends AbstractController {
                        $session->setLength(new \DateTime('06:00:00'));
 
                        //Check if admin
-                       if ($this->isGranted('ROLE_ADMIN')) {
+                       if ($this->checker->isGranted('ROLE_ADMIN')) {
                                //Check if morning
                                if ($slot == 'Morning') {
                                        //Set begin at 9h
@@ -332,23 +337,17 @@ class ApplicationController extends AbstractController {
                                //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(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
 
-                               //Set title
-                               $title = $this->translator->trans('Application add');
-
                                //Render the view
-                               return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+$this->context);
+                               return $this->render('@RapsysAir/application/add.html.twig', ['form' => $form->createView()]+$this->context);
                        }
 
                        //Check if admin
-                       if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \DateTime('00:00:00')) {
+                       if (!$this->checker->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(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
 
-                               //Set title
-                               $title = $this->translator->trans('Application add');
-
                                //Render the view
-                               return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+$this->context);
+                               return $this->render('@RapsysAir/application/add.html.twig', ['form' => $form->createView()]+$this->context);
                        }
 
                        //Queue session save
@@ -361,11 +360,11 @@ class ApplicationController extends AbstractController {
                }
 
                //Set user
-               $user = $this->getUser();
+               $user = $this->security->getUser();
 
                //Replace with requested user for admin
-               if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
-                       $user = $this->getDoctrine()->getRepository(User::class)->findOneById($data['user']);
+               if ($this->checker->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
+                       $user = $this->doctrine->getRepository(User::class)->findOneById($data['user']);
                }
 
                //Protect application fetching
@@ -438,7 +437,7 @@ class ApplicationController extends AbstractController {
                                unset($route['_route'], $route['_controller']);
 
                                //Check if session view route
-                               if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
+                               if ($name == 'rapsysair_session_view' && !empty($route['id'])) {
                                        //Replace id
                                        $route['id'] = $session->getId();
                                //Other routes
@@ -457,6 +456,6 @@ class ApplicationController extends AbstractController {
                }
 
                //Redirect to cleanup the form
-               return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
+               return $this->redirectToRoute('rapsysair', ['session' => $session->getId()]);
        }
 }