src/Controller/EmailController.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3. * ==============================================================
  4. *     Autor            :  Farid Benjomaa
  5. *     Modified by        :  
  6. *    COPYRIGHT (C) 2025, Media-Technologies
  7. * ==============================================================
  8. */
  9. namespace App\Controller;
  10. use App\Entity\Client;
  11. use App\Entity\Emails;
  12. use App\Form\EmailsType;
  13. use App\Services\CallApiServices;
  14. use App\Services\QuestionMailService;
  15. use PharIo\Manifest\Email;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  22. class EmailController extends AbstractController
  23. {
  24.     private $callApiServices;
  25.     private $questionMailService;
  26.     private $parameters;
  27.     public function __construct(
  28.         CallApiServices $callApiServices
  29.         QuestionMailService $questionMailService
  30.         ParameterBagInterface $params
  31.     ) {
  32.         $this->callApiServices $callApiServices;
  33.         $this->questionMailService $questionMailService;
  34.         $this->parameters $params;
  35.     }
  36.     /**
  37.      * @Route("/email", name="app_email")
  38.      */
  39.     public function index(): Response
  40.     {
  41.         $user $this->getUser();
  42.         $clientId 0;
  43.         if ($user) {
  44.             $clientId $user->getClientId();
  45.         }
  46.         $client $this->questionMailService->getEmail($this->callApiServices$user);
  47.         // dd($client);
  48.         return $this->render('email/email.html.twig', [
  49.             'clientId' => $clientId
  50.         ]);
  51.     }
  52.     /**
  53.      * @Route("/receptionMail", name="reception_mail")
  54.      */
  55.     public function reception(CallApiServices $callApiServices): Response
  56.     {
  57.         $user $this->getUser();
  58.         $clientId 0;
  59.         if ($user) {
  60.             $clientId $user->getClientId();
  61.         }
  62.         return $this->render('email/reception.html.twig', [
  63.             'clientId' => $clientId
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route("/send", name="envoie_mail")
  68.      */
  69.     public function send(): Response
  70.     {
  71.         $user $this->getUser();
  72.         $clientId 0;
  73.         if ($user) {
  74.             $clientId $user->getClientId();
  75.         }
  76.         return $this->render('email/envoie.html.twig', [
  77.             'clientId' => $clientId
  78.         ]);
  79.     }
  80.     /**
  81.      * @Route("/mailView/{id}", name="mail_view")
  82.      */
  83.     public function mailView(EntityManagerInterface $em$id): Response
  84.     {
  85.         $client $this->getUser();
  86.         $clientId 0;
  87.         if ($client instanceof Client) {
  88.             $mail $client->getEmailsSend();
  89.             $clientId $client->getClientId();
  90.         }
  91.         foreach ($mail as $key => $mailId) {
  92.             $previous array_key_exists($key 1$mail) ? $mail[$key 1] : false;
  93.             $next array_key_exists($key 1$mail) ? $mail[$key 1] : false;
  94.             $last array_key_last($mail);
  95.             $first array_key_first($mail);
  96.             if ($previous == false) {
  97.                 $previous $last;
  98.             } else {
  99.                 $previous $key 1;
  100.             }
  101.             if ($next == false) {
  102.                 $next $first;
  103.             } else {
  104.                 $next $key 1;
  105.             }
  106.             $newMail[$key] = [
  107.                 'previous' => $previous,
  108.                 'current' => $key,
  109.                 'next' => $next
  110.             ];
  111.         }
  112.         return $this->render('email/mailView.html.twig', [
  113.             "mail" => $mail[$id],
  114.             "current" => $newMail[$id],
  115.             "previous" => $newMail[$id]['previous'],
  116.             "next" => $newMail[$id]['next'],
  117.             'clientId' => $clientId,
  118.         ]);
  119.     }
  120.     /**
  121.      * @Route("/receivedMail/{id}", name="app_receivedMail")
  122.      */
  123.     public function receivedMail(EntityManagerInterface $em$idCallApiServices $callApiServices): Response
  124.     {
  125.         $user $this->getUser();
  126.         $clientId 0;
  127.         if ($user) {
  128.             $clientId $user->getClientId();
  129.         }
  130.         $client $this->questionMailService->getEmail($this->callApiServices$user);
  131.         if ($client instanceof Client) {
  132.             $mail $client->getEmailsRecieved();
  133.         }
  134.         foreach ($mail as $key => $mailId) {
  135.             $previous array_key_exists($key 1$mail) ? $mail[$key 1] : false;
  136.             $next array_key_exists($key 1$mail) ? $mail[$key 1] : false;
  137.             $last array_key_last($mail);
  138.             $first array_key_first($mail);
  139.             if ($previous == false) {
  140.                 $previous $last;
  141.             } else {
  142.                 $previous $key 1;
  143.             }
  144.             if ($next == false) {
  145.                 $next $first;
  146.             } else {
  147.                 $next $key 1;
  148.             }
  149.             $newMail[$key] = [
  150.                 'previous' => $previous,
  151.                 'current' => $key,
  152.                 'next' => $next
  153.             ];
  154.             $isRead $mail[$key]['isRead'] = true;
  155.             $callApiServices->questionsMailMajMessage($mail[$key]['serviceId'], $mail[$key]['clientId'], $mail[$key]['complementId'], $mail[$key]['id'], $mail[$key]['answered'], $isRead);
  156.         }
  157.         return $this->render('email/receivedMailView.html.twig', [
  158.             "mail" => $mail[$id],
  159.             "current" => $newMail[$id],
  160.             "previous" => $newMail[$id]['previous'],
  161.             "next" => $newMail[$id]['next'],
  162.             'clientId' => $clientId
  163.         ]);
  164.     }
  165.     /**
  166.      * @Route("/delete/{id}", name="delete")
  167.      */
  168.     public function delete(Emails $emailEntityManagerInterface $em): Response
  169.     {
  170.         $user $this->getUser();
  171.         $clientId 0;
  172.         if ($user) {
  173.             $clientId $user->getClientId();
  174.         }
  175.         $em->remove($email);
  176.         $em->flush();
  177.         return $this->redirectToRoute("reception_mail", [
  178.             'clientId' => $clientId
  179.         ]);
  180.     }
  181.     /**
  182.      * @Route("/questionMail/{id}", name="question_mail")
  183.      */
  184.     public function questionMail(Request $requestEntityManagerInterface $em$idCallApiServices $callApiServicesQuestionMailService $questionMailService): Response
  185.     {
  186.         $user $this->getUser();
  187.         $clientId 0;
  188.         if ($user) {
  189.             $clientId $user->getClientId();
  190.         }
  191.         $serviceId $this->parameters->get('app.serviceId');
  192.         $accountId $this->parameters->get('app.accountId');
  193.         $conseiller $callApiServices->conseillerEtPlanning($id$serviceId);
  194.         $email = new Emails();
  195.         $form $this->createForm(EmailsType::class, $email);
  196.         $prestationEcriteMails $callApiServices->prestationEcriteMails($accountId$serviceId);
  197.         $cost $prestationEcriteMails['questionCost'];
  198.         $form->handleRequest($request);
  199.         if ($form->isSubmitted() && $form->isValid()) {
  200.             $email->setSender($this->getUser());
  201.             $sender $email->getSender();
  202.             $emailSend $callApiServices->questionsMailEnvoiMessage($serviceId$sender->getClientId(), $conseiller[0]['complementId'], $email->getTitle(), $email->getContent(), $email->getDate(), $cost);
  203.             
  204.             if (isset($emailSend['responseCode'])) {
  205.                 $this->addFlash("messageErreur""Votre message n'a pas été envoyé car vous ne disposé pas de credit suffisant.");
  206.                 return $this->render('email/questionMail.html.twig', [
  207.                     "mailForm" => $form->createView(),
  208.                     "conseiller" => $conseiller
  209.                 ]);
  210.             } else {
  211.                 if (isset($conseiller[0]['userMail'])) {
  212.                     $questionMailService->sendEmail($conseiller$serviceId$accountId);
  213.                     $this->addFlash("message""Votre message a été envoyé avec succès.");
  214.                     return $this->redirectToRoute("app_email");
  215.                 }
  216.             }
  217.         }
  218.         return $this->render('email/questionMail.html.twig', [
  219.             "mailForm" => $form->createView(),
  220.             "conseiller" => $conseiller,
  221.             'clientId' => $clientId,
  222.             'cost' => $cost
  223.         ]);
  224.     }
  225. }