src/Package/Openform/Front/Controller/PageController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Package\Openform\Front\Controller;
  3. use Gedmo\Sluggable\Util\Urlizer;
  4. use App\Package\Openform\Entity\Page;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Filesystem\Filesystem;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  11. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  12. use App\Package\Openform\Front\Controller\BaseController;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
  15. /**
  16.  * PageController
  17.  *
  18.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  19.  * @copyright  2019 Openform
  20.  * @since      07.2019
  21.  */
  22. class PageController extends BaseController
  23. {
  24.     
  25.     private $zip_path '';
  26.     private $em;
  27.     
  28.     public function __construct(RequestStack $requestRouteLocalizer $routeLocalizerEntityManagerInterface $em)
  29.     {
  30.         parent::__construct($request$routeLocalizer$em);
  31.         
  32.         $this->em $em;
  33.     }    
  34.     public function indexAction(Request $request)
  35.     {
  36.         $locale $request->getLocale();
  37.         $entity $this->em->getRepository(Page::class)->getVisibleBySlug($request->get('slug'), $locale);
  38.         if (!$entity) {
  39.             throw new NotFoundHttpException();
  40.         }
  41.         // Przekierowanie
  42.         $redirectLink $entity->getTranslation()[$locale]->getLink(); 
  43.         if (strlen(trim($redirectLink)) > 0) {
  44.             $parsedUrl parse_url($redirectLink);
  45.             if (!isset($parsedUrl['host'])) {
  46.                 $domain $request->getSchemeAndHttpHost();
  47.                 return $this->redirect($domain '/' ltrim($redirectLink'/'));
  48.             } else {
  49.                 return $this->redirect($redirectLink);
  50.             }
  51.         } 
  52.         
  53.         
  54.         // HOME
  55.         if ($entity->getIdname() == 'home') {
  56.             $routeName 'index' . ($locale == 'pl' '_default' '');
  57.             return $this->redirectToRoute($routeName);
  58.         }
  59.         
  60.         if ($entity->getIdname() == 'page_collections') {
  61.             return $this->forward('App\Package\Openform\Front\Controller\CollectionsController::indexAction', [
  62.                     'request' => $request
  63.                     'entity' => $entity
  64.             ]);
  65.         }
  66.         if ($entity->getIdname() == 'page_sitemap') {
  67.             return $this->forward('App\Package\Openform\Front\Controller\SitemapController::indexAction', [
  68.                     'request' => $request
  69.                     'entity' => $entity
  70.             ]);
  71.         }
  72.         return $this->render('@openform_front_templates/Page/index.html.twig', [
  73.             'entity' => $entity,
  74. //            'langLinks' => $this->generateLangLinksPage($this->em, $entity),
  75.         ]);
  76.     }
  77.     
  78.     /**
  79.      * Get file attached to blockPage or blockNews filesets
  80.      * @Route("/plik-do-pobrania/{id}/{hash}", name="page_file_download_default", requirements={"id"="\d+"})
  81.      * @Route("/{locale}/file-to-download/{id}/{hash}", name="page_file_download", requirements={"locale"="%app.front.locale.rest%", "id"="\d+"})
  82.      * @param Request $request
  83.      * @param int $id
  84.      * @throws NotFoundHttpException
  85.      * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\RedirectResponse
  86.      */
  87.     public function downloadFileAction(Request $requestint $id)
  88.     {
  89.         
  90.         $file $this->getDoctrine()->getManager()->getRepository('Openform:FilesetsFile')->find($id);
  91.         if (!$file) {
  92.             throw new NotFoundHttpException();
  93.         }
  94.         if (!$file->getStat() || ($file->getLang() != null && $file->getLang() != $request->getLocale())) {
  95.             throw new NotFoundHttpException();
  96.         }
  97.         if ($file->getFilePath() && $file->getParent() && $file->getParent()->getStat()) {
  98.             if (is_file($file->getProjectDir() . $file->getFilePath())) {
  99.                 $title $file->getTranslation()[$request->getLocale()]->getTitle();
  100.                 if ($title != '') {
  101.                     $title Urlizer::urlize($title);
  102.                 } else {
  103.                     $title Urlizer::urlize($file->getFilePath());
  104.                 }
  105.                 if ($title != '') {
  106.                     $title $title '.' strtolower(pathinfo($file->getFilePath(), PATHINFO_EXTENSION));
  107.                 } else {
  108.                     $title $file->getTitle();
  109.                 }
  110.                 $respons = new BinaryFileResponse($file->getProjectDir() . $file->getFilePath());
  111.                 $respons->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$title);
  112.                 return $respons;
  113.             } else
  114.                 throw new NotFoundHttpException();
  115.         }
  116.         return $this->redirect(
  117.             $this->routeLocalizer->generate('index', [], $request->getLocale())
  118.         );
  119.     }
  120.     /**
  121.      * User click download button in box
  122.      * similar to function UserController:downloadebookAction
  123.      * @param Request $request
  124.      * @param int $id BookVariant.id
  125.      * @throws NotFoundHttpException
  126.      * @return \Symfony\Component\HttpFoundation\BinaryFileResponse|\Symfony\Component\HttpFoundation\RedirectResponse
  127.      * 
  128.      * @Route("/{locale}/download-packit/{id}/{hash}", name="page_file_download_packit", requirements={"locale"="%app.front.locale.rest%", "hash"="[a-zA-Z0-9\-_]+", "id"="\d+" } )
  129.      * @Route("/pobierz-pakiet/{id}/{hash}", name="page_file_download_packit_default", requirements={ "hash"="[a-zA-Z0-9\-_]+", "id"="\d+" } )
  130.      */
  131.     public function downloadeFilePackitAction(Request $request$id$hash)
  132.     {
  133.         $locale $request->getLocale();
  134.         /** @var \App\Package\Openform\Entity\Filesets */
  135.         $fileset $this->getDoctrine()->getManager()->getRepository('Openform:Filesets')->find($id);
  136.         if (!$fileset || $fileset->makeHash() !== $hash) {
  137.             throw new NotFoundHttpException();
  138.         }
  139.         if ($fileset->getStat() && isset($fileset->getTranslation()[$locale]) && $fileset->getTranslation()[$locale]->getTitle()) {
  140.             $files $fileset->getVisibleFiles($locale);
  141.             $this->setZipPath();
  142.             if (count($files)) {
  143.                 //if many files zippin
  144.                 if (count($files) > 1) {
  145.                     $hasFiles false;
  146.                     $zip = new \ZipArchive();
  147.                     $filename 'zip-packit-files-' $fileset->getId() . '.zip';
  148.                     if ($zip->open($this->zip_path $filename\ZipArchive::CREATE \ZipArchive::OVERWRITE) !== TRUE) {
  149.                         throw new NotFoundHttpException();
  150.                     }
  151.                     foreach ($files as $file) {
  152.                         if ($file->getStat() && $file->getFilePath()) {
  153.                             if (is_file($file->getProjectDir() . $file->getFilePath())) {
  154.                                 $zip->addFile($file->getProjectDir() . $file->getFilePath(), \pathinfo($file->getFilePath(), PATHINFO_BASENAME));
  155.                                 $hasFiles true;
  156.                             } else {
  157.                                 throw new NotFoundHttpException();
  158.                             }
  159.                         }
  160.                     }
  161.                     $zip->close();
  162.                     if ($hasFiles && is_file($this->zip_path $filename)) {
  163.                         $book_title Urlizer::urlize(trim($fileset->getTranslation()[$locale]->getTitle()));
  164.                         if ($book_title == '') {
  165.                             $book_title $filename;
  166.                         }
  167.                         $respons = new BinaryFileResponse($this->zip_path $filename);
  168.                         $respons->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$book_title '.zip');
  169.                         return $respons;
  170.                     } else
  171.                         throw new NotFoundHttpException();
  172.                 }
  173.                 //if one file
  174.                 else {
  175.                     foreach ($files as $file) {
  176.                         if ($file->getStat() && $file->getFilePath()) {
  177.                             if (is_file($file->getProjectDir() . $file->getFilePath())) {
  178.                                 $respons = new BinaryFileResponse($file->getProjectDir() . $file->getFilePath());
  179.                                 $respons->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
  180.                                 return $respons;
  181.                             } else {
  182.                                 throw new NotFoundHttpException();
  183.                             }
  184.                         }
  185.                     }
  186.                 }
  187.             } else {
  188.                 throw new NotFoundHttpException();
  189.             }
  190.         }
  191.         throw new NotFoundHttpException();
  192.         // return $this->redirect(
  193.         //     $this->routeLocalizer->generate('index', [], $request->getLocale() )
  194.         // );
  195.     }
  196.     /**
  197.      * helper, make folder if not exists and set path to zip folder files to download
  198.      * @return void
  199.      */
  200.     private function setZipPath(): void
  201.     {
  202.         $this->zip_path str_replace('public_html/media''media_secured'__MEDIA_DIR__) . '/zip_tmp/';
  203.         if (!is_dir($this->zip_path)) {
  204.             $f = new Filesystem();
  205.             $f->mkdir($this->zip_path);
  206.         }
  207.     }
  208. }