<?php
namespace App\EventSubscriber;
use Swift_Image;
use Swift_Mailer;
use Swift_SmtpTransport;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\Event;
use Twig_Environment;
class WorkflowMailer implements EventSubscriberInterface
{
/**
* @var Twig_Environment
*/
protected $twig;
/**
* @var Swift_Mailer
*/
protected $mailer;
/**
* @var Swift_SmtpTransport
*/
protected $transport;
/**
* WorkflowMailer constructor.
* @param Twig_Environment $twig
*/
public function __construct(Twig_Environment $twig)
{
$this->twig = $twig;
$this->transport = new \Swift_SendmailTransport('/usr/sbin/sendmail -bs');
$this->mailer = new Swift_Mailer($this->transport);
}
public function onDeposerConge(Event $event)
{
$message = (new \Swift_Message('Congés: Nouvelle Demande | '
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()
]);
$data['imgSrc'] = $message->embed(
Swift_Image::fromPath(
__DIR__ . '/../../public/assets/img/auroneCalendrier.png'
)
);
$data['demande'] = $event->getSubject();
// => Validateur
$message->setTo($event->getSubject()->getValidateur()->getEmail());
$message->setBody(
$this->twig->render('mail/onDeposerCongeValidateur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
public function onAccepter(Event $event)
{
$message = (new \Swift_Message('Congés: Demande de Congé Acceptée | '
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getValidateur()->getPrenom() . ' '
. $event->getSubject()->getValidateur()->getNom()
]);
$data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ . '/../../public/assets/img/auroneCalendrier.png'));
$data['demande'] = $event->getSubject();
// Get all users (emails) to notify except current applicant
$usersToNotify = $event->getSubject()->getAllNotifiedUsersExceptCurrent();
// Notify colleagues
$message->setTo($usersToNotify);
$message->setBody(
$this->twig->render('mail/onAccepterRemplacant.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
// Notify applicant
$message->setTo( $event->getSubject()->getDemandeur()->getEmail());
$message->setBody(
$this->twig->render('mail/onAccepterDemandeur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
public function onRefuser(Event $event)
{
$message = (new \Swift_Message('Congés: Demande de Congé Réfusée | '
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getValidateur()->getPrenom() . ' '
. $event->getSubject()->getValidateur()->getNom()
]);
$data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ . '/../../public/assets/img/auroneCalendrier.png'));
$data['demande'] = $event->getSubject();
// => Demandeur
$message->setTo($event->getSubject()->getDemandeur()->getEmail());
$message->setBody(
$this->twig->render('mail/onRefuserDemandeur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
public function onIgnorer(Event $event) // CRONNNNNNNN !!!!!
{
$message = (new \Swift_Message('Congés: Demande de Congé Expirée | '
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom(['[email protected]' => 'Aurone | Congés']);
$data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ . '/../../public/assets/img/auroneCalendrier.png'));
$data['demande'] = $event->getSubject();
if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
// => Remplacant
$message->setTo($event->getSubject()->getRemplacant()->getEmail());
$message->setBody(
$this->twig->render('mail/onIgnorerRemplacant.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
// => Demandeur
$message->setTo($event->getSubject()->getDemandeur()->getEmail());
$message->setBody(
$this->twig->render('mail/onIgnorerDemandeur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
public function onAnnuler(Event $event)
{
$message = (new \Swift_Message('Congés: Demande de Congé Annulée | '
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()
]);
$data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ . '/../../public/assets/img/auroneCalendrier.png'));
$data['demande'] = $event->getSubject();
// => Validateur
$message->setTo($event->getSubject()->getValidateur()->getEmail());
$message->setBody(
$this->twig->render('mail/onAnnulerValidateur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
// => Remplacant
$message->setTo($event->getSubject()->getRemplacant()->getEmail());
$message->setBody(
$this->twig->render('mail/onAnnulerRemplacant.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
}
public function onDeposerAnnulation(Event $event)
{
$message = (new \Swift_Message("Congés: Demande d'Annulation Déposée | "
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()
]);
$data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ . '/../../public/assets/img/auroneCalendrier.png'));
$data['demande'] = $event->getSubject();
// => Validateur
$message->setTo($event->getSubject()->getValidateur()->getEmail());
$message->setBody(
$this->twig->render('mail/onDeposerAnnulationValidateur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
// => Remplacant
$message->setTo($event->getSubject()->getRemplacant()->getEmail());
$message->setBody(
$this->twig->render('mail/onDeposerAnnulationRemplacant.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
}
public function onAccepterAnnulation(Event $event)
{
$message = (new \Swift_Message("Congés: Demande d'Annulation Acceptée | "
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getValidateur()->getPrenom() . ' '
. $event->getSubject()->getValidateur()->getNom()
]);
$data['imgSrc'] = $message->embed(Swift_Image::fromPath(__DIR__ . '/../../public/assets/img/auroneCalendrier.png'));
$data['demande'] = $event->getSubject();
if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
// => Remplacant
$message->setTo($event->getSubject()->getRemplacant()->getEmail());
$message->setBody(
$this->twig->render('mail/onAccepterAnnulationRemplacant.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
// => Demandeur
$message->setTo($event->getSubject()->getDemandeur()->getEmail());
$message->setBody(
$this->twig->render('mail/onAccepterAnnulationDemandeur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
public function onRefuserAnnulation(Event $event)
{
$message = (new \Swift_Message("Congés: Demande d'Annulation Réfusée | "
. $event->getSubject()->getDemandeur()->getPrenom() . ' '
. $event->getSubject()->getDemandeur()->getNom()))
->setFrom([
'[email protected]' =>
$event->getSubject()->getValidateur()->getPrenom() . ' '
. $event->getSubject()->getValidateur()->getNom()
]);
$data['imgSrc'] = $message->embed(
Swift_Image::fromPath(
__DIR__ . '/../../public/assets/img/auroneCalendrier.png'
)
);
$data['demande'] = $event->getSubject();
if ($event->getSubject()->getRemplacant() !== $event->getSubject()->getValidateur()) {
// => Remplacant
$message->setTo($event->getSubject()->getRemplacant()->getEmail());
$message->setBody(
$this->twig->render('mail/onRefuserAnnulationRemplacant.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
// => Demandeur
$message->setTo($event->getSubject()->getDemandeur()->getEmail());
$message->setBody(
$this->twig->render('mail/onRefuserAnnulationDemandeur.html.twig', $data),
'text/html',
'UTF-8'
);
$this->mailer->send($message);
}
public static function getSubscribedEvents()
{
return [
'workflow.workflow_conge.completed.deposer_conge' => 'onDeposerConge',
'workflow.workflow_conge.completed.accepter' => 'onAccepter',
'workflow.workflow_conge.completed.refuser' => 'onRefuser',
'workflow.workflow_conge.completed.ignorer' => 'onIgnorer',
'workflow.workflow_conge.completed.annuler' => 'onAnnuler',
'workflow.workflow_conge.completed.deposer_annulation' => 'onDeposerAnnulation',
'workflow.workflow_conge.completed.accepter_annulation' => 'onAccepterAnnulation',
'workflow.workflow_conge.completed.refuser_annulation' => 'onRefuserAnnulation',
];
}
}