<?php
namespace App\Controller;
use App\Entity\MessagePatient;
use App\Entity\Notification;
use App\Entity\PaperPatient;
use App\Entity\Patient;
use App\Entity\User;
use App\Form\MessagePatientType;
use App\Form\PaperPatientType;
use App\Repository\MessagePatientRepository;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
class MessageController extends AbstractController
{
/**
* @Route("/message/professional/{id}", name="index_message_professional", methods={"GET","POST"})
* @IsGranted("ROLE_PROFESSIONAL")
*/
public function index(?UserInterface $user, EntityManagerInterface $entityManager, Patient $patient, Request $request)
{
if (!$user) {
return $this->redirectToRoute('app_login');
}
$haveOffice = true;
if (!$user->getOffice()) {
$haveOffice = false;
}
$messagePatient = new MessagePatient();
$form = $this->createForm(MessagePatientType::class, $messagePatient);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($user->getOffice() === $patient->getUser()->getOffice()) { //vérifie que le professionnel fasse parti du même cabinet que le patient cible
$messagePatient->setAuthor($user);
$messagePatient->setType(1);
$messagePatient->setAffectedPatient($patient);
$entityManager->persist($messagePatient);
$entityManager->flush();
$this->addFlash('info', "Nouveau message envoyé.");
return $this->redirectToRoute('index_message_professional', ['id' => $patient->getId()]);
}
}
$messages = $entityManager->getRepository('App\Entity\MessagePatient')->findBy(['affectedPatient' => $patient], ['createdAt' => 'DESC']);
foreach ($messages as &$message) {
if ($message->getListProfessionalViewed() != null) {
$listProfessional = explode(';', $message->getListProfessionalViewed());
$message->setView(false);
$fullName = [];
foreach ($listProfessional as $professionalUuid) {
$professional = $entityManager->getRepository('App\Entity\Professional')->find(Uuid::fromString($professionalUuid));
if ($user->getIsProfessional()) {
$userProfessional = $user->getProfessional();
if ($professional == $userProfessional) {
$message->setView(true);
}
}
$fistName = $professional->getFirstName();
$lastName = strtoupper($professional->getLastName()[0]);
$fullName[] = $lastName . '.' . $fistName;
}
$message->setListProfessionalViewed(implode(', ', $fullName));
}
}
return $this->render('message/index.html.twig', [
'form' => $form->createView(),
'messages' => $messages,
'current_menu' => 'patient',
'patient' => $patient,
'haveOffice' => $haveOffice
]);
}
/**
* @Route("/message/update/{idMessage}", name="message_update_view", options = { "expose" = true })
* définis un message comme vu
*/
public function ajaxUpdateMessageView($idMessage, ?UserInterface $user, EntityManagerInterface $entityManager)
{
if (!$user) {
return $this->redirectToRoute('app_login');
}
$messagePatient = $entityManager->getRepository('App\Entity\MessagePatient')->find(json_decode($idMessage));
$patient = $messagePatient->getAffectedPatient();
$userOffice = $user->getOffice();
if ($userOffice === $patient->getUser()->getOffice() || $userOffice === $messagePatient->getAuthor()->getOffice()) {
if ($user->getIsProfessional()) {
$professional = $user->getProfessional();
$uuid = $professional->getId()->toRfc4122();
$listProfessional = $messagePatient->getListProfessionalViewed();
if ($listProfessional === null) {
$listProfessional = [];
} else {
$listProfessional = explode(';', $listProfessional);
}
$listProfessional[] = $uuid;
$listProfessional = implode(';', $listProfessional);
$messagePatient->setListProfessionalViewed($listProfessional);
}
$messagePatient->setView(1);
$entityManager->persist($messagePatient);
$entityManager->flush();
}
return new JsonResponse([
'status' => 1,
'msg' => ''
]);
}
/**
* @Route("/message/patient", name="index_message_patient", methods={"GET","POST"})
* @IsGranted("ROLE_PATIENT")
*/
public function indexPatient(?UserInterface $user, EntityManagerInterface $entityManager, Request $request)
{
if (!$user) {
return $this->redirectToRoute('app_login');
}
$haveOffice = true;
if (!$user->getOffice()) {
$haveOffice = false;
}
$patient = $user->getPatient();
$messagePatient = new MessagePatient();
$form = $this->createForm(MessagePatientType::class, $messagePatient);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//création d'une notification
$office = $user->getOffice();
$users = $entityManager->getRepository('App\Entity\User')->findBy(['office' => $office, 'isProfessional' => 1]);
foreach ($users as $oUser) {
$notification = new Notification();
$notification->setType(2);
$notification->setUser($oUser);
$entityManager->persist($notification);
}
$messagePatient->setAuthor($user);
$messagePatient->setType(2);
$messagePatient->setAffectedPatient($patient);
$entityManager->persist($messagePatient);
$entityManager->flush();
$this->addFlash('info', "Nouveau message envoyé.");
return $this->redirectToRoute('index_message_patient');
}
$messages = $entityManager->getRepository('App\Entity\MessagePatient')->findBy(['affectedPatient' => $patient], ['createdAt' => 'DESC']);
return $this->render('message/index.html.twig', [
'controller_name' => 'MessageController',
'form' => $form->createView(),
'messages' => $messages,
'current_menu' => 'message',
'haveOffice' => $haveOffice
]);
}
/**
* @Route("/json/message/new", name="have_message_new_json", options = { "expose" = true })
* vérifie s'il existe des messages non lus afin d'afficher une notification
*/
public function ajaxHaveNewMessage(?UserInterface $user, MessagePatientRepository $messagePatientRepository, EntityManagerInterface $entityManager)
{
if (!$user) {
return new JsonResponse(0);
}
if ($user->getIsProfessional()) {//si l'utilisateur est un professionnel
$idUser = $user->getId();
$notifications = $entityManager->getRepository('App\Entity\Notification')->findBy(['user' => $idUser, 'isViewed' => '0'], ['createdAt' => 'DESC'], 1);
if (empty($notifications)) {
return new JsonResponse(0);
} else {
return new JsonResponse(1);
}
// $officeMessagesNotViewInCache = $cache->getItem('number_messages.from.patient');
// //si le nombre de message est en cache
// if($officeMessagesNotViewInCache->isHit()){
// if(count($officeMessagesNotViewInCache->get()) > 0){
// return new JsonResponse(1);
// }
// return new JsonResponse(0);
// }
//
// $office = $user->getOffice();
// $users = $office->getUsers();
// $arrayPatient = [];
// foreach ($users as $user){
// if(!$user->getIsProfessional()){
// $arrayPatient[] = $user->getPatient()->getId();//récupère les patients du cabinet
// }
// }
// $officeMessagesNotView = $messagePatientRepository->getMessageNotViewForPatients($arrayPatient);
// $officeMessagesNotViewInCache = $cache->getItem('number_messages.from.patient'); //création du cache
// $officeMessagesNotViewInCache->set($officeMessagesNotView); //mise en cache du nombre de message
// $officeMessagesNotViewInCache->expiresAfter(7200); //durée du cache
// $cache->save($officeMessagesNotViewInCache); //sauvegarde du cache
//
// if(count($officeMessagesNotView) > 0){
// return new JsonResponse(1);
// }
} else {
// $cache = new FilesystemAdapter();
// $officeMessagesNotViewInCache = $cache->getItem('number_messages.from.professional');
//
// //si le nombre de message est en cache
// if ($officeMessagesNotViewInCache->isHit()) {
// if (count($officeMessagesNotViewInCache->get()) > 0) {
// return new JsonResponse(1);
// }
// return new JsonResponse(0);
// }
$idPatient = $user->getPatient()->getId();
$officeMessagesNotView = $messagePatientRepository->findBy(['view' => 0, 'type' => 1, 'affectedPatient' => $idPatient]);
// $officeMessagesNotViewInCache = $cache->getItem('number_messages.from.professional'); //création du cache
// $officeMessagesNotViewInCache->set($officeMessagesNotView); //mise en cache du nombre de message
// $officeMessagesNotViewInCache->expiresAfter(7200); //durée du cache
// $cache->save($officeMessagesNotViewInCache); //sauvegarde du cache
if (count($officeMessagesNotView) > 0) {
return new JsonResponse(1);
}
}
return new JsonResponse(0);
}
}