<?php
namespace App\Controller\Frontend;
use App\Application\Abonnent\AbonnentService;
use App\Application\Epaper\EpaperService;
use App\Form\Abo\AbonnentLoginType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Routing\Annotation\Route;
#[Route(path: '/epaper')]
class EpaperController extends AbstractController
{
#[Route(path: '', name: 'fe.epaper')]
public function index(
Request $request,
AbonnentService $abonnentService,
EpaperService $epaperService
): Response {
$abonnent = $abonnentService->authenticate($request);
$ausgaben = $epaperService->getAllIssuesSinceLastYear();
$epaperService->generateImagesFromPDF($ausgaben);
$ausgabenMitBild = $epaperService->getIssuesWithPicture($ausgaben, 5);
$abonnentLoginForm = $this->createForm(AbonnentLoginType::class, ['http_referer' => $request->getRequestUri()], ['csrf_protection' => false]);
return $this->render('frontend/epaper/index.html.twig', [
'abonnent' => $abonnent,
'ausgaben' => $ausgaben,
'ausgabenMitBild' => $ausgabenMitBild,
'lastIssue' => $epaperService->getLastIssueValues(),
'abonnentLoginForm' => $abonnentLoginForm->createView(),
]);
}
#[Route(path: '/pdf/{filename}', name: 'fe.epaper.pdf')]
public function pdf(
Request $request,
AbonnentService $abonnentService,
string $filename
): BinaryFileResponse {
// Wenn kein Referer
if ($request->headers->get('referer') === null) {
throw new UnauthorizedHttpException('This route cannot be accessed directly!');
}
// Falls nicht authentifiziert.
if (!$abonnentService->isAuthenticated($request) && !$this->isGranted('MODULE_ARTIKEL')) {
throw new UnauthorizedHttpException('You are not authenticated for this ressource, please login!');
}
$file = $this->getParameter('pdf_file_dir').'/'.$filename;
return $this->file($file);
}
#[Route(path: '/image/{filename}', name: 'fe.epaper.image')]
public function image(
string $filename
): BinaryFileResponse {
$file = $this->getParameter('pdf_images_file_dir').'/'.$filename;
return $this->file($file);
}
}