src/EventSubscriber/WorkflowMailer.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Swift_Image;
  4. use Swift_Mailer;
  5. use Swift_SmtpTransport;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Workflow\Event\Event;
  8. use Twig_Environment;
  9. class WorkflowMailer implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Twig_Environment
  13.      */
  14.     protected $twig;
  15.     /**
  16.      * @var Swift_Mailer
  17.      */
  18.     protected $mailer;
  19.     /**
  20.      * @var Swift_SmtpTransport
  21.      */
  22.     protected $transport;
  23.     /**
  24.      * WorkflowMailer constructor.
  25.      * @param Twig_Environment $twig
  26.      */
  27.     public function __construct(Twig_Environment $twig)
  28.     {
  29.         $this->twig $twig;
  30.         $this->transport = new \Swift_SendmailTransport('/usr/sbin/sendmail -bs');
  31.         $this->mailer = new Swift_Mailer($this->transport);
  32.     }
  33.     public function onDeposerConge(Event $event)
  34.     {
  35.         $message = (new \Swift_Message('Congés: Nouvelle Demande | '
  36.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  37.             $event->getSubject()->getDemandeur()->getNom()))
  38.             ->setFrom([
  39.                 '[email protected]=>
  40.                 $event->getSubject()->getDemandeur()->getPrenom() . ' '
  41.                     $event->getSubject()->getDemandeur()->getNom()
  42.             ]);
  43.         $data['imgSrc'] = $message->embed(
  44.             Swift_Image::fromPath(
  45.                 __DIR__ '/../../public/assets/img/auroneCalendrier.png'
  46.             )
  47.         );
  48.         $data['demande'] = $event->getSubject();
  49.         // => Validateur
  50.         $message->setTo($event->getSubject()->getValidateur()->getEmail());
  51.         $message->setBody(
  52.             $this->twig->render('mail/onDeposerCongeValidateur.html.twig'$data),
  53.             'text/html',
  54.             'UTF-8'
  55.         );
  56.         $this->mailer->send($message);
  57.     }
  58.     public function onAccepter(Event $event)
  59.     {
  60.         $message = (new \Swift_Message('Congés: Demande de Congé Acceptée | '
  61.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  62.             $event->getSubject()->getDemandeur()->getNom()))
  63.             ->setFrom([
  64.                 '[email protected]=>
  65.                 $event->getSubject()->getValidateur()->getPrenom() . ' '
  66.                     $event->getSubject()->getValidateur()->getNom()
  67.             ]);
  68.         $data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ '/../../public/assets/img/auroneCalendrier.png'));
  69.         $data['demande'] = $event->getSubject();
  70.         // Get all users (emails) to notify except current applicant
  71.         $usersToNotify $event->getSubject()->getAllNotifiedUsersExceptCurrent();
  72.         // Notify colleagues
  73.         $message->setTo($usersToNotify);
  74.         $message->setBody(
  75.             $this->twig->render('mail/onAccepterRemplacant.html.twig'$data),
  76.             'text/html',
  77.             'UTF-8'
  78.         );
  79.         $this->mailer->send($message);
  80.   
  81.         // Notify applicant
  82.         $message->setTo$event->getSubject()->getDemandeur()->getEmail());
  83.         $message->setBody(
  84.             $this->twig->render('mail/onAccepterDemandeur.html.twig'$data),
  85.             'text/html',
  86.             'UTF-8'
  87.         );
  88.         $this->mailer->send($message);
  89.     }
  90.     public function onRefuser(Event $event)
  91.     {
  92.         $message = (new \Swift_Message('Congés: Demande de Congé Réfusée | '
  93.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  94.             $event->getSubject()->getDemandeur()->getNom()))
  95.             ->setFrom([
  96.                 '[email protected]=>
  97.                 $event->getSubject()->getValidateur()->getPrenom() . ' '
  98.                     $event->getSubject()->getValidateur()->getNom()
  99.             ]);
  100.         $data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ '/../../public/assets/img/auroneCalendrier.png'));
  101.         $data['demande'] = $event->getSubject();
  102.         // => Demandeur
  103.         $message->setTo($event->getSubject()->getDemandeur()->getEmail());
  104.         $message->setBody(
  105.             $this->twig->render('mail/onRefuserDemandeur.html.twig'$data),
  106.             'text/html',
  107.             'UTF-8'
  108.         );
  109.         $this->mailer->send($message);
  110.     }
  111.     public function onIgnorer(Event $event// CRONNNNNNNN !!!!!
  112.     {
  113.         $message = (new \Swift_Message('Congés: Demande de Congé Expirée | '
  114.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  115.             $event->getSubject()->getDemandeur()->getNom()))
  116.             ->setFrom(['[email protected]=> 'Aurone | Congés']);
  117.         $data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ '/../../public/assets/img/auroneCalendrier.png'));
  118.         $data['demande'] = $event->getSubject();
  119.         if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
  120.             // => Remplacant
  121.             $message->setTo($event->getSubject()->getRemplacant()->getEmail());
  122.             $message->setBody(
  123.                 $this->twig->render('mail/onIgnorerRemplacant.html.twig'$data),
  124.                 'text/html',
  125.                 'UTF-8'
  126.             );
  127.             $this->mailer->send($message);
  128.         }
  129.         // => Demandeur
  130.         $message->setTo($event->getSubject()->getDemandeur()->getEmail());
  131.         $message->setBody(
  132.             $this->twig->render('mail/onIgnorerDemandeur.html.twig'$data),
  133.             'text/html',
  134.             'UTF-8'
  135.         );
  136.         $this->mailer->send($message);
  137.     }
  138.     public function onAnnuler(Event $event)
  139.     {
  140.         $message = (new \Swift_Message('Congés: Demande de Congé Annulée | '
  141.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  142.             $event->getSubject()->getDemandeur()->getNom()))
  143.             ->setFrom([
  144.                 '[email protected]=>
  145.                 $event->getSubject()->getDemandeur()->getPrenom() . ' '
  146.                     $event->getSubject()->getDemandeur()->getNom()
  147.             ]);
  148.         $data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ '/../../public/assets/img/auroneCalendrier.png'));
  149.         $data['demande'] = $event->getSubject();
  150.         // => Validateur
  151.         $message->setTo($event->getSubject()->getValidateur()->getEmail());
  152.         $message->setBody(
  153.             $this->twig->render('mail/onAnnulerValidateur.html.twig'$data),
  154.             'text/html',
  155.             'UTF-8'
  156.         );
  157.         $this->mailer->send($message);
  158.         if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
  159.             // => Remplacant
  160.             $message->setTo($event->getSubject()->getRemplacant()->getEmail());
  161.             $message->setBody(
  162.                 $this->twig->render('mail/onAnnulerRemplacant.html.twig'$data),
  163.                 'text/html',
  164.                 'UTF-8'
  165.             );
  166.             $this->mailer->send($message);
  167.         }
  168.     }
  169.     public function onDeposerAnnulation(Event $event)
  170.     {
  171.         $message = (new \Swift_Message("Congés: Demande d'Annulation Déposée | "
  172.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  173.             $event->getSubject()->getDemandeur()->getNom()))
  174.             ->setFrom([
  175.                 '[email protected]=>
  176.                 $event->getSubject()->getDemandeur()->getPrenom() . ' '
  177.                     $event->getSubject()->getDemandeur()->getNom()
  178.             ]);
  179.         $data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ '/../../public/assets/img/auroneCalendrier.png'));
  180.         $data['demande'] = $event->getSubject();
  181.         // => Validateur
  182.         $message->setTo($event->getSubject()->getValidateur()->getEmail());
  183.         $message->setBody(
  184.             $this->twig->render('mail/onDeposerAnnulationValidateur.html.twig'$data),
  185.             'text/html',
  186.             'UTF-8'
  187.         );
  188.         $this->mailer->send($message);
  189.         if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
  190.             // => Remplacant
  191.             $message->setTo($event->getSubject()->getRemplacant()->getEmail());
  192.             $message->setBody(
  193.                 $this->twig->render('mail/onDeposerAnnulationRemplacant.html.twig'$data),
  194.                 'text/html',
  195.                 'UTF-8'
  196.             );
  197.             $this->mailer->send($message);
  198.         }
  199.     }
  200.     public function onAccepterAnnulation(Event $event)
  201.     {
  202.         $message = (new \Swift_Message("Congés: Demande d'Annulation Acceptée | "
  203.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  204.             $event->getSubject()->getDemandeur()->getNom()))
  205.             ->setFrom([
  206.                 '[email protected]=>
  207.                 $event->getSubject()->getValidateur()->getPrenom() . ' '
  208.                     $event->getSubject()->getValidateur()->getNom()
  209.             ]);
  210.         $data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ '/../../public/assets/img/auroneCalendrier.png'));
  211.         $data['demande'] = $event->getSubject();
  212.         if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
  213.             // => Remplacant
  214.             $message->setTo($event->getSubject()->getRemplacant()->getEmail());
  215.             $message->setBody(
  216.                 $this->twig->render('mail/onAccepterAnnulationRemplacant.html.twig'$data),
  217.                 'text/html',
  218.                 'UTF-8'
  219.             );
  220.             $this->mailer->send($message);
  221.         }
  222.         // => Demandeur
  223.         $message->setTo($event->getSubject()->getDemandeur()->getEmail());
  224.         $message->setBody(
  225.             $this->twig->render('mail/onAccepterAnnulationDemandeur.html.twig'$data),
  226.             'text/html',
  227.             'UTF-8'
  228.         );
  229.         $this->mailer->send($message);
  230.     }
  231.     public function onRefuserAnnulation(Event $event)
  232.     {
  233.         $message = (new \Swift_Message("Congés: Demande d'Annulation Réfusée | "
  234.             $event->getSubject()->getDemandeur()->getPrenom() . ' '
  235.             $event->getSubject()->getDemandeur()->getNom()))
  236.             ->setFrom([
  237.                 '[email protected]=>
  238.                 $event->getSubject()->getValidateur()->getPrenom() . ' '
  239.                     $event->getSubject()->getValidateur()->getNom()
  240.             ]);
  241.         $data['imgSrc'] = $message->embed(
  242.             Swift_Image::fromPath(
  243.                 __DIR__ '/../../public/assets/img/auroneCalendrier.png'
  244.             )
  245.         );
  246.         $data['demande'] = $event->getSubject();
  247.         if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
  248.             // => Remplacant
  249.             $message->setTo($event->getSubject()->getRemplacant()->getEmail());
  250.             $message->setBody(
  251.                 $this->twig->render('mail/onRefuserAnnulationRemplacant.html.twig'$data),
  252.                 'text/html',
  253.                 'UTF-8'
  254.             );
  255.             $this->mailer->send($message);
  256.         }
  257.         // => Demandeur
  258.         $message->setTo($event->getSubject()->getDemandeur()->getEmail());
  259.         $message->setBody(
  260.             $this->twig->render('mail/onRefuserAnnulationDemandeur.html.twig'$data),
  261.             'text/html',
  262.             'UTF-8'
  263.         );
  264.         $this->mailer->send($message);
  265.     }
  266.     public static function getSubscribedEvents()
  267.     {
  268.         return [
  269.             'workflow.workflow_conge.completed.deposer_conge' => 'onDeposerConge',
  270.             'workflow.workflow_conge.completed.accepter' => 'onAccepter',
  271.             'workflow.workflow_conge.completed.refuser' => 'onRefuser',
  272.             'workflow.workflow_conge.completed.ignorer' => 'onIgnorer',
  273.             'workflow.workflow_conge.completed.annuler' => 'onAnnuler',
  274.             'workflow.workflow_conge.completed.deposer_annulation' => 'onDeposerAnnulation',
  275.             'workflow.workflow_conge.completed.accepter_annulation' => 'onAccepterAnnulation',
  276.             'workflow.workflow_conge.completed.refuser_annulation' => 'onRefuserAnnulation',
  277.         ];
  278.     }
  279. }