3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Event listener for contributions
10 * Copyright © 2020 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 2020-08-25
37 namespace Galette\Events
;
40 use Galette\Core\GaletteMail
;
41 use Galette\Core\History
;
42 use Galette\Core\Login
;
43 use Galette\Core\Password
;
44 use Galette\Core\Preferences
;
45 use Galette\Entity\Adherent
;
46 use Galette\Entity\Contribution
;
47 use Galette\Entity\Texts
;
49 use League\Event\Event
;
50 use League\Event\ListenerAcceptorInterface
;
51 use League\Event\ListenerProviderInterface
;
52 use Slim\Flash\Messages
;
56 * Event listener for contributions
59 * @name MemberListener
61 * @author Johan Cwiklinski <johan@x-tnd.be>
62 * @copyright 2020 The Galette Team
63 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
64 * @link https://galette.eu
65 * @since Available since 2020-08-25
67 class ContribListener
implements ListenerProviderInterface
69 /** @var Preferences */
85 * @param Preferences $preferences Preferences instance
86 * @param Router $router Router instance
87 * @param History $history History instance
88 * @param Messages $flash Messages instance
89 * @param Login $login Login instance
90 * @param Db $zdb Db instance
92 public function __construct(
93 Preferences
$preferences,
100 $this->preferences
= $preferences;
101 $this->router
= $router;
102 $this->history
= $history;
103 $this->flash
= $flash;
104 $this->login
= $login;
109 * Set up contribution listeners
111 * @param ListenerAcceptorInterface $acceptor Listener
115 public function provideListeners(ListenerAcceptorInterface
$acceptor)
117 $acceptor->addListener(
119 function ($event, $contrib) {
120 $this->contributionAdded($event, $contrib);
126 * Contribution added listener
128 * @param Event $event Raised event
129 * @param Contribution $contrib Added contribution
133 public function contributionAdded(Event
$event, Contribution
$contrib)
136 '[' . get_class($this) . '] Event contribution.add emitted for #' . $contrib->id
,
140 $this->callPostContributionScript($contrib);
142 if ($contrib->sendEMail()) {
143 $this->sendContribEmail($contrib, true);
145 $this->sendAdminEmail($contrib, true);
149 * Get texts replacements array for member
151 * @param Contribution $contrib Contribution instance
152 * @param Adherent $member Member instance
156 private function getReplacements(Contribution
$contrib, Adherent
$member): array
160 'name_adh' => custom_html_entity_decode(
163 'firstname_adh' => custom_html_entity_decode(
166 'lastname_adh' => custom_html_entity_decode(
169 'mail_adh' => custom_html_entity_decode(
172 'login_adh' => custom_html_entity_decode(
175 'deadline' => custom_html_entity_decode(
178 'contrib_info' => custom_html_entity_decode(
181 'contrib_amount' => custom_html_entity_decode(
184 'contrib_type' => custom_html_entity_decode(
185 $contrib->type
->libelle
193 * Send account email to member
195 * @param Contribution $contrib Contribution
196 * @param boolean $new New contribution or editing existing one
200 private function sendContribEmail(Contribution
$contrib, $new)
202 if ($this->preferences
->pref_mail_method
== GaletteMail
::METHOD_DISABLED
) {
203 //if email has been disabled in the preferences, we should not be here ;
204 //we do not throw an error, just a simple warning that will be show later
205 $msg = _T("You asked Galette to send a confirmation email to the member, but email has been disabled in the preferences.");
206 $this->flash
->addMessage(
213 // Get member information
214 $member = new Adherent($this->zdb
);
215 $member->load($contrib->member
);
217 if ($member->getEmail() == '' && !$member->self_adh
) {
218 $this->flash
->addMessage(
220 _T("- You can't send a confirmation by email if the member hasn't got an address!")
228 $this->getReplacements($contrib, $member)
232 if (!$contrib->isCotis()) {
235 $mtxt = $texts->getTexts($text, $member->language
);
237 $mail = new GaletteMail($this->preferences
);
238 $mail->setSubject($texts->getSubject());
239 $mail->setRecipients(
241 $member->getEmail() => $member->sname
246 if (strpos($mtxt->tbody
, '{LINK_MEMBERCARD}') !== false) {
247 //member card link is present in mail model, let's add it
248 $links = new Links($this->zdb
);
249 if ($hash = $links->generateNewLink(Links
::TARGET_MEMBERCARD
, $contrib->member
)) {
250 $link_card = $this->preferences
->getURL() .
251 $this->router
->pathFor('directlink', ['hash' => $hash]);
256 if (strpos($mtxt->tbody
, '{LINK_CONTRIBPDF}') !== false) {
257 //contribution receipt link is present in mail model, let's add it
258 $links = new Links($this->zdb
);
259 $ltype = $contrib->type
->isExtension() ? Links
::TARGET_INVOICE
: Links
::TARGET_RECEIPT
;
260 if ($hash = $links->generateNewLink($ltype, $contrib->id
)) {
261 $link_pdf = $this->preferences
->getURL() .
262 $this->router
->pathFor('directlink', ['hash' => $hash]);
266 //set replacements, even if empty, to be sure.
267 $texts->setReplaces([
268 'link_membercard' => $link_card,
269 'link_contribpdf' => $link_pdf
272 $mail->setMessage($texts->getBody());
273 $sent = $mail->send();
278 array('/%name/', '/%email/'),
279 array($member->sname
, $member->getEmail()),
280 _T("Email sent to user %name (%email)")
285 array('/%name/', '/%email/'),
286 array($member->sname
, $member->getEmail()),
287 _T("A problem happened while sending contribution receipt to user %name (%email)")
289 $this->history
->add($txt);
290 $this->flash
->addMessage(
298 * Send new contribution email to admin
300 * @param Contribution $contrib Contribution
301 * @param boolean $new New contribution or editing existing one
305 private function sendAdminEmail(Contribution
$contrib, $new)
308 $this->preferences
->pref_mail_method
== GaletteMail
::METHOD_DISABLED
309 ||
!$this->preferences
->pref_bool_mailadh
310 ||
(!$new && $contrib->member
!= $this->login
->id
)
315 // Get member information
316 $member = new Adherent($this->zdb
);
317 $member->load($contrib->member
);
322 $this->getReplacements($contrib, $member)
325 // Sent email to admin if pref checked
326 // Get email text in database
328 if (!$contrib->isCotis()) {
329 $text = 'newdonation';
331 $texts->getTexts($text, $this->preferences
->pref_lang
);
333 $mail = new GaletteMail($this->preferences
);
334 $mail->setSubject($texts->getSubject());
337 foreach ($this->preferences
->vpref_email_newadh
as $pref_email) {
338 $recipients[$pref_email] = $pref_email;
340 $mail->setRecipients($recipients);
342 $mail->setMessage($texts->getBody());
343 $sent = $mail->send();
348 array('/%name/', '/%email/'),
349 array($member->sname
, $member->getEmail()),
350 _T("Email sent to admin for user %name (%email)")
355 array('/%name/', '/%email/'),
356 array($member->sname
, $member->getEmail()),
357 _T("A problem happened while sending to admin notification for user %name (%email) contribution")
359 $this->history
->add($txt);
360 $this->flash
->addMessage(
368 * Call post contribution script from Preferences
370 * @param Contribution $contrib Added contribution
374 private function callPostContributionScript($contrib)
376 //if an external script has been configured, we call it
377 if ($this->preferences
->pref_new_contrib_script
) {
378 $es = new \Galette\IO\
ExternalScript($this->preferences
);
379 $res = $contrib->executePostScript($es);
382 //send admin an email with all details
383 if ($this->preferences
->pref_mail_method
> GaletteMail
::METHOD_DISABLED
) {
384 $mail = new GaletteMail($this->preferences
);
386 _T("Post contribution script failed")
390 foreach ($this->preferences
->vpref_email_newadh
as $pref_email) {
391 $recipients[$pref_email] = $pref_email;
393 $mail->setRecipients($recipients);
395 $message = _T("The configured post contribution script has failed.");
396 $message .= "\n" . _T("You can find contribution information and script output below.");
400 $mail->setMessage($message);
401 $sent = $mail->send();
404 $txt = _T('Post contribution script has failed.');
405 $this->history
->add($txt, $message);
406 $warning_detected[] = $txt;
407 //Mails are disabled... We log (not safe, but)...
409 'Email to admin has not been sent. Here was the data: ' .
410 "\n" . print_r($res, true),
415 //Mails are disabled... We log (not safe, but)...
417 'Post contribution script has failed. Here was the data: ' .
418 "\n" . print_r($res, true),