<?php
namespace App\Controller;
use App\Repository\CompanyRepository;
use App\Repository\MessagingRepository;
use App\Repository\TicketRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Services\Filter;
use App\Entity\Ticket;
use GuzzleHttp\Client;
use DateTime;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Serializer\SerializerInterface;
class ConversationController extends AbstractController
{
private $em;
private $message;
private $ticket;
private $userRepo;
private $companyrepo;
private $filtre;
private $serializer;
public function __construct(
EntityManagerInterface $em,
MessagingRepository $message,
TicketRepository $ticket,
UserRepository $userRepo,
CompanyRepository $companyrepo,
Filter $filtre,
SerializerInterface $serializer)
{
$this->em = $em;
$this->message = $message;
$this->ticket = $ticket;
$this->userRepo = $userRepo;
$this->companyrepo = $companyrepo;
$this->filtre = $filtre;
$this->serializer = $serializer;
}
/**
* @Route("/notification", name="messaging_notify")
*/
public function notification(){
$usercon = $this->userRepo->find($this->getUser());
if ($usercon) {
$Newtickets = $this->ticket->getTicketsByProductsIsNewForClient($usercon);
// dd($Newtickets);
if (empty($Newtickets)) {
$data[] = false;
return $this->json($data, 200, []);
}
}
else{
$Newtickets = $this->ticket->getTicketsByProductsIsNew($this->getUser());
if (empty($Newtickets)) {
$data[] = false;
return $this->json($data, 200, []);
}
}
$data[] = true;
$data[] = $Newtickets;
return $this->json($data, 200, [],['groups' => 'post:read']);
}
/**
* @Route("/{param}/compteur-message", name="compter_Message")
*/
public function compt()
{
$usercon= $this->userRepo->find($this->getUser());
$messagings=[];
if($usercon){
$messagings=$this->message->findMessage(["user_id"=>$usercon->getId()]);
if ($messagings == null) {
$messagings=$this->message->getMessageNewparAmis($usercon->getId());
}
}else{
$messagings=$this->message->findMessage(["company_id"=>$this->getUser()->getId()]);
if ($messagings == null) {
$messagings=$this->message->getMessageNewparAmis($this->getUser()->getId());
}
}
$data['nbMessage'] = count($messagings);
return $this->json($data, 200, []);
}
/**
* @Route("{param}/ticket/message-lu/{numero}/{user}", name="message_lu")
*/
public function luMessage(Ticket $ticket, $user, MessagingRepository $messagingRepository, PublisherInterface $publisher)
{
$messagenonlu = null;
$messaging = null;
$paramuser = [];
$messagings = $messagingRepository->findBy(['ticket' => $ticket], ['id' => 'ASC']);
if ($user == 'client') {
if ($ticket->getProduct()) {
foreach ($messagings as $messaging) {
if ( $messaging->getIsNew() == true) {
$messaging->setIsNew(false);
}
}
}else{
foreach ($messagings as $messaging) {
if ($messaging->getIsNew() == true) {
$messaging->setIsNew(false);
}
}
}
$paramuser = ["user_id"=>$this->getUser()->getId()];
}else {
if ($ticket->getProduct()) {
foreach ($messagings as $messaging) {
if ($messaging->getMessageUser() && $messaging->getIsNew() == true) {
$messaging->setIsNew(false);
}
}
}else{
foreach ($messagings as $messaging) {
if ($messaging->getIsNew() == true) {
$messaging->setIsNew(false);
}
}
}
$paramuser = ["company_id"=>$this->getUser()->getId()];
}
$this->em->flush();
if ($ticket->getProduct()) {
$messagenonlu= $this->message->findMessage($paramuser);
}else{
$messagenonlu= $this->message->getMessageNewparAmis($this->getUser()->getId());
}
$encoded_msg = $this->serializer->serialize([
'newMessage2'=>count($messagenonlu),
'iduser'=>$this->getUser()->getId()
],'json', ['groups' => 'post:read']);
$update = new Update(
[$_ENV['MERCURE_SUBSCRIBER_URL'].$ticket->getNumero()],
$encoded_msg
);
$publisher($update);
return $this->json(true,200 ,[]);
}
/**
* @Route("{param}/liste/message/{user}", name="listes_message")
*/
public function listeMessage( $user, PublisherInterface $publisher)
{
$numtikets=$this->ticket->getTicketall($this->getUser()->getId());
$listesm = [];
foreach ($numtikets as $numtiket) {
$listes =$this->message->expeditaireMessage($numtiket->getNumero());
$sms=[];
foreach ($listes as $key => $value) {
$sms[0] = $value;
}
if (isset($sms[0])) {
$destinataire = $this->filtre->voirDestinataire($sms[0]->getTicket(), $this->getUser());
if ($destinataire->getStatuCon() && $destinataire->getEtatNetwork()) {
$connecte =$this->filtre->diferenceetredate($destinataire->getEtatNetwork());
$status= $destinataire->getStatuCon() && $connecte ? true : false;
}
else{
$status= false;
}
$taille=sizeof($destinataire->getRoles());
$listesm[]=[
'message'=>$sms[0],
'utilisataire'=>$destinataire,
'role'=> $taille == 2 ? "prestataire":"client",
'status'=> $status
];
}
}
if ($user == 'client') {
$messageNew = $this->render('client_space/inc/new_message.html.twig', [
'listesMessages' => $listesm
]);
}else {
$messageNew = $this->render('company_space/inc/new_message.html.twig', [
'listesMessages' => $listesm
]);
}
$data['userNewMessage']= $messageNew;
return $this->json($data);
}
/**
* @Route("{param}/lists-personnes-connecte", name="listePersonnesConnecte")
*/
public function pconnecte($param)
{
$datejour = new DateTime('now');
$userconnects = $this->userRepo->findUserConect();
$companyconnects = $this->companyrepo->findCompanyConect($datejour);
// dd($userconnects,$companyconnects);
$minute = 5;
$clientconectes = [];
foreach ($userconnects as $userconnect) {
$datecone =$userconnect->getEtatNetwork();
$diiffdate = $datecone->diff($datejour);
if ($diiffdate->i < $minute ) {
$clientconectes []= $userconnect;
}
}
$companyconectes = [];
foreach ($companyconnects as $companyconnect) {
$dateconee =$companyconnect->getEtatNetwork();
$diffdatee = $dateconee->diff($datejour);
if ($diffdatee->i < $minute ) {
$companyconectes []= $companyconnect;
}
}
// dd($clientconectes, $companyconectes);
if ($param == 'mon-espace-client') {
$clientconVew = $this->render('client_space/ticket/contact_cli.html.twig', [
'userconnect' => $clientconectes
]);
$companyconVew = $this->render('client_space/ticket/contact_prest.html.twig', [
'companyconnect' => $companyconectes
]);
}
else{
$clientconVew = $this->render('company_space/ticket/contact_cli.html.twig', [
'userconnect' => $clientconectes
]);
$companyconVew = $this->render('company_space/ticket/contact_prest.html.twig', [
'companyconnect' => $companyconectes
]);
}
$data['userconnect']= $clientconVew;
$data['companyconnect']= $companyconVew;
// dd($data);
return $this->json($data);
}
/**
* @Route("{param}/cree_conversation/{id}/{type_user}", name="user_en_conversation")
*/
public function conversationavec($id, $type_user, $param)
{
$conversation = $this->ticket->ticketexist($id,$this->getUser()->getId());
$conversationinverce = $this->ticket->ticketexistinverce($id,$this->getUser()->getId());
$conv_avec_preo = $this->ticket->ticketexistavecprduit($id,$this->getUser());
$conv_prest= $this->ticket->ticketprestavecclient($id,$this->getUser()->getId());
// dd($conversation,$conv_avec_preo,$conv_prest,$conversationinverce );
if ($conversation) {
$data['numero'] = $conversation[0]->getNumero();
}
else if ($conversationinverce) {
$data['numero'] = $conversationinverce[0]->getNumero();
}
else if ($conv_avec_preo && $param == "mon-espace-client") {
$data['numero'] = $conv_avec_preo[0]->getNumero();
}
else if ($conv_prest && $param == "mon-espace-prestataire") {
$data['numero'] = $conv_prest[0]->getNumero();
}
else{
$ticket = new Ticket();
if ($param == "mon-espace-client") {
$ticket->setCreatedBy($this->getUser());
$ticket->setExpeditaireId($this->getUser()->getId());
$ticket->setTypeExpeditaire('client');
$vewhtm = 'front_client_space_ticket_single';
}else{
$clientdestinat = $this->userRepo->findOneById($id);
if ($clientdestinat) {
$ticket->setCreatedBy($clientdestinat);
}
$ticket->setExpeditaireId($this->getUser()->getId());
$ticket->setTypeExpeditaire('prestataire');
$vewhtm = 'front_company_space_ticket_single';
}
$ticket->setNumero(strtoupper(uniqid()));
$ticket->setDestinataireId($id);
$ticket->setTypeDestinataire($type_user);
$ticket->setSubject('contact' .$type_user);
$this->em->persist($ticket);
$this->em->flush();
$data['numero'] = $ticket->getNumero();
}
// dd($data);
return $this->json($data,200 ,[]);
}
/**
* @Route("{param}/lists-utilisateur", name="listeUtilisateur")
*/
public function utilisataire($param)
{
$userconnects = $this->userRepo->findAll();
$companyconnects = $this->companyrepo->findAll();
$clientconectes = [];
foreach ($userconnects as $userconnect) {
if ($userconnect->getEtatNetwork() != null && $userconnect->getStatuCon() != null ) {
$connecteNetwork = $this->filtre->diferenceetredate($userconnect->getEtatNetwork());
$authetcon=$userconnect->getStatuCon();
$clientconect= $connecteNetwork && $authetcon ? true :false;
}
else{
$clientconect = false;
}
$clientconectes []= [
'client'=>$userconnect,
'clientconect'=>$clientconect
];
}
$companyconectes = [];
foreach ($companyconnects as $companyconnect) {
if ($companyconnect->getEtatNetwork() != null && $companyconnect->getStatuCon() != null ) {
$connecteNetwork = $this->filtre->diferenceetredate($companyconnect->getEtatNetwork());
$authetcon = $companyconnect->getStatuCon();
$prestconect = $connecteNetwork && $authetcon ? true : false;
}
else{
$prestconect = false;
}
$companyconectes []= [
'prest'=>$companyconnect,
'prestconect'=>$prestconect
];
}
// dd($clientconectes,$companyconectes);
if ($param == 'mon-espace-client') {
$clientconVew = $this->render('client_space/ticket/client_liste.html.twig', [
'userconnect' => $clientconectes
]);
$companyconVew = $this->render('client_space/ticket/prest_liste.html.twig', [
'companyconnect' => $companyconectes
]);
}
else{
$clientconVew = $this->render('company_space/ticket/client_liste.html.twig', [
'userconnects' => $clientconectes
]);
$companyconVew = $this->render('company_space/ticket/prest_liste.html.twig', [
'companyconnects' => $companyconectes
]);
}
$data['userconnect']= $clientconVew;
$data['companyconnect']= $companyconVew;
return $this->json($data);
}
/**
* @Route("notifficationUpdate", name="notiff_Update")
*/
public function update_notiff()
{
$nbmesages = $this->message->findBy(['id' => $this->getUser()->getId()], ['id' => 'ASC']);
foreach ($nbmesages as $nbmesage) {
if ($nbmesage->getIsLu() == true) {
$nbmesage->setIsLu(false);
}
}
$this->em->flush();
return $this->json(true,200 ,[]);
}
// /**
// * @Route("teste_req/{numero}", name="test_requete")
// */
// public function testes(Ticket $ticket)
// {
// $resultats = $this->ticket->getDestinataire($ticket)[0];
// if ($resultats['DestinataireId'] == $this->getUser()->getId()) {
// dd('mondest',$resultats['expeditaireId']);
// }
// else{
// dd('mondest',$resultats['DestinataireId']);
// }
// }
// /**
// * @Route("teste_listemesage", name="test_listemesage")
// */
// public function listemesage()
// {
// // $numtikets=$this->ticket->getTicketall($this->getUser()->getId());
// // $listesm = [];
// // foreach ($numtikets as $numtiket) {
// // $listes =$this->message->expeditaireMessage($numtiket->getNumero());
// // $sms=[];
// // foreach ($listes as $key => $value) {
// // $sms[0] = $value;
// // }
// // if (isset($sms[0])) {
// // $destinataire = $this->filtre->voirDestinataire($sms[0]->getTicket(), $this->getUser());
// // if ($destinataire->getStatuCon() && $destinataire->getEtatNetwork()) {
// // $connecte =$this->filtre->diferenceetredate($destinataire->getEtatNetwork());
// // $status= $destinataire->getStatuCon() && $connecte ? true : false;
// // }
// // $taille=sizeof($destinataire->getRoles());
// // $listesm[]=[
// // 'message'=>$sms[0],
// // 'utilisataire'=>$destinataire,
// // 'role'=> $taille == 2 ? "prestataire":"client",
// // 'status'=> $status
// // ];
// // }
// // // $listesm[]= [isset($sms[0])?$sms[0]:[];
// // }
// ///////////////////////////////////
// // $nbsms=$this->message->getNbMessageNewparAmis($this->getUser()->getId());
// /////////////////////////////
// $usercon= $this->userRepo->find($this->getUser());
// $messagings=[];
// if($usercon){
// $messagings=$this->message->findMessage(["user_id"=>$usercon->getId()]);
// if ($messagings == null) {
// $messagings=$this->message->getMessageNewparAmis($usercon->getId());
// }
// }else{
// $messagings=$this->message->findMessage(["company_id"=>$this->getUser()->getId()]);
// if ($messagings == null) {
// $messagings=$this->message->getMessageNewparAmis($this->getUser()->getId());
// }
// }
// dd($messagings);
// }
}