<?php
namespace Gh\FrontBundle\Controller;
use Gh\FrontBundle\Classes\Sidebar\SidebarAuthor,
Gh\FrontBundle\Classes\TitleClass,
Gh\CommonBundle\Classes\Meta\MetaClass,
Gh\FrontBundle\Classes\BreadcrumbClass,
Gh\FrontBundle\Classes\ArticleClass,
Gh\FrontBundle\Classes\MenuActiveClass,
Gh\FrontBundle\Classes\Sidebar\SidebarClass,
Gh\FrontBundle\Classes\Sidebar\SidebarCard,
Gh\FrontBundle\Classes\Sidebar\SidebarLinkedCards,
Gh\FrontBundle\Classes\ListValues\SectionTypeClass,
Gh\FrontBundle\Classes\Helpers\PaginatorHelper,
Gh\DatabaseBundle\Entity\GhCard,
Gh\DatabaseBundle\Entity\GhDossier,
Symfony\Component\Routing\Annotation\Route,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Gh\FrontBundle\Classes\Sidebar\SidebarLatestDossiers;
use Gh\FrontBundle\Classes\Sidebar\SidebarNewsPrevious;
class ArticleController extends GhController
{
/**
* @Route(
* "/news/{newsId}-{slug}.html",
* name="GhFrontBundle_Article_news",
* requirements={"newsId" = "([0-9]+)", "slug" = "([0-9a-zA-Z_-]+)"}
* )
* @Template()
* @param $newsId
* @param $slug
* @return array
*/
public function newsAction(string $newsId, string $slug): array
{
$em = $this->getDoctrine()->getManager();
$news = $em->getRepository('GhDatabaseBundle:GhNews')->getNewsWithConsolesAndCards($newsId);
// Throw a 404 error if no news found
if ($news === null) {
throw $this->createNotFoundException('Aucune news n\'a été trouvée');
}
// Get main news card
list($mainNewsCard, $mainNewsCardTitle) = $news->getMainNewsCard($em);
// Get linked cards (grouped by consoles)
$linkedCards = $news->getLinkedCards($mainNewsCardTitle);
// Get next and previous news
$nearNews = $em->getRepository('GhDatabaseBundle:GhNews')->getPreviousAndNextNews($newsId);
// Get list of previous news
$previousNews = $em->getRepository('GhDatabaseBundle:GhNews')->getListPreviousNews(
$newsId,
$this->container->getParameter('news_number_previous_related_block')
);
// Generate title
$title = $news->getTitle();
if (
$mainNewsCard
&& count($mainNewsCard) > 0
&& $mainNewsCard[0]->getCategory() != GhCard::CARD_CATEGORY_MISC
) {
$title .= ' ('.$mainNewsCard[0]->getDisplayTitle().')';
}
TitleClass::getInstance()->setTitle($title);
// Generate breadcrumb
$breadcrumb = BreadcrumbClass::getInstance();
$breadcrumb->addPath(
SectionTypeClass::getInstance()->getLabelById('news', $news->getType()),
$this->get('router'),
'GhFrontBundle_Article_newsSubsection',
array(
'subsectionId' => $news->getType(),
'slug' => $news->getTypeSlugified(),
'pageId' => 1
)
);
$breadcrumb->addPath($news->getTitle());
// Generate meta
$meta = MetaClass::getInstance();
$descriptionLabel = ArticleClass::stripTags(ArticleClass::truncateArticle($news->getContent(), $this->container->getParameter('news_card_meta_truncate')));
// SEO: if the description is too short, the page is not indexed well on Google
if (strlen($descriptionLabel) < 50) {
$descriptionLabel = $news->getTitle().' - '.SectionTypeClass::getInstance()->getLabelById('news', $news->getType()).' - Retrouvez toutes les infos jeux vidéo sur GameHope !';
}
$meta->addMeta('description', $descriptionLabel);
if ($mainNewsCard && count($mainNewsCard) > 0) {
$meta->setMetaCard($mainNewsCard[0]);
}
$newsTags = $news->getTags();
if (count($newsTags) > 0) {
$meta->addMeta('keywords', implode(', ', $newsTags));
$meta->addMeta('news_keywords', implode(', ', $newsTags));
}
$meta->addMeta('og:type', 'article');
$meta->addMeta('og:description', $news->getCatcherfacebook());
$meta->addMeta('og:image', $this->container->getParameter('base_url_absolute').$news->getArtworkPath($this->container, ''));
// Set menu active
MenuActiveClass::getInstance()->setMenuActive('daily');
// Generate canonical URL
$this->updateCanonicalUrl(NULL, array('newsId' => $news->getId(), 'slug' => $news->getSlug()));
// Increment view counter
$news->setCounter($news->getCounter() + 1);
$em->flush();
// Push the sidebar Previous News
SidebarClass::getInstance()->push('previousNews', new SidebarNewsPrevious($previousNews), -1);
// Push the sidebar Author
$member = $em->getRepository('GhDatabaseBundle:GhTeam')->findOneByName($news->getAuthor());
SidebarClass::getInstance()->push('author', new SidebarAuthor($member), -1);
// Push the sidebars Card and Linked cards
SidebarClass::getInstance()->push('linkedCards', new SidebarLinkedCards($linkedCards), -1);
return array(
'news' => $news,
'mainNewsCard' => $mainNewsCard,
'nearNews' => $nearNews
);
}
/**
* This method is very close to newsTagAction(): if one method is updated, the other one
* should be probably updated also.
*
* @Route(
* "/news/{subsectionId}-{slug}/page-{pageId}.html",
* name="GhFrontBundle_Article_newsSubsection",
* requirements={"subsectionId" = "([0-9]+)", "slug" = "([0-9a-zA-Z_-]+)", "pageId" = "([0-9]+)"}
* )
* @Template("@GhFront/article/news_subsection_tag.html.twig")
*/
public function newsSubsectionAction($subsectionId, $slug, $pageId)
{
$em = $this->getDoctrine()->getManager();
$label = SectionTypeClass::getInstance()->getLabelById('news', $subsectionId);
// Generate title
$title = $label.' : toutes les news (Page '.$pageId.')';
TitleClass::getInstance()->setTitle($title);
// Generate breadcrumb
$breadcrumb = BreadcrumbClass::getInstance();
$breadcrumb->addPath($label,
$this->get('router'),
'GhFrontBundle_Article_newsSubsection',
array(
'subsectionId' => $subsectionId,
'slug' => $slug,
'pageId' => 1
)
);
if ($pageId > 1) {
$breadcrumb->addPath('Page '.$pageId);
}
// Compute pagination
$paginator = new PaginatorHelper($em->getRepository('GhDatabaseBundle:GhNews')->countNewsBySubsectionId($subsectionId), $pageId, $this->container->getParameter('news_subsection_tag_per_page'));
$paginator->setSfLinkedController($this);
$paginator->setRouter($this->get('router'));
$paginator->setSfUrlParameters(array('subsectionId' => $subsectionId, 'slug' => $slug));
$paginator->setPageParameterName('pageId');
$paginator->setPaginationStep($this->container->getParameter('news_subsection_tag_pagination_step'));
// Get the list of news
$associatedNews = $em->getRepository('GhDatabaseBundle:GhNews')->getNewsListBySubsectionId($subsectionId, $paginator->getOffset(), $paginator->getLimit());
// Throw a 404 error if no news found
if (count($associatedNews) == 0) {
throw $this->createNotFoundException('Aucune news n\'a été trouvée');
}
return array('label' => $label, 'paginator' => $paginator, 'associatedNews' => $associatedNews);
}
/**
* This method is very close to newsSubsectionAction(): if one method is updated, the other one
* should be probably updated also.
*
* @Route(
* "/news/tag-{slug}/page-{pageId}.html",
* name="GhFrontBundle_Article_newsTag",
* requirements={"slug" = "([0-9a-zA-Z_-]+)", "pageId" = "([0-9]+)"}
* )
* @Template("@GhFront/article/news_subsection_tag.html.twig")
*/
public function newsTagAction($slug, $pageId)
{
$em = $this->getDoctrine()->getManager();
// Remove "-" in slug because some tags are separated by a space
$formattedSlug = str_replace('-', ' ', $slug);
// Generate title
$title = ucfirst($formattedSlug).' : tout sur ce tag (Page '.$pageId.')';
TitleClass::getInstance()->setTitle($title);
// Generate breadcrumb
$breadcrumb = BreadcrumbClass::getInstance();
$breadcrumb->addPath(ucfirst($formattedSlug),
$this->get('router'),
'GhFrontBundle_Article_newsTag',
array(
'slug' => $slug,
'pageId' => 1
)
);
if ($pageId > 1) {
$breadcrumb->addPath('Page '.$pageId);
}
// Compute pagination
$paginator = new PaginatorHelper($em->getRepository('GhDatabaseBundle:GhNews')->countNewsByTag($formattedSlug), $pageId, $this->container->getParameter('news_subsection_tag_per_page'));
$paginator->setSfLinkedController($this);
$paginator->setRouter($this->get('router'));
$paginator->setSfUrlParameters(array('slug' => $slug));
$paginator->setPageParameterName('pageId');
$paginator->setPaginationStep($this->container->getParameter('news_subsection_tag_pagination_step'));
// Get the list of news
$associatedNews = $em->getRepository('GhDatabaseBundle:GhNews')->getNewsListByTag($formattedSlug, $paginator->getOffset(), $paginator->getLimit());
// Throw a 404 error if no news found
if (count($associatedNews) == 0) {
throw $this->createNotFoundException('Aucune news n\'a été trouvée');
}
return array('label' => ucfirst($formattedSlug), 'paginator' => $paginator, 'associatedNews' => $associatedNews);
}
/**
* @Route(
* "/dossiers/{dossierId}-{dossierSlug}/",
* name="GhFrontBundle_Article_dossier",
* requirements={"dossierId" = "([0-9]+)", "dossierSlug" = "([0-9a-zA-Z_-]+)"}
* )
* @Template()
*/
public function dossierAction($dossierId, $dossierSlug)
{
return $this->dossierFactorized($dossierId, $dossierSlug);
}
/**
* @Route(
* "/dossiers/{dossierId}-{dossierSlug}/{pageId}-{pageSlug}.html",
* name="GhFrontBundle_Article_dossierPage",
* requirements={"dossierId" = "([0-9]+)", "dossierSlug" = "([0-9a-zA-Z_-]+)", "pageId" = "([0-9]+)", "pageSlug" = "([0-9a-zA-Z_-]+)"}
* )
* @Template()
*/
public function dossierPageAction($dossierId, $dossierSlug, $pageId, $pageSlug)
{
return $this->dossierFactorized($dossierId, $dossierSlug, $pageId, $pageSlug);
}
private function dossierFactorized($dossierId, $dossierSlug, $pageId = NULL, $pageSlug = NULL) {
// Generate canonical URL
$this->updateCanonicalUrl(NULL, array('dossierId' => $dossierId, 'dossierSlug' => $dossierSlug, 'pageId' => $pageId, 'pageSlug' => $pageSlug));
$em = $this->getDoctrine()->getManager();
$dossier = $em->getRepository('GhDatabaseBundle:GhDossier')->findOneById($dossierId);
// Throw a 404 error if no dossier found
if ($dossier === null) {
throw $this->createNotFoundException('Aucun dossier n\'a été trouvé');
}
$em->getRepository('GhDatabaseBundle:GhDossier')->incrementViewsCounter($dossier);
//get dossier elements : pages, paragraphs and pictures, all at once
$dossierPages = $em->getRepository('GhDatabaseBundle:GhDossierArticle')->getDossierArticleByDossierId($dossierId);
//get page to display
if($pageId === NULL) {
$pageToDisplay = current($dossierPages);
} else {
foreach($dossierPages as $oneDossierPage) {
if($oneDossierPage->getPosition() == $pageId) {
$pageToDisplay = $oneDossierPage;
break;
}
}
}
if (count($dossierPages) > 1) {
$title = $dossier->getTitle()." : ".$pageToDisplay->getPagetitle();
} else {
$title = $dossier->getTitle();
}
TitleClass::getInstance()->setTitle($title);
// Initialization of common date for dossiers (overridden below for Geek section)
$labelType = SectionTypeClass::getInstance()->getLabelById(GhDossier::TYPE_DOSSIER, $dossier->getType());
$tableContentsRoute = 'GhFrontBundle_TableContents_dossierDefault';
$tableContentsLabel = 'Dossiers';
MenuActiveClass::getInstance()->setMenuActive('dossier');
// If the sub section is empty, check in Geek section
if ($labelType == '') {
$labelType = SectionTypeClass::getInstance()->getLabelById(GhDossier::TYPE_GEEK, $dossier->getType());
$tableContentsRoute = 'GhFrontBundle_TableContents_geekDefault';
$tableContentsLabel = 'Geekeries';
MenuActiveClass::getInstance()->setMenuActive('geek');
}
$breadcrumb = BreadcrumbClass::getInstance();
$breadcrumb->addPath($tableContentsLabel, $this->get('router'), $tableContentsRoute);
$breadcrumb->addPath(
$labelType,
$this->get('router'),
$tableContentsRoute,
array(),
'?subsection='.$dossier->getType()
);
if (count($dossierPages) > 1 && $pageToDisplay->getPosition() > 1) {
$breadcrumb->addPath($dossier->getTitle(), $this->get('router'), 'GhFrontBundle_Article_dossier', array('dossierId' => $dossierId, 'dossierSlug' => $dossierSlug));
$breadcrumb->addPath('Page '.$pageToDisplay->getPosition());
} else {
//no link in breadcrumb if page = 1
$breadcrumb->addPath($dossier->getTitle());
}
$meta = MetaClass::getInstance();
$meta->addMeta('og:type', 'article');
$meta->addMeta('description', ArticleClass::stripTags(ArticleClass::truncateArticle($dossier->getIntroduction(), $this->container->getParameter('misc_truncate_og_description'))));
$meta->addMeta('og:description', $dossier->getCatcherfacebook());
$meta->addMeta('og:image', $this->container->getParameter('base_url_absolute').substr($dossier->getThumbnailUrl($this->container, true), 1));
// Sidebar Latest Dossiers
$latestDossiers = $em->getRepository('GhDatabaseBundle:GhDossier')->getListForSitemap(0, $this->container->getParameter('dossier_latest_number'));
SidebarClass::getInstance()->push('dossierSummary', new SidebarLatestDossiers($latestDossiers), -1);
// Sidebar Related Cards
$relatedCards = [];
if ($dossier->getCards() != null) {
foreach ($dossier->getCards() as $dossierCard) {
$card = $dossierCard->getCardid();
if (!(array_key_exists($card->getTitle(), $relatedCards))) {
$relatedCards[$card->getTitle()] = $card;
}
$relatedCards[$card->getTitle()]->addConsolesAvailable($card);
}
}
SidebarClass::getInstance()->push('linkedCards', new SidebarLinkedCards($relatedCards), -1);
// Sidebar Author
$member = $em->getRepository('GhDatabaseBundle:GhTeam')->findOneByName($dossier->getAuthor());
SidebarClass::getInstance()->push('author', new SidebarAuthor($member), -1);
return array(
'dossier' => $dossier,
'dossierPages' => $dossierPages,
'labelType' => $labelType,
'pageToDisplay' => $pageToDisplay,
'artWork' => $dossier->getArtwork($this->container)
);
}
}