From 20d42d319acd76d163a3970715f23d5d482102a8 Mon Sep 17 00:00:00 2001 From: Matthieu Lamalle Date: Fri, 12 Jun 2020 15:18:53 +0200 Subject: [PATCH] =?UTF-8?q?Envoie=20de=20notification=20=C3=A0=20la=20pose?= =?UTF-8?q?=20et=20la=20vadliation=20de=20cong=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schedule-2.0/.env | 11 +++ src/schedule-2.0/config/bundles.php | 1 + .../config/packages/swiftmailer.yaml | 5 ++ .../config/packages/test/swiftmailer.yaml | 5 ++ src/schedule-2.0/config/services.yaml | 11 +++ .../src/Command/SendMailCommand.php | 87 +++++++++++++++++++ .../src/Controller/EventController.php | 11 +++ .../src/Controller/ValidationController.php | 9 +- .../src/Service/notificationService.php | 75 ++++++++++++++++ .../Notif/attentevalidation.html.twig | 25 ++++++ .../templates/Notif/validation.html.twig | 23 +++++ 11 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 src/schedule-2.0/config/packages/swiftmailer.yaml create mode 100644 src/schedule-2.0/config/packages/test/swiftmailer.yaml create mode 100644 src/schedule-2.0/src/Command/SendMailCommand.php create mode 100644 src/schedule-2.0/src/Service/notificationService.php create mode 100644 src/schedule-2.0/templates/Notif/attentevalidation.html.twig create mode 100644 src/schedule-2.0/templates/Notif/validation.html.twig diff --git a/src/schedule-2.0/.env b/src/schedule-2.0/.env index 32372c0..3c5f4b5 100644 --- a/src/schedule-2.0/.env +++ b/src/schedule-2.0/.env @@ -32,6 +32,11 @@ APP_NAME=Schedule APP_ENV=PROD APP_CRON=false +# MAIL sendmail / smtp +MAILER_METHOD=sendmail +MAILER_URL= +MAILER_NOREPLY=noreply@noreply.fr + # BDD DATABASE_NAME= DATABASE_USER= @@ -46,3 +51,9 @@ CAS_USERNAME=username CAS_EMAIL=email CAS_LASTNAME=lastname CAS_FIRSTNAME=firstname + +###> symfony/swiftmailer-bundle ### +# For Gmail as a transport, use: "gmail://username:password@localhost" +# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode=" +# Delivery is disabled by default via "null://localhost" +MAILER_URL= \ No newline at end of file diff --git a/src/schedule-2.0/config/bundles.php b/src/schedule-2.0/config/bundles.php index 36f3dcf..40aec7b 100644 --- a/src/schedule-2.0/config/bundles.php +++ b/src/schedule-2.0/config/bundles.php @@ -19,4 +19,5 @@ return [ Tetranz\Select2EntityBundle\TetranzSelect2EntityBundle::class => ['all' => true], Oneup\UploaderBundle\OneupUploaderBundle::class => ['all' => true], Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' => true], + Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], ]; diff --git a/src/schedule-2.0/config/packages/swiftmailer.yaml b/src/schedule-2.0/config/packages/swiftmailer.yaml new file mode 100644 index 0000000..db8d136 --- /dev/null +++ b/src/schedule-2.0/config/packages/swiftmailer.yaml @@ -0,0 +1,5 @@ +swiftmailer: + url: '%env(MAILER_URL)%' + spool: + type: file + path: '%kernel.project_dir%/var/spoolmail' \ No newline at end of file diff --git a/src/schedule-2.0/config/packages/test/swiftmailer.yaml b/src/schedule-2.0/config/packages/test/swiftmailer.yaml new file mode 100644 index 0000000..db8d136 --- /dev/null +++ b/src/schedule-2.0/config/packages/test/swiftmailer.yaml @@ -0,0 +1,5 @@ +swiftmailer: + url: '%env(MAILER_URL)%' + spool: + type: file + path: '%kernel.project_dir%/var/spoolmail' \ No newline at end of file diff --git a/src/schedule-2.0/config/services.yaml b/src/schedule-2.0/config/services.yaml index 1ac6453..16a1acd 100644 --- a/src/schedule-2.0/config/services.yaml +++ b/src/schedule-2.0/config/services.yaml @@ -9,6 +9,8 @@ parameters: appName: '%env(resolve:APP_NAME)%' appEnv: '%env(resolve:APP_ENV)%' appCron: '%env(resolve:APP_CRON)%' + appMailmethod: '%env(resolve:MAILER_METHOD)%' + appMailnoreply: '%env(resolve:MAILER_NOREPLY)%' casHost: '%env(resolve:CAS_HOST)%' casPort: '%env(resolve:CAS_PORT)%' casPath: '%env(resolve:CAS_PATH)%' @@ -59,3 +61,12 @@ services: tags: - { name: kernel.event_listener, event: kernel.request, method: onDomainParse } + app.sendmail.transport: + class: Swift_SendmailTransport + public: true + arguments: ['/usr/sbin/sendmail -t'] + + app.mail.service: + public: true + class: App\Service\mailService + arguments: ["@mailer", "@twig"] diff --git a/src/schedule-2.0/src/Command/SendMailCommand.php b/src/schedule-2.0/src/Command/SendMailCommand.php new file mode 100644 index 0000000..c3b2833 --- /dev/null +++ b/src/schedule-2.0/src/Command/SendMailCommand.php @@ -0,0 +1,87 @@ +container = $container; + $this->em = $em; + } + + protected function configure() + { + $this + ->setName('app:sendMail') + ->setDescription('Envoi des mails') + ->setHelp('Envoi des mails') + ->addArgument('message-limit', InputArgument::OPTIONAL, 'limit message Mail') + ->addArgument('env', InputArgument::OPTIONAL, 'env Mail') + ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') + ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->output = $output; + $this->filesystem = new Filesystem(); + $this->rootlog = $this->container->get('kernel')->getRootDir()."/../var/logs/"; + + $this->writelnred(''); + $this->writelnred('== app:sendMail'); + $this->writelnred('=========================================================================================================='); + + $appMailmethod=$this->container->getParameter("appMailmethod"); + + $command = $this->getApplication()->find("swiftmailer:spool:send"); + $tbparameter["--message-limit"]=100; + $tbparameter["--env"]="prod"; + $tbparameter["--time-limit"]=5; + if($appMailmethod=="sendmail") $tbparameter["--transport"]="app.sendmail.transport"; + + + $parameter = new ArrayInput($tbparameter); + try{ + $returnCode = $command->run($parameter, $output); + } + catch(\Exception $e) { + $this->writeln(""); + $this->writelnred("Impossible d'envoyer des mails"); + $this->writeln(""); + return 0; + } + $this->writeln(""); + + return 1; + + } + + private function writelnred($string) { + $this->output->writeln(''.$string.''); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + private function writeln($string) { + $this->output->writeln($string); + $this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n"); + } + +} diff --git a/src/schedule-2.0/src/Controller/EventController.php b/src/schedule-2.0/src/Controller/EventController.php index 1e7e4cd..bc3b5c1 100755 --- a/src/schedule-2.0/src/Controller/EventController.php +++ b/src/schedule-2.0/src/Controller/EventController.php @@ -17,6 +17,11 @@ class EventController extends AbstractController private $route = "app_event"; private $render = "Event/"; private $entity = "App:Event"; + private $notificator; + + public function __construct(\App\Service\notificationService $notificator) { + $this->notificator = $notificator; + } public function list(Request $request) { @@ -245,6 +250,12 @@ class EventController extends AbstractController $em->persist($event); $em->flush(); + if($task->getNature()->getIsvacation()){ + $idevent=$event->getId(); + $valid_url = $this->generateUrl('app_validationholiday'); + $this->notificator->sendNotifAttenteValid("Congés en attente de validation", $iduser, $idevent, $valid_url); + + } $output=$this->formatEvent($event); } diff --git a/src/schedule-2.0/src/Controller/ValidationController.php b/src/schedule-2.0/src/Controller/ValidationController.php index 66bc702..f8cc772 100755 --- a/src/schedule-2.0/src/Controller/ValidationController.php +++ b/src/schedule-2.0/src/Controller/ValidationController.php @@ -10,7 +10,11 @@ use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse; class ValidationController extends AbstractController { private $knpSnappy; - public function __construct(\Knp\Snappy\Pdf $knpSnappy) { $this->knpSnappy = $knpSnappy; } + private $notificator; + public function __construct(\Knp\Snappy\Pdf $knpSnappy, \App\Service\notificationService $notificator) { + $this->knpSnappy = $knpSnappy; + $this->notificator = $notificator; + } public function validation(Request $request) { @@ -510,6 +514,9 @@ class ValidationController extends AbstractController $event->setValidateholiday(true); $em->persist($event); $em->flush(); + $iduser=$this->get("session")->get("iduser"); + $idevent=$event->getId(); + $this->notificator->sendNotifValid("Congé validé", $iduser, $event); } $output=[]; diff --git a/src/schedule-2.0/src/Service/notificationService.php b/src/schedule-2.0/src/Service/notificationService.php new file mode 100644 index 0000000..4c70c68 --- /dev/null +++ b/src/schedule-2.0/src/Service/notificationService.php @@ -0,0 +1,75 @@ +mailer = $mailer; + $this->twig = $twig; + $this->em = $em; + } + + public function sendNotifAttenteValid($subject, $iduser, $idevent, $valid_url) + { + $template = $this->twig->load('Notif/attentevalidation.html.twig'); + + + $user=$this->em->getRepository("App:User")->find($iduser); + $event=$this->em->getRepository("App:Event")->find($idevent); + + $parameters=[ + 'date' => new \DateTime(), + 'username' => $user->getUsername(), + 'start' => $event->getStart(), + 'end' => $event->getEnd(), + 'duration' => $event->getDuration(), + 'valid_link' => $valid_url, + ]; + $bodyHtml = $template->renderBlock('body', $parameters); + + $message = (new \Swift_Message()) + ->setFrom('schedule@cadoles.com') + ->setSubject($user->getUsername() . " = " . $subject) + ->setTo('conges@cadoles.com') + ->setBody($bodyHtml, 'text/html'); + + $response = $this->mailer->send($message); + + return $response; + } + public function sendNotifValid($subject, $iduser, $idevent) + { + $template = $this->twig->load('Notif/validation.html.twig'); + + + $user=$this->em->getRepository("App:User")->find($iduser); + $event=$this->em->getRepository("App:Event")->find($idevent); + + $parameters=[ + 'date' => new \DateTime(), + 'username' => $user->getUsername(), + 'start' => $event->getStart(), + 'end' => $event->getEnd(), + 'duration' => $event->getDuration(), + ]; + $bodyHtml = $template->renderBlock('body', $parameters); + + $message = (new \Swift_Message()) + ->setFrom('schedule@cadoles.com') + ->setSubject($user->getUsername() . " = " . $subject) + ->setTo('conges@cadoles.com') + ->setBody($bodyHtml, 'text/html'); + + $response = $this->mailer->send($message); + + + return $response; + } +} diff --git a/src/schedule-2.0/templates/Notif/attentevalidation.html.twig b/src/schedule-2.0/templates/Notif/attentevalidation.html.twig new file mode 100644 index 0000000..a224421 --- /dev/null +++ b/src/schedule-2.0/templates/Notif/attentevalidation.html.twig @@ -0,0 +1,25 @@ +{% block body %} +{% autoescape %} +

Date de la demande : {{ date|date("d/m/Y H:i") }}

+

+ Utilisateur = {{ username }} +

+

+ Début = {{ start|date("d/m/Y H:i") }} +

+

+ Fin = {{ end|date("d/m/Y H:i") }} +

+

+ Durée = {{ duration }} +

+

+ Type = Congé +

+

+ Lien pour valider = {{ valid_link }} +

+ +{% endautoescape %} +{% endblock %} + diff --git a/src/schedule-2.0/templates/Notif/validation.html.twig b/src/schedule-2.0/templates/Notif/validation.html.twig new file mode 100644 index 0000000..4a35676 --- /dev/null +++ b/src/schedule-2.0/templates/Notif/validation.html.twig @@ -0,0 +1,23 @@ +{% block body %} +{% autoescape %} +

VALIDATION

+

+ Utilisateur = {{ username }} +

+

+ Début = {{ start|date("d/m/Y H:i") }} +

+

+ Fin = {{ end|date("d/m/Y H:i") }} +

+

+ Durée = {{ duration }} +

+

+ Type = Congé +

+ + +{% endautoescape %} +{% endblock %} +