src/Controller/DefaultController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as SecurityAccess;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Filesystem\Filesystem;
  6. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  7. use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Core\Security;
  14. class DefaultController extends AbstractController
  15. {
  16.     /**
  17.      * home page.
  18.      *
  19.      * @Route("/", name="home", methods="GET")
  20.      */
  21.     public function index(Request $requestSecurity $security): Response
  22.     {
  23.         if ($security->isGranted('ROLE_USER')) {
  24.             if ($security->isGranted('ROLE_GESTION_REVUES') or $security->isGranted('ROLE_GESTION_NUMEROS')) {
  25.                 return $this->redirectToRoute('admin_revue_index');
  26.             }
  27.             return $this->redirectToRoute('admin_dashboard');
  28.         }
  29.         return $this->redirectToRoute('app_login');
  30.     }
  31.     /**
  32.      * changeLocale.
  33.      *
  34.      * @Route("/changelocal", name="change_locale", methods="GET")
  35.      */
  36.     public function changeLocale(Request $request$_locale): Response
  37.     {
  38.         //$request = $request();
  39.         $url $request->query->get('_route') ?: 'home';
  40.         $param $request->query->get('_route_params');
  41.         $param['_locale'] = $_locale;
  42.         if ('app_login' === $url) {
  43.             $param['change_locale'] = 1;
  44.         }
  45.         switch ($_locale) {
  46.             case 'fr':
  47.                 $this->get('session')->set('_locale''en');
  48.                 $request->setLocale('en');
  49.                 break;
  50.             case 'en':
  51.                 $this->get('session')->set('_locale''fr');
  52.                 $request->setLocale('fr');
  53.                 break;
  54.             default:
  55.                 $this->get('session')->set('_locale''fr');
  56.                 $request->setLocale('fr');
  57.                 break;
  58.         }
  59.         return $this->redirectToRoute($url$param);
  60.     }
  61.     /**
  62.      * return a security private upload.
  63.      *
  64.      * @Route("/upload/{format}/{upload}/{dir}", defaults={"format": "origin", "dir": ""}, name="admin_private_upload", methods="GET")
  65.      * @SecurityAccess("is_granted('ROLE_USER')")
  66.      */
  67.     public function upload(Request $requeststring $dir ''string $uploadstring $format 'origin'KernelInterface $kernel): Response
  68.     {
  69.         $fs = new Filesystem();
  70.         if ('' == $dir) {
  71.             if ('origin' == $format) {
  72.                 $filePath $kernel->getProjectDir().'/data/'.$upload;
  73.             } else {
  74.                 $filePath $kernel->getProjectDir().'/data/'.$format.'/'.$upload;
  75.             }
  76.         } else {
  77.             $filePath $kernel->getProjectDir().'/data/'.$format.'/'.$dir.'/'.$upload;
  78.         }
  79.         if ($fs->exists($filePath)) {
  80.             $response = new BinaryFileResponse($filePath);
  81.             $response->headers->set('Content-Type'mime_content_type($filePath));
  82.             $response->setContentDisposition(
  83.                     ResponseHeaderBag::DISPOSITION_INLINE,
  84.                     basename($filePath)
  85.                 );
  86.             return $response;
  87.         }
  88.         $filePath $kernel->getProjectDir().'/data/not-found.png';
  89.         if ($fs->exists($filePath)) {
  90.             $response = new BinaryFileResponse($filePath);
  91.             $response->headers->set('Content-Type'mime_content_type($filePath));
  92.             $response->setContentDisposition(
  93.                     ResponseHeaderBag::DISPOSITION_INLINE,
  94.                     basename($filePath)
  95.                 );
  96.             return $response;
  97.         }
  98.         throw new FileNotFoundException($filePath);
  99.     }
  100. }