src\Controller\Site\NewsController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Site;
  3. use App\Controller\BaseController;
  4. use App\Entity\News;
  5. use App\Entity\Typenews;
  6. use App\Repository\NewsRepository;
  7. use App\Repository\TypenewsRepository;
  8. use App\Traits\GtranslateTrait;
  9. use Cocur\Slugify\Slugify;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Google\Cloud\Translate\V2\TranslateClient;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use App\Entity\Commentaire;
  18. use App\Form\CommentaireType;
  19. use App\Repository\CommentaireRepository;
  20. class NewsController extends BaseController
  21. {
  22.     use GtranslateTrait;
  23.     private $em,$paginator;
  24.     public function __construct(EntityManagerInterface $emPaginatorInterface $paginator)
  25.     {
  26.         $this->em=$em;
  27.         $this->paginator $paginator;
  28.     }
  29.     /**
  30.      * @Route("/actualites/{slug?}", name="site_news")
  31.      * @throws \Exception
  32.      */
  33.     public function index(Request $requestCommentaireRepository $commentaireRepository,Typenews $typenews=null)
  34.     {
  35.         //$this->makeSlug();
  36.         $total_per_page 20;
  37.         $selectedDate=$request->query->get('date');
  38.         $date=$selectedDate?new \DateTime($selectedDate):false;
  39.         $q=($request->query->get('q'))?$request->query->get('q'):false;
  40.         $type=($request->query->get('type'))?$request->query->get('type'):false;
  41.         $typeId=($typenews)? $typenews->getId():null;
  42.         $all=$this->em->getRepository(News::class)->findByCat($typeId,$date,$q,$type);
  43.         $paginedData=$this->paginator->paginate($all,$request->query->getInt('page'1),$total_per_page);
  44.         $items $paginedData->getItems();
  45.         $lang $request->query->get('lang');
  46.         if ($lang == 'en'){
  47.             $translated false;
  48.             foreach ($items as $item) {
  49.                 if (!$t_en $item->getTitreEn()){
  50.                     $item->setTitreEn($this->g_translate($item->getTitre()));
  51.                     //dump($t_en, $item);
  52.                     $translated true;
  53.                 }
  54.             }
  55.             if ($translated){
  56.                 $this->em->flush();
  57.                 $all=$this->em->getRepository(News::class)->findByCat($typeId,$date,$q,$type);
  58.                 $paginedData $this->paginator->paginate($all,$request->query->getInt('page'1),$total_per_page);
  59.             }
  60.         }else{
  61.             $lang 'fr';
  62.         }
  63.         //dd($paginedData->getItems());
  64.         return $this->render('site/news/index.html.twig', [
  65.             'newsfinance'=>$paginedData,
  66.             'selectedDate'=>$selectedDate,
  67.             'typeId' => $typeId,
  68.             'commentaires'=>$commentaireRepository->findAll(),
  69.             'type'=>$type,
  70.             'q' => $q,
  71.             'lang' => $lang,
  72.         ]);
  73.     }
  74.       /**
  75.      * @Route("/actualite/{id}", name="site_news_show")
  76.      */
  77.     public function show(News $newsNewsRepository $repositoryRequest $request,CommentaireRepository $commentaireRepository) : Response
  78.     {
  79.         $lang $request->query->get('lang');
  80.         //dd($request->headers->get('referer'));
  81.         if ($lang == 'en'){
  82.             //dd($this->g_translate("Bonjour le monde"));
  83.             $t_translated $d_translated false;
  84.             if ($news->getTitreEn() == null) {
  85.                 $t_en $this->g_translate($news->getTitre());
  86.                 $t_translated true;
  87.                 $news->setTitreEn($t_en);
  88.             }
  89.             if ($news->getDescriptionEn() == null){
  90.                 $d_en $this->g_translate($news->getDescription());
  91.                 $d_translated true;
  92.                 $news->setDescriptionEn($d_en);
  93.             }
  94.             if ($t_translated OR $d_translated){
  95.                 $this->em->persist($news);
  96.                 $this->em->flush();
  97.                 $this->em->refresh($news);
  98.             }
  99.             if ($news->getTitreEn() == null OR $news->getDescriptionEn() == null){
  100.                 $r $request->headers->get('referer');
  101.                 return $this->redirect($r $r '/');
  102.             }
  103.         }else{
  104.             $lang 'fr';
  105.         }
  106.         //dd();
  107.         if(!$news->getStatus()){
  108.             return  $this->redirectToRoute('site_news');
  109.         }
  110.           $idnews=$request->attributes->get('id');
  111.          $commentaire = new Commentaire();
  112.         $form $this->createForm(CommentaireType::class, $commentaire);
  113.         $form->handleRequest($request);
  114.         if ($form->isSubmitted() && $form->isValid()) {
  115.              $commentaire->setUser($this->getUser());
  116.              $commentaire->setNews($news);
  117.             $entityManager $this->getDoctrine()->getManager();
  118.             $entityManager->persist($commentaire);
  119.             $entityManager->flush();
  120.           return $this->redirectToRoute('site_news_show',['id'=>$idnews]);
  121.         }
  122.         $status=true;
  123.         $related=$repository->findRelated($news,3,true);
  124.         $others $repository->findRelated($news,9);
  125.        $nbNews=$this->em->getRepository(Commentaire::class)->getNb($status,$idnews);
  126.         $all=$this->em->getRepository(Commentaire::class)->findByNews($status,$idnews);
  127.         $paginedData=$this->paginator->paginate($all,$request->query->getInt('page'1),2);
  128.         //dd($news->getImageCode());
  129.         return $this->render('site/news/show.html.twig', [
  130.             'news'=>$news,
  131.             'related'=>$related,
  132.             'others'=>$others,
  133.             'nbnews'=>$nbNews,
  134.              'commentaires' => $paginedData,
  135.             'form' => $form->createView(),
  136.             'lang' => $lang,
  137.         ]);
  138.     }
  139.     private function makeSlug(){
  140.         $typenewsRepository $this->em->getRepository(Typenews::class);
  141.         $slugify=new Slugify();
  142.         foreach ($typenewsRepository->findAll() as $item) {
  143.             $item->setSlug($slugify->slugify($item->getLibelle()));
  144.         }
  145.         $this->em->flush();
  146.     }
  147. }