src/Controller/ChatController.php line 184

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Ratchet\MessageComponentInterface;
  8. use Ratchet\ConnectionInterface;
  9. use Ratchet\Server\IoServer;
  10. use Ratchet;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\StreamedResponse;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use App\Chat\Chat;
  15. use App\Entity\Message;
  16. use App\Repository\MessageRepository;
  17. use App\Repository\OffersRepository;
  18. use App\Repository\UserRepository;
  19. use App\Repository\AccountFormRepository;
  20. use App\Repository\ProposalRepository;
  21. use App\Form\QuotationType;
  22. class ChatController extends AbstractController
  23. {
  24.     private $sessionInterface;
  25.     public function __constructSessionInterface $session)
  26.     {
  27.         $this->session $session;
  28.     }
  29.     /**
  30.      * @Route("/sendMessageChater/{statut}/{idStatut}/{offerId?}/{proposalId?}", name="sendMessageChater")
  31.      */
  32.     public function sendMessageChater(ProposalRepository $proposalRepositoryAccountFormRepository $accountFormRepositoryRequest $requestMessageRepository $messageRepository,UserRepository $userRepository $idStatut$statutOffersRepository $offersRepository$offerId$proposalId)
  33.     {
  34.         $this->denyAccessUnlessGranted('ROLE_USER');
  35.         $messages $messageRepository->findBy(["{$statut}=>$idStatut]);
  36.         $messageReceiver $userRepository->find($idStatut);
  37.         //$accountForm = $accountFormRepository->find()
  38.         if(isset($offerId)){
  39.             $offer $offersRepository->find($offerId);
  40.             $offerMessage $messageRepository->findBy(["offers" => $offerId"visitor" => $offer->getUser(), "vendor" => $messageReceiver]);
  41.             $proposal $proposalRepository->find($proposalId);
  42.             array_pop($offerMessage);
  43.             return $this->render('chat.html.twig', ['messages' => $offerMessage'messageReceiver' => $messageReceiver'offer' => $offer'proposal' => $proposal]); 
  44.         }
  45.         /*if(isset($messageChat) && $messageChat !== null )
  46.         {
  47.             $response = JsonResponse::fromJsonString('{"message-status" : " message envoyé"}');
  48.             } 
  49.             else
  50.             {
  51.                 $response = JsonResponse::fromJsonString('{"message-status" : " message non envoyé"}');
  52.                 }*/
  53.                 //$response->setStatusCode(Response::HTTP_OK);
  54.                 
  55.                 //$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); 
  56.         return $this->render('chat.html.twig', ['messages' => $messages'messageReceiver' => $messageReceiver/*, 'proposal' => $proposal*/]); 
  57.     }
  58.     /**
  59.      * @Route("/chat/{statut}/{idStatut?}/{offerId?}/{proposalId?}", name="chat")
  60.      */
  61.     public function chat(ProposalRepository $proposalRepositoryAccountFormRepository $accountFormRepositoryRequest $requestMessageRepository $messageRepository,UserRepository $userRepository $idStatut$statutOffersRepository $offersRepository$offerId$proposalId)
  62.     {
  63.         /*var_dump($idStatut);
  64.         var_dump($this->getUser()->getId());*/
  65.         $this->denyAccessUnlessGranted('ROLE_USER');
  66.         if($statut === 'vendor')
  67.         {
  68.             $messages $messageRepository->findBy(["vendor" => $this->getUser()->getId(), "visitor" => $idStatut]);
  69.         }
  70.         else
  71.         {
  72.             $messages $messageRepository->findBy(["visitor" => $this->getUser()->getId(), "vendor" => $idStatut]);
  73.         }
  74.         $messageReceiver $userRepository->find($idStatut);
  75.         //$accountForm = $accountFormRepository->find()
  76.         if(isset($offerId)){
  77.             $offer $offersRepository->find($offerId);
  78.             $offerMessage $messageRepository->findBy(["offers" => $offerId"visitor" => $offer->getUser(), "vendor" => $messageReceiver]);
  79.             $proposal $proposalRepository->find($proposalId);
  80.             array_pop($offerMessage);
  81.             return $this->render('chat_individual.html.twig', ['messages' => $offerMessage'messageReceiver' => $messageReceiver'offer' => $offer'proposal' => $proposal]); 
  82.         }
  83.             
  84.         return $this->render('chat_individual.html.twig', ['messages' => $messages'messageReceiver' => $messageReceiver/*, 'proposal' => $proposal*/]); 
  85.         }
  86.      /**
  87.      * @Route("/allMessageChater/{statut}/{idStatut?}/", name="allMessageChater")
  88.      */
  89.     public function allMessageChater(Request $requestMessageRepository $messageRepository,UserRepository $userRepository $idStatut$statut)
  90.     {
  91.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); 
  92. //        $messages = $messageRepository->findBy(["{$statut}" =>$idStatut]);
  93.         $user $this->getUser();
  94.         $messages $messageRepository->findBy(["{$statut}=> $user->getId()]);
  95.         $chaters =array();
  96.         foreach( $messages as $message)
  97.         {
  98.             
  99.             switch ($statut ){
  100.                 case 'vendor':
  101.                     $participants $message->getVisitor();
  102.                     if(!in_array($participants$chaters))
  103.                     $chaters[] = $participants;
  104.                 break;
  105.                 case 'visitor':
  106.                     $participants $message->getVendor();
  107.                     if(!in_array($participants$chaters))
  108.                     $chaters[] = $participants;
  109.                 break;
  110.             }
  111.             if($idStatut !== null)
  112.             {
  113.              $newParticipant $userRepository->find($idStatut);
  114.              if(!in_array($newParticipant$chaters))
  115.              {
  116.                 $chaters[] = $newParticipant;
  117.              }  
  118.             }
  119.             
  120.             //$userVisitor =  $userRepository->find($idVisitor);
  121.         }
  122.         
  123.         
  124.         /*if(isset($messageChat) && $messageChat !== null )
  125.         {
  126.             $response = JsonResponse::fromJsonString('{"message-status" : " message envoyé"}');
  127.         } 
  128.         else
  129.         {
  130.             $response = JsonResponse::fromJsonString('{"message-status" : " message non envoyé"}');
  131.         }*/
  132.         //$response->setStatusCode(Response::HTTP_OK);
  133.         return $this->render('chatAllMessage.html.twig', ['participants' => $chaters'statut' => $statut'messages' => $messages]); 
  134.     }
  135.     /**
  136.     * @Route("/checkMessageChater", name="checkMessageChater")
  137.     */
  138.     public function checkMessageChater(Request $requestMessageRepository $messageRepository,UserRepository $userRepository)
  139.     {
  140.        // $this->denyAccessUnlessGranted('ROLE_USER');
  141.        if($this->isGranted('ROLE_USER') === false)
  142.        {
  143.             return new JsonResponse(['message' => 'identifiez vous pour accéder à cette page']);
  144.        }
  145.         $user $this->getUser();
  146.         $accountForm $userRepository->find($user->getId());
  147.         $messagesVisitor $messageRepository->findBy(["visitor" => $user->getId(), "isRead" => false]);
  148.         if(!empty($messagesVisitor)){
  149.             $numberMessageVisitor count($messagesVisitor);
  150.             $messagesVisitor[$numberMessageVisitor-1]->getMessageOwner();
  151.         }
  152.         if($accountForm){
  153.             $messagesJobber $messageRepository->findBy(["vendor" => $user->getId(), "isRead" => false]);
  154.         }
  155.         //dd($messagesVisitor);
  156.         if(count($messagesVisitor) > || count($messagesJobber) > 0)
  157.         {
  158.            
  159.             return new JsonResponse(['status' => 'ok''message' => 'message trouvé']);
  160.         }
  161.         else
  162.         {
  163.             return new JsonResponse(['status' => 'ko''message' => 'aucun message trouvé']);
  164.         }
  165.     }
  166.     
  167.     
  168.     /**
  169.      * @Route("/chater/{statut}/{idStatut}/{offerId?}", name="chater")
  170.      */
  171.     public function startChatOffersRepository $offersRepositoryRequest $requestMessageRepository $messageRepositoryUserRepository $userRepository$statut$idStatut$offerId)
  172.     {
  173.         $this->denyAccessUnlessGranted('ROLE_USER');
  174.         //dd($offerId);
  175.         //$offer = $offersRepository->find($offerId);
  176.         // if statement set status vendor / visitor with retrieving the parameter url status (?status=vendor/visitor)
  177.         // get the id of the status's owner with the parameter url idStatus (?idStatus=idOwnerStatus)
  178.         //$status = $request->query->get('status');
  179.         //$idStatus = $request->query->get('idStatus');
  180.         if(isset($statut) && $statut === 'vendor')
  181.         {
  182.             $jobber $this->getUser();
  183.             $receiver $user $userRepository->find($idStatut);
  184.         } 
  185.         else
  186.         {
  187.             $user $this->getUser();
  188.             $receiver $jobber $userRepository->find($idStatut);
  189.         }
  190.         //getter
  191.         $lastMessages $messageRepository->findBy(['vendor' => $jobber->getId(), 'visitor' => $user->getId()]);
  192.         $lastMessage count($lastMessages);
  193.         if ($lastMessage 0)
  194.         {
  195.             $lastMessageId $lastMessages[$lastMessage 1]->getLastMessageId();
  196.         }
  197.        // if($request->request->get('messageId') !== $lastMessageId)
  198.        //{   
  199.             $messageChat $request->request->get('message');
  200.             $messageId $request->request->get('messageId');
  201.             $lastMessageId $request->request->get('lastMessageId');
  202.             $vendorId $request->request->get('vendor');
  203.             //setter
  204.             $message = new Message($jobber$user/*, $offer*/);
  205.             if( !empty($lastMessages) && count($lastMessages) > 0)
  206.             {
  207.                 $lastMessage count($lastMessages);
  208.                 $lastMessageId $lastMessages[$lastMessage 1]->getLastMessageId();
  209.                 $lastMessageId++;
  210.             } 
  211.             else
  212.             {
  213.                 $lastMessageId 0;
  214.             }
  215.             if($messageChat !== null)
  216.             {
  217.                 //$message->setOffers($offer);
  218.                 $message->setIsRead(false);
  219.                 $message->setLastMessageId($lastMessageId);
  220.                 $message->setContent($messageChat);
  221.                 $message->setMessageOwner($this->getUser());
  222.                 $message->setMessageReceiver($receiver);
  223.                 $em $this->getDoctrine()->getManager();
  224.                 $em->persist($message);
  225.                 $em->flush();
  226.             }
  227.             
  228.             //}
  229.             //foreach($messages as $message )
  230.             //{
  231.         ob_start();
  232.        // $testy = "id: {$visitor->getFirstname()}";
  233.        // $testy = "id: ".ceil(microtime(true));
  234.        if(count($lastMessages) > )
  235.        {
  236.             $content "id: ".$lastMessages[$lastMessage 1]->getLastMessageId();        
  237.             $content .= "\n";
  238.             $content .= "event: chatter";
  239.             $content .= "\n";
  240.             $content .= "data: {\"content\" : \"{$lastMessages[$lastMessage -1]->getContent()} \", \"owner\": {$lastMessages[$lastMessage 1]->getMessageOwner()->getId()} } ";
  241.             $content .= "\n\n";
  242.             echo $content;
  243.        }
  244.         //}
  245.       //  }
  246.         $response = new StreamedResponse(function(){
  247.         ob_end_flush();
  248.         //if(isset($messageChat) && $messageChat !== null)
  249.         //{
  250.             //$this->session->remove('message');
  251.         //}
  252.         });
  253.         //$response->setStatusCode(Response::HTTP_OK);
  254.         $response->headers->set('Content-Type''text/event-stream');   
  255.         $response->headers->set('Connection''keep-alive');
  256.         $response->headers->set('Cache-Control''no-cache');   
  257.         return $response
  258.     }
  259.     /**
  260.      * @Route("chatDatetime/{statut}/{idStatut}/{offerId?}", name="chatDatetime")
  261.      */
  262.     public function chatDatetimeOffersRepository $offersRepositoryRequest $requestMessageRepository $messageRepositoryUserRepository $userRepository$statut$idStatut$offerId) {
  263.         $this->denyAccessUnlessGranted('ROLE_USER');
  264.         $user $this->getUser();
  265.         //dd($user->getId());
  266.         if(isset($statut) && $statut === 'vendor')
  267.         {
  268.             $messages $messageRepository->findBy(['vendor' => $user->getId(), 'visitor' => $idStatut]);
  269.         } 
  270.         else
  271.         {
  272.             $user $this->getUser();
  273.             $messages $messageRepository->findBy(['vendor' => $idStatut'visitor' => $user->getId()]);
  274.         }
  275.         if (!empty($messages) )
  276.         {
  277.             $lastMessages $messages[count($messages)-1];
  278.             return new JsonResponse(['datetime' => $lastMessages->getDate()]);   
  279.         }
  280.         else
  281.         {
  282.             return new JsonResponse(['message' => 'aucun message trouvé']);
  283.         }
  284.     }
  285. }