]> Raphaƫl G. Git Repositories - airbundle/blobdiff - Controller/SessionController.php
Switch to fpdf
[airbundle] / Controller / SessionController.php
index 6359f6a7febaa29242d6e14e1b048c865c77b37e..30a0a730bd786e587dd58d8e7de4ccb18d8166ac 100644 (file)
@@ -3,6 +3,7 @@
 namespace Rapsys\AirBundle\Controller;
 
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\RequestContext;
 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
@@ -10,7 +11,6 @@ use Rapsys\AirBundle\Entity\Application;
 use Rapsys\AirBundle\Entity\User;
 use Rapsys\AirBundle\Entity\Slot;
 use Rapsys\AirBundle\Entity\Session;
-use Rapsys\AirBundle\Entity\Link;
 use Rapsys\AirBundle\Entity\Location;
 
 class SessionController extends DefaultController {
@@ -25,7 +25,7 @@ class SessionController extends DefaultController {
         *
         * @throws \RuntimeException When user has not at least guest role
         */
-       public function edit(Request $request, $id) {
+       public function edit(Request $request, $id): Response {
                //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')]));
 
@@ -56,8 +56,8 @@ class SessionController extends DefaultController {
                //Set now
                $now = new \DateTime('now');
 
-               //Create SessionEditType form
-               $form = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [
+               //Create SessionType form
+               $form = $this->createForm('Rapsys\AirBundle\Form\SessionType', null, [
                        //Set the action
                        'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]),
                        //Set the form attribute
@@ -435,7 +435,7 @@ class SessionController extends DefaultController {
         *
         * @return Response The rendered view
         */
-       public function index(Request $request) {
+       public function index(Request $request): Response {
                //Fetch doctrine
                $doctrine = $this->getDoctrine();
 
@@ -456,42 +456,6 @@ class SessionController extends DefaultController {
                //Set title
                $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
 
-               //Init context
-               $context = [];
-
-               //Create application form for role_guest
-               if ($this->isGranted('ROLE_GUEST')) {
-                       //Create ApplicationType form
-                       $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
-                               //Set the action
-                               'action' => $this->generateUrl('rapsys_air_application_add'),
-                               //Set the form attribute
-                               'attr' => [ 'class' => 'col' ],
-                               //Set admin
-                               'admin' => $this->isGranted('ROLE_ADMIN'),
-                               //Set default user to current
-                               'user' => $this->getUser()->getId(),
-                               //Set default slot to evening
-                               //XXX: default to Evening (3)
-                               'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
-                       ]);
-
-                       //Add form to context
-                       $context['application'] = $application->createView();
-               //Create login form for anonymous
-               } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
-                       //Create ApplicationType form
-                       $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
-                               //Set the action
-                               'action' => $this->generateUrl('rapsys_user_login'),
-                               //Set the form attribute
-                               'attr' => [ 'class' => 'col' ]
-                       ]);
-
-                       //Add form to context
-                       $context['login'] = $login->createView();
-               }
-
                //Compute period
                $period = new \DatePeriod(
                        //Start from first monday of week
@@ -500,7 +464,7 @@ class SessionController extends DefaultController {
                        new \DateInterval('P1D'),
                        //End with next sunday and 4 weeks
                        new \DateTime(
-                               $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
+                               $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
                        )
                );
 
@@ -513,7 +477,7 @@ class SessionController extends DefaultController {
                $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
 
                //Render the view
-               return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
+               return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context);
        }
 
        /**
@@ -526,7 +490,7 @@ class SessionController extends DefaultController {
         *
         * @return Response The rendered view
         */
-       public function view(Request $request, $id) {
+       public function view(Request $request, $id): Response {
                //Fetch doctrine
                $doctrine = $this->getDoctrine();
 
@@ -557,13 +521,10 @@ class SessionController extends DefaultController {
                //Set title
                $title = $this->translator->trans($this->config['site']['title']).' - '.$section.' - '.$this->translator->trans(!empty($session['au_id'])?'Session %id% by %pseudonym%':'Session %id%', ['%id%' => $id, '%pseudonym%' => $session['au_pseudonym']]);
 
-               //Init context
-               $context = [];
-
                //Create application form for role_guest
                if ($this->isGranted('ROLE_GUEST')) {
                        //Create ApplicationType form
-                       $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
+                       $applicationForm = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
                                //Set the action
                                'action' => $this->generateUrl('rapsys_air_application_add'),
                                //Set the form attribute
@@ -579,13 +540,13 @@ class SessionController extends DefaultController {
                        ]);
 
                        //Add form to context
-                       $context['application'] = $application->createView();
+                       $this->context['forms']['application'] = $applicationForm->createView();
 
                        //Set now
                        $now = new \DateTime('now');
 
-                       //Create SessionEditType form
-                       $session_edit = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [
+                       //Create SessionType form
+                       $sessionForm = $this->createForm('Rapsys\AirBundle\Form\SessionType', null, [
                                //Set the action
                                'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]),
                                //Set the form attribute
@@ -613,19 +574,7 @@ class SessionController extends DefaultController {
                        ]);
 
                        //Add form to context
-                       $context['session_edit'] = $session_edit->createView();
-               //Create login form for anonymous
-               } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
-                       //Create ApplicationType form
-                       $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
-                               //Set the action
-                               'action' => $this->generateUrl('rapsys_user_login'),
-                               //Set the form attribute
-                               'attr' => [ 'class' => 'col' ]
-                       ]);
-
-                       //Add form to context
-                       $context['login'] = $login->createView();
+                       $this->context['forms']['session'] = $sessionForm->createView();
                }
 
                //Add session in context
@@ -666,77 +615,26 @@ class SessionController extends DefaultController {
                        ],
                        'snippet' => [
                                'id' => $session['p_id'],
-                               'description' => $session['p_description']
+                               'description' => $session['p_description'],
+                               'class' => $session['p_class'],
+                               'contact' => $session['p_contact'],
+                               'donate' => $session['p_donate'],
+                               'link' => $session['p_link'],
+                               'profile' => $session['p_profile']
                        ],
                        'applications' => null
                ];
 
                //With application
                if (!empty($session['a_id'])) {
-                       //Init links
-                       $links = null;
-
-                       //Merge array
-                       if (!empty($session['i_type']) && !empty($session['i_url'])) {
-                               //Extract links type
-                               $session['i_type'] = explode("\n", $session['i_type']);
-
-                               //Extract links url
-                               $session['i_url'] = explode("\n", $session['i_url']);
-
-                               //Set links array
-                               $links = [];
-
-                               //Iterate on links type
-                               foreach($session['i_type'] as $i => $type) {
-                                       //Type is contact, donate or link
-                                       if (in_array($type, [Link::TYPE_CONTACT, Link::TYPE_DONATE, Link::TYPE_LINK])) {
-                                               //Set title
-                                               $linkTitle = $this->translator->trans(ucfirst($type));
-                                       }
-
-                                       //Type is contact
-                                       if ($type == Link::TYPE_CONTACT) {
-                                               //Set description
-                                               $description = $this->translator->trans('Send a message to %pseudonym%', [ '%pseudonym%' => $session['au_pseudonym'] ]);
-                                       //Type is donate
-                                       } elseif ($type == Link::TYPE_DONATE) {
-                                               //Set description
-                                               $description = $this->translator->trans('Donate to %pseudonym%', [ '%pseudonym%' => $session['au_pseudonym'] ]);
-                                       //Type is link
-                                       } elseif ($type == Link::TYPE_LINK) {
-                                               //Set description
-                                               $description = $this->translator->trans('Link to %pseudonym%', [ '%pseudonym%' => $session['au_pseudonym'] ]);
-                                       //Type is social
-                                       } elseif ($type == Link::TYPE_SOCIAL) {
-                                               //Set description
-                                               $description = $this->translator->trans('Consult %pseudonym% social profile', [ '%pseudonym%' => $session['au_pseudonym'] ]);
-
-                                               //Set title
-                                               $linkTitle = $this->translator->trans('Social network');
-                                       //Unknown type
-                                       } else {
-                                               //Throw explode
-                                               throw new \InvalidArgumentException('Invalid type');
-                                       }
-
-                                       //Set link entry
-                                       $links[$i] = [
-                                               'description' => $description,
-                                               'title' => $linkTitle,
-                                               'type' => $type,
-                                               'url' => $session['i_url'][$i]
-                                       ];
-                               }
-                       }
                        $context['session']['application'] = [
                                'user' => [
                                        'id' => $session['au_id'],
                                        'by' => $this->translator->trans('by %pseudonym%', [ '%pseudonym%' => $session['au_pseudonym'] ]),
-                                       'title' => $session['au_pseudonym'],
-                                       'links' => $links
+                                       'title' => $session['au_pseudonym']
                                ],
                                'id' => $session['a_id'],
+                               'canceled' => $session['a_canceled'],
                                'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]),
                        ];
                }
@@ -788,7 +686,7 @@ class SessionController extends DefaultController {
                        new \DateInterval('P1D'),
                        //End with next sunday and 4 weeks
                        new \DateTime(
-                               $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
+                               $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
                        )
                );