<?php
namespace App\Controller\Frontend;
use App\Entity\EspanyolTv;
use App\Entity\HomeSlide;
use App\Entity\NewsCategory;
use App\Entity\NewsCategoryBanner;
use App\Entity\NewsCategoryBannerI18n;
use App\Entity\Section;
use App\Entity\News;
use App\Entity\SlideNews;
use App\Entity\File;
use App\Entity\NewsI18n;
use App\Classes\Constants;
use App\Service\BesoccerApiHelper;
use App\Service\TranslatorHelper;
use App\Service\Url;
use App\Service\UrlHelper;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
/**
* Class NewsController
* @package App\Controller\Frontend
*/
class NewsController extends AbstractController {
/**
* @Route(
* {
* "es" : "{_locale}/noticias",
* "ca" : "{_locale}/noticies",
* "en" : "{_locale}/news",
* "cn" : "{_locale}/消息",
* }, name="news",
* requirements={
* "_locale": "es|ca|en|cn",
* "_format" : "html"
* }
* )
*/
public function news(Request $request): Response {
$aSlides = $this->getDoctrine()->getRepository(HomeSlide::class)->getAllByLang(
$request->getLocale(),
true,
false
);
// dump($aSlides);
$newsSlides = [];
foreach ($aSlides as $key => $value) {
// if (!$value->getTranslations()[0]->getLink()) {
$slideNews = $this->getDoctrine()->getRepository(SlideNews::class)->findOneBy(['slide' => $value, 'language' => $request->getLocale()]);
// dump($slideNews);
if ($slideNews) {
$news = $this->getDoctrine()->getRepository(News::class)->getOneByLang($slideNews->getNews()->getId(), $request->getLocale());
// dump($news);
if ($news) {
// dump($news->getTranslations()[0]->getSeoTitle());
$url = $this->generateUrl('new', array('seoSlug' => $news->getTranslations()[0]->getTitle(), 'id' => $news->getId()));
// dump($url);
// dump($news->getTranslations()[0]);
// $news->getTranslations()[0]->setLink($url);
$newsSlides[$key]['slide'] = $news;
$newsSlides[$key]['url'] = $url;
}
} else {
// dump($news->getTranslations()[0]->getSeoTitle());
// $url = $this->generateUrl('new', array('seoSlug' => $value->getTranslations()[0]->getTitle(), 'id' => $value->getId()));
// dump($url);
// dump($news->getTranslations()[0]);
// $news->getTranslations()[0]->setLink($url);
$newsSlides[$key]['slide'] = $value;
$newsSlides[$key]['url'] = $value->getTranslations()[0]->getLink();
}
// }
}
return $this->render('Frontend/news/news.html.twig', [
'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
'news',
$request->getLocale()
),
'newsCategories' => $this->getDoctrine()->getRepository(NewsCategory::class)->getAllByLang(
$request->getLocale()
),
'news' => $this->getDoctrine()->getRepository(News::class)->getAllByLang($request->getLocale(), 21),
'videos' => $this->getDoctrine()->getRepository(EspanyolTv::class)->getAllByLang(
$request->getLocale(),
1,
21
),
'aSlides' => $newsSlides
]);
}
/**
* @Route(
* {
* "es" : "{_locale}/noticias/{seoSlugCategory}/{id}/{currentPage}",
* "ca" : "{_locale}/noticies/{seoSlugCategory}/{id}/{currentPage}",
* "en" : "{_locale}/information/{seoSlugCategory}/{id}/{currentPage}",
* "cn" : "{_locale}/消息/{seoSlugCategory}/{id}/{currentPage}",
* }, name="newsByCateg",
* requirements={
* "_locale": "es|ca|en|cn",
* "_format" : "html"
* },
* defaults={
* "currentPage" = 1
* }
* )
*/
public function newsByCateg(Request $request, $id, $currentPage): Response {
$oCategory = $this->getDoctrine()->getRepository(NewsCategory::class)->getOneByLang($id, $request->getLocale());
if ($oCategory === null) {
throw $this->createNotFoundException('La categoría no existe.');
}
$oNews = $this->getDoctrine()->getRepository(News::class)->getAllRelatedByCateg(
$id,
$request->getLocale(),
$currentPage
);
$totalPages = ceil($oNews->count() / Constants::NEWS_PER_PAGE);
return $this->render('Frontend/news/news_by_categ.html.twig', [
'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
'news',
$request->getLocale()
),
'category' => $oCategory,
'totalPages' => $totalPages,
'currentPage' => $currentPage,
'news' => $oNews
]);
}
/**
* @Route(
* {
* "es" : "{_locale}/hemeroteca/{currentPage}",
* "ca" : "{_locale}/hemeroteca/{currentPage}",
* "en" : "{_locale}/news-library/{currentPage}",
* "cn" : "{_locale}/报纸图书馆/{currentPage}",
* }, name="newsPaginated",
* requirements={
* "_locale": "es|ca|en|cn",
* "_format" : "html"
* },
* defaults={
* "currentPage" = 1
* }
* )
* https://www.drauta.com/como-paginar-los-resultados-con-doctrine
*/
public function newsPaginated(Request $request, $currentPage): Response {
$oNews = $this->getDoctrine()->getRepository(News::class)->getAllByLangPaginated(
$request->getLocale(),
$currentPage
);
$totalPages = ceil($oNews->count() / Constants::NEWS_PER_PAGE);
return $this->render('Frontend/news/news_paginated.html.twig', [
'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
'news',
$request->getLocale()
),
'totalPages' => $totalPages,
'currentPage' => $currentPage,
'news' => $oNews
]);
}
/**
* @Route(
* {
* "es" : "{_locale}/noticia/{seoSlug}/{id}",
* "ca" : "{_locale}/noticia/{seoSlug}/{id}",
* "en" : "{_locale}/new/{seoSlug}/{id}",
* "cn" : "{_locale}/new-cn/{seoSlug}/{id}",
* }, name="new",
* requirements={
* "_locale": "es|ca|en|cn",
* "_format" : "html"
* }
* )
*/
public function new(Request $request, $id, BesoccerApiHelper $besoccerApiHelper, UrlHelper $urlHelper, TranslatorHelper $translator): Response {
$oNew = $this->getDoctrine()->getRepository(News::class)->getOneByLang($id, $request->getLocale());
if ($oNew === null) {
throw $this->createNotFoundException('La noticia no existe.');
}
$aMatchInfo = [];
if ($oNew->getBesoccerId() !== null) {
$aMatchInfo = $besoccerApiHelper->getMatchInfo($oNew->getBesoccerId(), $oNew->getPublicationDate());
}
$currentSection = $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
'news',
$request->getLocale()
);
$currentSection->ogFields = [
'url' => rtrim($_ENV['PATH_ABS'], '/') .
$this->generateUrl('new', [
'id' => $id,
'seoSlug' => $urlHelper->makeUrlPhrase($oNew->getTranslations()[0]->getTitle())
]),
];
if (!empty($oNew->getImages())) {
$aImg = $oNew->getImages()[0];
$currentSection->ogFields['image'] = $urlHelper->createImageUrl(
$aImg['section'],
$aImg['itemId'],
$aImg['id'],
'backendGallery',
$aImg['extension']
);
}
$fileExists = false;
if ($oNew->getImages()) {
$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'];
if (file_exists($imgSocialMediaPath)) {
$fileExists = true;
} else {
$fileExists = false;
}
}
$newsCategories = $this->getDoctrine()->getRepository(NewsCategory::class)->getAllCategoriesFromNew(
$id,
$request->getLocale()
);
$banners = [];
foreach ($newsCategories as $key => $record) {
if ($record->getBanner()) {
$banners[] = $record->getBanner();
}
}
$caegoriesGetFiles = [];
foreach ($banners as $key => $record) {
$image = $this->getDoctrine()->getRepository(File::class)->findOneBy(array('itemId' => $record->getId(), 'section' => 'newscategorybanner', 'type' => 'image_' . $request->getLocale()), ['sort' => 'ASC']);
$caegoriesGetFiles[$key]['image'] = ($image) ? $urlHelper->createImageUrl('newscategorybanner', $record->getId(), $image->getId(), 'backendGallery', $image->getExtension()) : null;
$caegoriesGetFiles[$key]['record'] = $translator->getTranslations(NewsCategoryBannerI18n::class, $record->getId())[$request->getLocale()];
}
return $this->render('Frontend/news/new.html.twig', [
'currentSection' => $currentSection,
'new' => $oNew,
'relatedNews' => $this->getDoctrine()->getRepository(News::class)->getRelatedByCateg(
$oNew,
$request->getLocale()
),
'banners' => $caegoriesGetFiles,
'nextMatch' => $besoccerApiHelper->getNextMatch($request),
'matchInfo' => $aMatchInfo,
'fileExists' => $fileExists
]);
}
/**
* @Route(
* {
* "es" : "{_locale}/preview/{id}",
* "ca" : "{_locale}/preview/{id}",
* "en" : "{_locale}/preview/{id}",
* "cn" : "{_locale}/preview/{id}",
* }, name="preview",
* requirements={
* "_locale": "es|ca|en|cn",
* "_format" : "html"
* }
* )
*/
public function preview(
Request $request,
$id,
BesoccerApiHelper $besoccerApiHelper,
TranslatorHelper $translator
): Response {
// dump($id);
// dump($request->getLocale());
// die;
$oNew = $this->getDoctrine()->getRepository(News::class)->getOneByLangForPreview($id, $request->getLocale());
if ($oNew === null) {
throw $this->createNotFoundException('La noticia no existe o no existe en este idioma.');
}
// dump($oNew);
$aMatchInfo = [];
if ($oNew->getBesoccerId() !== null) {
$aMatchInfo = $besoccerApiHelper->getMatchInfo($oNew->getBesoccerId(), $oNew->getPublicationDate());
}
return $this->render('Frontend/news/preview.html.twig', [
'currentSection' => $this->getDoctrine()->getRepository(Section::class)->getOneByLang(
'news',
$request->getLocale()
),
'new' => $oNew,
'relatedNews' => $this->getDoctrine()->getRepository(News::class)->getRelatedByCateg(
$oNew,
$request->getLocale()
),
'nextMatch' => $besoccerApiHelper->getNextMatch($request),
'matchInfo' => $aMatchInfo,
'newTranslations' => $translator->getTranslations(NewsI18n::class, $id)
]);
}
}