Envoie de notification à la pose et la vadliation de congés

This commit is contained in:
2020-06-12 15:18:53 +02:00
parent 08249abbf5
commit 20d42d319a
11 changed files with 262 additions and 1 deletions

View File

@@ -32,6 +32,11 @@ APP_NAME=Schedule
APP_ENV=PROD APP_ENV=PROD
APP_CRON=false APP_CRON=false
# MAIL sendmail / smtp
MAILER_METHOD=sendmail
MAILER_URL=
MAILER_NOREPLY=noreply@noreply.fr
# BDD # BDD
DATABASE_NAME= DATABASE_NAME=
DATABASE_USER= DATABASE_USER=
@@ -46,3 +51,9 @@ CAS_USERNAME=username
CAS_EMAIL=email CAS_EMAIL=email
CAS_LASTNAME=lastname CAS_LASTNAME=lastname
CAS_FIRSTNAME=firstname 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=

View File

@@ -19,4 +19,5 @@ return [
Tetranz\Select2EntityBundle\TetranzSelect2EntityBundle::class => ['all' => true], Tetranz\Select2EntityBundle\TetranzSelect2EntityBundle::class => ['all' => true],
Oneup\UploaderBundle\OneupUploaderBundle::class => ['all' => true], Oneup\UploaderBundle\OneupUploaderBundle::class => ['all' => true],
Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' => true], Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
]; ];

View File

@@ -0,0 +1,5 @@
swiftmailer:
url: '%env(MAILER_URL)%'
spool:
type: file
path: '%kernel.project_dir%/var/spoolmail'

View File

@@ -0,0 +1,5 @@
swiftmailer:
url: '%env(MAILER_URL)%'
spool:
type: file
path: '%kernel.project_dir%/var/spoolmail'

View File

@@ -9,6 +9,8 @@ parameters:
appName: '%env(resolve:APP_NAME)%' appName: '%env(resolve:APP_NAME)%'
appEnv: '%env(resolve:APP_ENV)%' appEnv: '%env(resolve:APP_ENV)%'
appCron: '%env(resolve:APP_CRON)%' appCron: '%env(resolve:APP_CRON)%'
appMailmethod: '%env(resolve:MAILER_METHOD)%'
appMailnoreply: '%env(resolve:MAILER_NOREPLY)%'
casHost: '%env(resolve:CAS_HOST)%' casHost: '%env(resolve:CAS_HOST)%'
casPort: '%env(resolve:CAS_PORT)%' casPort: '%env(resolve:CAS_PORT)%'
casPath: '%env(resolve:CAS_PATH)%' casPath: '%env(resolve:CAS_PATH)%'
@@ -59,3 +61,12 @@ services:
tags: tags:
- { name: kernel.event_listener, event: kernel.request, method: onDomainParse } - { 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"]

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
class SendMailCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->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('<fg=red>'.$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");
}
}

View File

@@ -17,6 +17,11 @@ class EventController extends AbstractController
private $route = "app_event"; private $route = "app_event";
private $render = "Event/"; private $render = "Event/";
private $entity = "App:Event"; private $entity = "App:Event";
private $notificator;
public function __construct(\App\Service\notificationService $notificator) {
$this->notificator = $notificator;
}
public function list(Request $request) public function list(Request $request)
{ {
@@ -245,6 +250,12 @@ class EventController extends AbstractController
$em->persist($event); $em->persist($event);
$em->flush(); $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); $output=$this->formatEvent($event);
} }

View File

@@ -10,7 +10,11 @@ use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
class ValidationController extends AbstractController class ValidationController extends AbstractController
{ {
private $knpSnappy; 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) public function validation(Request $request)
{ {
@@ -510,6 +514,9 @@ class ValidationController extends AbstractController
$event->setValidateholiday(true); $event->setValidateholiday(true);
$em->persist($event); $em->persist($event);
$em->flush(); $em->flush();
$iduser=$this->get("session")->get("iduser");
$idevent=$event->getId();
$this->notificator->sendNotifValid("Congé validé", $iduser, $event);
} }
$output=[]; $output=[];

View File

@@ -0,0 +1,75 @@
<?php
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
class notificationService
{
private $em;
protected $mailer;
protected $twig;
public function __construct(EntityManagerInterface $em, \Swift_Mailer $mailer, \Twig\Environment $twig)
{
$this->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;
}
}

View File

@@ -0,0 +1,25 @@
{% block body %}
{% autoescape %}
<h2>Date de la demande : {{ date|date("d/m/Y H:i") }} </h2>
<p>
<b>Utilisateur =</b> {{ username }}
</p>
<p>
<b>Début =</b> {{ start|date("d/m/Y H:i") }}
</p>
<p>
<b>Fin =</b> {{ end|date("d/m/Y H:i") }}
</p>
<p>
<b>Durée =</b> {{ duration }}
</p>
<p>
<b>Type =</b> Congé
</p>
<p>
<b>Lien pour valider =</b> {{ valid_link }}
</p>
{% endautoescape %}
{% endblock %}

View File

@@ -0,0 +1,23 @@
{% block body %}
{% autoescape %}
<h2>VALIDATION</h2>
<p>
<b>Utilisateur =</b> {{ username }}
</p>
<p>
<b>Début =</b> {{ start|date("d/m/Y H:i") }}
</p>
<p>
<b>Fin =</b> {{ end|date("d/m/Y H:i") }}
</p>
<p>
<b>Durée =</b> {{ duration }}
</p>
<p>
<b>Type =</b> Congé
</p>
{% endautoescape %}
{% endblock %}