<?php
namespace App\Controller\Site;
use App\Controller\BaseController;
use App\Entity\News;
use App\Entity\Typenews;
use App\Repository\NewsRepository;
use App\Repository\TypenewsRepository;
use App\Traits\GtranslateTrait;
use Cocur\Slugify\Slugify;
use Doctrine\ORM\EntityManagerInterface;
use Google\Cloud\Translate\V2\TranslateClient;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Commentaire;
use App\Form\CommentaireType;
use App\Repository\CommentaireRepository;
class NewsController extends BaseController
{
use GtranslateTrait;
private $em,$paginator;
public function __construct(EntityManagerInterface $em, PaginatorInterface $paginator)
{
$this->em=$em;
$this->paginator = $paginator;
}
/**
* @Route("/actualites/{slug?}", name="site_news")
* @throws \Exception
*/
public function index(Request $request, CommentaireRepository $commentaireRepository,Typenews $typenews=null)
{
//$this->makeSlug();
$total_per_page = 20;
$selectedDate=$request->query->get('date');
$date=$selectedDate?new \DateTime($selectedDate):false;
$q=($request->query->get('q'))?$request->query->get('q'):false;
$type=($request->query->get('type'))?$request->query->get('type'):false;
$typeId=($typenews)? $typenews->getId():null;
$all=$this->em->getRepository(News::class)->findByCat($typeId,$date,$q,$type);
$paginedData=$this->paginator->paginate($all,$request->query->getInt('page', 1),$total_per_page);
$items = $paginedData->getItems();
$lang = $request->query->get('lang');
if ($lang == 'en'){
$translated = false;
foreach ($items as $item) {
if (!$t_en = $item->getTitreEn()){
$item->setTitreEn($this->g_translate($item->getTitre()));
//dump($t_en, $item);
$translated = true;
}
}
if ($translated){
$this->em->flush();
$all=$this->em->getRepository(News::class)->findByCat($typeId,$date,$q,$type);
$paginedData = $this->paginator->paginate($all,$request->query->getInt('page', 1),$total_per_page);
}
}else{
$lang = 'fr';
}
//dd($paginedData->getItems());
return $this->render('site/news/index.html.twig', [
'newsfinance'=>$paginedData,
'selectedDate'=>$selectedDate,
'typeId' => $typeId,
'commentaires'=>$commentaireRepository->findAll(),
'type'=>$type,
'q' => $q,
'lang' => $lang,
]);
}
/**
* @Route("/actualite/{id}", name="site_news_show")
*/
public function show(News $news, NewsRepository $repository, Request $request,CommentaireRepository $commentaireRepository) : Response
{
$lang = $request->query->get('lang');
//dd($request->headers->get('referer'));
if ($lang == 'en'){
//dd($this->g_translate("Bonjour le monde"));
$t_translated = $d_translated = false;
if ($news->getTitreEn() == null) {
$t_en = $this->g_translate($news->getTitre());
$t_translated = true;
$news->setTitreEn($t_en);
}
if ($news->getDescriptionEn() == null){
$d_en = $this->g_translate($news->getDescription());
$d_translated = true;
$news->setDescriptionEn($d_en);
}
if ($t_translated OR $d_translated){
$this->em->persist($news);
$this->em->flush();
$this->em->refresh($news);
}
if ($news->getTitreEn() == null OR $news->getDescriptionEn() == null){
$r = $request->headers->get('referer');
return $this->redirect($r ? $r : '/');
}
}else{
$lang = 'fr';
}
//dd();
if(!$news->getStatus()){
return $this->redirectToRoute('site_news');
}
$idnews=$request->attributes->get('id');
$commentaire = new Commentaire();
$form = $this->createForm(CommentaireType::class, $commentaire);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$commentaire->setUser($this->getUser());
$commentaire->setNews($news);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($commentaire);
$entityManager->flush();
return $this->redirectToRoute('site_news_show',['id'=>$idnews]);
}
$status=true;
$related=$repository->findRelated($news,3,true);
$others = $repository->findRelated($news,9);
$nbNews=$this->em->getRepository(Commentaire::class)->getNb($status,$idnews);
$all=$this->em->getRepository(Commentaire::class)->findByNews($status,$idnews);
$paginedData=$this->paginator->paginate($all,$request->query->getInt('page', 1),2);
//dd($news->getImageCode());
return $this->render('site/news/show.html.twig', [
'news'=>$news,
'related'=>$related,
'others'=>$others,
'nbnews'=>$nbNews,
'commentaires' => $paginedData,
'form' => $form->createView(),
'lang' => $lang,
]);
}
private function makeSlug(){
$typenewsRepository = $this->em->getRepository(Typenews::class);
$slugify=new Slugify();
foreach ($typenewsRepository->findAll() as $item) {
$item->setSlug($slugify->slugify($item->getLibelle()));
}
$this->em->flush();
}
}