<?php
namespace App\Controller;
use App\Entity\NewsArticle;
use App\Entity\User;
use App\Repository\NewsArticleRepository;
use App\Services\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/news",
* host="{web_subdomain}.fwthorpe.{web_tld}",
* defaults={"web_subdomain"="%web_subdomain%","web_tld"="%web_tld%"},
* requirements={"web_subdomain"="%web_subdomain%|","web_tld"="%web_tld%"}
* )
*/
class NewsController extends AbstractController
{
/**
* @Route("/", name="news_route")
* @param NewsArticleRepository $newsArticleRepository
* @param UserService $userService
* @return Response
*/
public function indexAction(NewsArticleRepository $newsArticleRepository, UserService $userService): Response
{
$newsArticles = $newsArticleRepository->findBy(['archived' => 0, 'published' => 1], ['datePosted' => 'DESC']);
$isAdminUser = $userService->getIsAdminUser($this->getUser());
return $this->render('about/latest-news.html.twig', ['news_articles' => $newsArticles, 'isAdminUser' => $isAdminUser]);
}
/**
* @Route("/article/{id}", name="news_article_view_route")
*/
public function viewAction(NewsArticle $newsArticle): Response
{
return $this->render('about/news-article.html.twig', ['news_article' => $newsArticle]);
}
/**
* @Route("/image/{size}/{filename}", name="news_article_image_route", methods={"GET"})
*/
public function newsArticleImageAction(Request $request, string $size, string $filename): BinaryFileResponse
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if (!$extension) {
throw $this->createNotFoundException('No image extension provided');
}
$file = $_ENV['NEWS_ARTICLE_IMAGE_DIR'] . '/' . $size . '/' . $filename;
$filesystem = new Filesystem();
if (!$filesystem->exists($file)) {
throw $this->createNotFoundException('Image does not exist');
}
return new BinaryFileResponse($file);
}
/**
* @Route("/thumbnail/{size}/{filename}", name="news_article_thumbnail_route", methods={"GET"})
*/
public function newsArticleThumbnailAction(Request $request, string $size, string $filename): BinaryFileResponse
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if (!$extension) {
throw $this->createNotFoundException('No image extension provided');
}
$file = $_ENV['NEWS_ARTICLE_THUMBNAIL_DIR'] . '/' . $size . '/' . $filename;
$filesystem = new Filesystem();
if (!$filesystem->exists($file)) {
throw $this->createNotFoundException('Image does not exist');
}
return new BinaryFileResponse($file);
}
}