1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace App\EventListener;
use App\Events\MLCEvents;
use App\Events\FluxEvent;
use App\Entity\Flux;
use App\Entity\GlobalParameter;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Listener responsable des évènements liés aux flux (transfert, transaction, retrait, vente...)
*/
class MLCEventListener implements EventSubscriberInterface
{
protected $em;
protected $mailer;
protected $params;
protected $templating;
protected $translator;
protected $router;
protected $userManager;
protected $tokenGenerator;
public function __construct(EntityManagerInterface $em, \Swift_Mailer $mailer, RouterInterface $router, ParameterBagInterface $params, EngineInterface $templating, TranslatorInterface $translator, UserManagerInterface $userManager, TokenGeneratorInterface $tokenGenerator)
{
$this->em = $em;
$this->mailer = $mailer;
$this->params = $params;
$this->templating = $templating;
$this->translator = $translator;
$this->router = $router;
$this->userManager = $userManager;
$this->tokenGenerator = $tokenGenerator;
}
// public function setMailer(TwigSwiftMailer $maileri)
// {
// }
public static function getSubscribedEvents()
{
return array(
MLCEvents::REGISTRATION_ADHERENT => 'onRegistrationAdherent',
MLCEvents::FLUX => 'onFlux',
MLCEvents::COTISATION_COTISATION => 'onCotisation',
MLCEvents::COTISATION_COTISATION_ADHERENT => 'onCotisation',
MLCEvents::COTISATION_COTISATION_PRESTATAIRE => 'onCotisation',
MLCEvents::TRANSACTION_ADHERENT_ADHERENT => 'onTransaction',
MLCEvents::TRANSACTION_ADHERENT_PRESTATAIRE => 'onTransaction',
MLCEvents::TRANSACTION_PRESTATAIRE_ADHERENT => 'onTransaction',
MLCEvents::TRANSACTION_PRESTATAIRE_PRESTATAIRE => 'onTransaction',
MLCEvents::TRANSFERT_COMPTOIR_GROUPE => 'onTransfert',
MLCEvents::TRANSFERT_GROUPE_COMPTOIR => 'onTransfert',
MLCEvents::TRANSFERT_GROUPE_SIEGE => 'onTransfert',
MLCEvents::TRANSFERT_SIEGE_GROUPE => 'onTransfert',
MLCEvents::TRANSFERT_PRESTATAIRE_COMPTOIR => 'onDepot',
MLCEvents::TRANSFERT_PRESTATAIRE_SIEGE => 'onReconversion',
MLCEvents::RETRAIT_COMPTOIR_ADHERENT => 'onRetrait',
MLCEvents::RETRAIT_COMPTOIR_PRESTATAIRE => 'onRetrait',
MLCEvents::VENTE_COMPTOIR_ADHERENT => 'onVente',
MLCEvents::VENTE_COMPTOIR_PRESTATAIRE => 'onVente',
);
}
public function onRegistrationAdherent(UserEvent $event)
{
$user = $event->getUser();
//Set confirmation token (alike resetting password)
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
// this depends on requirements
$user->setPasswordRequestedAt(new \DateTime());
$this->userManager->updateUser($user);
$subject = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_NAME_SMALL).' : '.$this->translator->trans('Création de votre compte adhérent');
$mail = (new \Swift_Message($subject))
->setFrom($this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_NOTIF_EMAIL))
->setTo($user->getEmail())
->setBody(
$this->templating->render(
'email/admin_add_adherent.html.twig',
array(
'subject' => $subject,
'user' => $user,
'confirmationUrl' => $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), UrlGeneratorInterface::ABSOLUTE_URL),
)
),
'text/html'
);
$this->mailer->send($mail);
}
public function onFlux(FluxEvent $event)
{
foreach ($event->getFlux()->getUsersToNotify() as $user) {
if ($user instanceof User && $user->getAlertemailflux()) {
$this->sendMail($user, $event->getFlux());
} elseif ($user == 'siege') {
$users = $this->em->getRepository(User::class)->findByRole('ROLE_ADMIN_SIEGE');
foreach ($users as $userAdminSiege) {
if ($userAdminSiege->getAlertemailflux()) {
$this->sendMail($userAdminSiege, $event->getFlux());
}
}
}
}
}
private function sendMail(User $user, Flux $flux)
{
// @TODO => un mail différent en fonction du type de flux (et de l'utilisateur ?)
$subject = $this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_NAME_SMALL).' : ';
$mail = (new \Swift_Message($subject))
->setFrom($this->em->getRepository(GlobalParameter::class)->val(GlobalParameter::MLC_NOTIF_EMAIL))
->setTo($user->getEmail())
->setBody(
$this->templating->render(
'email/notification_flux.html.twig',
array(
'subject' => $subject,
'user' => $user,
'flux' => $flux
)
),
'text/html'
);
$this->mailer->send($mail);
}
// @TODO => notifications/emails differents suivant le type d'action
public function onCotisation(FluxEvent $event)
{
}
public function onTransaction(FluxEvent $event)
{
}
public function onTransfert(FluxEvent $event)
{
}
public function onDepot(FluxEvent $event)
{
}
public function onReconversion(FluxEvent $event)
{
}
public function onRetrait(FluxEvent $event)
{
}
public function onVente(FluxEvent $event)
{
}
}