src/Controller/ConversationController.php line 315

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\CompanyRepository;
  4. use App\Repository\MessagingRepository;
  5. use App\Repository\TicketRepository;
  6. use App\Repository\UserRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Services\Filter;
  12. use App\Entity\Ticket;
  13. use GuzzleHttp\Client;
  14. use DateTime;
  15. use Symfony\Component\Mercure\Update;
  16. use Symfony\Component\Mercure\PublisherInterface;
  17. use Symfony\Component\Serializer\SerializerInterface;
  18. class ConversationController extends AbstractController
  19. {
  20.     private $em;
  21.     private $message;
  22.     private $ticket;
  23.     private $userRepo;
  24.     private $companyrepo;
  25.     private $filtre;
  26.     private $serializer;
  27.     public function __construct(
  28.         EntityManagerInterface $em,
  29.         MessagingRepository $message,
  30.         TicketRepository $ticket,
  31.         UserRepository $userRepo,
  32.         CompanyRepository $companyrepo,
  33.         Filter $filtre,
  34.         SerializerInterface $serializer)
  35.     {
  36.         $this->em $em;
  37.         $this->message $message;
  38.         $this->ticket $ticket;
  39.         $this->userRepo $userRepo;
  40.         $this->companyrepo $companyrepo;
  41.         $this->filtre $filtre;
  42.         $this->serializer $serializer;
  43.     }
  44.     
  45.     /**
  46.      * @Route("/notification", name="messaging_notify")
  47.      */
  48.     public function notification(){
  49.         $usercon $this->userRepo->find($this->getUser());
  50.         if ($usercon) {
  51.             $Newtickets $this->ticket->getTicketsByProductsIsNewForClient($usercon);
  52.             // dd($Newtickets);
  53.             if (empty($Newtickets)) {
  54.                 $data[] = false;
  55.                 return $this->json($data200, []);
  56.             }
  57.         }
  58.         else{
  59.             $Newtickets $this->ticket->getTicketsByProductsIsNew($this->getUser());
  60.             if (empty($Newtickets)) {
  61.                 $data[] = false;
  62.                 return $this->json($data200, []);
  63.             }
  64.         }
  65.         $data[] = true;
  66.         $data[] = $Newtickets;
  67.         return $this->json($data200, [],['groups' => 'post:read']);
  68.     }
  69.      /**
  70.      * @Route("/{param}/compteur-message", name="compter_Message")
  71.      */
  72.     public function compt()
  73.     {
  74.         $usercon=  $this->userRepo->find($this->getUser());
  75.         $messagings=[];
  76.         if($usercon){
  77.             $messagings=$this->message->findMessage(["user_id"=>$usercon->getId()]);
  78.             if ($messagings == null) {
  79.                 $messagings=$this->message->getMessageNewparAmis($usercon->getId());
  80.             }
  81.         }else{
  82.             $messagings=$this->message->findMessage(["company_id"=>$this->getUser()->getId()]);
  83.             if ($messagings == null) {
  84.                 $messagings=$this->message->getMessageNewparAmis($this->getUser()->getId());
  85.             }
  86.         }
  87.         $data['nbMessage'] = count($messagings);
  88.         return $this->json($data200, []);
  89.     }
  90.     /**
  91.      * @Route("{param}/ticket/message-lu/{numero}/{user}", name="message_lu")
  92.      */
  93.     public function luMessage(Ticket $ticket$userMessagingRepository $messagingRepositoryPublisherInterface $publisher)
  94.     {
  95.         $messagenonlu null;
  96.         $messaging null;
  97.         $paramuser = [];
  98.         $messagings $messagingRepository->findBy(['ticket' => $ticket], ['id' => 'ASC']);
  99.         
  100.         if ($user == 'client') {
  101.             if ($ticket->getProduct()) { 
  102.                 foreach ($messagings as $messaging) {
  103.                     if ( $messaging->getIsNew() == true) {
  104.                         $messaging->setIsNew(false);
  105.                     }      
  106.                 }
  107.             }else{
  108.                 foreach ($messagings as $messaging) {
  109.                     if ($messaging->getIsNew() == true) {
  110.                         $messaging->setIsNew(false);
  111.                     }      
  112.                 }
  113.             }
  114.             $paramuser = ["user_id"=>$this->getUser()->getId()];
  115.         }else {
  116.             if ($ticket->getProduct()) {
  117.                 
  118.                 foreach ($messagings as $messaging) {
  119.                     
  120.                     if ($messaging->getMessageUser() && $messaging->getIsNew() == true) {
  121.                         $messaging->setIsNew(false);
  122.                     }      
  123.                 }
  124.             }else{
  125.                 foreach ($messagings as $messaging) {
  126.                     if ($messaging->getIsNew() == true) {
  127.                         $messaging->setIsNew(false);
  128.                     }      
  129.                 }
  130.             }
  131.             $paramuser = ["company_id"=>$this->getUser()->getId()];
  132.         }
  133.         
  134.         $this->em->flush();
  135.         if ($ticket->getProduct()) {
  136.             $messagenonlu$this->message->findMessage($paramuser);
  137.         }else{
  138.             $messagenonlu$this->message->getMessageNewparAmis($this->getUser()->getId());
  139.         }
  140.         $encoded_msg $this->serializer->serialize([
  141.             'newMessage2'=>count($messagenonlu),
  142.              'iduser'=>$this->getUser()->getId()
  143.         ],'json', ['groups' => 'post:read']);
  144.         
  145.         $update = new Update(
  146.             [$_ENV['MERCURE_SUBSCRIBER_URL'].$ticket->getNumero()],
  147.             $encoded_msg
  148.         );
  149.        $publisher($update);
  150.         return $this->json(true,200 ,[]);
  151.     }
  152.     /**
  153.      * @Route("{param}/liste/message/{user}", name="listes_message")
  154.      */
  155.     public function listeMessage$userPublisherInterface $publisher)
  156.     {
  157.       
  158.         $numtikets=$this->ticket->getTicketall($this->getUser()->getId());
  159.         $listesm = [];
  160.         foreach ($numtikets as $numtiket) {
  161.             $listes =$this->message->expeditaireMessage($numtiket->getNumero());
  162.             $sms=[];
  163.             foreach ($listes as $key => $value) {
  164.                 $sms[0] = $value;
  165.             }
  166.              if (isset($sms[0])) {
  167.                 $destinataire $this->filtre->voirDestinataire($sms[0]->getTicket(), $this->getUser());
  168.                 if ($destinataire->getStatuCon() && $destinataire->getEtatNetwork()) {
  169.                     $connecte =$this->filtre->diferenceetredate($destinataire->getEtatNetwork());
  170.                     $status$destinataire->getStatuCon() && $connecte true false;
  171.                 }
  172.                 else{
  173.                     $statusfalse;
  174.                 }
  175.                 $taille=sizeof($destinataire->getRoles());
  176.                 $listesm[]=[
  177.                     'message'=>$sms[0],
  178.                     'utilisataire'=>$destinataire,
  179.                     'role'=> $taille == "prestataire":"client",
  180.                     'status'=> $status
  181.                 ];
  182.              }
  183.         
  184.         }
  185.         if ($user == 'client') {
  186.                 $messageNew $this->render('client_space/inc/new_message.html.twig', [
  187.                                 'listesMessages' => $listesm
  188.                              ]);
  189.             }else {
  190.                 $messageNew $this->render('company_space/inc/new_message.html.twig', [
  191.                                  'listesMessages' => $listesm
  192.                              ]);
  193.             }
  194.        
  195.         $data['userNewMessage']= $messageNew;
  196.         return $this->json($data);
  197.     }
  198.     /**
  199.      * @Route("{param}/lists-personnes-connecte", name="listePersonnesConnecte")
  200.      */
  201.     public function pconnecte($param)
  202.     {
  203.         $datejour = new DateTime('now');
  204.         $userconnects $this->userRepo->findUserConect();
  205.         $companyconnects $this->companyrepo->findCompanyConect($datejour);
  206.     //    dd($userconnects,$companyconnects);
  207.         $minute 5;
  208.         $clientconectes = [];
  209.         foreach ($userconnects as $userconnect) {
  210.            $datecone =$userconnect->getEtatNetwork();
  211.            $diiffdate $datecone->diff($datejour);
  212.            if ($diiffdate->$minute ) {
  213.                $clientconectes []= $userconnect;
  214.            }
  215.         }
  216.         $companyconectes = [];
  217.         foreach ($companyconnects as $companyconnect) {
  218.            $dateconee =$companyconnect->getEtatNetwork();
  219.            $diffdatee $dateconee->diff($datejour);
  220.            if ($diffdatee->$minute ) {
  221.                $companyconectes []= $companyconnect;
  222.            }
  223.         }
  224.         //  dd($clientconectes, $companyconectes);
  225.         if ($param == 'mon-espace-client') {
  226.                 $clientconVew =  $this->render('client_space/ticket/contact_cli.html.twig', [
  227.                 'userconnect' => $clientconectes
  228.                 ]);
  229.                 $companyconVew $this->render('client_space/ticket/contact_prest.html.twig', [
  230.                 'companyconnect' => $companyconectes
  231.                 ]);
  232.         }
  233.         else{
  234.                 
  235.                 $clientconVew =  $this->render('company_space/ticket/contact_cli.html.twig', [
  236.                 'userconnect' => $clientconectes
  237.                 ]);
  238.                 $companyconVew $this->render('company_space/ticket/contact_prest.html.twig', [
  239.                 'companyconnect' => $companyconectes
  240.                 ]);
  241.         }
  242.         $data['userconnect']= $clientconVew;
  243.         $data['companyconnect']= $companyconVew;
  244.         // dd($data);
  245.         return $this->json($data);
  246.     }
  247.     /**
  248.      * @Route("{param}/cree_conversation/{id}/{type_user}", name="user_en_conversation")
  249.      */
  250.     public function conversationavec($id$type_user$param)
  251.     {
  252.        $conversation $this->ticket->ticketexist($id,$this->getUser()->getId());
  253.        $conversationinverce $this->ticket->ticketexistinverce($id,$this->getUser()->getId());
  254.        $conv_avec_preo $this->ticket->ticketexistavecprduit($id,$this->getUser());
  255.        $conv_prest$this->ticket->ticketprestavecclient($id,$this->getUser()->getId());
  256.     //    dd($conversation,$conv_avec_preo,$conv_prest,$conversationinverce );
  257.        if ($conversation) {
  258.         $data['numero'] = $conversation[0]->getNumero();
  259.        }
  260.        else if ($conversationinverce) {
  261.         $data['numero'] = $conversationinverce[0]->getNumero();
  262.        }
  263.        else if ($conv_avec_preo && $param == "mon-espace-client") {
  264.         $data['numero'] = $conv_avec_preo[0]->getNumero();
  265.        } 
  266.        else if ($conv_prest && $param == "mon-espace-prestataire") {
  267.         $data['numero'] = $conv_prest[0]->getNumero();
  268.        }
  269.        else{
  270.        $ticket = new Ticket();
  271.         if ($param == "mon-espace-client") {
  272.             $ticket->setCreatedBy($this->getUser());
  273.             $ticket->setExpeditaireId($this->getUser()->getId());
  274.             $ticket->setTypeExpeditaire('client');
  275.             $vewhtm 'front_client_space_ticket_single';
  276.         }else{
  277.             $clientdestinat $this->userRepo->findOneById($id);
  278.             if ($clientdestinat) {
  279.                 $ticket->setCreatedBy($clientdestinat);
  280.             }
  281.             $ticket->setExpeditaireId($this->getUser()->getId());
  282.             $ticket->setTypeExpeditaire('prestataire');
  283.             $vewhtm 'front_company_space_ticket_single';
  284.         }
  285.         $ticket->setNumero(strtoupper(uniqid()));
  286.         $ticket->setDestinataireId($id);
  287.         $ticket->setTypeDestinataire($type_user);
  288.         $ticket->setSubject('contact' .$type_user);
  289.         $this->em->persist($ticket);
  290.         $this->em->flush();
  291.         $data['numero'] = $ticket->getNumero();
  292.        }
  293.     //    dd($data);
  294.         return $this->json($data,200 ,[]);    
  295.     }
  296.     /**
  297.      * @Route("{param}/lists-utilisateur", name="listeUtilisateur")
  298.      */
  299.     public function utilisataire($param)
  300.     {
  301.         $userconnects $this->userRepo->findAll();
  302.         $companyconnects $this->companyrepo->findAll();
  303.         $clientconectes = [];
  304.         foreach ($userconnects as $userconnect) {
  305.             if ($userconnect->getEtatNetwork() != null && $userconnect->getStatuCon() != null ) {
  306.                 $connecteNetwork $this->filtre->diferenceetredate($userconnect->getEtatNetwork());
  307.                 $authetcon=$userconnect->getStatuCon();
  308.                 $clientconect$connecteNetwork && $authetcon true :false;
  309.             }
  310.             else{
  311.                 $clientconect false;
  312.             }
  313.             $clientconectes []= [
  314.                 'client'=>$userconnect,
  315.                 'clientconect'=>$clientconect
  316.             ];
  317.         }
  318.         $companyconectes = [];
  319.         foreach ($companyconnects as $companyconnect) {
  320.             if ($companyconnect->getEtatNetwork() != null && $companyconnect->getStatuCon() != null ) {
  321.                 $connecteNetwork $this->filtre->diferenceetredate($companyconnect->getEtatNetwork());
  322.                 $authetcon $companyconnect->getStatuCon();
  323.                 $prestconect $connecteNetwork && $authetcon true false;
  324.             }
  325.             else{
  326.                 $prestconect false;
  327.             }
  328.             $companyconectes []= [
  329.                 'prest'=>$companyconnect,
  330.                 'prestconect'=>$prestconect
  331.             ];
  332.         }
  333.         // dd($clientconectes,$companyconectes);
  334.         if ($param == 'mon-espace-client') {
  335.                 $clientconVew =  $this->render('client_space/ticket/client_liste.html.twig', [
  336.                 'userconnect' => $clientconectes
  337.                 ]);
  338.                 $companyconVew $this->render('client_space/ticket/prest_liste.html.twig', [
  339.                 'companyconnect' => $companyconectes
  340.                 ]);
  341.         }
  342.         else{
  343.                 
  344.                 $clientconVew =  $this->render('company_space/ticket/client_liste.html.twig', [
  345.                 'userconnects' => $clientconectes
  346.                 ]);
  347.                 $companyconVew $this->render('company_space/ticket/prest_liste.html.twig', [
  348.                 'companyconnects' => $companyconectes
  349.                 ]);
  350.         }
  351.         $data['userconnect']= $clientconVew;
  352.         $data['companyconnect']= $companyconVew;
  353.         return $this->json($data);
  354.     }
  355.     /**
  356.      * @Route("notifficationUpdate", name="notiff_Update")
  357.     */
  358.     public function update_notiff()
  359.     {
  360.         $nbmesages $this->message->findBy(['id' => $this->getUser()->getId()], ['id' => 'ASC']);
  361.         
  362.         foreach ($nbmesages as $nbmesage) {
  363.             if ($nbmesage->getIsLu() == true) {
  364.                 $nbmesage->setIsLu(false);
  365.             }      
  366.         }
  367.         $this->em->flush();
  368.         return $this->json(true,200 ,[]);
  369.     }
  370.     //  /**
  371.     //  * @Route("teste_req/{numero}", name="test_requete")
  372.     //  */
  373.     // public function testes(Ticket $ticket)
  374.     // {
  375.     //   $resultats = $this->ticket->getDestinataire($ticket)[0];
  376.     //   if ($resultats['DestinataireId'] == $this->getUser()->getId()) {
  377.         
  378.     //       dd('mondest',$resultats['expeditaireId']);
  379.     //   }
  380.     //   else{
  381.     //     dd('mondest',$resultats['DestinataireId']);
  382.     //   }
  383.     // }
  384.     //  /**
  385.     //  * @Route("teste_listemesage", name="test_listemesage")
  386.     //  */
  387.     // public function listemesage()
  388.     // {
  389.     //         // $numtikets=$this->ticket->getTicketall($this->getUser()->getId());
  390.     //         // $listesm = [];
  391.     //         // foreach ($numtikets as $numtiket) {
  392.     //         //     $listes =$this->message->expeditaireMessage($numtiket->getNumero());
  393.     //         //     $sms=[];
  394.     //         //     foreach ($listes as $key => $value) {
  395.     //         //         $sms[0] = $value;
  396.     //         //     }
  397.     //         //      if (isset($sms[0])) {
  398.     //         //         $destinataire = $this->filtre->voirDestinataire($sms[0]->getTicket(), $this->getUser());
  399.     //         //         if ($destinataire->getStatuCon() && $destinataire->getEtatNetwork()) {
  400.     //         //             $connecte =$this->filtre->diferenceetredate($destinataire->getEtatNetwork());
  401.     //         //             $status= $destinataire->getStatuCon() && $connecte ? true : false;
  402.     //         //         }
  403.     //         //         $taille=sizeof($destinataire->getRoles());
  404.     //         //         $listesm[]=[
  405.     //         //             'message'=>$sms[0],
  406.     //         //             'utilisataire'=>$destinataire,
  407.     //         //             'role'=> $taille == 2 ? "prestataire":"client",
  408.     //         //             'status'=> $status
  409.     //         //         ];
  410.     //         //      }
  411.     //         //     // $listesm[]= [isset($sms[0])?$sms[0]:[];
  412.     //         // }
  413.     //         ///////////////////////////////////
  414.     //         // $nbsms=$this->message->getNbMessageNewparAmis($this->getUser()->getId());
  415.     //         /////////////////////////////
  416.     //         $usercon=  $this->userRepo->find($this->getUser());
  417.     //         $messagings=[];
  418.     //         if($usercon){
  419.     //             $messagings=$this->message->findMessage(["user_id"=>$usercon->getId()]);
  420.     //             if ($messagings == null) {
  421.     //                 $messagings=$this->message->getMessageNewparAmis($usercon->getId());
  422.     //             }
  423.     //         }else{
  424.     //             $messagings=$this->message->findMessage(["company_id"=>$this->getUser()->getId()]);
  425.     //             if ($messagings == null) {
  426.     //                 $messagings=$this->message->getMessageNewparAmis($this->getUser()->getId());
  427.     //             }
  428.     //         }
  429.     //     dd($messagings); 
  430.     // }
  431. }