src/Controller/Frontend/NewsController.php line 190

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\EspanyolTv;
  4. use App\Entity\HomeSlide;
  5. use App\Entity\NewsCategory;
  6. use App\Entity\NewsCategoryBanner;
  7. use App\Entity\NewsCategoryBannerI18n;
  8. use App\Entity\Section;
  9. use App\Entity\News;
  10. use App\Entity\SlideNews;
  11. use App\Entity\File;
  12. use App\Entity\NewsI18n;
  13. use App\Classes\Constants;
  14. use App\Service\BesoccerApiHelper;
  15. use App\Service\TranslatorHelper;
  16. use App\Service\Url;
  17. use App\Service\UrlHelper;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. /**
  23.  * Class NewsController
  24.  * @package App\Controller\Frontend
  25.  */
  26. class NewsController extends AbstractController {
  27.     /**
  28.      * @Route(
  29.      *   {
  30.      *       "es" : "{_locale}/noticias",
  31.      *       "ca" : "{_locale}/noticies",
  32.      *       "en" : "{_locale}/news",
  33.      *       "cn" : "{_locale}/消息",
  34.      *    }, name="news",
  35.      *       requirements={
  36.      *          "_locale": "es|ca|en|cn",
  37.      *          "_format" : "html"
  38.      *      }
  39.      * )
  40.      */
  41.     public function news(Request $request): Response {
  42.         $aSlides $this->getDoctrine()->getRepository(HomeSlide::class)->getAllByLang(
  43.                 $request->getLocale(),
  44.                 true,
  45.                 false
  46.         );
  47. //        dump($aSlides);
  48.         $newsSlides = [];
  49.         foreach ($aSlides as $key => $value) {
  50. //            if (!$value->getTranslations()[0]->getLink()) {
  51.             $slideNews $this->getDoctrine()->getRepository(SlideNews::class)->findOneBy(['slide' => $value'language' => $request->getLocale()]);
  52. //                dump($slideNews);
  53.             if ($slideNews) {
  54.                 $news $this->getDoctrine()->getRepository(News::class)->getOneByLang($slideNews->getNews()->getId(), $request->getLocale());
  55. //                    dump($news);
  56.                 if ($news) {
  57. //                        dump($news->getTranslations()[0]->getSeoTitle());
  58.                     $url $this->generateUrl('new', array('seoSlug' => $news->getTranslations()[0]->getTitle(), 'id' => $news->getId()));
  59. //                           dump($url);
  60. //                           dump($news->getTranslations()[0]);
  61. //                        $news->getTranslations()[0]->setLink($url);
  62.                     $newsSlides[$key]['slide'] = $news;
  63.                     $newsSlides[$key]['url'] = $url;
  64.                 }
  65.             } else {
  66. //                        dump($news->getTranslations()[0]->getSeoTitle());
  67. //                        $url = $this->generateUrl('new', array('seoSlug' => $value->getTranslations()[0]->getTitle(), 'id' => $value->getId()));
  68. //                           dump($url);
  69. //                           dump($news->getTranslations()[0]);
  70. //                        $news->getTranslations()[0]->setLink($url);
  71.                 $newsSlides[$key]['slide'] = $value;
  72.                 $newsSlides[$key]['url'] = $value->getTranslations()[0]->getLink();
  73.             }
  74. //            }
  75.         }
  76.         return $this->render('Frontend/news/news.html.twig', [
  77.                     'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
  78.                             'news',
  79.                             $request->getLocale()
  80.                     ),
  81.                     'newsCategories' => $this->getDoctrine()->getRepository(NewsCategory::class)->getAllByLang(
  82.                             $request->getLocale()
  83.                     ),
  84.                     'news' => $this->getDoctrine()->getRepository(News::class)->getAllByLang($request->getLocale(), 21),
  85.                     'videos' => $this->getDoctrine()->getRepository(EspanyolTv::class)->getAllByLang(
  86.                             $request->getLocale(),
  87.                             1,
  88.                             21
  89.                     ),
  90.                     'aSlides' => $newsSlides
  91.         ]);
  92.     }
  93.     /**
  94.      * @Route(
  95.      *   {
  96.      *       "es" : "{_locale}/noticias/{seoSlugCategory}/{id}/{currentPage}",
  97.      *       "ca" : "{_locale}/noticies/{seoSlugCategory}/{id}/{currentPage}",
  98.      *       "en" : "{_locale}/information/{seoSlugCategory}/{id}/{currentPage}",
  99.      *       "cn" : "{_locale}/消息/{seoSlugCategory}/{id}/{currentPage}",
  100.      *    }, name="newsByCateg",
  101.      *       requirements={
  102.      *          "_locale": "es|ca|en|cn",
  103.      *          "_format" : "html"
  104.      *      },
  105.      *     defaults={
  106.      *          "currentPage" = 1
  107.      *     }
  108.      * )
  109.      */
  110.     public function newsByCateg(Request $request$id$currentPage): Response {
  111.         $oCategory $this->getDoctrine()->getRepository(NewsCategory::class)->getOneByLang($id$request->getLocale());
  112.         if ($oCategory === null) {
  113.             throw $this->createNotFoundException('La categoría no existe.');
  114.         }
  115.         $oNews $this->getDoctrine()->getRepository(News::class)->getAllRelatedByCateg(
  116.                 $id,
  117.                 $request->getLocale(),
  118.                 $currentPage
  119.         );
  120.         $totalPages ceil($oNews->count() / Constants::NEWS_PER_PAGE);
  121.         return $this->render('Frontend/news/news_by_categ.html.twig', [
  122.                     'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
  123.                             'news',
  124.                             $request->getLocale()
  125.                     ),
  126.                     'category' => $oCategory,
  127.                     'totalPages' => $totalPages,
  128.                     'currentPage' => $currentPage,
  129.                     'news' => $oNews
  130.         ]);
  131.     }
  132.     /**
  133.      * @Route(
  134.      *   {
  135.      *       "es" : "{_locale}/hemeroteca/{currentPage}",
  136.      *       "ca" : "{_locale}/hemeroteca/{currentPage}",
  137.      *       "en" : "{_locale}/news-library/{currentPage}",
  138.      *       "cn" : "{_locale}/报纸图书馆/{currentPage}",
  139.      *    }, name="newsPaginated",
  140.      *       requirements={
  141.      *          "_locale": "es|ca|en|cn",
  142.      *          "_format" : "html"
  143.      *      },
  144.      *     defaults={
  145.      *          "currentPage" = 1
  146.      *     }
  147.      * )
  148.      * https://www.drauta.com/como-paginar-los-resultados-con-doctrine
  149.      */
  150.     public function newsPaginated(Request $request$currentPage): Response {
  151.         $oNews $this->getDoctrine()->getRepository(News::class)->getAllByLangPaginated(
  152.                 $request->getLocale(),
  153.                 $currentPage
  154.         );
  155.         $totalPages ceil($oNews->count() / Constants::NEWS_PER_PAGE);
  156.         return $this->render('Frontend/news/news_paginated.html.twig', [
  157.                     'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
  158.                             'news',
  159.                             $request->getLocale()
  160.                     ),
  161.                     'totalPages' => $totalPages,
  162.                     'currentPage' => $currentPage,
  163.                     'news' => $oNews
  164.         ]);
  165.     }
  166.     /**
  167.      * @Route(
  168.      *   {
  169.      *       "es" : "{_locale}/noticia/{seoSlug}/{id}",
  170.      *       "ca" : "{_locale}/noticia/{seoSlug}/{id}",
  171.      *       "en" : "{_locale}/new/{seoSlug}/{id}",
  172.      *       "cn" : "{_locale}/new-cn/{seoSlug}/{id}",
  173.      *    }, name="new",
  174.      *       requirements={
  175.      *          "_locale": "es|ca|en|cn",
  176.      *          "_format" : "html"
  177.      *      }
  178.      * )
  179.      */
  180.     public function new(Request $request$idBesoccerApiHelper $besoccerApiHelperUrlHelper $urlHelperTranslatorHelper $translator): Response {
  181.         $oNew $this->getDoctrine()->getRepository(News::class)->getOneByLang($id$request->getLocale());
  182.         if ($oNew === null) {
  183.             throw $this->createNotFoundException('La noticia no existe.');
  184.         }
  185.         $aMatchInfo = [];
  186.         if ($oNew->getBesoccerId() !== null) {
  187.             $aMatchInfo $besoccerApiHelper->getMatchInfo($oNew->getBesoccerId(), $oNew->getPublicationDate());
  188.         }
  189.         $currentSection $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
  190.                 'news',
  191.                 $request->getLocale()
  192.         );
  193.         $currentSection->ogFields = [
  194.             'url' => rtrim($_ENV['PATH_ABS'], '/') .
  195.             $this->generateUrl('new', [
  196.                 'id' => $id,
  197.                 'seoSlug' => $urlHelper->makeUrlPhrase($oNew->getTranslations()[0]->getTitle())
  198.             ]),
  199.         ];
  200.         if (!empty($oNew->getImages())) {
  201.             $aImg $oNew->getImages()[0];
  202.             $currentSection->ogFields['image'] = $urlHelper->createImageUrl(
  203.                     $aImg['section'],
  204.                     $aImg['itemId'],
  205.                     $aImg['id'],
  206.                     'backendGallery',
  207.                     $aImg['extension']
  208.             );
  209.         }
  210.         $fileExists false;
  211.         if ($oNew->getImages()) {
  212.             $imgSocialMediaPath $this->getParameter('kernel.project_dir') . '/public/uploads/_images/' $oNew->getImages()[0]['section'] . '/' $oNew->getImages()[0]['itemId'] . '/resized/' $oNew->getImages()[0]['id'] . '_socialmedia.' $oNew->getImages()[0]['extension'];
  213.             if (file_exists($imgSocialMediaPath)) {
  214.                 $fileExists true;
  215.             } else {
  216.                 $fileExists false;
  217.             }
  218.         }
  219.         $newsCategories $this->getDoctrine()->getRepository(NewsCategory::class)->getAllCategoriesFromNew(
  220.                 $id,
  221.                 $request->getLocale()
  222.         );
  223.         $banners = [];
  224.         foreach ($newsCategories as $key => $record) {
  225.             if ($record->getBanner()) {
  226.                 $banners[] = $record->getBanner();
  227.             }
  228.         }
  229.         $caegoriesGetFiles = [];
  230.         foreach ($banners as $key => $record) {
  231.             $image $this->getDoctrine()->getRepository(File::class)->findOneBy(array('itemId' => $record->getId(), 'section' => 'newscategorybanner''type' => 'image_' $request->getLocale()), ['sort' => 'ASC']);
  232.             $caegoriesGetFiles[$key]['image'] = ($image) ? $urlHelper->createImageUrl('newscategorybanner'$record->getId(), $image->getId(), 'backendGallery'$image->getExtension()) : null;
  233.             $caegoriesGetFiles[$key]['record'] = $translator->getTranslations(NewsCategoryBannerI18n::class, $record->getId())[$request->getLocale()];
  234.         }
  235.         return $this->render('Frontend/news/new.html.twig', [
  236.                     'currentSection' => $currentSection,
  237.                     'new' => $oNew,
  238.                     'relatedNews' => $this->getDoctrine()->getRepository(News::class)->getRelatedByCateg(
  239.                             $oNew,
  240.                             $request->getLocale()
  241.                     ),
  242.                     'banners' => $caegoriesGetFiles,
  243.                     'nextMatch' => $besoccerApiHelper->getNextMatch($request),
  244.                     'matchInfo' => $aMatchInfo,
  245.                     'fileExists' => $fileExists
  246.         ]);
  247.     }
  248.     /**
  249.      * @Route(
  250.      *   {
  251.      *       "es" : "{_locale}/preview/{id}",
  252.      *       "ca" : "{_locale}/preview/{id}",
  253.      *       "en" : "{_locale}/preview/{id}",
  254.      *       "cn" : "{_locale}/preview/{id}",
  255.      *    }, name="preview",
  256.      *       requirements={
  257.      *          "_locale": "es|ca|en|cn",
  258.      *          "_format" : "html"
  259.      *      }
  260.      * )
  261.      */
  262.     public function preview(
  263.             Request $request,
  264.             $id,
  265.             BesoccerApiHelper $besoccerApiHelper,
  266.             TranslatorHelper $translator
  267.     ): Response {
  268. //         dump($id);
  269. //         dump($request->getLocale());
  270. //         die;
  271.         $oNew $this->getDoctrine()->getRepository(News::class)->getOneByLangForPreview($id$request->getLocale());
  272.         if ($oNew === null) {
  273.             throw $this->createNotFoundException('La noticia no existe o no existe en este idioma.');
  274.         }
  275. //        dump($oNew);
  276.         $aMatchInfo = [];
  277.         if ($oNew->getBesoccerId() !== null) {
  278.             $aMatchInfo $besoccerApiHelper->getMatchInfo($oNew->getBesoccerId(), $oNew->getPublicationDate());
  279.         }
  280.         return $this->render('Frontend/news/preview.html.twig', [
  281.                     'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
  282.                             'news',
  283.                             $request->getLocale()
  284.                     ),
  285.                     'new' => $oNew,
  286.                     'relatedNews' => $this->getDoctrine()->getRepository(News::class)->getRelatedByCateg(
  287.                             $oNew,
  288.                             $request->getLocale()
  289.                     ),
  290.                     'nextMatch' => $besoccerApiHelper->getNextMatch($request),
  291.                     'matchInfo' => $aMatchInfo,
  292.                     'newTranslations' => $translator->getTranslations(NewsI18n::class, $id)
  293.         ]);
  294.     }
  295. }