src/Notaris/FrontendBundle/Controller/BrochureController.php line 70

Open in your IDE?
  1. <?php
  2. namespace Notaris\FrontendBundle\Controller;
  3. use Notaris\AdminBundle\Entity\Attachment;
  4. use Notaris\AdminBundle\ValueObject\BrochureType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. class BrochureController extends AbstractController
  11. {
  12.     public function indexAction($subsiteSlug, $sectionSlug = null)
  13.     {
  14.         $localeService = $this->get('notaris.locale.listener');
  15.         $locale = $localeService->getLocale();
  16.         $type = BrochureType::brochure();
  17.         if ($sectionSlug === null || $sectionSlug === '') {
  18.             $sectionSlug = 'brochures';
  19.             if($locale == 'fr'){
  20.                 $sectionSlug = 'publications';
  21.             }
  22.         }
  23.         if($locale == 'de'):
  24.             $locale = 'fr';
  25.         endif;
  26.         $currentslug = $sectionSlug;
  27.         $manager = $this->getDoctrine()->getManager();
  28.         $subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
  29.         $section_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Section');
  30.         $subsite = $subsite_repo->findOneBySlug($subsiteSlug);
  31.         if(!$subsite or !$subsite->getIsActive()){
  32.             return $this->redirect($this->generateUrl('notaris_frontend_homepage'), 301);
  33.         }
  34.         $brochure_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Brochure');
  35.         $brochures = [];
  36.         if($subsiteSlug == 'nieuws-pers' || $subsiteSlug == 'nouveautes') {
  37.             if ($sectionSlug === 'infofiches' || $sectionSlug === 'fiches-dinfo') {
  38.                 $type = BrochureType::infofiche();
  39.                 $brochures = $brochure_repo->getListBrochuresIsGeneralPublic($locale, BrochureType::infofiche());
  40.             } else {
  41.                 $brochures = $brochure_repo->getListBrochuresIsGeneralPublic($locale, BrochureType::brochure());
  42.             }
  43.         } else {
  44.             $brochures = $brochure_repo->getListBrochuresSubsitePublic($locale, $subsite->getId()   );
  45.         }
  46.         $section = $section_repo->findOneBySlug($sectionSlug);
  47.         if(!$section){
  48.             throw new NotFoundHttpException('Sorry not existing!');
  49.         }
  50.         return $this->render('@NotarisFrontend/brochure/index.html.twig', [
  51.             'brochures' => $brochures, 'section'=>$section, 'subsite' => $subsite, 'currentslug' => $currentslug, 'type' => $type
  52.         ]);
  53.     }
  54.     public function brochuresAction($subsiteSlug) {
  55.         $localeService = $this->get('notaris.locale.listener');
  56.         $locale = $localeService->getLocale();
  57.         if ($locale === 'fr') {
  58.             return $this->indexAction($subsiteSlug, 'publications');
  59.         }
  60.         return $this->indexAction($subsiteSlug, 'brochures');
  61.     }
  62.     public function infofichesAction($subsiteSlug) {
  63.         $localeService = $this->get('notaris.locale.listener');
  64.         $locale = $localeService->getLocale();
  65.         if ($locale === 'fr') {
  66.             return $this->indexAction($subsiteSlug, 'fiches-dinfo');
  67.         }
  68.         return $this->indexAction($subsiteSlug, 'infofiches');
  69.     }
  70.     public function downloadAction($brochureId)
  71.     {
  72.         $localeService = $this->get('notaris.locale.listener');
  73.         $locale = $localeService->getLocale();
  74.         if($locale == 'de'):
  75.             $locale = 'fr';
  76.         endif;
  77.         $manager = $this->getDoctrine()->getManager();
  78.         $brochure_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Brochure');
  79.         $brochure = $brochure_repo->find($brochureId);
  80.         $brochure->setCountClicked($brochure->getCountClicked()+1);
  81.         $manager->flush();
  82.         $attachment = new Attachment();
  83.         $attachment = $brochure->getAsset()->getAssetAttachments()[0]->getAttachment();
  84.         $file = $this->get('kernel')->getRootDir() . "/../web/uploads/$locale" . $attachment->getFilename() ;
  85.         $response = new BinaryFileResponse($file);
  86.         BinaryFileResponse::trustXSendfileTypeHeader();
  87.         $response->headers->set('Content-type', $attachment->getFileType());
  88.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $attachment->getOriginalFilename()  );
  89.         return $response;
  90.     }
  91. }