]> Raphaël G. Git Repositories - airbundle/blobdiff - Controller/DefaultController.php
Cleanup
[airbundle] / Controller / DefaultController.php
index cfe5f58b73277da138cb78103ba9ca8889ec24ac..647baf844bb85af78275f564262555467072e5dd 100644 (file)
@@ -20,7 +20,6 @@ use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
 use Symfony\Component\Mime\Address;
-use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
 
 use Rapsys\AirBundle\Entity\Dance;
@@ -28,7 +27,7 @@ use Rapsys\AirBundle\Entity\Location;
 use Rapsys\AirBundle\Entity\Session;
 use Rapsys\AirBundle\Entity\Snippet;
 use Rapsys\AirBundle\Entity\User;
-use Rapsys\AirBundle\Pdf\DisputePdf;
+use Rapsys\AirBundle\Token\AnonymousToken;
 
 /**
  * {@inheritdoc}
@@ -37,14 +36,14 @@ class DefaultController extends AbstractController {
        /**
         * The about page
         *
-        * @desc Display the about informations
+        * Display the about informations
         *
         * @param Request $request The request instance
         * @return Response The rendered view
         */
        public function about(Request $request): Response {
                //Set page
-               $this->context['title'] = $this->translator->trans('About');
+               $this->context['title']['page'] = $this->translator->trans('About');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Libre Air about');
@@ -68,7 +67,7 @@ class DefaultController extends AbstractController {
        /**
         * The contact page
         *
-        * @desc Send a contact mail to configured contact
+        * Send a contact mail to configured contact
         *
         * @param Request $request The request instance
         *
@@ -76,7 +75,7 @@ class DefaultController extends AbstractController {
         */
        public function contact(Request $request): Response {
                //Set page
-               $this->context['title'] = $this->translator->trans('Contact');
+               $this->context['title']['page'] = $this->translator->trans('Contact');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Contact Libre Air');
@@ -94,7 +93,7 @@ class DefaultController extends AbstractController {
                $data = [];
 
                //With user
-               if ($user = $this->getUser()) {
+               if ($user = $this->security->getUser()) {
                        //Set data
                        $data = [
                                'name' => $user->getRecipientName(),
@@ -104,8 +103,8 @@ class DefaultController extends AbstractController {
 
                //Create the form according to the FormType created previously.
                //And give the proper parameters
-               $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', $data, [
-                       'action' => $this->generateUrl('rapsys_air_contact'),
+               $form = $this->factory->create('Rapsys\AirBundle\Form\ContactType', $data, [
+                       'action' => $this->generateUrl('rapsysair_contact'),
                        'method' => 'POST'
                ]);
 
@@ -122,7 +121,7 @@ class DefaultController extends AbstractController {
                                        //Set sender
                                        ->from(new Address($data['mail'], $data['name']))
                                        //Set recipient
-                                       ->to(new Address($this->context['contact']['mail'], $this->context['contact']['title']))
+                                       ->to(new Address($this->context['contact']['address'], $this->context['contact']['name']))
                                        //Set subject
                                        ->subject($data['subject'])
 
@@ -150,10 +149,10 @@ class DefaultController extends AbstractController {
                                } catch(TransportExceptionInterface $e) {
                                        if ($message = $e->getMessage()) {
                                                //Add error message mail unreachable
-                                               $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->context['contact']['mail'], '%message%' => $this->translator->trans($message)])));
+                                               $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->context['contact']['address'], '%message%' => $this->translator->trans($message)])));
                                        } else {
                                                //Add error message mail unreachable
-                                               $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->context['contact']['mail']])));
+                                               $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->context['contact']['address']])));
                                        }
                                }
                        }
@@ -163,127 +162,6 @@ class DefaultController extends AbstractController {
                return $this->render('@RapsysAir/form/contact.html.twig', ['form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
        }
 
-       /**
-        * The dispute page
-        *
-        * @desc Generate a dispute document
-        *
-        * @param Request $request The request instance
-        *
-        * @return Response The rendered view or redirection
-        */
-       public function dispute(Request $request): Response {
-               //Prevent non-guest to access here
-               $this->denyAccessUnlessGranted('ROLE_USER', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('User')]));
-
-               //Set page
-               $this->context['title'] = $this->translator->trans('Dispute');
-
-               //Set description
-               $this->context['description'] = $this->translator->trans('Libre Air dispute');
-
-               //Set keywords
-               $this->context['keywords'] = [
-                       $this->translator->trans('dispute'),
-                       $this->translator->trans('Libre Air'),
-                       $this->translator->trans('outdoor'),
-                       $this->translator->trans('Argentine Tango'),
-                       $this->translator->trans('calendar')
-               ];
-
-               //Create the form according to the FormType created previously.
-               //And give the proper parameters
-               $form = $this->createForm('Rapsys\AirBundle\Form\DisputeType', ['court' => 'Paris', 'abstract' => 'Pour constater cette prétendue infraction, les agents verbalisateurs ont pénétré dans un jardin privatif, sans visibilité depuis la voie publique, situé derrière un batiment privé, pour ce faire ils ont franchi au moins un grillage de chantier ou des potteaux métalliques séparant le terrain privé de la voie publique de l\'autre côté du batiment.'], [
-                       'action' => $this->generateUrl('rapsys_air_dispute'),
-                       'method' => 'POST'
-               ]);
-
-               if ($request->isMethod('POST')) {
-                       // Refill the fields in case the form is not valid.
-                       $form->handleRequest($request);
-
-                       if ($form->isValid()) {
-                               //Get data
-                               $data = $form->getData();
-
-                               //Gathering offense
-                               if (!empty($data['offense']) && $data['offense'] == 'gathering') {
-                                       //Add gathering
-                                       $output = DisputePdf::genGathering($data['court'], $data['notice'], $data['agent'], $data['service'], $data['abstract'], $this->translator->trans($this->getUser()->getCivility()->getTitle()), $this->getUser()->getForename(), $this->getUser()->getSurname());
-                               //Traffic offense
-                               } elseif (!empty($data['offense'] && $data['offense'] == 'traffic')) {
-                                       //Add traffic
-                                       $output = DisputePdf::genTraffic($data['court'], $data['notice'], $data['agent'], $data['service'], $data['abstract'], $this->translator->trans($this->getUser()->getCivility()->getTitle()), $this->getUser()->getForename(), $this->getUser()->getSurname());
-                               //Unsupported offense
-                               } else {
-                                       header('Content-Type: text/plain');
-                                       die('TODO');
-                                       exit;
-                               }
-
-                               //Send common headers
-                               header('Content-Type: application/pdf');
-
-                               //Send remaining headers
-                               header('Cache-Control: private, max-age=0, must-revalidate');
-                               header('Pragma: public');
-
-                               //Send content-length
-                               header('Content-Length: '.strlen($output));
-
-                               //Display the pdf
-                               echo $output;
-
-                               //Die for now
-                               exit;
-
-#                              //Create message
-#                              $message = (new TemplatedEmail())
-#                                      //Set sender
-#                                      ->from(new Address($data['mail'], $data['name']))
-#                                      //Set recipient
-#                                      //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
-#                                      ->to(new Address($this->config['contact']['mail'], $this->config['contact']['title']))
-#                                      //Set subject
-#                                      ->subject($data['subject'])
-#
-#                                      //Set path to twig templates
-#                                      ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
-#                                      ->textTemplate('@RapsysAir/mail/contact.text.twig')
-#
-#                                      //Set context
-#                                      ->context(
-#                                              [
-#                                                      'subject' => $data['subject'],
-#                                                      'message' => strip_tags($data['message']),
-#                                              ]+$this->context
-#                                      );
-#
-#                              //Try sending message
-#                              //XXX: mail delivery may silently fail
-#                              try {
-#                                      //Send message
-#                                      $this->mailer->send($message);
-#
-#                                      //Redirect on the same route with sent=1 to cleanup form
-#                                      return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
-#                              //Catch obvious transport exception
-#                              } catch(TransportExceptionInterface $e) {
-#                                      if ($message = $e->getMessage()) {
-#                                              //Add error message mail unreachable
-#                                              $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config['contact']['mail'], '%message%' => $this->translator->trans($message)])));
-#                                      } else {
-#                                              //Add error message mail unreachable
-#                                              $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']])));
-#                                      }
-#                              }
-                       }
-               }
-
-               //Render template
-               return $this->render('@RapsysAir/default/dispute.html.twig', ['form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
-       }
-
        /**
         * The index page
         *
@@ -297,7 +175,7 @@ class DefaultController extends AbstractController {
                $this->context['cities'] = $this->doctrine->getRepository(Location::class)->findCitiesAsArray($this->period);
 
                //Add calendar
-               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
+               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED'));
 
                //Add dances
                $this->context['dances'] = $this->doctrine->getRepository(Dance::class)->findNamesAsArray();
@@ -309,7 +187,7 @@ class DefaultController extends AbstractController {
                $response = new Response();
 
                //With logged user
-               if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                        //Set last modified
                        $response->setLastModified(new \DateTime('-1 year'));
 
@@ -387,7 +265,7 @@ class DefaultController extends AbstractController {
                $dances = implode($this->translator->trans(' and '), array_filter(array_merge([implode(', ', array_slice($dances, 0, -1))], array_slice($dances, -1)), 'strlen'));
 
                //Set title
-               $this->context['title'] = $this->translator->trans('%dances% %cities%', ['%dances%' => $dances, '%cities%' => $cities]);
+               $this->context['title']['page'] = $this->translator->trans('%dances% %cities%', ['%dances%' => $dances, '%cities%' => $cities]);
 
                //Set description
                //TODO: handle french translation when city start with a A, change à in en !
@@ -404,14 +282,14 @@ class DefaultController extends AbstractController {
        /**
         * The organizer regulation page
         *
-        * @desc Display the organizer regulation policy
+        * Display the organizer regulation policy
         *
         * @param Request $request The request instance
         * @return Response The rendered view
         */
        public function organizerRegulation(Request $request): Response {
                //Set page
-               $this->context['title'] = $this->translator->trans('Organizer regulation');
+               $this->context['title']['page'] = $this->translator->trans('Organizer regulation');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Libre Air organizer regulation');
@@ -437,14 +315,14 @@ class DefaultController extends AbstractController {
        /**
         * The terms of service page
         *
-        * @desc Display the terms of service policy
+        * Display the terms of service policy
         *
         * @param Request $request The request instance
         * @return Response The rendered view
         */
        public function termsOfService(Request $request): Response {
                //Set page
-               $this->context['title'] = $this->translator->trans('Terms of service');
+               $this->context['title']['page'] = $this->translator->trans('Terms of service');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Libre Air terms of service');
@@ -470,14 +348,14 @@ class DefaultController extends AbstractController {
        /**
         * The frequently asked questions page
         *
-        * @desc Display the frequently asked questions
+        * Display the frequently asked questions
         *
         * @param Request $request The request instance
         * @return Response The rendered view
         */
        public function frequentlyAskedQuestions(Request $request): Response {
                //Set page
-               $this->context['title'] = $this->translator->trans('Frequently asked questions');
+               $this->context['title']['page'] = $this->translator->trans('Frequently asked questions');
 
                //Set description
                $this->context['description'] = $this->translator->trans('Libre Air frequently asked questions');
@@ -504,7 +382,7 @@ class DefaultController extends AbstractController {
        /**
         * List all users
         *
-        * @desc Display all user with a group listed as users
+        * Display all user with a group listed as users
         *
         * @param Request $request The request instance
         *
@@ -512,19 +390,25 @@ class DefaultController extends AbstractController {
         */
        public function userIndex(Request $request): Response {
                //With admin role
-               if ($this->isGranted('ROLE_ADMIN')) {
+               if ($this->checker->isGranted('ROLE_ADMIN')) {
+                       //Set title
+                       $this->context['title']['page'] = $this->translator->trans('Libre Air user list');
+
                        //Set section
-                       $section = $this->translator->trans('Libre Air users');
+                       $this->context['title']['section'] = $this->translator->trans('User');
 
                        //Set description
-                       $this->context['description'] = $this->translator->trans('Libre Air user list');
+                       $this->context['description'] = $this->translator->trans('Lists Libre air users');
                //Without admin role
                } else {
+                       //Set title
+                       $this->context['title']['page'] = $this->translator->trans('Libre Air organizer list');
+
                        //Set section
-                       $section = $this->translator->trans('Libre Air organizers');
+                       $this->context['title']['section'] = $this->translator->trans('Organizer');
 
                        //Set description
-                       $this->context['description'] = $this->translator->trans('Libre Air organizers list');
+                       $this->context['description'] = $this->translator->trans('Lists Libre air organizers');
                }
 
                //Set keywords
@@ -535,14 +419,11 @@ class DefaultController extends AbstractController {
                        $this->translator->trans('Libre Air')
                ];
 
-               //Set title
-               $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
-
                //Fetch users
                $users = $this->doctrine->getRepository(User::class)->findIndexByGroupId();
 
                //With admin role
-               if ($this->isGranted('ROLE_ADMIN')) {
+               if ($this->checker->isGranted('ROLE_ADMIN')) {
                        //Display all users
                        $this->context['groups'] = $users;
                //Without admin role
@@ -552,13 +433,13 @@ class DefaultController extends AbstractController {
                }
 
                //Render the view
-               return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section]+$this->context);
+               return $this->render('@RapsysAir/user/index.html.twig', $this->context);
        }
 
        /**
         * List all sessions for the user
         *
-        * @desc Display all sessions for the user with an application or login form
+        * Display all sessions for the user with an application or login form
         *
         * @param Request $request The request instance
         * @param int $id The user id
@@ -573,10 +454,10 @@ class DefaultController extends AbstractController {
                }
 
                //Create token
-               $token = new AnonymousToken('', $this->context['user']['mail'], $this->context['user']['roles']);
+               $token = new AnonymousToken($this->context['user']['roles']);
 
                //Prevent access when not admin, user is not guest and not currently logged user
-               if (!($isAdmin = $this->isGranted('ROLE_ADMIN')) && !($isGuest = $this->decision->decide($token, ['ROLE_GUEST']))) {
+               if (!($isAdmin = $this->checker->isGranted('ROLE_ADMIN')) && !($isGuest = $this->decision->decide($token, ['ROLE_GUEST']))) {
                        //Throw access denied
                        throw new AccessDeniedException($this->translator->trans('Unable to access user: %id%', ['%id%' => $id]));
                }
@@ -584,11 +465,11 @@ class DefaultController extends AbstractController {
                //With invalid user slug
                if ($this->context['user']['slug'] !== $user) {
                        //Redirect to cleaned url
-                       return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+                       return $this->redirectToRoute('rapsysair_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
                }
 
                //Fetch calendar
-               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'), null, null, $id);
+               $this->context['calendar'] = $this->doctrine->getRepository(Session::class)->findAllByPeriodAsCalendarArray($this->period, !$this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED'), null, null, $id);
 
                //Get locations at less than 2 km
                $this->context['locations'] = $this->doctrine->getRepository(Location::class)->findAllByUserIdAsArray($id, $this->period, 2);
@@ -652,7 +533,7 @@ class DefaultController extends AbstractController {
                $response = new Response();
 
                //With logged user
-               if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+               if ($this->checker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
                        //Set last modified
                        $response->setLastModified(new \DateTime('-1 year'));
 
@@ -715,7 +596,10 @@ class DefaultController extends AbstractController {
                $ins = implode($this->translator->trans(' and '), array_filter(array_merge([implode(', ', array_slice($ins, 0, -1))], array_slice($ins, -1)), 'strlen'));
 
                //Set title
-               $this->context['title'] = $this->translator->trans('%pseudonym% organizer', ['%pseudonym%' => $this->context['user']['pseudonym']]);
+               $this->context['title']['page'] = $this->translator->trans('%pseudonym% organizer', ['%pseudonym%' => $this->context['user']['pseudonym']]);
+
+               //Set section
+               $this->context['title']['section'] = $this->translator->trans('User');
 
                //With locations
                if (!empty($locations)) {
@@ -735,7 +619,7 @@ class DefaultController extends AbstractController {
 
                //Create snippet forms for role_guest
                //TODO: optimize this call
-               if ($isAdmin || $isGuest && $this->getUser() && $this->context['user']['id'] == $this->getUser()->getId()) {
+               if ($isAdmin || $isGuest && $this->security->getUser() && $this->context['user']['id'] == $this->security->getUser()->getId()) {
                        //Fetch all user snippet
                        $snippets = $this->doctrine->getRepository(Snippet::class)->findByUserIdLocaleIndexByLocationId($id, $this->locale);
 
@@ -751,16 +635,7 @@ class DefaultController extends AbstractController {
                                //Without existing snippet
                                } else {
                                        //Init snippet
-                                       $current = new Snippet();
-
-                                       //Set default locale
-                                       $current->setLocale($this->locale);
-
-                                       //Set default user
-                                       $current->setUser($user);
-
-                                       //Set default location
-                                       $current->setLocation($this->doctrine->getRepository(Location::class)->findOneById($location['id']));
+                                       $current = new Snippet($this->locale, $this->doctrine->getRepository(Location::class)->findOneById($location['id']), $user);
                                }
 
                                //Create SnippetType form
@@ -792,7 +667,7 @@ class DefaultController extends AbstractController {
                                        $this->addFlash('notice', $this->translator->trans('Snippet for %user% %location% updated', ['%location%' => $location['at'], '%user%' => $this->context['user']['pseudonym']]));
 
                                        //Redirect to cleaned url
-                                       return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+                                       return $this->redirectToRoute('rapsysair_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
                                }
 
                                //Add form to context
@@ -843,7 +718,7 @@ class DefaultController extends AbstractController {
                                                        $this->addFlash('notice', $this->translator->trans('Image for %user% %location% deleted', ['%location%' => $location['at'], '%user%' => $this->context['user']['pseudonym']]));
 
                                                        //Redirect to cleaned url
-                                                       return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+                                                       return $this->redirectToRoute('rapsysair_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
                                                }
                                        }
 
@@ -883,7 +758,7 @@ class DefaultController extends AbstractController {
                                                $this->addFlash('notice', $this->translator->trans('Image for %user% %location% updated', ['%location%' => $location['at'], '%user%' => $this->context['user']['pseudonym']]));
 
                                                //Redirect to cleaned url
-                                               return $this->redirectToRoute('rapsys_air_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
+                                               return $this->redirectToRoute('rapsysair_user_view', ['id' => $id, 'user' => $this->context['user']['slug']]);
                                        }
                                }