vendor/score/cms/src/Controller/Document/DocumentPublicController.php line 93

Open in your IDE?
  1. <?php
  2. namespace Score\CmsBundle\Controller\Document;
  3. use Doctrine\Common\Collections\Collection;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Score\CmsBundle\Entity\Document\Document;
  12. use Score\CmsBundle\Entity\Document\DocumentCategory;
  13. use Score\CmsBundle\Entity\Document\DocumentDomain;
  14. class DocumentPublicController extends AbstractController {
  15.     public function documentsByDomainAction($domain = null, $size = "small", $asArray = false) {
  16.         //public function documentsByCategoryAction(Request $request, ManagerRegistry $doctrine, $domain): Response {
  17.         $em = $this->getDoctrine()->getManager();
  18.         $domainEntity = $em->getRepository(DocumentDomain::class)->findOneBySeoName($domain);
  19.         $documents = [];
  20.         if ($domainEntity) {
  21.             foreach ($domainEntity->getDocumentDescriptions() as $dd) {
  22.                 if (!$dd->getDocument()->getIsDeleted() && $dd->getDocument()->getPublic() && $dd->getDocument()->getVisibility()){
  23.                     $documents[] = $dd->getDocument();
  24.                 }
  25.             }
  26.         }
  27.         if ($asArray) {
  28.             return $documents;
  29.         }
  30.         return $this->render("@ScoreCms/Public/default/documents_block.html.twig", [
  31.             'category' => "",
  32.             'documents' => $documents,
  33.             'domain' => $domainEntity,
  34.             'size' => $size
  35.         ]);
  36.     }
  37.     public function documentsByCategoryAction($category = null, $size = "small", $asArray = false) {
  38.         //public function documentsByCategoryAction(Request $request, ManagerRegistry $doctrine, $category): Response {
  39.         $em = $this->getDoctrine()->getManager();
  40.         $categoryEntity = $em->getRepository(DocumentCategory::class)->findOneBy(["seoName" => $category, 'visibility' => true]);
  41.         $documents = [];
  42.         if ($categoryEntity) {
  43.             $documents = $categoryEntity->getDocuments()->filter(function($d){return !$d->getIsDeleted() &&  $d->getPublic() && $d->getVisibility();});
  44.         }
  45.         if ($asArray) {
  46.             return $documents;
  47.         }
  48.         return $this->render("@ScoreCms/Public/default/documents_block.html.twig", [
  49.             'category' => $categoryEntity,
  50.             'documents' => $documents,
  51.             'domain' => "",
  52.             'size' => $size
  53.         ]);
  54.     }
  55.     /**
  56.      * @Route("/dokumenty", name="documents")
  57.      * @Route("/dokumenty/{filter}", name="documents-filtered")
  58.      */
  59.     public function listAction(Request $request, ManagerRegistry $doctrine, $filter = null): Response {
  60.         $em = $doctrine->getManager();
  61.         $documents = [];
  62.         if ($filter) { // filter kategorii ak nenajde ide na oblasti
  63.             $documents = $this->documentsByCategoryAction($filter, null, true);
  64.             if (!count($documents)) {
  65.                 $documents = $this->documentsByDomainAction($filter, null, true);
  66.             }
  67.         } else {
  68.             $documents = $em->getRepository(Document::class)->findBy(['isDeleted' => false, 'public' => true]);
  69.         }
  70.         return $this->render('@ScoreCms/Public/default/documents.html.twig', [
  71.             'documents' => $documents,
  72.         ]);
  73.     }
  74.     /**
  75.      * @Route("/dokument/{seoId}", name="document")
  76.      */
  77.     public function documentAction(Request $request, ManagerRegistry $doctrine, $seoId): Response {
  78.         $em = $doctrine->getManager();
  79.         $document = $em->getRepository(Document::class)->findOneBySeoId($seoId);
  80.         if (!$document || $document->getIsDeleted() || !$document->getPublic()) {
  81.             return $this->redirectToRoute('documents');
  82.         }
  83.         if (!$document->getHasDetail() && $document->getActualFile()) {
  84.             return $this->redirectToRoute('document-download-ext', [
  85.                 'seoId' => $document->getSeoid(),
  86.                 'ext' => $document->getExtension()
  87.             ]);
  88.         }
  89.         return $this->render('@ScoreCms/Public/default/document.html.twig', [
  90.             'document' => $document,
  91.         ]);
  92.     }
  93.     /**
  94.      * @Route("/dokument/f/{seoId}.{ext}", name="document-download-ext")
  95.      * @Route("/dokument/f/{seoId}", name="document-download")
  96.      */
  97.     public function documentDownloadAction(Request $request, ManagerRegistry $doctrine, $seoId, $ext = null): Response {
  98.         $em = $doctrine->getManager();
  99.         $document = $em->getRepository(Document::class)->findOneBySeoId($seoId);
  100.         if (!$document || $document->getIsDeleted() || !$document->getPublic()) {
  101.             return $this->redirectToRoute('documents');
  102.         }
  103.         $file = $document->getActualFile();
  104.         if (!$file) {
  105.             return $this->redirectToRoute('documents');
  106.         }
  107.         if ($file->getIsExternal()) {
  108.             return $this->redirect($file->getUrl());
  109.         }
  110.         $root = $this->getParameter('kernel.project_dir') . '/public';
  111.         $response = new BinaryFileResponse($root . $file->getUrl());
  112.         return $response;
  113.     }
  114.     /**
  115.      * @Route("/document-info/{seoId}.{ext}", name="document-info-ext")
  116.      * @Route("/document-info/{seoId}", name="document-info")
  117.      */
  118.     public function documentInfoAction(Request $request, ManagerRegistry $doctrine, $seoId, $ext = null): Response {
  119.         $em = $doctrine->getManager();
  120.         $document = $em->getRepository(Document::class)->findOneBySeoId($seoId);
  121.         if (!$document || $document->getIsDeleted() || !$document->getPublic()) {
  122.             return $this->redirectToRoute('documents');
  123.         }
  124.         return new JsonResponse([
  125.             "name" => $document->getName(),
  126.             "info" => $document->getInfo(),
  127.             "link" => $this->generateUrl('document-download-ext', [
  128.                 'seoId' => $document->getSeoId(),
  129.                 'ext' => $document->getExtension(),
  130.             ])
  131.         ]);
  132.     }
  133. }