src/Controller/SecurityController.php line 860

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Sondage;
  4. use App\Entity\TypeSondage;
  5. use App\Entity\Comment;
  6. use App\Entity\Enquetes
  7. use App\Entity\ReponseUser;
  8. use App\Entity\Infousers;
  9. use App\Entity\PayeUsers;
  10. use App\Entity\UserVip;
  11. use App\Entity\User;
  12. use App\Entity\UsersAuthToken;
  13. use App\Entity\Forums;
  14. use App\Entity\UserNiveauAtteint;
  15. use App\Entity\UserOccupationPrincipale;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. use Symfony\Component\Security\Core\Security;
  24. use Pusher\Pusher;
  25. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  26. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  27. use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
  28. use Symfony\Component\Mailer\MailerInterface;
  29. use Symfony\Contracts\Translation\TranslatorInterface;
  30. use Symfony\Component\Mime\Email;
  31. class SecurityController extends AbstractController
  32. {
  33.     private $session;
  34.     private $security;
  35.     private $pusher;
  36.     public function __construct(SessionInterface $session,Security $security,Pusher $pusher)
  37.     {
  38.         $this->session $session;
  39.         $this->security $security;
  40.         $this->pusher $pusher;
  41.     }
  42.     
  43.     public function redirectadmin()
  44.     {
  45.         if($this->getUser()){
  46.             return null;
  47.         }
  48.         return $this->redirectToRoute('home');
  49.         
  50.     }
  51.     public function home()
  52.     {
  53.         return $this->redirectToRoute('home');
  54.     }
  55.      public function index(Request $request)
  56.     {
  57.         if($request->get('auth') && $request->get('auth')=="success"){
  58.             $entityManager $this->getDoctrine()->getManager();
  59.             $tokenuser uniqid().$request->get('_csrf_token').uniqid();
  60.             $user $entityManager->getRepository(User::class)->findOneBy(['username' => $request->get('login')]);
  61.             if (!$user) {
  62.                 $user $entityManager->getRepository(User::class)->findOneBy(['phone' => $request->get('login')]);
  63.             }
  64.             if (!$user) {
  65.                 $user $entityManager->getRepository(User::class)->findOneBy(['email' => $request->get('login')]);
  66.             }
  67.             if ($user) {
  68.                 
  69.                 $AuthToken = new UsersAuthToken();
  70.                 $AuthToken->setIdUser($user->getId());
  71.                 $AuthToken->setToken($tokenuser);
  72.                 $AuthToken->setCreateAt(new \DateTime("now"));
  73.                 $AuthToken->setExpired(0);
  74.                 $entityManager->persist($AuthToken);
  75.                 $entityManager->flush();
  76.                 
  77.                 return $this->redirectToRoute('home',["token"=>$tokenuser]);
  78.             }
  79.         }
  80.         if ($this->getUser()) {
  81.             $page_sondage $this->session->get('page_sondage');
  82.             if ($page_sondage && $page_sondage!=""){
  83.                 $reponseuser = new ReponseuserController($this->security,$this->pusher);
  84.                 $user_sondage $this->session->get('reponse_user_sondage');
  85.                 $user_sondage_type $this->session->get('reponse_user_sondage_type');
  86.                 if($user_sondage !=""){
  87.                     $request->attributes->set('user_sondage',$user_sondage);
  88.                     $request->attributes->set('user_sondage_type',$user_sondage_type);
  89.                     $request->attributes->set('user_sondage_login','oui');
  90.                     $reponseuser->voter_sondage($request,$page_sondage,$this->getDoctrine());
  91.                 }
  92.                 
  93.                 $sondage$this->getDoctrine()->getRepository(Sondage::class)->find($page_sondage);
  94.                 if($request->getLocale()=="fr"){
  95.                     $slug $sondage->getSlugFr();
  96.                 }else{
  97.                     $slug $sondage->getSlugAr();
  98.                 }
  99.                 if($sondage->getCategories()){
  100.                     $categorie$this->getDoctrine()->getRepository(TypeSondage::class)->find($sondage->getCategories()[0]);
  101.                     if($request->getLocale()=="fr"){
  102.                         $categorie=$categorie->getSlug();
  103.                     }else{
  104.                         $categorie=$categorie->getArSlug();
  105.                     }
  106.                     
  107.                 }else{
  108.                     $categorie="null";
  109.                 }
  110.                 $this->session->set('page_sondage',"");
  111.                 $this->session->set('user_sondage',"");
  112.                 $this->session->set('user_sondage_type',"");
  113.                 $this->session->set('user_sondage_login',"");
  114.                 if($request->get('token') && $request->get('token')!=""){
  115.                     return $this->redirectToRoute('detail_sondage', array(
  116.                         'categorie' => $categorie,
  117.                         'slug' =>   $slug,
  118.                         'token' =>$request->get('token')
  119.                     ));
  120.                 }else{
  121.                     return $this->redirectToRoute('detail_sondage', array(
  122.                         'categorie' => $categorie,
  123.                         'slug' =>   $slug
  124.                     ));
  125.                 }
  126.                 
  127.             }
  128.         }
  129.         $sondages $this->getDoctrine()->getRepository(Sondage::class)->findBy(['post_status' => 'publish'],['id' => 'DESC']);
  130.         $sql "
  131.            SELECT s.id as id_s , 
  132.                 (SELECT count(*) FROM reponse_user r WHERE s.id = r.id_sondage ) as nbvotes ,
  133.                 (SELECT count(*) FROM comment c WHERE s.id = c.id_sondage ) as nbcomments
  134.             FROM sondage s
  135.         ";
  136.         $nbvotesnbcomments $this->getDoctrine()->getManager()->getConnection()->prepare($sql);
  137.         $nbvotesnbcomments->execute();
  138.         $nbvotesnbcomments $nbvotesnbcomments->fetchAll();
  139.         $nbvotes_nbcomments = [];
  140.         foreach ($nbvotesnbcomments as $key => $value) {
  141.             $nbvotes_nbcomments [$value["id_s"]] = $value;
  142.         }
  143.         $sondages_art_culture $this->get_sondages_home_by_categorie($sondages,1,$request->getLocale(),4,$nbvotes_nbcomments);
  144.         $sondages_celebrites $this->get_sondages_home_by_categorie($sondages,2,$request->getLocale(),6,$nbvotes_nbcomments);
  145.         $sondages_divertissement $this->get_sondages_home_by_categorie($sondages,3,$request->getLocale(),4,$nbvotes_nbcomments);
  146.         $sondages_economie $this->get_sondages_home_by_categorie($sondages,4,$request->getLocale(),4,$nbvotes_nbcomments);
  147.         $sondages_medias $this->get_sondages_home_by_categorie($sondages,5,$request->getLocale(),4,$nbvotes_nbcomments);
  148.         $sondages_santes $this->get_sondages_home_by_categorie($sondages,12,$request->getLocale(),4,$nbvotes_nbcomments);
  149.         $sondages_society $this->get_sondages_home_by_categorie($sondages,8,$request->getLocale(),6,$nbvotes_nbcomments);
  150.         $sondages_policy $this->get_sondages_home_by_categorie($sondages,9,$request->getLocale(),4,$nbvotes_nbcomments);
  151.         $sondages_sport $this->get_sondages_home_by_categorie($sondages,7,$request->getLocale(),5,$nbvotes_nbcomments);
  152.         $sondages_divers $this->get_sondages_home_by_categorie($sondages,11,$request->getLocale(),5,$nbvotes_nbcomments);
  153.         
  154.         
  155.         $typesondage $this->getDoctrine()->getRepository(TypeSondage::class)->findAll();
  156.         $categories = [] ;
  157.         foreach ($typesondage as $key => $value) {
  158.             $categories [$value->getId()] =$value;
  159.         }
  160.         $sondages_slider $this->prepare_slider_home($request->getLocale());
  161.         $pays$this->getDoctrine()->getRepository(PayeUsers::class)->findBy(['parent' => null]);
  162.         $paysSondage=[];
  163.         foreach ($pays as $key => $value) {
  164.             if($request->getLocale()=="fr"){
  165.                 $paysSondage [$value->getId()] =$value->getLibelle();
  166.             }elseif($request->getLocale()=="ar"){
  167.                 $paysSondage [$value->getId()] =$value->getLibelleAr();
  168.             }
  169.         }
  170.         return $this->render('security/index.html.twig', [
  171.             'page'                      => 'Home',
  172.             'controller_name'           => 'SecurityController',
  173.             'categories'                => $categories,
  174.             'sondages_slider'              => $sondages_slider,
  175.             'sondages_art_culture'      => $sondages_art_culture,
  176.             'sondages_celebrites'       => $sondages_celebrites,
  177.             'sondages_divertissement'   => $sondages_divertissement,
  178.             'sondages_economie'         => $sondages_economie,
  179.             'sondages_medias'           => $sondages_medias,
  180.             'sondages_santes'           => $sondages_santes,
  181.             'sondages_sport'            => $sondages_sport,
  182.             'sondages_society'          => $sondages_society,
  183.             'sondages_policy'           => $sondages_policy,
  184.             'sondages_divers'           => $sondages_divers,
  185.             'pays'                      => $paysSondage
  186.         ]);
  187.     }
  188.     public function get_sondages_home_by_categorie($sondages,$categorie,$long,$nb_sondages,$nbvotes_nbcomments)
  189.     {
  190.         if($long=="fr"){
  191.             foreach ($sondages as $key => $sondage) {
  192.                 if( $sondage->getLongSondage() =="ar" ){
  193.                     unset($sondages[$key]);
  194.                 }else{
  195.                     if(!in_array($categorie$sondage->getCategories())){
  196.                         unset($sondages[$key]);
  197.                     }
  198.                 }
  199.             }
  200.         }else{
  201.             foreach ($sondages as $key => $sondage) {
  202.                 if( $sondage->getLongSondage() =="fr" ){
  203.                     unset($sondages[$key]);
  204.                 }else{
  205.                     if(!in_array($categorie$sondage->getCategories())){
  206.                         unset($sondages[$key]);
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.         
  212.         $new_sondages=[];
  213.         //if($nb_sondages<=sizeof($sondages)){
  214.             $i=0;
  215.             foreach ($sondages as $key => $sondage) {
  216.                 if($i<$nb_sondages){
  217.                 $new_sondages [] =$sondage
  218.                 }else{
  219.                     break;
  220.                 }
  221.                 $i++;
  222.             }
  223.             foreach ($new_sondages as $key => &$sondage) {
  224.                 //$comments = $this->getDoctrine()->getRepository(Comment::class)->findBy(['id_sondage'=>$sondage->getId()]);
  225.                 $sondage->{'nbcomments'} = $nbvotes_nbcomments[$sondage->getId()]["nbcomments"];//sizeof($comments);
  226.                 //$reponseUser= $this->getDoctrine()->getRepository(ReponseUser::class)->findBy(["id_sondage"=>$sondage->getId()]);
  227.                 $sondage->{'nbvotes'} = $nbvotes_nbcomments[$sondage->getId()]["nbvotes"]+$sondage->getVoteVartuel();//sizeof($reponseUser);
  228.             }
  229.         //}
  230.         return $new_sondages;
  231.     }
  232.     public function admin(Request $request)
  233.     {
  234.         if ($this->getUser()) {
  235.             if ($this->getUser()->getValid()==null){
  236.                 // $PayesUser = $this->getDoctrine()->getRepository(PayeUsers::class)->findBy(['parent' => 1]);
  237.                 
  238.                 // foreach ($PayesUser  as $key => &$PayeUser ) {
  239.                 //     $filss = $this->getDoctrine()->getRepository(PayeUsers::class)->findBy(['parent' => $PayeUser->getId()]);
  240.                 //     foreach ($filss  as $key => &$fils ) {
  241.                 //         $fils->{'fils'}=$this->getDoctrine()->getRepository(PayeUsers::class)->findBy(['parent' => $fils->getId()]);
  242.                 //     }
  243.                 //     $PayeUser->{'fils'}=$filss;
  244.                 // }
  245.                 // $infouser = $this->getDoctrine()->getRepository(Infousers::class)->findBy(['id_user'=>$this->getUser()->getId()]);
  246.                 // $NiveausAtteint = $this->getDoctrine()->getRepository(UserNiveauAtteint::class)->findAll();
  247.                 // $OccupationsPrincipale = $this->getDoctrine()->getRepository(UserOccupationPrincipale::class)->findAll();
  248.                 // return $this->render('user/complete_info.html.twig', [
  249.                 //     'page'                  => 'User',
  250.                 //     'controller_name'       => 'UserController',
  251.                 //     'PayesUser'             => $PayesUser,
  252.                 //     'infouser'              => $infouser[0],
  253.                 //     'NiveausAtteint'        => $NiveausAtteint,
  254.                 //     'OccupationsPrincipale' => $OccupationsPrincipale 
  255.                 // ]);
  256.             }
  257.             $page_sondage $this->session->get('page_sondage');
  258.             if ($page_sondage && $page_sondage!=""){
  259.                 $reponseuser = new ReponseuserController($this->security,$this->pusher);
  260.                 $user_sondage $this->session->get('reponse_user_sondage');
  261.                 $user_sondage_type $this->session->get('reponse_user_sondage_type');
  262.                 if($user_sondage !=""){
  263.                     $request->attributes->set('user_sondage',$user_sondage);
  264.                     $request->attributes->set('user_sondage_type',$user_sondage_type);
  265.                     $request->attributes->set('user_sondage_login','oui');
  266.                     $reponseuser->voter_sondage($request,$page_sondage,$this->getDoctrine());
  267.                 }
  268.                 
  269.                 $sondage$this->getDoctrine()->getRepository(Sondage::class)->find($page_sondage);
  270.                 if($request->getLocale()=="fr"){
  271.                     $slug $sondage->getSlugFr();
  272.                 }else{
  273.                     $slug $sondage->getSlugAr();
  274.                 }
  275.                 if($sondage->getCategories()){
  276.                     $categorie$this->getDoctrine()->getRepository(TypeSondage::class)->find($sondage->getCategories()[0]);
  277.                     if($request->getLocale()=="fr"){
  278.                         $categorie=$categorie->getSlug();
  279.                     }else{
  280.                         $categorie=$categorie->getArSlug();
  281.                     }
  282.                     
  283.                 }else{
  284.                     $categorie="null";
  285.                 }
  286.                 $this->session->set('page_sondage',"");
  287.                 $this->session->set('user_sondage',"");
  288.                 $this->session->set('user_sondage_type',"");
  289.                 $this->session->set('user_sondage_login',"");
  290.                 return $this->redirectToRoute('detail_sondage', array(
  291.                 'categorie' => $categorie,
  292.                 'slug' =>   $slug
  293.                 ));
  294.             }
  295.             $page_enquete $this->session->get('page_enquete');
  296.             if ($page_enquete && $page_enquete!=""){
  297.                 $sondage$this->getDoctrine()->getRepository(Enquetes::class)->find($page_enquete);
  298.                 $categorie=$sondage->getCategories()[0];
  299.                 $this->session->set('page_enquete',"");
  300.                 return $this->redirectToRoute('detail_enquete_id', array(
  301.                 'id_categorie' => $categorie,
  302.                 'id' =>   $page_enquete
  303.                 ));
  304.             }
  305.             $infouser $this->getDoctrine()->getRepository(Infousers::class)->findBy(['id_user'=>$this->getUser()->getId()]);
  306.             if($infouser){
  307.                 $info_user$infouser[0];
  308.                 $all_points_user$this->getDoctrine()->getRepository(ReponseUser::class)->findBy(["id_user"=>$this->getUser()->getId()]);
  309.                 $points_sondage 0;
  310.                 foreach ($all_points_user as $key => $value) {
  311.                     $points_sondage +=$value->getNpPoints();
  312.                 }
  313.                 $points_vip 0;
  314.                 if ($this->getUser()->getUserVip()==7) {
  315.                     $all_points_user$this->getDoctrine()->getRepository(UserVip::class)->findBy(["id_user"=>$this->getUser()->getId()]);
  316.                     foreach ($all_points_user as $key => $value) {
  317.                         $points_vip +=$value->getNbPoint();
  318.                     }
  319.                 }
  320.                 $points_heritage 0;
  321.                 $infouser_parents $this->getDoctrine()->getRepository(Infousers::class)->findBy(['id_parent'=>$this->getUser()->getId()]);
  322.                 foreach ($infouser_parents as $key => $infouser_parent) {
  323.                     if($infouser_parent->getValide()==1){
  324.                         $points_heritage $points_heritage 200 ;
  325.                     }
  326.                 }
  327.             }
  328.             return $this->render('admin/index.html.twig', [
  329.              'page'             => 'Home',
  330.              'controller_name'  => 'SecurityController',
  331.              'infouser'         => $infouser[0],
  332.              'points_vip'       => $points_vip,
  333.              'points_sondage'   => $points_sondage,
  334.              'points_heritage' => $points_heritage,
  335.              'token' => $request->get("token")
  336.             ]);
  337.         }else{
  338.              return $this->redirectToRoute('home');
  339.         }
  340.         
  341.     }
  342.     
  343.     /**
  344.      * @Route("/blockmonaccount", name="blockmonaccount")
  345.     */
  346.     public function blockmonaccount(Request $request)
  347.     {
  348.         if ($this->getUser()) {
  349.             $infouser $this->getDoctrine()->getRepository(Infousers::class)->findBy(['id_user'=>$this->getUser()->getId()]);
  350.             return $this->render('security/block_mon_account.html.twig', [
  351.                 'user'      => $this->getUser(),
  352.                 'infouser'  => $infouser[0],
  353.                 'connected' =>  "oui"
  354.            ]);
  355.         }else{
  356.             return $this->render('security/block_mon_account.html.twig', [
  357.                 'user'      => null,
  358.                 'infouser'  => null,
  359.                 'connected' =>  "non"
  360.            ]);
  361.         }
  362.         
  363.     }
  364.     
  365.     
  366.     /**
  367.      * @Route("/nav_categories-{slog_name}", name="nav_categories")
  368.     */
  369.     public function nav_categories($slog_name null)
  370.     {
  371.         $sql "UPDATE `sondage` SET expirer=1 WHERE date_fin <  CURDATE()  OR (date_fin =  CURDATE() AND `time_fin`<= CURTIME())";
  372.         $update_sondage $this->getDoctrine()->getManager()->getConnection()->prepare($sql);
  373.         $update_sondage->execute();
  374.         $categories $this->getDoctrine()->getRepository(TypeSondage::class)->findBy([],["sort"=>"ASC"]);
  375.         foreach ($categories as $key => $categorie) {
  376.             if($categorie->getSort()==0)
  377.                 unset( $categories[$key] );
  378.         }
  379.         //dd($slog_name);
  380.         return $this->render('nav_categories.html.twig', [
  381.              'nav_categories'              => $categories,
  382.              'slog_name'  =>  $slog_name
  383.         ]);
  384.     }
  385.     
  386.     /**
  387.      * @Route("/get_parrain", name="get_parrain")
  388.     */
  389.     public function get_parrain()
  390.     {
  391.         $name="";
  392.         $user $this->getUser();
  393.         if( $user  && !in_array("ROLE_ADMIN"$user->getRoles())){
  394.             $infouser $this->getDoctrine()->getRepository(Infousers::class)->findBy(["id_user"=>$user->getId()]);
  395.             if($infouser){
  396.                 $parent $this->getDoctrine()->getRepository(Infousers::class)->find($infouser[0]->getIdParent());
  397.                 if($parent){
  398.                     $name=$parent->getNom();
  399.                 }
  400.             }
  401.         }
  402.         
  403.         return $this->render('admin/get_parrain.html.twig', [
  404.              'name'=> $name
  405.         ]);
  406.     }
  407.     public function login(Request $request,AuthenticationUtils $authenticationUtils): Response
  408.     {
  409.         if($request->get('sondage')){
  410.             $this->session->set('page_sondage'$request->get('sondage'));
  411.             $this->session->set('reponse_user_sondage'$request->get('user_sondage'));
  412.             $this->session->set('reponse_user_sondage_type'$request->get('user_sondage_type'));
  413.         }
  414.         if($request->get('enquete')){
  415.             $this->session->set('page_enquete'$request->get('enquete'));
  416.         }
  417.        
  418.         
  419.         
  420.         if ($this->getUser()) {
  421.              return $this->redirectToRoute('home');
  422.         }
  423.         // get the login error if there is one
  424.         $error $authenticationUtils->getLastAuthenticationError();
  425.         // last username entered by the user
  426.         $lastUsername $authenticationUtils->getLastUsername();
  427.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  428.     }
  429.     
  430.     
  431.     public function logout()
  432.     {
  433.         
  434.         //throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  435.     }
  436.     
  437.     public function app_logout_user(Request $request)
  438.     {
  439.        
  440.         dd($request->get('token'));
  441.         
  442.     }
  443.     public function login_google(Request $request)
  444.     {
  445.         if ($this->getUser()) {
  446.              return $this->redirectToRoute('home');
  447.         }
  448.         $user $this->getDoctrine()->getRepository(User::class)->findBy(["email" => $request->get("google_email")]);
  449.         if($user && sizeof($user)>){
  450.             $user $user[0];
  451.             if($user->getIdGoogle() == $request->get("google_id") ){
  452.                 $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  453.                 $this->get('security.token_storage')->setToken($token);
  454.                 $this->get('session')->set('_security_main'serialize($token));
  455.                 $entityManager $this->getDoctrine()->getManager();
  456.                 $tokenuser uniqid().$request->get('_csrf_token').uniqid();
  457.                 $AuthToken = new UsersAuthToken();
  458.                 $AuthToken->setIdUser($user->getId());
  459.                 $AuthToken->setToken($tokenuser);
  460.                 $AuthToken->setCreateAt(new \DateTime("now"));
  461.                 $AuthToken->setExpired(0);
  462.                 $entityManager->persist($AuthToken);
  463.                 $entityManager->flush();
  464.                 return $this->redirectToRoute('home',["token"=>$tokenuser]);
  465.             }else{
  466.                 return $this->redirectToRoute('app_login',['error' => "l'email google existe déjà"]);
  467.             }
  468.         }else{
  469.             return $this->redirectToRoute('add_user',[
  470.                 "email"=>$request->get("google_email"),
  471.                 "name"=>$request->get("google_name"),
  472.                 "prenom"=>$request->get("google_prenom"),
  473.                 "google_id"=>$request->get("google_id"),
  474.                 "login"=>$request->get("google_login"),
  475.                 "photo"=>$request->get("google_photo"),
  476.             ]);
  477.         }
  478.     }
  479.     public function login_facebook(Request $request)
  480.     {
  481.         if ($this->getUser()) {
  482.             return $this->redirectToRoute('home');
  483.         }
  484.         $user $this->getDoctrine()->getRepository(User::class)->findBy(["email" => $request->get("fb_email")]);
  485.         if($user && sizeof($user)>){
  486.             $user $user[0];
  487.             if($user->getIdFacebook() == $request->get("fb_id") ){
  488.                 $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  489.                 $this->get('security.token_storage')->setToken($token);
  490.                 $this->get('session')->set('_security_main'serialize($token));
  491.                 $entityManager $this->getDoctrine()->getManager();
  492.                 $tokenuser uniqid().$request->get('_csrf_token').uniqid();
  493.                 $AuthToken = new UsersAuthToken();
  494.                 $AuthToken->setIdUser($user->getId());
  495.                 $AuthToken->setToken($tokenuser);
  496.                 $AuthToken->setCreateAt(new \DateTime("now"));
  497.                 $AuthToken->setExpired(0);
  498.                 $entityManager->persist($AuthToken);
  499.                 $entityManager->flush();
  500.                 return $this->redirectToRoute('home',["token"=>$tokenuser]);
  501.             }else{
  502.                 return $this->redirectToRoute('app_login',['error' => "l'email facebook existe déjà"]);
  503.             }
  504.         }else{
  505.             return $this->redirectToRoute('add_user',[
  506.                 "email"=>$request->get("fb_email"),
  507.                 "name"=>$request->get("fb_name"),
  508.                 "prenom"=>$request->get("fb_prenom"),
  509.                 "fb_id"=>$request->get("fb_id"),
  510.                 "login"=>$request->get("fb_login"),
  511.                 "photo"=>$request->get("fb_photo"),
  512.             ]);
  513.         }
  514.        
  515.     }
  516.     public function login_key_token(Request $request)
  517.     {
  518.         if ($request->get("login_key_token")!="") {
  519.             $token $this->getDoctrine()->getRepository(UsersAuthToken::class)->findOneBy(["token" => $request->get("login_key_token")]);
  520.             if( $token ){
  521.                 $user $this->getDoctrine()->getRepository(User::class)->find($token->getIdUser());
  522.                 $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  523.                 $this->get('security.token_storage')->setToken($token);
  524.                 $this->get('session')->set('_security_main'serialize($token));
  525.                 return new Response("connected"); 
  526.             }
  527.         }
  528.         return new Response("error"); 
  529.     }
  530.     
  531.     public function forgottenPassword(Request $requestUserPasswordEncoderInterface $encoderMailerInterface $mailerTokenGeneratorInterface $tokenGenerator,TranslatorInterface $translator): Response
  532.     {
  533.         if ($request->isMethod('POST')) {
  534.  
  535.             $email $request->request->get('email');
  536.  
  537.             $entityManager $this->getDoctrine()->getManager();
  538.             $user $entityManager->getRepository(User::class)->findOneByEmail($email);
  539.  
  540.             if ($user === null) {
  541.                 $this->addFlash('danger''Email Inconnu, recommence !');
  542.                 return $this->redirectToRoute('app_forgotten_password');
  543.             }
  544.             $token $tokenGenerator->generateToken();
  545.  
  546.             try{
  547.                 $user->setResetToken($token);
  548.                 $entityManager->flush();
  549.             } catch (\Exception $e) {
  550.                 $this->addFlash('warning'$e->getMessage());
  551.                 return $this->redirectToRoute('home');
  552.             }
  553.  
  554.             $url $this->generateUrl('app_reset_password', array('token' => $token), UrlGeneratorInterface::ABSOLUTE_URL);
  555.             $infouser $this->getDoctrine()->getRepository(Infousers::class)->findBy(['id_user'=>$user->getId()]);
  556.             $message = (new Email())
  557.                 ->from('noreply@e-sondage.com')
  558.                 ->to($user->getEmail())
  559.                 ->subject($translator->trans('Oubli de mot de passe - Réinisialisation'))
  560.                 ->text('Sending emails is fun again!')
  561.                 ->html($this->renderView('security/emails/resetPasswordMail.html.twig', ['user'=>$user,'infouser'=>$infouser[0],'url'=>$url ] ));
  562.             $mailer->send($message);
  563.  
  564.             $this->addFlash('notice''Mail envoyé, tu vas pouvoir te connecter Ã  nouveau !');
  565.  
  566.             
  567.             return $this->redirectToRoute('valider_email',["type"=>"reset_password"]); 
  568.         }
  569.  
  570.         return $this->render('security/forgottenPassword.html.twig');
  571.     }
  572.  
  573.     public function resetPassword(Request $requeststring $tokenUserPasswordEncoderInterface $passwordEncoder)
  574.     {
  575.         $entityManager $this->getDoctrine()->getManager();
  576.         $user $entityManager->getRepository(User::class)->findOneBy(["reset_token"=>$token]);
  577.         /* @var $user User */
  578.         if ($user === null) {
  579.             $this->addFlash('danger''Mot de passe non reconnu');
  580.             return $this->redirectToRoute('home');
  581.         }
  582.         //Reset avec le mail envoyé
  583.         if ($request->isMethod('POST')) {
  584.             
  585.             $password $passwordEncoder->encodePassword($user$request->get('password'));
  586.             $user->setResetToken(null);
  587.             $entityManager->flush();
  588.             $entityManager->getRepository(User::class)->upgradePassword($user,$password);
  589.             // $user->setPassword($passwordEncoder->encodePassword($user, $request->get('password')));
  590.             // 
  591.  
  592.             $this->addFlash('notice''Mot de passe mis Ã  jour !');
  593.  
  594.             return $this->redirectToRoute('app_login');
  595.         }else {
  596.             return $this->render('security/resetPassword.html.twig', ['token' => $token]);
  597.         }
  598.  
  599.     }
  600.     public function prepare_slider_home($long)
  601.     {
  602.         $sondages_slider $this->getDoctrine()->getRepository(Sondage::class)->findBy(["top" => "on",'post_status' => 'publish'],['date_deb' => 'DESC']);
  603.         $enquetes_slider $this->getDoctrine()->getRepository(Enquetes::class)->findBy(["top" => "on"],['id' => 'DESC']);
  604.         
  605.         foreach ($sondages_slider as $key => $slider) {
  606.             if ($long=="fr") {
  607.                 if( $slider->getLongSondage() =="ar" ){
  608.                     unset($sondages_slider[$key]);
  609.                 } 
  610.             }else{
  611.                  if( $slider->getLongSondage() =="fr" ){
  612.                     unset($sondages_slider[$key]);
  613.                 } 
  614.             }
  615.         }
  616.         foreach ($enquetes_slider as $key => $slider) {
  617.             if ($long=="fr") {
  618.                 if( $slider->getLongEnquete() =="ar" ){
  619.                     unset($enquetes_slider[$key]);
  620.                 } 
  621.             }else{
  622.                  if( $slider->getLongEnquete() =="fr" ){
  623.                     unset($enquetes_slider[$key]);
  624.                 } 
  625.             }
  626.         }
  627.         $slider_home=[];
  628.         foreach ($sondages_slider as $key => $sondage) {
  629.             $reponseUser$this->getDoctrine()->getRepository(ReponseUser::class)->findBy(["id_sondage"=>$sondage->getId()]);
  630.             $reponseUser =sizeof($reponseUser);
  631.             $slider_home[]=[
  632.                 "id"=>$sondage->getId(),
  633.                 "name" => ($long=="fr") ? $sondage->getNameSondage() : $sondage->getArNameSondage() ,
  634.                 "categories" => $sondage->getCategories(),
  635.                 "image"=>$sondage->getImage(),
  636.                 "expirer"=>$sondage->getExpirer(),
  637.                 "dateFin" => $sondage->getDateFin(),
  638.                 "timeFin" => $sondage->getTimeFin(),
  639.                 "paye_sondage" => $sondage->getPayeSondage(),
  640.                 "slug" => ($long=="fr") ? $sondage->getSlugFr() : $sondage->getSlugAr() ,
  641.                 "nbvotes" => $reponseUser+$sondage->getVoteVartuel(),
  642.                 "nbcomments" => "0",
  643.                 "type" => "sondage"
  644.             ];
  645.         }
  646.         // foreach ($enquetes_slider as $key => $enquete) {
  647.         //     $slider_home[]=[
  648.         //         "id"=>$sondage->getId(),
  649.         //         "name" => ($long=="fr") ? $enquete->getNameEnquete() : $enquete->getArNameEnquete() ,
  650.         //         "categories" => $enquete->getCategories(),
  651.         //         "image"=>$enquete->getImage(),
  652.         //         "expirer"=>$enquete->getExpirer(),
  653.         //         "dateFin" => $enquete->getDateFin(),
  654.         //         "timeFin" => $enquete->getTimeFin(),
  655.         //         "paye_sondage" => $sondage->getPayeSondage(),
  656.         //         "slug" => ($long=="fr") ? $enquete->getSlugFr() : $enquete->getSlugAr() ,
  657.         //         "nbvotes" => "0",
  658.         //         "nbcomments" => "0",
  659.         //         "type" => "enquete"
  660.                 
  661.         //     ];
  662.         // }
  663.         $new_slider_home=[];
  664.         foreach ($slider_home as $key => $slider) {
  665.             $slider_s1=$slider_home[0];
  666.             try {
  667.                 if($slider_home[$key+1]){
  668.                     $slider_s1=$slider_home[$key+1];
  669.                 }
  670.             } catch (\Throwable $th) {}
  671.             $slider_s2=$slider_home[1];
  672.             try {
  673.                 if($slider_home[$key+2]){
  674.                     $slider_s2=$slider_home[$key+2];
  675.                 }
  676.             } catch (\Throwable $th) {}
  677.             $new_slider_home[]=[
  678.                 "slider_p" => $slider,
  679.                 "slider_s1" => $slider_s1,
  680.                 "slider_s2" => $slider_s2,
  681.             ];
  682.         }
  683.         //dd( $new_slider_home );
  684.         // $sondages_slider_old=[];
  685.         // foreach ($sondages_slider as $key => $slider) {
  686.         //     $sondages_slider_old[]=$slider;
  687.         // }
  688.         // $sondages_slider= $sondages_slider_old;
  689.         // $new_sondages_slider=[];  
  690.         // $i=0; 
  691.         // $block_sondages_slider=[]; 
  692.         
  693.         // foreach ($sondages_slider as $key => $slider) {
  694.         //     if ($i==0) {
  695.         //         $block_sondages_slider=[]; 
  696.         //     }
  697.         //     $block_sondages_slider [] = $slider;
  698.         //     $i++;
  699.         //     if ($i==3) {
  700.         //        $new_sondages_slider [] = $block_sondages_slider;
  701.         //        $i=0;
  702.         //     }
  703.         // }
  704.         // if ($i==2) {
  705.         //     $block_sondages_slider [] = $sondages_slider[0];
  706.         //     $new_sondages_slider [] = $block_sondages_slider;
  707.         // }
  708.         // if ($i==1) {
  709.         //     $block_sondages_slider [] = $sondages_slider[0];
  710.         //     $block_sondages_slider [] = $sondages_slider[1];
  711.         //     $new_sondages_slider [] = $block_sondages_slider;
  712.         // }
  713.         
  714.         return  $new_slider_home;
  715.        
  716.     }
  717.     
  718.     public function Categories(Request $request,$categorie)
  719.     {
  720.         if($request->getLocale()=="fr"){
  721.             $categorie_id $this->getDoctrine()->getRepository(TypeSondage::class)->findBy(["slug"=>$categorie]);
  722.         }else{
  723.             $categorie_id $this->getDoctrine()->getRepository(TypeSondage::class)->findBy(["ar_slug"=>$categorie]);
  724.         }
  725.         if(!$categorie_id){
  726.               if($request->getLocale()=="ar"){
  727.                 $categorie_id $this->getDoctrine()->getRepository(TypeSondage::class)->findBy(["slug"=>$categorie]);
  728.             }else{
  729.                 $categorie_id $this->getDoctrine()->getRepository(TypeSondage::class)->findBy(["ar_slug"=>$categorie]);
  730.             }           
  731.         }
  732.         if($categorie_id){
  733.             $categorie_id =$categorie_id[0];
  734.             $categorie_id $categorie_id->getId();
  735.             $sondages $this->getDoctrine()->getRepository(Sondage::class)->findBy(['post_status' => 'publish'],['date_deb' => 'DESC']);
  736.             $enquetes $this->getDoctrine()->getRepository(Enquetes::class)->findBy([],['id' => 'DESC']);
  737.             $forums $this->getDoctrine()->getRepository(Forums::class)->findBy([],['id' => 'DESC']);
  738.             if($request->getLocale()=="fr"){
  739.                 foreach ($sondages as $key => $sondage) {
  740.                     if( $sondage->getLongSondage() =="ar" ){
  741.                         unset($sondages[$key]);
  742.                     }else{
  743.                         if(!in_array($categorie_id$sondage->getCategories())){
  744.                             unset($sondages[$key]);
  745.                         }
  746.                     }
  747.                 }
  748.                 foreach ($enquetes as $key => $enquete) {
  749.                     if( $enquete->getLongEnquete() =="ar" ){
  750.                         unset($enquetes[$key]);
  751.                     }else{
  752.                         if(!in_array($categorie_id$enquete->getCategories())){
  753.                             unset($enquetes[$key]);
  754.                         }
  755.                     }
  756.                 }
  757.             }else{
  758.                 foreach ($sondages as $key => $sondage) {
  759.                     if( $sondage->getLongSondage() =="fr" ){
  760.                         unset($sondages[$key]);
  761.                     }else{
  762.                         if(!in_array($categorie_id$sondage->getCategories())){
  763.                             unset($sondages[$key]);
  764.                         }
  765.                     }
  766.                 }
  767.                 foreach ($enquetes as $key => $enquete) {
  768.                     if( $enquete->getLongEnquete() =="fr" ){
  769.                         unset($enquetes[$key]);
  770.                     }else{
  771.                         if(!in_array($categorie_id$enquete->getCategories())){
  772.                             unset($enquetes[$key]);
  773.                         }
  774.                     }
  775.                 }
  776.             }
  777.         }else{
  778.             $sondages = [] ;
  779.             $enquetes = [] ;
  780.             $forums = [] ;
  781.         }
  782.         $all=[];
  783.         foreach ($sondages as $key => &$sondage) {
  784.             $comments $this->getDoctrine()->getRepository(Comment::class)->findBy(['id_sondage'=>$sondage->getId()]);
  785.             $sondage->{'nbcomments'} = sizeof($comments);
  786.             $reponseUser$this->getDoctrine()->getRepository(ReponseUser::class)->findBy(["id_sondage"=>$sondage->getId()]);
  787.             $sondage->{'nbvotes'} = sizeof($reponseUser);
  788.             $sondage->{'namefr'} = $sondage->getNameSondage();
  789.             $sondage->{'namear'} = $sondage->getArNameSondage();
  790.             $sondage->{'slugfr'} = $sondage->getSlugFr();
  791.             $sondage->{'slugar'} = $sondage->getSlugAr();
  792.             $sondage->{'descriptionfr'} = $sondage->getDescriptionFr();
  793.             $sondage->{'descriptionar'} = $sondage->getDescriptionAr();
  794.             $sondage->{'typeall'} = "sondage";
  795.             $all[]= $sondage;
  796.         }
  797.         foreach ($enquetes as $key => &$enquete) {
  798.             //$comments = $this->getDoctrine()->getRepository(Comment::class)->findBy(['id_sondage'=>$sondage->getId()]);
  799.             $enquete->{'nbcomments'} = 0;//sizeof($comments);
  800.             //$reponseUser= $this->getDoctrine()->getRepository(ReponseUser::class)->findBy(["id_sondage"=>$sondage->getId()]);
  801.             $enquete->{'nbvotes'} = 0;//sizeof($reponseUser);
  802.             $enquete->{'namefr'} = $enquete->getNameEnquete();
  803.             $enquete->{'namear'} = $enquete->getArNameEnquete();
  804.             $enquete->{'slugfr'} = $enquete->getSlugFr();
  805.             $enquete->{'slugar'} = $enquete->getSlugAr();
  806.             $enquete->{'descriptionfr'} = $enquete->getDescriptionFr();
  807.             $enquete->{'descriptionar'} = $enquete->getDescriptionAr();
  808.             $enquete->{'typeall'} = "enquete";
  809.             $all[]= $enquete;
  810.         }
  811.         foreach ($forums as $key => &$forum) {
  812.             //$comments = $this->getDoctrine()->getRepository(Comment::class)->findBy(['id_sondage'=>$sondage->getId()]);
  813.             $forum->{'nbcomments'} = 0;//sizeof($comments);
  814.             //$reponseUser= $this->getDoctrine()->getRepository(ReponseUser::class)->findBy(["id_sondage"=>$sondage->getId()]);
  815.             $forum->{'nbvotes'} = 0;//sizeof($reponseUser);
  816.             $forum->{'namefr'} = $forum->getLibelle();
  817.             $forum->{'namear'} = $forum->getLibelle();
  818.             $forum->{'slugfr'} = $forum->getSlug();
  819.             $forum->{'slugar'} = $forum->getSlug();
  820.             $forum->{'descriptionfr'} = $forum->getDescription();
  821.             $forum->{'descriptionar'} = $forum->getDescription();
  822.             $forum->{'expirer'} = 0;
  823.             $forum->{'nbPoint'} = 0;
  824.             $forum->{'typeall'} = "forum";
  825.             $all[]= $forum;
  826.         }
  827.         
  828.         $categories $this->getDoctrine()->getRepository(TypeSondage::class)->findAll();
  829.         $liste_categories=[];
  830.         foreach ($categories as $key => $value) {
  831.             $liste_categories[$value->getId()] = $value;
  832.         } 
  833.         
  834.         $categorie_name $liste_categories[$categorie_id]->getLibelle();
  835.         if($request->getLocale()=="ar")
  836.             $categorie_name $liste_categories[$categorie_id]->getArLibelle();
  837.         //dd($all);
  838.         return $this->render('liste-categories-all.html.twig', [
  839.             'controller_name'       => 'Home',
  840.             'categorie_id'        =>  $categorie_id,
  841.             'categorie'             => $categorie,
  842.             'all'              => $all,
  843.             'liste_categories'      => $liste_categories,
  844.             'categorie_name'       => $categorie_name
  845.           ]);
  846.     }
  847.      /**
  848.      * @Route("/change-locale/{locale}", name="change_locale")
  849.     */
  850.     public function changeLocale(Request $request,$locale)
  851.     {
  852.         $request->getSession()->set("_locale",$locale);
  853.         $url $request->headers->get('referer');
  854.         $url str_replace("/ar","/".$locale,$url);
  855.         $url str_replace("/fr","/".$locale,$url); 
  856.         $ref str_replace("app_dev.php/"""parse_url$url,PHP_URL_PATH ));
  857.         $route $this->container->get('router')->match($ref)['_route'];
  858.         return $this->redirectToRoute($route);
  859.         //return $this->redirect( $url );
  860.     }
  861.     /**
  862.      * @Route("/{_locale}/typed_text", name="typed_text")
  863.     */
  864.     public function typed_text(Request $request)
  865.     {
  866.         return $this->render('typed_text.html.twig');
  867.     }
  868.     /**
  869.      * @Route("/{_locale}/pusher", name="pusher")
  870.     */
  871.     public function pusher(Request $request,Pusher $pusher)
  872.     {
  873.         $pusher->trigger('nb-votes-sondage''update', [
  874.             "id"=>55,
  875.             "nbVotes" => 366
  876.         ]);
  877.         return $this->render('typed_text.html.twig');
  878.     }
  879. }
  880.