<?php
namespace Notaris\FrontendBundle\Controller;
use Notaris\AdminBundle\Entity\Attachment;
use Notaris\AdminBundle\ValueObject\BrochureType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BrochureController extends AbstractController
{
public function indexAction($subsiteSlug, $sectionSlug = null)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
$type = BrochureType::brochure();
if ($sectionSlug === null || $sectionSlug === '') {
$sectionSlug = 'brochures';
if($locale == 'fr'){
$sectionSlug = 'publications';
}
}
if($locale == 'de'):
$locale = 'fr';
endif;
$currentslug = $sectionSlug;
$manager = $this->getDoctrine()->getManager();
$subsite_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Subsite');
$section_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Section');
$subsite = $subsite_repo->findOneBySlug($subsiteSlug);
if(!$subsite or !$subsite->getIsActive()){
return $this->redirect($this->generateUrl('notaris_frontend_homepage'), 301);
}
$brochure_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Brochure');
$brochures = [];
if($subsiteSlug == 'nieuws-pers' || $subsiteSlug == 'nouveautes') {
if ($sectionSlug === 'infofiches' || $sectionSlug === 'fiches-dinfo') {
$type = BrochureType::infofiche();
$brochures = $brochure_repo->getListBrochuresIsGeneralPublic($locale, BrochureType::infofiche());
} else {
$brochures = $brochure_repo->getListBrochuresIsGeneralPublic($locale, BrochureType::brochure());
}
} else {
$brochures = $brochure_repo->getListBrochuresSubsitePublic($locale, $subsite->getId() );
}
$section = $section_repo->findOneBySlug($sectionSlug);
if(!$section){
throw new NotFoundHttpException('Sorry not existing!');
}
return $this->render('@NotarisFrontend/brochure/index.html.twig', [
'brochures' => $brochures, 'section'=>$section, 'subsite' => $subsite, 'currentslug' => $currentslug, 'type' => $type
]);
}
public function brochuresAction($subsiteSlug) {
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
if ($locale === 'fr') {
return $this->indexAction($subsiteSlug, 'publications');
}
return $this->indexAction($subsiteSlug, 'brochures');
}
public function infofichesAction($subsiteSlug) {
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
if ($locale === 'fr') {
return $this->indexAction($subsiteSlug, 'fiches-dinfo');
}
return $this->indexAction($subsiteSlug, 'infofiches');
}
public function downloadAction($brochureId)
{
$localeService = $this->get('notaris.locale.listener');
$locale = $localeService->getLocale();
if($locale == 'de'):
$locale = 'fr';
endif;
$manager = $this->getDoctrine()->getManager();
$brochure_repo = $manager->getRepository('Notaris\AdminBundle\Entity\Brochure');
$brochure = $brochure_repo->find($brochureId);
$brochure->setCountClicked($brochure->getCountClicked()+1);
$manager->flush();
$attachment = new Attachment();
$attachment = $brochure->getAsset()->getAssetAttachments()[0]->getAttachment();
$file = $this->get('kernel')->getRootDir() . "/../web/uploads/$locale" . $attachment->getFilename() ;
$response = new BinaryFileResponse($file);
BinaryFileResponse::trustXSendfileTypeHeader();
$response->headers->set('Content-type', $attachment->getFileType());
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $attachment->getOriginalFilename() );
return $response;
}
}