src/Controller/Frontend/DocumentController.php line 411

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Security\Core\Security;
  6. use App\Entity\Media\DocumentType;
  7. use App\Entity\Media\Category;
  8. use App\Entity\Media\Document;
  9. use App\Form\Media\DocumentFrontFilterType;
  10. use App\Entity\Federation\Classe;
  11. use App\Entity\Federation\Boat;
  12. use App\Entity\Federation\Skipper;
  13. use Symfony\Component\Finder\Exception\AccessDeniedException;
  14. use App\Entity\Media\Conversion;
  15. use App\Entity\Media\Tag;
  16. use App\Entity\History\History;
  17. use App\Form\Media\DownloadMultipleType;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  20. use Symfony\Component\HttpFoundation\File\File;
  21. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  22. use Gedmo\Sluggable\Util\Urlizer;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use App\Entity\Federation\Participation;
  25. use App\Entity\Site\AccessSite;
  26. use App\Entity\Media\Cart;
  27. use App\Service\Site\SiteManager;
  28. use App\Service\Aws\AwsManager;
  29. use App\Service\Media\DownloadManager;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. class DocumentController extends AbstractController
  32. {
  33.     public function listAction(Request $requeststring $documentTypeSlug$pageCategory $category nullSecurity $security$clearSiteManager $siteManager)
  34.     {
  35.         $requestDatas $request->request->all();
  36.         $page intval($page);
  37.         if ($page 1$page 1;
  38.         $itemsPerPage $this->getParameter('application.front_item_per_page');
  39.         if($clear == 'clear')
  40.             $this->get('session')->remove('document_frontend_filter');
  41.         if($request->isMethod('POST') && !empty($requestDatas))
  42.             $this->get('session')->set('document_frontend_filter'$requestDatas['document_frontend_filter']);
  43.         $em $this->getDoctrine()->getManager();
  44.         $user $security->getUser();
  45.         $documentType $em->getRepository(DocumentType::class)->findOneByDirectoryName($documentTypeSlug);
  46.         $access $siteManager->userAccessSite($user);
  47.         if(empty($category) || (!is_null($category) && !$access->getCategories()->contains($category)))
  48.             throw new AccessDeniedException();
  49.         $form $this->createForm(DocumentFrontFilterType::class, null, array(
  50.             'race' => $siteManager->getCurrentSite()->getRace()
  51.         ));
  52.         $filter true;
  53.         if(!empty($this->get('session')->get('document_frontend_filter')))
  54.         {
  55.             $sessionDatas $this->get('session')->get('document_frontend_filter');
  56.             $classes $boats $skippers $tags = array();
  57.             if(empty($sessionDatas['classe']) && empty($sessionDatas['boat']) && empty($sessionDatas['skipper']))
  58.                 $filter false;
  59.             if(!empty($sessionDatas['date']))
  60.             {
  61.                 $date explode('/'$sessionDatas['date']);
  62.                 $form->get('date')->setData(new \DateTime($date[2].'-'.$date[1].'-'.$date[0]));
  63.             }
  64.             if(!empty($sessionDatas['tags']))
  65.             {
  66.                 foreach ($sessionDatas['tags'] as $tag)
  67.                     array_push($tags$em->getRepository(Tag::class)->find($tag));
  68.                 $form->get('tags')->setData($tags);
  69.             }
  70.             if(!empty($sessionDatas['classe']))
  71.             {
  72.                 $classe $em->getRepository(Classe::class)->find($sessionDatas['classe']);
  73.                 array_push($classes$classe);
  74.                 $form->get('classe')->setData($classe);
  75.             } else {
  76.                 if($category->isRestrictiveCategory())
  77.                     $classes $access->getClasses();
  78.                 else
  79.                     $classes = [];
  80.             }
  81.             if(!empty($sessionDatas['boat']))
  82.             {
  83.                 $boat $em->getRepository(Boat::class)->find($sessionDatas['boat']);
  84.                 array_push($boats$boat);
  85.                 $form->get('boat')->setData($boat);
  86.             } else {
  87.                 if($category->isRestrictiveCategory())
  88.                     $boats $access->getBoats();
  89.                 else
  90.                     $boats = [];
  91.             }
  92.             if(!empty($sessionDatas['skipper']))
  93.             {
  94.                 $skipper $em->getRepository(Skipper::class)->find($sessionDatas['skipper']);
  95.                 array_push($skippers$skipper);
  96.                 $form->get('skipper')->setData($skipper);
  97.             } else {
  98.                 if($category->isRestrictiveCategory())
  99.                     $skippers $access->getSkippers();
  100.                 else
  101.                     $skippers = [];
  102.             }
  103.             //Get All Result
  104.             $documents $this->getDoctrine()
  105.                 ->getManager()
  106.                 ->getRepository(Document::class)
  107.                 ->getFrontAccessDocuments($page$itemsPerPage$documentType$filter$siteManager->getCurrentSite()->getRace(), $category$classes$boats$skippers$form['date']->getData(), $tags);
  108.         } else {
  109.             if($category->isRestrictiveCategory()){
  110.                 $classes $access->getClasses();
  111.                 $boats $access->getBoats();
  112.                 $skippers $access->getSkippers();
  113.             } else {
  114.                 $classes = [];
  115.                 $boats = [];
  116.                 $skippers = [];
  117.             }
  118.             //Get All Result
  119.             $documents $this->getDoctrine()
  120.                 ->getManager()
  121.                 ->getRepository(Document::class)
  122.                 ->getFrontAccessDocuments($page$itemsPerPage$documentTypefalse$siteManager->getCurrentSite()->getRace(), $category$classes$boats$skippers);
  123.         }
  124.         $nbPages ceil(count($documents) / $itemsPerPage);
  125.         if ($page $nbPages && $page 1)
  126.             throw $this->createNotFoundException($this->get('translator')->trans("messages.error.page_no_exist", array('%page%' => $page), 'InodiaBundlesBackendBundle'));
  127.         $this->forward('App\Controller\Frontend\CartController::loadCart', ['isRequest' => true]);
  128.         return $this->render('Frontend/Views/Document/list.html.twig', array(
  129.             'access' => $access,
  130.             'documentType' => $documentType,
  131.             'category' => $category,
  132.             'documents' => $documents,
  133.             'form' => $form->createView(),
  134.             'nbPages' => $nbPages,
  135.             'page' => $page,
  136.             'cart' => $em->getRepository(Cart::class)->findOneByAccessSite($access)
  137.         ));
  138.     }
  139.     
  140.     public function proofValidationAction(Request $requestDocument $documentSecurity $securityTranslatorInterface $translator)
  141.     {
  142.         $user $security->getUser();
  143.         
  144.         if(sizeof($document->getSkippers()) > || sizeof($document->getBoats()) > 0){
  145.             $referents $this->getDoctrine()->getManager()->getRepository(\App\Entity\Federation\Participation::class)->getReferents($document->getRace(), $document->getBoats(), $document->getSkippers());
  146.             
  147.             foreach ($referents as $key => $referent)
  148.                 $referents[$key] = $this->getDoctrine()->getManager()->getRepository(\App\Entity\User\User::class)->find($referent['id']);
  149.         }
  150.         
  151.         if(!in_array($user$referents) || $document->getStatus() != 1)
  152.             return new AccessDeniedException();
  153.         $document->setStatus(0);
  154.         $document->setProofValidationUser($user);
  155.         $document->setProofValidationDate(new \DateTime());
  156.         
  157.         $this->getDoctrine()->getManager()->flush();
  158.         $this->addFlash('success'$translator->trans('document.email.proof-validation-confirmation'));
  159.         
  160.         return $this->redirectToRoute('frontend_homepage');
  161.     }
  162.     
  163.     public function viewAction(Document $documentSiteManager $siteManagerSecurity $securityAwsManager $awsManager)
  164.     {
  165.         $em $this->getDoctrine()->getManager();
  166.         $proofValidation false;
  167.         $user $security->getUser();
  168.         $access $siteManager->userAccessSite($user);
  169.         
  170.         if(sizeof($document->getSkippers()) > || sizeof($document->getBoats()) > 0){
  171.             $referents $this->getDoctrine()->getManager()->getRepository(\App\Entity\Federation\Participation::class)->getReferents($document->getRace(), $document->getBoats(), $document->getSkippers());
  172.             
  173.             if(!empty($referents))
  174.             {
  175.                 foreach ($referents as $key => $referent)
  176.                     if(!empty($referent['id']))
  177.                         $referents[$key] = $this->getDoctrine()->getManager()->getRepository(\App\Entity\User\User::class)->find($referent['id']);
  178.             }
  179.         }
  180.         
  181.         if(isset($referents))
  182.             if(in_array($user$referents) && $document->getStatus() == 1)
  183.                 $proofValidation true;
  184.         if($document->getDocumentType()->getDirectoryName() == 'documents') {
  185.             $pathinfo pathinfo($document->getFile()['Key']);
  186.             $previewUrl $awsManager->createGetUrlPreviewPresignedRequest($document->getRace()->getDirectoryName(), $document->getFile()['Key'], null'application/'.$pathinfo['extension']);
  187.         }
  188.         return $this->render('Frontend/Views/Document/view-'.$document->getDocumentType()->getDirectoryName().'.html.twig', array(
  189.             'document' => $document,
  190.             'proofValidation' => $proofValidation,
  191.             'cart' => $em->getRepository(Cart::class)->findOneByAccessSite($access),
  192.             'previewUrl' => !empty($previewUrl) ? $previewUrl '',
  193.             'application' => !empty($pathinfo['extension']) ? 'application/'.$pathinfo['extension'] : '',
  194.         ));
  195.     }
  196.     
  197.     public function pictureAction(Request $requestDocument $document)
  198.     {
  199.         $proofValidation false;
  200.         $downloadUrls = array();
  201.         $em $this->getDoctrine()->getManager();
  202.         $user $this->get('security.token_storage')->getToken()->getUser();
  203.         $form $this->createForm(DownloadMultipleType::class, null, array()); 
  204.         
  205.         $form->handleRequest($request);        
  206.         if ($form->isSubmitted() && $form->isValid()) {
  207.             $documents $form['documents']->getData();
  208.             
  209.             foreach($documents as $downloadId)
  210.             {
  211.                 $download $em->getRepository(Document::class)->find($downloadId);
  212.                 $parentDocument $download->getParentDocument();
  213.                 $this->saveDownload($user$download);                
  214.                 array_push($downloadUrls$download->getUri());
  215.             }
  216.             
  217.             if(!empty($downloadUrls))
  218.             {                
  219.                 $name str_replace($parentDocument->getDocumentType()->getDirectoryName().'/'''$parentDocument->getDirectoryName()).'.zip';
  220.                 $path 'download/'.$name;
  221.                 $this->createArchive($downloadUrls$path);
  222.                 
  223.                 $response = new BinaryFileResponse($path);
  224.                 $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT$name);
  225.                 
  226.                 $response->deleteFileAfterSend(true);
  227.                 
  228.                 return $response;
  229.             }
  230.         }
  231.         
  232.         if(sizeof($document->getSkippers()) > || sizeof($document->getBoats()) > 0){
  233.             $referents $em->getRepository(\App\Entity\Federation\Participation::class)->getReferents($document->getRace(), $document->getBoats(), $document->getSkippers());
  234.             
  235.             foreach ($referents as $key => $referent)
  236.             {
  237.                 if(!empty($referent['id']))
  238.                     $referents[$key] = $em->getRepository(\App\Entity\User\User::class)->find($referent['id']);
  239.             }
  240.         }
  241.         
  242.         if(isset($referents))
  243.             if(in_array($user$referents) && $document->getStatus() == 1)
  244.                 $proofValidation true;
  245.                 
  246.         return $this->render('Frontend/Views/Document/picture.html.twig', array(
  247.             'document' => $document,
  248.             'proofValidation' => $proofValidation,
  249.             'form' => $form->createView()
  250.         ));
  251.     }
  252.     
  253.     public function downloadAction(Document $documentConversion $conversion nullAwsManager $awsDownloadManager $downloadManagerSecurity $security)
  254.     {
  255.         $bucket $document->getRace()->getDirectoryName();
  256.         $user $security->getUser();
  257.         $name Urlizer::urlize($document->getTitle());
  258.                 
  259.         if(!is_null($conversion))
  260.             $download $conversion;
  261.         else 
  262.             $download $document;
  263.         
  264.         $fileName explode('.'basename($download->getFile()['Key']));
  265.         $extension end($fileName);
  266.         if($user && $download)
  267.         {               
  268.             $this->saveDownload($user$download$downloadManager);
  269.             $generateUrl $aws->createGetUrlPresignedRequest($bucket$download->getFile()['Key'], $name.'.'.$extension);
  270.             return $this->redirect($generateUrl);
  271.         }
  272.         
  273.         exit();
  274.     }
  275.         
  276.     public function saveDownload($user$downloadDownloadManager $downloadManager)
  277.     {
  278.         $downloadManager->saveDownload($download);
  279.     }
  280.     
  281.     public function createArchive($urls$path)
  282.     {
  283.         $zip false;
  284.         
  285.         if(!empty($urls))
  286.         {
  287.             $zip = new \ZipArchive();
  288.             $zip->open($path,  \ZipArchive::CREATE);
  289.             
  290.             foreach($urls as $url){
  291.                 $file file_get_contents($url);
  292.                 $zip->addFromString(basename($url), $file);
  293.             }
  294.             
  295.             $zip->close();            
  296.         }
  297.                 
  298.         return $zip;
  299.     }
  300.     
  301.     public function getDocumentsFiltersOptionsAction(Request $requestCategory $categoryClasse $classe nullBoat $boat nullSkipper $skipper nullSiteManager $siteManager)
  302.     {
  303.         $response = array(
  304.             'classes' => array(),
  305.             'boats' => array(),
  306.             'skippers' => array()
  307.         );
  308.         
  309.         if($request->isXmlHttpRequest()) {
  310.             
  311.             $em $this->getDoctrine()->getManager();
  312.             $race $siteManager->getCurrentSite()->getRace();
  313.             $user $this->get('security.token_storage')->getToken()->getUser();
  314.             $access $siteManager->userAccessSite($user);
  315.             
  316.             if(is_null($boat)) $response['boats'][] = array('id' => '''text' => '');
  317.             if(is_null($skipper)) $response['skippers'][] = array('id' => '''text' => '');
  318.             if(is_null($classe)) $response['classes'][] = array('id' => '''text' => '');
  319.             
  320.             if(!is_null($boat))
  321.             {                
  322.                 $response['boats'][] = array('id' => $boat->getId(), 'text' => $boat->getName());
  323.                 $response['classes'][] = array('id' => $boat->getClasse()->getId(), 'text' => $boat->getClasse()->getName());
  324.                 
  325.                 $participation $em->getRepository(Participation::class)->findOneBy(array('race' => $race'boat' => $boat));
  326.                 $response['skippers'][] = array('id' => $participation->getSkipper()->getId(), 'text' => $participation->getSkipper()->getName());
  327.             
  328.             } else  if(!is_null($skipper)){
  329.                 
  330.                 $response['skippers'][] = array('id' => $skipper->getId(), 'text' => $skipper->getName());
  331.                 
  332.                 $participation $em->getRepository(Participation::class)->findOneBy(array('race' => $race'skipper' => $skipper));
  333.                 $response['boats'][] = array('id' => $participation->getBoat()->getId(), 'text' => $participation->getBoat()->getName());
  334.                 $response['classes'][] = array('id' => $participation->getBoat()->getClasse()->getId(), 'text' => $participation->getBoat()->getClasse()->getName());
  335.                 
  336.             
  337.             } else if(!is_null($classe)) {
  338.                 
  339.                 $response['classes'][] = array('id' => $classe->getId(), 'text' => $classe->getName());
  340.                 $boats $em->getRepository(Boat::class)->findBy(array('classe' => $classe));
  341.                 
  342.                 if(sizeof($access->getBoats()) > 0)
  343.                 {
  344.                     foreach($access->getBoats() as $boat)
  345.                         foreach($boats as $b)
  346.                             if($b == $boat)
  347.                                 $response['boats'][] = array('id' => $boat->getId(), 'text' => $boat->getName());
  348.                     
  349.                 } else if(sizeof($boats) > 0){
  350.                     foreach($boats as $boat)
  351.                         $response['boats'][] = array('id' => $boat->getId(), 'text' => $boat->getName());
  352.                 }
  353.                 
  354.                 if(!empty($response['boats']))
  355.                 {
  356.                     foreach($response['boats'] as $boat)
  357.                     {
  358.                         $participation $em->getRepository(Participation::class)->findOneBy(array('race' => $race'boat' => $boat));
  359.                         if(!empty($participation))
  360.                             $response['skippers'][] = array('id' => $participation->getSkipper()->getId(), 'text' => $participation->getSkipper()->getName());
  361.                     }   
  362.                 }
  363.                 
  364.             } else {
  365.                 
  366.                 $response $this->getDocumentsFiltersOptionsByAccess($access$response);
  367.             }
  368.                     
  369.         }
  370.         return new JsonResponse($response200);
  371.     }
  372.     
  373.     public function searchInOptionsArray($array null$id)
  374.     {
  375.         $find false;
  376.         
  377.         foreach($array as $value)
  378.             if($value['id'] == $id)
  379.                 $find true;
  380.         
  381.         return $find;        
  382.     }
  383.     
  384.     public function getDocumentsFiltersOptionsByAccess(AccessSite $access$response)
  385.     {   
  386.         $result $response;
  387.         $em $this->getDoctrine()->getManager();
  388.         $participations $em->getRepository(Participation::class)->findBy(array('race' => $access->getSite()->getRace()));
  389.         
  390.         if(sizeof($access->getClasses()) == 0)
  391.         {
  392.             foreach($participations as $participation)
  393.                 if(!$this->searchInOptionsArray($result['classes'], $participation->getBoat()->getClasse()->getId()))
  394.                     $result['classes'][] = array('id' => $participation->getBoat()->getClasse()->getId(), 'text' => $participation->getBoat()->getClasse()->getName());
  395.             
  396.         } else {
  397.             
  398.             foreach($access->getClasses() as $classe)
  399.                 $result['classes'][] = array('id' => $classe->getId(), 'text' => $classe->getName());             
  400.         }
  401.         
  402.         if(sizeof($access->getBoats()) == 0)
  403.         {
  404.             foreach($participations as $participation)
  405.                 $result['boats'][] = array('id' => $participation->getBoat()->getId(), 'text' => $participation->getBoat()->getName());
  406.             
  407.         } else {
  408.             
  409.             foreach($access->getBoats() as $boat)
  410.                 $result['boats'][] = array('id' => $boat->getId(), 'text' => $boat->getName());
  411.         }
  412.         
  413.         if(sizeof($access->getSkippers()) == 0)
  414.         {
  415.             foreach($participations as $participation)
  416.                 $result['skippers'][] = array('id' => $participation->getSkipper()->getId(), 'text' => $participation->getSkipper()->getName());
  417.             
  418.         } else {
  419.             
  420.             foreach($access->getSkippers() as $skipper)
  421.                 $result['skippers'][] = array('id' => $skipper->getId(), 'text' => $skipper->getName());
  422.         }   
  423.         
  424.         return $result;
  425.     }
  426. }