From 58f92b72a64d86aca0ead2430b7d0e4a21460696 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:15:34 +0100 Subject: [PATCH 01/16] Remove donation and site fields Remove vote entity --- Resources/config/doctrine/User.orm.yml | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Resources/config/doctrine/User.orm.yml b/Resources/config/doctrine/User.orm.yml index d1e1336..491ae32 100644 --- a/Resources/config/doctrine/User.orm.yml +++ b/Resources/config/doctrine/User.orm.yml @@ -7,17 +7,9 @@ Rapsys\AirBundle\Entity\User: type: string length: 16 nullable: true - donation: - type: string - length: 254 - nullable: true - site: - type: string - length: 254 - nullable: true oneToMany: -# votes: -# targetEntity: Rapsys\AirBundle\Entity\Vote +# links: +# targetEntity: Rapsys\AirBundle\Entity\Link # mappedBy: user applications: targetEntity: Rapsys\AirBundle\Entity\Application -- 2.41.1 From 42ed76590da8c8c8f5f34119317c4f200bd4de9e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:16:29 +0100 Subject: [PATCH 02/16] Remove commented link relation --- Resources/config/doctrine/User.orm.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Resources/config/doctrine/User.orm.yml b/Resources/config/doctrine/User.orm.yml index 491ae32..dacee58 100644 --- a/Resources/config/doctrine/User.orm.yml +++ b/Resources/config/doctrine/User.orm.yml @@ -8,9 +8,6 @@ Rapsys\AirBundle\Entity\User: length: 16 nullable: true oneToMany: -# links: -# targetEntity: Rapsys\AirBundle\Entity\Link -# mappedBy: user applications: targetEntity: Rapsys\AirBundle\Entity\Application mappedBy: user -- 2.41.1 From bf931483b4140b53d95a30a93808bfc118ed18c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:17:11 +0100 Subject: [PATCH 03/16] Add class, contact, donate, link and profile fields --- Resources/config/doctrine/Snippet.orm.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Resources/config/doctrine/Snippet.orm.yml b/Resources/config/doctrine/Snippet.orm.yml index a6e9165..4b172d6 100644 --- a/Resources/config/doctrine/Snippet.orm.yml +++ b/Resources/config/doctrine/Snippet.orm.yml @@ -15,6 +15,25 @@ Rapsys\AirBundle\Entity\Snippet: length: 2 description: type: text + class: + type: text + nullable: true + contact: + type: string + length: 254 + nullable: true + donate: + type: string + length: 254 + nullable: true + link: + type: string + length: 254 + nullable: true + profile: + type: string + length: 254 + nullable: true created: type: datetime updated: -- 2.41.1 From 9a2872ff29bd61c67ca3ddb64e1658550a1e0d2f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:17:40 +0100 Subject: [PATCH 04/16] Rename SessionEdit to Session form type Remove application and login form Cleanup context Add canceled support Document return type --- Controller/SessionController.php | 77 ++++++-------------------------- 1 file changed, 14 insertions(+), 63 deletions(-) diff --git a/Controller/SessionController.php b/Controller/SessionController.php index e15fef6..26ceb94 100644 --- a/Controller/SessionController.php +++ b/Controller/SessionController.php @@ -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; @@ -24,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')])); @@ -55,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 @@ -434,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(); @@ -455,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 @@ -512,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); } /** @@ -525,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(); @@ -556,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 @@ -578,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 @@ -612,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 @@ -670,7 +620,7 @@ class SessionController extends DefaultController { 'contact' => $session['p_contact'], 'donate' => $session['p_donate'], 'link' => $session['p_link'], - 'social' => $session['p_social'] + 'profile' => $session['p_profile'] ], 'applications' => null ]; @@ -684,6 +634,7 @@ class SessionController extends DefaultController { 'title' => $session['au_pseudonym'] ], 'id' => $session['a_id'], + 'canceled' => $session['a_canceled'], 'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]), ]; } -- 2.41.1 From 866c33cd64b80c9695d61cc20792dded195c7aea Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:19:54 +0100 Subject: [PATCH 05/16] Add add and edit location controller methods Add keywords and description context Remove application form Limit period to only 2 weeks when not logged --- Controller/LocationController.php | 376 ++++++++++++++++++++++++++++-- 1 file changed, 357 insertions(+), 19 deletions(-) diff --git a/Controller/LocationController.php b/Controller/LocationController.php index aa21c34..6a77657 100644 --- a/Controller/LocationController.php +++ b/Controller/LocationController.php @@ -3,11 +3,351 @@ 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; use Rapsys\AirBundle\Entity\Slot; use Rapsys\AirBundle\Entity\Session; use Rapsys\AirBundle\Entity\Location; class LocationController extends DefaultController { + /** + * Add location + * + * @desc Persist location in database + * + * @param Request $request The request instance + * + * @return Response The rendered view or redirection + * + * @throws \RuntimeException When user has not at least admin role + */ + public function add(Request $request) { + //Prevent non-guest to access here + $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Admin')])); + + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', null, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_add'), + //Set the form attribute + 'attr' => [] + ]); + + //Refill the fields in case of invalid form + $form->handleRequest($request); + + //Handle invalid form + if (!$form->isSubmitted() || !$form->isValid()) { + //Set section + $section = $this->translator->trans('Location add'); + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + + //Render the view + return $this->render('@RapsysAir/location/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context); + } + + //Get doctrine + $doctrine = $this->getDoctrine(); + + //Get manager + $manager = $doctrine->getManager(); + + //Get location + $location = $form->getData(); + + //Set created + $location->setCreated(new \DateTime('now')); + + //Set updated + $location->setUpdated(new \DateTime('now')); + + //Queue location save + $manager->persist($location); + + //Flush to get the ids + $manager->flush(); + + //Add notice + $this->addFlash('notice', $this->translator->trans('Location %id% created', ['%id%' => $location->getId()])); + + //Extract and process referer + if ($referer = $request->headers->get('referer')) { + //Create referer request instance + $req = Request::create($referer); + + //Get referer path + $path = $req->getPathInfo(); + + //Get referer query string + $query = $req->getQueryString(); + + //Remove script name + $path = str_replace($request->getScriptName(), '', $path); + + //Try with referer path + try { + //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); + + //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 location view route + if ($name == 'rapsys_air_location_view' && !empty($route['id'])) { + //Replace id + $route['id'] = $location->getId(); + //Other routes + } else { + //Set location + $route['location'] = $location->getId(); + } + + //Generate url + return $this->redirectToRoute($name, $route); + //No route matched + } catch(MethodNotAllowedException|ResourceNotFoundException $e) { + //Unset referer to fallback to default route + unset($referer); + } + } + + //Redirect to cleanup the form + return $this->redirectToRoute('rapsys_air', ['location' => $location->getId()]); + } + + /** + * Edit location + * + * @desc Persist location in database + * + * @param Request $request The request instance + * + * @return Response The rendered view or redirection + * + * @throws \RuntimeException When user has not at least guest role + */ + public function edit(Request $request, $id) { + //Prevent non-admin to access here + $this->denyAccessUnlessGranted('ROLE_ADMIN', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Admin')])); + + //Get doctrine + $doctrine = $this->getDoctrine(); + + //Get location + if (empty($location = $doctrine->getRepository(Location::class)->findOneById($id))) { + throw $this->createNotFoundException($this->translator->trans('Unable to find location: %id%', ['%id%' => $id])); + } + + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', $location, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_edit', ['id' => $id]), + //Set the form attribute + 'attr' => [] + ]); + + //Refill the fields in case of invalid form + $form->handleRequest($request); + + //Handle invalid form + if (!$form->isSubmitted() || !$form->isValid()) { + //Set section + $section = $this->translator->trans('Location %id%', ['%id%' => $id]); + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + + //Render the view + return $this->render('@RapsysAir/location/edit.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'form' => $form->createView()]+$this->context); + } + + //Get manager + $manager = $doctrine->getManager(); + + //Set updated + $location->setUpdated(new \DateTime('now')); + + //Queue location save + $manager->persist($location); + + //Flush to get the ids + $manager->flush(); + + //Add notice + $this->addFlash('notice', $this->translator->trans('Location %id% updated', ['%id%' => $id])); + + //Extract and process referer + if ($referer = $request->headers->get('referer')) { + //Create referer request instance + $req = Request::create($referer); + + //Get referer path + $path = $req->getPathInfo(); + + //Get referer query string + $query = $req->getQueryString(); + + //Remove script name + $path = str_replace($request->getScriptName(), '', $path); + + //Try with referer path + try { + //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); + + //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 location view route + if ($name == 'rapsys_air_location_view' && !empty($route['id'])) { + //Replace id + $route['id'] = $location->getId(); + //Other routes + } else { + //Set location + $route['location'] = $location->getId(); + } + + //Generate url + return $this->redirectToRoute($name, $route); + //No route matched + } catch(MethodNotAllowedException|ResourceNotFoundException $e) { + //Unset referer to fallback to default route + unset($referer); + } + } + + //Redirect to cleanup the form + return $this->redirectToRoute('rapsys_air', ['location' => $location->getId()]); + } + + /** + * List all locations + * + * @desc Display all locations + * + * @param Request $request The request instance + * + * @return Response The rendered view + */ + public function index(Request $request): Response { + //Fetch doctrine + $doctrine = $this->getDoctrine(); + + //Set section + $section = $this->translator->trans('Libre Air locations'); + + //Set description + $this->context['description'] = $this->translator->trans('Libre Air location list'); + + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans('locations'), + $this->translator->trans('location list'), + $this->translator->trans('listing'), + $this->translator->trans('Libre Air') + ]; + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + + //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( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week' + ) + ); + + //Create location forms for role_admin + if ($this->isGranted('ROLE_ADMIN')) { + //Fetch all locations + $locations = $doctrine->getRepository(Location::class)->findAll(); + + //Rekey by id + $locations = array_reduce($locations, function($carry, $item){$carry[$item->getId()] = $item; return $carry;}, []); + + //Init locations to context + $this->context['forms']['locations'] = []; + + //Iterate on locations + foreach($locations as $locationId => $location) { + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', $location, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_edit', ['id' => $location->getId()]), + //Set the form attribute + 'attr' => [], + //Set block prefix + //TODO: make this shit works to prevent label collision + //XXX: see https://stackoverflow.com/questions/8703016/adding-a-prefix-to-a-form-label-for-translation + 'label_prefix' => 'location_'.$locationId + ]); + + //Add form to context + $this->context['forms']['locations'][$locationId] = $form->createView(); + } + + //Create LocationType form + $form = $this->createForm('Rapsys\AirBundle\Form\LocationType', null, [ + //Set the action + 'action' => $this->generateUrl('rapsys_air_location_add'), + //Set the form attribute + 'attr' => [ 'class' => 'col' ] + ]); + + //Add form to context + $this->context['forms']['location'] = $form->createView(); + } + + //Fetch locations + //XXX: we want to display all active locations anyway + $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period); + + //Render the view + return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+$this->context); + } + /** * List all sessions for the location * @@ -18,7 +358,7 @@ class LocationController 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(); @@ -30,12 +370,20 @@ class LocationController extends DefaultController { //Set section $section = $this->translator->trans('Argentine Tango at '.$location); + //Set description + $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session calendar %location%', [ '%location%' => $this->translator->trans('at '.$location) ]); + + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans($location), + $this->translator->trans('outdoor'), + $this->translator->trans('Argentine Tango'), + $this->translator->trans('calendar') + ]; + //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 @@ -56,19 +404,7 @@ class LocationController extends DefaultController { ]); //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(); + $this->context['forms']['application'] = $application->createView(); } //Compute period @@ -78,7 +414,9 @@ class LocationController extends DefaultController { //Iterate on each day new \DateInterval('P1D'), //End with next sunday and 4 weeks - new \DateTime('Monday this week + 5 week') + new \DateTime( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week' + ) ); //Fetch calendar @@ -89,6 +427,6 @@ class LocationController extends DefaultController { $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period); //Render the view - return $this->render('@RapsysAir/location/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context); + return $this->render('@RapsysAir/location/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context); } } -- 2.41.1 From 9a7d39fe6da03acfac7626442fcfbca40b5914a6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:23:24 +0100 Subject: [PATCH 06/16] Fix error display --- Controller/ErrorController.php | 35 +++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/Controller/ErrorController.php b/Controller/ErrorController.php index b9a3188..72e108f 100644 --- a/Controller/ErrorController.php +++ b/Controller/ErrorController.php @@ -18,7 +18,7 @@ class ErrorController extends DefaultController { /** * {@inheritdoc} */ - public function preview(Request $request, FlattenException $exception) { + public function show(Request $request, FlattenException $exception) { //Set section $section = $exception->getStatusCode().' '.$this->translator->trans($exception->getStatusText()); @@ -28,8 +28,37 @@ class ErrorController extends DefaultController { //Set the message $message = $exception->getMessage(); - //Set the trace - $trace = $exception->getAsString(); + //Init trace + $trace = null; + + //Prevent non admin access to trace + if ($this->isGranted('ROLE_ADMIN')) { + //Set project dir + $projectDir = $this->container->getParameter('kernel.project_dir').'/'; + + //Set the trace + //$trace = $exception->getAsString(); + $trace = ''; + + //Iterate on array + foreach($exception->toArray() as $current) { + $trace .= $current['class']; + + if (!empty($current['message'])) { + $trace .= ': '.$current['message']; + } + + if (!empty($current['trace'])) { + foreach($current['trace'] as $id => $sub) { + $trace .= "\n".'#'.$id.' '.$sub['class'].$sub['type'].$sub['function']; + if (!empty($sub['args'])) { + $trace .= '('.implode(', ', array_map(function($v){return $v[0].' '.$v[1];}, $sub['args'])).')'; + } + $trace .= ' in '.str_replace($projectDir, '', $sub['file']).':'.$sub['line']; + } + } + } + } //Render template return $this->render( -- 2.41.1 From 814ef7779e9061c781e7e15db080fe022aa3672c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:23:52 +0100 Subject: [PATCH 07/16] Add missing response use --- Controller/ApplicationController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Controller/ApplicationController.php b/Controller/ApplicationController.php index 45538ff..17c9f91 100644 --- a/Controller/ApplicationController.php +++ b/Controller/ApplicationController.php @@ -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\Form\FormError; use Symfony\Component\Routing\Exception\MethodNotAllowedException; -- 2.41.1 From dbd9139fe1367b8bd69b2d570d5c9831fbbe722f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:25:48 +0100 Subject: [PATCH 08/16] Add return types Add application and login forms in render Rename trait function to allow proxying the function Add faq method --- Controller/DefaultController.php | 154 +++++++++++++++++++++---------- 1 file changed, 107 insertions(+), 47 deletions(-) diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index f119ee3..eeb520c 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -13,6 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Form\FormError; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Address; @@ -25,7 +26,10 @@ use Symfony\Component\DependencyInjection\ContainerAwareTrait; class DefaultController { - use ControllerTrait; + use ControllerTrait { + //Rename render as _render + render as protected _render; + } ///Config array protected $config; @@ -67,8 +71,11 @@ class DefaultController { //Set the context $this->context = [ 'copy' => [ + 'by' => $translator->trans($this->config['copy']['by']), + 'link' => $this->config['copy']['link'], 'long' => $translator->trans($this->config['copy']['long']), - 'short' => $translator->trans($this->config['copy']['short']) + 'short' => $translator->trans($this->config['copy']['short']), + 'title' => $this->config['copy']['title'] ], 'site' => [ 'ico' => $this->config['site']['ico'], @@ -79,7 +86,8 @@ class DefaultController { 'url' => $router->generate($this->config['site']['url']), ], 'canonical' => null, - 'alternates' => [] + 'alternates' => [], + 'forms' => [] ]; //Get current locale @@ -139,7 +147,7 @@ class DefaultController { * * @return Response The rendered view or redirection */ - public function contact(Request $request, MailerInterface $mailer) { + public function contact(Request $request, MailerInterface $mailer): Response { //Set section $section = $this->translator->trans('Contact'); @@ -229,7 +237,7 @@ class DefaultController { * * @return Response The rendered view */ - public function index(Request $request) { + public function index(Request $request): Response { //Fetch doctrine $doctrine = $this->getDoctrine(); @@ -251,42 +259,6 @@ class 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 @@ -294,7 +266,9 @@ class DefaultController { //Iterate on each day new \DateInterval('P1D'), //End with next sunday and 4 weeks - new \DateTime('Monday this week + 5 week') + new \DateTime( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week' + ) ); //Fetch calendar @@ -302,10 +276,19 @@ class DefaultController { //Fetch locations //XXX: we want to display all active locations anyway - $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/); + $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period); //Render the view - return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context); + return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context); + + //Set Cache-Control must-revalidate directive + #$response->setPublic(true); + #$response->setMaxAge(300); + #$response->mustRevalidate(); + ##$response->setCache(['public' => true, 'max_age' => 300]); + + //Return the response + #return $response; } /** @@ -315,7 +298,7 @@ class DefaultController { * * @return Response The rendered view */ - public function organizerRegulation() { + public function organizerRegulation(): Response { //Set section $section = $this->translator->trans('Organizer regulation'); @@ -342,7 +325,7 @@ class DefaultController { * * @return Response The rendered view */ - public function termsOfService() { + public function termsOfService(): Response { //Set section $section = $this->translator->trans('Terms of service'); @@ -362,12 +345,89 @@ class DefaultController { return $this->render('@RapsysAir/default/terms_of_service.html.twig', ['title' => $title, 'section' => $section]+$this->context); } + /** + * The frequently asked questions page + * + * @desc Display the frequently asked questions + * + * @return Response The rendered view + */ + public function frequentlyAskedQuestions(): Response { + //Set section + $section = $this->translator->trans('Frequently asked questions'); + + //Set description + $this->context['description'] = $this->translator->trans('Libre Air frequently asked questions'); + + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans('frequently asked questions'), + $this->translator->trans('faq'), + $this->translator->trans('Libre Air') + ]; + + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; + + //Render template + return $this->render('@RapsysAir/default/frequently_asked_questions.html.twig', ['title' => $title, 'section' => $section]+$this->context); + } + /** * Return the bundle alias * * {@inheritdoc} */ - public function getAlias() { + public function getAlias(): string { return 'rapsys_air'; } + + /** + * Renders a view + * + * {@inheritdoc} + */ + protected function render(string $view, array $parameters = [], Response $response = null): Response { + //Create application form for role_guest + if ($this->isGranted('ROLE_GUEST')) { + //Without application form + if (empty($parameters['forms']['application'])) { + //Fetch doctrine + $doctrine = $this->getDoctrine(); + + //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 + $parameters['forms']['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 + $parameters['forms']['login'] = $login->createView(); + } + + //Call parent method + return $this->_render($view, $parameters, $response); + } } -- 2.41.1 From c9e96a3c19a41b0882d3e6c01bda1bfa2cd6a857 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 24 Feb 2021 01:30:31 +0100 Subject: [PATCH 09/16] Adapt template to new context tree Add creator link Add canonical and alternates links --- Resources/views/body.html.twig | 65 ++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/Resources/views/body.html.twig b/Resources/views/body.html.twig index 6dca55d..8f37879 100644 --- a/Resources/views/body.html.twig +++ b/Resources/views/body.html.twig @@ -10,23 +10,25 @@ {% if keywords is defined and keywords %} {% endif %} - - - {% for size, icon in site.png %} - {# Apple #} - {% if size in [120, 152, 167, 180] %} - {% if size == 180 %} - + {% if site is defined and site %} + + + {% for size, icon in site.png %} + {# Apple #} + {% if size in [120, 152, 167, 180] %} + {% if size == 180 %} + + {% endif %} + + {# Windows #} + {% elseif size in [70, 150, 310] %} + + {# Others #} + {% else %} + {% endif %} - - {# Windows #} - {% elseif size in [70, 150, 310] %} - - {# Others #} - {% else %} - - {% endif %} - {% endfor %} + {% endfor %} + {% endif %} {# stylesheet '//fonts.googleapis.com/css?family=Irish+Grover' '//fonts.googleapis.com/css?family=La+Belle+Aurore' '@RapsysAirBundle/Resources/public/css/{reset,screen}.css' #} {% stopwatch 'stylesheet' %} {% stylesheet '@rapsys_air_bundle/css/{reset,droidsans,screen}.css' %} @@ -53,15 +55,18 @@ {% block body %} {% block header %}