mirror of
https://github.com/quentin-g-dev/afup.git
synced 2026-03-24 17:22:06 +01:00
100 lines
2.8 KiB
PHP
100 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace AppBundle\Controller;
|
|
|
|
use AppBundle\Event\Model\Repository\EventRepository;
|
|
use AppBundle\Site\Form\NewsFiltersType;
|
|
use AppBundle\Site\Model\Article;
|
|
use AppBundle\Site\Model\Repository\ArticleRepository;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
class NewsController extends SiteBaseController
|
|
{
|
|
const ARTICLES_PER_PAGE = 5;
|
|
|
|
public function displayAction($code)
|
|
{
|
|
$articleRepository = $this->getArticleRepository();
|
|
|
|
$article = $articleRepository->findNewsBySlug($code);
|
|
|
|
if (null === $article) {
|
|
throw $this->createNotFoundException();
|
|
}
|
|
|
|
if (!($article->getPublishedAt() <= new \DateTime())) {
|
|
throw $this->createNotFoundException();
|
|
}
|
|
|
|
$this->getHeaderImageUrl($article);
|
|
|
|
return $this->render(
|
|
':site:news/display.html.twig',
|
|
[
|
|
'article' => $article,
|
|
'header_image' => $this->getHeaderImageUrl($article),
|
|
'previous' => $articleRepository->findPrevious($article),
|
|
'next' => $articleRepository->findNext($article),
|
|
'related_event' => $this->getRelatedEvent($article),
|
|
]
|
|
);
|
|
}
|
|
|
|
private function getRelatedEvent(Article $article)
|
|
{
|
|
if (null === ($eventId = $article->getEventId())) {
|
|
return null;
|
|
}
|
|
|
|
return $this->get('ting')->get(EventRepository::class)->get($eventId);
|
|
}
|
|
|
|
private function getHeaderImageUrl(Article $article)
|
|
{
|
|
if (null === ($theme = $article->getTheme())) {
|
|
return null;
|
|
}
|
|
|
|
$image = '/images/news/' . $theme . '.png';
|
|
|
|
$url = $this->container->getParameter('kernel.project_dir') . '/htdocs' . $image ;
|
|
|
|
if (false === is_file($url)) {
|
|
return null;
|
|
}
|
|
|
|
return $image;
|
|
}
|
|
|
|
public function listAction(Request $request)
|
|
{
|
|
$page = $request->get('page', 1);
|
|
|
|
$form = $this->createForm(NewsFiltersType::class);
|
|
$form->handleRequest($request);
|
|
|
|
$formData = $form->getData();
|
|
$filters = null === $formData ? [] : $formData;
|
|
|
|
return $this->render(
|
|
':site:news/list.html.twig',
|
|
[
|
|
'filters' => $filters,
|
|
'articles' => $this->getArticleRepository()->findPublishedNews($page, self::ARTICLES_PER_PAGE, $filters),
|
|
'total_items' => $this->getArticleRepository()->countPublishedNews($filters),
|
|
'current_page' => $page,
|
|
'articles_per_page' => self::ARTICLES_PER_PAGE,
|
|
'form' => $form->createView(),
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return ArticleRepository
|
|
*/
|
|
private function getArticleRepository()
|
|
{
|
|
return $this->get('ting')->get(ArticleRepository::class);
|
|
}
|
|
}
|