src/Gh/FrontBundle/Controller/ArticleController.php line 36

Open in your IDE?
  1. <?php
  2. namespace Gh\FrontBundle\Controller;
  3. use Gh\FrontBundle\Classes\Sidebar\SidebarAuthor,
  4.     Gh\FrontBundle\Classes\TitleClass,
  5.     Gh\CommonBundle\Classes\Meta\MetaClass,
  6.     Gh\FrontBundle\Classes\BreadcrumbClass,
  7.     Gh\FrontBundle\Classes\ArticleClass,
  8.     Gh\FrontBundle\Classes\MenuActiveClass,
  9.     Gh\FrontBundle\Classes\Sidebar\SidebarClass,
  10.     Gh\FrontBundle\Classes\Sidebar\SidebarCard,
  11.     Gh\FrontBundle\Classes\Sidebar\SidebarLinkedCards,
  12.     Gh\FrontBundle\Classes\ListValues\SectionTypeClass,
  13.     Gh\FrontBundle\Classes\Helpers\PaginatorHelper,
  14.     Gh\DatabaseBundle\Entity\GhCard,
  15.     Gh\DatabaseBundle\Entity\GhDossier,
  16.     Symfony\Component\Routing\Annotation\Route,
  17.     Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  18. use Gh\FrontBundle\Classes\Sidebar\SidebarLatestDossiers;
  19. use Gh\FrontBundle\Classes\Sidebar\SidebarNewsPrevious;
  20. class ArticleController extends GhController
  21. {
  22.     /**
  23.      * @Route(
  24.      *      "/news/{newsId}-{slug}.html",
  25.      *      name="GhFrontBundle_Article_news",
  26.      *      requirements={"newsId" = "([0-9]+)", "slug" = "([0-9a-zA-Z_-]+)"}
  27.      * )
  28.      * @Template()
  29.      * @param $newsId
  30.      * @param $slug
  31.      * @return array
  32.      */
  33.     public function newsAction(string $newsIdstring $slug): array
  34.     {       
  35.         $em $this->getDoctrine()->getManager();
  36.         $news $em->getRepository('GhDatabaseBundle:GhNews')->getNewsWithConsolesAndCards($newsId);
  37.         
  38.         // Throw a 404 error if no news found
  39.         if ($news === null) {
  40.             throw $this->createNotFoundException('Aucune news n\'a été trouvée');
  41.         }
  42.         
  43.         // Get main news card
  44.         list($mainNewsCard$mainNewsCardTitle) = $news->getMainNewsCard($em);
  45.         
  46.         // Get linked cards (grouped by consoles)
  47.         $linkedCards $news->getLinkedCards($mainNewsCardTitle);
  48.        
  49.         // Get next and previous news
  50.         $nearNews $em->getRepository('GhDatabaseBundle:GhNews')->getPreviousAndNextNews($newsId);
  51.         
  52.         // Get list of previous news
  53.         $previousNews $em->getRepository('GhDatabaseBundle:GhNews')->getListPreviousNews(
  54.             $newsId,
  55.             $this->container->getParameter('news_number_previous_related_block')
  56.         );
  57.         // Generate title
  58.         $title $news->getTitle();
  59.         if (
  60.             $mainNewsCard 
  61.             && count($mainNewsCard) > 
  62.             && $mainNewsCard[0]->getCategory() != GhCard::CARD_CATEGORY_MISC
  63.         ) {
  64.             $title .= ' ('.$mainNewsCard[0]->getDisplayTitle().')';
  65.         }
  66.         TitleClass::getInstance()->setTitle($title);
  67.         
  68.         // Generate breadcrumb
  69.         $breadcrumb BreadcrumbClass::getInstance();
  70.         $breadcrumb->addPath(
  71.             SectionTypeClass::getInstance()->getLabelById('news'$news->getType()), 
  72.             $this->get('router'), 
  73.             'GhFrontBundle_Article_newsSubsection'
  74.             array(
  75.                 'subsectionId' => $news->getType(), 
  76.                 'slug' => $news->getTypeSlugified(), 
  77.                 'pageId' => 1
  78.             )
  79.         );
  80.         $breadcrumb->addPath($news->getTitle());
  81.         
  82.         // Generate meta
  83.         $meta MetaClass::getInstance();
  84.         $descriptionLabel ArticleClass::stripTags(ArticleClass::truncateArticle($news->getContent(), $this->container->getParameter('news_card_meta_truncate')));
  85.         // SEO: if the description is too short, the page is not indexed well on Google
  86.         if (strlen($descriptionLabel) < 50) {
  87.             $descriptionLabel $news->getTitle().' - '.SectionTypeClass::getInstance()->getLabelById('news'$news->getType()).' - Retrouvez toutes les infos jeux vidéo sur GameHope !';
  88.         }
  89.         $meta->addMeta('description'$descriptionLabel);
  90.         if ($mainNewsCard && count($mainNewsCard) > 0) {
  91.             $meta->setMetaCard($mainNewsCard[0]);
  92.         }
  93.         
  94.         $newsTags $news->getTags();
  95.         if (count($newsTags) > 0) {
  96.             $meta->addMeta('keywords'implode(', '$newsTags));
  97.             $meta->addMeta('news_keywords'implode(', '$newsTags));
  98.         }
  99.         $meta->addMeta('og:type''article');
  100.         $meta->addMeta('og:description'$news->getCatcherfacebook());
  101.         $meta->addMeta('og:image'$this->container->getParameter('base_url_absolute').$news->getArtworkPath($this->container''));
  102.         // Set menu active
  103.         MenuActiveClass::getInstance()->setMenuActive('daily');
  104.         
  105.         // Generate canonical URL
  106.         $this->updateCanonicalUrl(NULL, array('newsId' => $news->getId(), 'slug' => $news->getSlug()));
  107.         
  108.         // Increment view counter
  109.         $news->setCounter($news->getCounter() + 1);
  110.         $em->flush();
  111.         // Push the sidebar Previous News
  112.         SidebarClass::getInstance()->push('previousNews', new SidebarNewsPrevious($previousNews), -1);
  113.         // Push the sidebar Author
  114.         $member $em->getRepository('GhDatabaseBundle:GhTeam')->findOneByName($news->getAuthor());
  115.         SidebarClass::getInstance()->push('author', new SidebarAuthor($member), -1);
  116.         // Push the sidebars Card and Linked cards
  117.         SidebarClass::getInstance()->push('linkedCards', new SidebarLinkedCards($linkedCards), -1);
  118.         return array(
  119.             'news' => $news
  120.             'mainNewsCard' => $mainNewsCard
  121.             'nearNews' => $nearNews
  122.         );
  123.     }
  124.     
  125.     /**
  126.      * This method is very close to newsTagAction(): if one method is updated, the other one
  127.      * should be probably updated also.
  128.      * 
  129.      * @Route(
  130.      *      "/news/{subsectionId}-{slug}/page-{pageId}.html",
  131.      *      name="GhFrontBundle_Article_newsSubsection",
  132.      *      requirements={"subsectionId" = "([0-9]+)", "slug" = "([0-9a-zA-Z_-]+)", "pageId" = "([0-9]+)"}
  133.      * )
  134.      * @Template("@GhFront/article/news_subsection_tag.html.twig")
  135.      */
  136.     public function newsSubsectionAction($subsectionId$slug$pageId)
  137.     {
  138.         $em $this->getDoctrine()->getManager();
  139.         
  140.         $label SectionTypeClass::getInstance()->getLabelById('news'$subsectionId);
  141.         
  142.         // Generate title
  143.         $title $label.' : toutes les news (Page '.$pageId.')';
  144.         TitleClass::getInstance()->setTitle($title);
  145.         
  146.         // Generate breadcrumb
  147.         $breadcrumb BreadcrumbClass::getInstance();
  148.         $breadcrumb->addPath($label
  149.             $this->get('router'), 
  150.             'GhFrontBundle_Article_newsSubsection'
  151.             array(
  152.                 'subsectionId' => $subsectionId
  153.                 'slug' => $slug
  154.                 'pageId' => 1
  155.             )
  156.         );
  157.         if ($pageId 1) {
  158.             $breadcrumb->addPath('Page '.$pageId);
  159.         }
  160.         
  161.         // Compute pagination
  162.         $paginator = new PaginatorHelper($em->getRepository('GhDatabaseBundle:GhNews')->countNewsBySubsectionId($subsectionId), $pageId$this->container->getParameter('news_subsection_tag_per_page'));
  163.         $paginator->setSfLinkedController($this);
  164.         $paginator->setRouter($this->get('router'));
  165.         $paginator->setSfUrlParameters(array('subsectionId' => $subsectionId'slug' => $slug));
  166.         $paginator->setPageParameterName('pageId');
  167.         $paginator->setPaginationStep($this->container->getParameter('news_subsection_tag_pagination_step'));
  168.         
  169.         // Get the list of news
  170.         $associatedNews $em->getRepository('GhDatabaseBundle:GhNews')->getNewsListBySubsectionId($subsectionId$paginator->getOffset(), $paginator->getLimit());
  171.         
  172.         // Throw a 404 error if no news found
  173.         if (count($associatedNews) == 0) {
  174.             throw $this->createNotFoundException('Aucune news n\'a été trouvée');
  175.         }
  176.         
  177.         return array('label' => $label'paginator' => $paginator'associatedNews' => $associatedNews);
  178.     }
  179.     
  180.     /**
  181.      * This method is very close to newsSubsectionAction(): if one method is updated, the other one
  182.      * should be probably updated also.
  183.      * 
  184.      * @Route(
  185.      *      "/news/tag-{slug}/page-{pageId}.html",
  186.      *      name="GhFrontBundle_Article_newsTag",
  187.      *      requirements={"slug" = "([0-9a-zA-Z_-]+)", "pageId" = "([0-9]+)"}
  188.      * )
  189.      * @Template("@GhFront/article/news_subsection_tag.html.twig")
  190.      */
  191.     public function newsTagAction($slug$pageId)
  192.     {
  193.         $em $this->getDoctrine()->getManager();
  194.         
  195.         // Remove "-" in slug because some tags are separated by a space
  196.         $formattedSlug str_replace('-'' '$slug);
  197.         
  198.         // Generate title
  199.         $title ucfirst($formattedSlug).' : tout sur ce tag (Page '.$pageId.')';
  200.         TitleClass::getInstance()->setTitle($title);
  201.         
  202.         // Generate breadcrumb
  203.         $breadcrumb BreadcrumbClass::getInstance();
  204.         $breadcrumb->addPath(ucfirst($formattedSlug), 
  205.             $this->get('router'), 
  206.             'GhFrontBundle_Article_newsTag'
  207.             array(
  208.                 'slug' => $slug
  209.                 'pageId' => 1
  210.             )
  211.         );
  212.         if ($pageId 1) {
  213.             $breadcrumb->addPath('Page '.$pageId);
  214.         }
  215.         
  216.         // Compute pagination
  217.         $paginator = new PaginatorHelper($em->getRepository('GhDatabaseBundle:GhNews')->countNewsByTag($formattedSlug), $pageId$this->container->getParameter('news_subsection_tag_per_page'));
  218.         $paginator->setSfLinkedController($this);
  219.         $paginator->setRouter($this->get('router'));
  220.         $paginator->setSfUrlParameters(array('slug' => $slug));
  221.         $paginator->setPageParameterName('pageId');
  222.         $paginator->setPaginationStep($this->container->getParameter('news_subsection_tag_pagination_step'));
  223.         
  224.         // Get the list of news
  225.         $associatedNews $em->getRepository('GhDatabaseBundle:GhNews')->getNewsListByTag($formattedSlug$paginator->getOffset(), $paginator->getLimit());
  226.         
  227.         // Throw a 404 error if no news found
  228.         if (count($associatedNews) == 0) {
  229.             throw $this->createNotFoundException('Aucune news n\'a été trouvée');
  230.         }
  231.         
  232.         return array('label' => ucfirst($formattedSlug), 'paginator' => $paginator'associatedNews' => $associatedNews);
  233.     }
  234.     
  235.     /**
  236.      * @Route(
  237.      *      "/dossiers/{dossierId}-{dossierSlug}/",
  238.      *      name="GhFrontBundle_Article_dossier",
  239.      *      requirements={"dossierId" = "([0-9]+)", "dossierSlug" = "([0-9a-zA-Z_-]+)"}
  240.      * )
  241.      * @Template()
  242.      */
  243.     public function dossierAction($dossierId$dossierSlug)
  244.     {   
  245.         return $this->dossierFactorized($dossierId$dossierSlug);
  246.     }
  247.     
  248.     /**
  249.      * @Route(
  250.      *      "/dossiers/{dossierId}-{dossierSlug}/{pageId}-{pageSlug}.html",
  251.      *      name="GhFrontBundle_Article_dossierPage",
  252.      *      requirements={"dossierId" = "([0-9]+)", "dossierSlug" = "([0-9a-zA-Z_-]+)", "pageId" = "([0-9]+)", "pageSlug" = "([0-9a-zA-Z_-]+)"}
  253.      * )
  254.      * @Template()
  255.      */
  256.     public function dossierPageAction($dossierId$dossierSlug$pageId$pageSlug)
  257.     {      
  258.         return $this->dossierFactorized($dossierId$dossierSlug$pageId$pageSlug);
  259.     }
  260.     
  261.     private function dossierFactorized($dossierId$dossierSlug$pageId NULL$pageSlug NULL) {
  262.         // Generate canonical URL
  263.         $this->updateCanonicalUrl(NULL, array('dossierId' => $dossierId'dossierSlug' => $dossierSlug'pageId' => $pageId'pageSlug' => $pageSlug));
  264.         
  265.         $em $this->getDoctrine()->getManager();
  266.         $dossier $em->getRepository('GhDatabaseBundle:GhDossier')->findOneById($dossierId);
  267.         
  268.         // Throw a 404 error if no dossier found
  269.         if ($dossier === null) {
  270.             throw $this->createNotFoundException('Aucun dossier n\'a été trouvé');
  271.         }
  272.         
  273.         $em->getRepository('GhDatabaseBundle:GhDossier')->incrementViewsCounter($dossier);
  274.         
  275.         //get dossier elements : pages, paragraphs and pictures, all at once
  276.         $dossierPages $em->getRepository('GhDatabaseBundle:GhDossierArticle')->getDossierArticleByDossierId($dossierId);
  277.         //get page to display 
  278.         if($pageId === NULL) {
  279.             $pageToDisplay current($dossierPages);
  280.         } else {
  281.             foreach($dossierPages as $oneDossierPage) {
  282.                 if($oneDossierPage->getPosition() == $pageId) {
  283.                     $pageToDisplay $oneDossierPage;
  284.                     break;
  285.                 }
  286.             }
  287.         }
  288.         if (count($dossierPages) > 1) {
  289.             $title $dossier->getTitle()." : ".$pageToDisplay->getPagetitle();
  290.         } else {
  291.             $title $dossier->getTitle();
  292.         }
  293.         TitleClass::getInstance()->setTitle($title);
  294.         
  295.         // Initialization of common date for dossiers (overridden below for Geek section)
  296.         $labelType SectionTypeClass::getInstance()->getLabelById(GhDossier::TYPE_DOSSIER$dossier->getType());
  297.         $tableContentsRoute 'GhFrontBundle_TableContents_dossierDefault';
  298.         $tableContentsLabel 'Dossiers';
  299.         MenuActiveClass::getInstance()->setMenuActive('dossier');
  300.         
  301.         // If the sub section is empty, check in Geek section
  302.         if ($labelType == '') {
  303.             $labelType SectionTypeClass::getInstance()->getLabelById(GhDossier::TYPE_GEEK$dossier->getType());
  304.             $tableContentsRoute 'GhFrontBundle_TableContents_geekDefault';
  305.             $tableContentsLabel 'Geekeries';
  306.             MenuActiveClass::getInstance()->setMenuActive('geek');
  307.         }
  308.         
  309.         $breadcrumb BreadcrumbClass::getInstance();
  310.         $breadcrumb->addPath($tableContentsLabel$this->get('router'), $tableContentsRoute);
  311.         $breadcrumb->addPath(
  312.                 $labelType
  313.                 $this->get('router'), 
  314.                 $tableContentsRoute
  315.                 array(),
  316.                 '?subsection='.$dossier->getType()
  317.             );
  318.         if (count($dossierPages) > && $pageToDisplay->getPosition() > 1) {
  319.             $breadcrumb->addPath($dossier->getTitle(), $this->get('router'), 'GhFrontBundle_Article_dossier', array('dossierId' => $dossierId'dossierSlug' => $dossierSlug));
  320.             $breadcrumb->addPath('Page '.$pageToDisplay->getPosition());
  321.         } else {
  322.             //no link in breadcrumb if page = 1
  323.             $breadcrumb->addPath($dossier->getTitle());
  324.         }
  325.         
  326.         $meta MetaClass::getInstance();
  327.         $meta->addMeta('og:type''article');
  328.         $meta->addMeta('description'ArticleClass::stripTags(ArticleClass::truncateArticle($dossier->getIntroduction(), $this->container->getParameter('misc_truncate_og_description'))));
  329.         $meta->addMeta('og:description'$dossier->getCatcherfacebook());
  330.         $meta->addMeta('og:image'$this->container->getParameter('base_url_absolute').substr($dossier->getThumbnailUrl($this->containertrue), 1));
  331.         // Sidebar Latest Dossiers
  332.         $latestDossiers $em->getRepository('GhDatabaseBundle:GhDossier')->getListForSitemap(0$this->container->getParameter('dossier_latest_number'));
  333.         SidebarClass::getInstance()->push('dossierSummary', new SidebarLatestDossiers($latestDossiers), -1);
  334.         // Sidebar Related Cards
  335.         $relatedCards = [];
  336.         if ($dossier->getCards() != null) {
  337.             foreach ($dossier->getCards() as $dossierCard) {
  338.                 $card $dossierCard->getCardid();
  339.                 if (!(array_key_exists($card->getTitle(), $relatedCards))) {
  340.                     $relatedCards[$card->getTitle()] = $card;
  341.                 }
  342.                 $relatedCards[$card->getTitle()]->addConsolesAvailable($card);
  343.             }
  344.         }
  345.         SidebarClass::getInstance()->push('linkedCards', new SidebarLinkedCards($relatedCards), -1);
  346.         // Sidebar Author
  347.         $member $em->getRepository('GhDatabaseBundle:GhTeam')->findOneByName($dossier->getAuthor());
  348.         SidebarClass::getInstance()->push('author', new SidebarAuthor($member), -1);
  349.         return array(   
  350.             'dossier' => $dossier,
  351.             'dossierPages' => $dossierPages,
  352.             'labelType' => $labelType,
  353.             'pageToDisplay' => $pageToDisplay,
  354.             'artWork' => $dossier->getArtwork($this->container)
  355.         );
  356.     }
  357. }