src/Controller/Vitrine/EbooksController.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Vitrine;
  3. use App\Entity\Core\Ebook;
  4. use App\Entity\Core\EbookHasRegistrations;
  5. use App\Form\Core\EbookRegistrationForm;
  6. use App\Services\Core\Core;
  7. use App\Services\Core\Users;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. /**
  20.  * Gestion des ebooks
  21.  */
  22. class EbooksController extends AbstractController
  23. {
  24.     private $em;
  25.     private $us;
  26.     public function __construct (EntityManagerInterface $emUsers $us)
  27.     {
  28.         $this->em $em;
  29.         $this->us $us;
  30.     }
  31.     /**
  32.      * Affichage du formulaire
  33.      * @param Request $request
  34.      * @param Users $us
  35.      * @param Ebook $ebook
  36.      * @return Response
  37.      */
  38.     public function view(Request $request,Ebook $ebook): Response
  39.     {
  40.         $themeSelection $_ENV['THEME_BLOG'];
  41.         $randomPassword $this->us->randomPasswordSecurised(50).uniqid();
  42.         $registration = new EbookHasRegistrations();
  43.         $form $this->createForm(EbookRegistrationForm::class, $registration);
  44.         $form->handleRequest($request);
  45.         if ($form->isSubmitted() && $form->isValid()) {
  46.             $registration->setEbook($ebook);
  47.             $registration->setDownloaded(false);
  48.             $registration->setOpenedAt(null);
  49.             $registration->setSendedAt(new \DateTime('now'));
  50.             $registration->setCodeRegistration($randomPassword);
  51.             $this->em->persist($registration);
  52.             $this->em->flush();
  53.             return $this->redirectToRoute('visitors_ebooks_sv',['identifiant' => $randomPassword]);
  54.         }
  55.         return $this->render('vitrine/'.$themeSelection.'/ebooks/sourcecode.html.twig',[
  56.             'ebook' => $ebook,
  57.             'form' => $form->createView()
  58.         ]);
  59.     }
  60.     /**
  61.      * Visualisation des informations d'un ebook (pour le télécharger)
  62.      * @param Request $request
  63.      * @param $identifiant
  64.      * @return mixed
  65.      */
  66.     public function save(Request $request$identifiant)
  67.     {
  68.         $themeSelection $_ENV['THEME_BLOG'];
  69.         $registration $this->em->getRepository(EbookHasRegistrations::class)->findOneBy(['codeRegistration' => $identifiant]);
  70.         if($registration == null) {
  71.             return $this->redirectToRoute('homepage');
  72.         }
  73.         $ebook $registration->getEbook();
  74.         return $this->render('vitrine/'.$themeSelection.'/ebooks/page_afterregistered.html.twig',[
  75.             'ebook' => $ebook,
  76.             'identifiant' => $identifiant,
  77.         ]);
  78.     }
  79.     /**
  80.      * Télécharger un Ebook en fonction de l'identifiant
  81.      * @param Request $request
  82.      * @param UploaderHelper $helper
  83.      * @param $identifiant
  84.      * @return Response
  85.      */
  86.     public function download(Request $requestUploaderHelper $helper$identifiant)
  87.     {
  88.         $registration $this->em->getRepository(EbookHasRegistrations::class)->findOneBy(['codeRegistration' => $identifiant]);
  89.         if($registration == null) {
  90.             return $this->redirectToRoute('homepage');
  91.         }
  92.         $registration->setDownloaded(true);
  93.         $this->em->persist($registration);
  94.         $this->em->flush();
  95.         $ebook $registration->getEbook();
  96.         $path $_ENV["PATH_FOLDER"];
  97.         $pathname $path.$helper->asset($ebook);
  98.         $contentType mime_content_type($pathname);
  99.         if (ob_get_level()) ob_end_clean();
  100.         header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  101.         header("Cache-Control: post-check=0, pre-check=0"false);
  102.         header("Pragma: no-cache");
  103.         header("Content-type: ".$contentType);
  104.         header("Content-Disposition: inline; filename=".$contentType);
  105.         header('Content-Transfer-Encoding: binary');
  106.         header('Accept-Ranges: bytes');
  107.         clearstatcache();
  108.         readfile($pathname);
  109.         clearstatcache();
  110.         return new Response();
  111.     }
  112. }