src/Controller/Front/CommonController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\User;
  4. use App\Factory\UserExtensionFactory;
  5. use App\Services\DTV\YamlConfig\YamlReader;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. class CommonController extends AbstractController
  12. {
  13. private YamlReader $yamlReader;
  14. private EntityManagerInterface $em;
  15. /**
  16. * @param YamlReader $yamlReader
  17. * @param EntityManagerInterface $em
  18. */
  19. public function __construct( YamlReader $yamlReader, EntityManagerInterface $em )
  20. {
  21. $this->yamlReader = $yamlReader;
  22. $this->em = $em;
  23. }
  24. /**
  25. * @return Response
  26. */
  27. public function cgvShow(): Response
  28. {
  29. return $this->render( 'front/common/cgv.html.twig' );
  30. }
  31. /**
  32. * @return Response
  33. */
  34. public function cguShow(): Response
  35. {
  36. return $this->render( 'front/common/cgu.html.twig' );
  37. }
  38. /**
  39. * @return Response
  40. */
  41. public function cssCustom(): Response
  42. {
  43. $response = $this->render( 'css/front.css.twig' );
  44. $response->headers->set( 'Content-Type', 'text/css' );
  45. return $response;
  46. }
  47. /**
  48. * @return Response
  49. */
  50. public function mentionLegaleShow(): Response
  51. {
  52. return $this->render( 'front/common/mention_legale.html.twig' );
  53. }
  54. /**
  55. * @param Request $request
  56. * @param string $locale
  57. *
  58. * @return RedirectResponse
  59. */
  60. public function switchLanguage( Request $request, string $locale ): RedirectResponse
  61. {
  62. $global = $this->yamlReader->getGlobal();
  63. // la traduction n'est pas active
  64. if ( !isset( $global[ 'language' ][ 'enabled' ] ) && $global[ 'language' ][ 'enabled' ] ) {
  65. return $this->redirectToRoute( 'front_homepage' );
  66. }
  67. /** @var User $currentUser */
  68. $currentUser = $this->getUser();
  69. // la variable passée existe dans la configuration
  70. if ( in_array( strtolower( $locale ), $global[ 'language' ][ 'available' ], TRUE ) ) {
  71. $currentUser->addExtension( UserExtensionFactory::setLanguage( $currentUser, strtolower( $locale ) ) );
  72. $this->em->flush();
  73. }
  74. return $this->redirect( $request->server->get( 'HTTP_REFERER' ) );
  75. }
  76. }