4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Events
;
25 use Galette\Core\GaletteMail
;
26 use Galette\Core\History
;
27 use Galette\Core\Links
;
28 use Galette\Core\Login
;
29 use Galette\Core\Preferences
;
30 use Galette\Entity\Adherent
;
31 use Galette\Entity\Contribution
;
32 use Galette\Entity\Texts
;
34 use League\Event\ListenerRegistry
;
35 use League\Event\ListenerSubscriber
;
36 use Slim\Flash\Messages
;
37 use Slim\Routing\RouteParser
;
40 * Event listener for contributions
42 * @author Johan Cwiklinski <johan@x-tnd.be>
44 class ContribListener
implements ListenerSubscriber
46 private Preferences
$preferences;
47 private RouteParser
$routeparser;
48 private History
$history;
49 private Messages
$flash;
56 * @param Preferences $preferences Preferences instance
57 * @param RouteParser $routeparser RouteParser instance
58 * @param History $history History instance
59 * @param Messages $flash Messages instance
60 * @param Login $login Login instance
61 * @param Db $zdb Db instance
63 public function __construct(
64 Preferences
$preferences,
65 RouteParser
$routeparser,
71 $this->preferences
= $preferences;
72 $this->routeparser
= $routeparser;
73 $this->history
= $history;
74 $this->flash
= $flash;
75 $this->login
= $login;
80 * Set up contribution listeners
82 * @param ListenerRegistry $acceptor Listener
86 public function subscribeListeners(ListenerRegistry
$acceptor): void
88 $acceptor->subscribeTo(
90 function (GaletteEvent
$event): void
{
91 $this->contributionAdded($event->getObject());
97 * Contribution added listener
99 * @param Contribution $contrib Added contribution
103 public function contributionAdded(Contribution
$contrib): void
106 '[' . get_class($this) . '] Event contribution.add emitted for #' . $contrib->id
,
110 $this->callPostContributionScript($contrib);
112 if ($contrib->sendEMail()) {
113 $this->sendContribEmail($contrib, true);
115 $this->sendAdminEmail($contrib, true);
119 * Send account email to member
121 * @param Contribution $contrib Contribution
122 * @param boolean $new New contribution or editing existing one
126 private function sendContribEmail(Contribution
$contrib, bool $new): void
128 if ($this->preferences
->pref_mail_method
== GaletteMail
::METHOD_DISABLED
) {
129 //if email has been disabled in the preferences, we should not be here ;
130 //we do not throw an error, just a simple warning that will be show later
131 $msg = _T("You asked Galette to send a confirmation email to the member, but email has been disabled in the preferences.");
132 $this->flash
->addMessage(
139 // Get member information
140 $member = new Adherent($this->zdb
);
141 $member->load($contrib->member
);
143 if ($member->getEmail() == '' && !$member->self_adh
) {
144 $this->flash
->addMessage(
146 _T("- You can't send a confirmation by email if the member hasn't got an address!")
157 ->setContribution($contrib);
160 if (!$contrib->isFee()) {
163 $mtxt = $texts->getTexts($text, $member->language
);
165 $mail = new GaletteMail($this->preferences
);
166 $mail->setSubject($texts->getSubject());
167 $mail->setRecipients(
169 $member->getEmail() => $member->sname
174 if (strpos($mtxt->tbody
, '{LINK_MEMBERCARD}') !== false) {
175 //member card link is present in mail model, let's add it
176 $links = new Links($this->zdb
);
177 if ($hash = $links->generateNewLink(Links
::TARGET_MEMBERCARD
, $contrib->member
)) {
178 $link_card = $this->preferences
->getURL() .
179 $this->routeparser
->urlFor('directlink', ['hash' => $hash]);
182 $texts->setMemberCardLink($link_card);
185 if (strpos($mtxt->tbody
, '{LINK_CONTRIBPDF}') !== false) {
186 //contribution receipt link is present in mail model, let's add it
187 $links = new Links($this->zdb
);
188 $ltype = $contrib->type
->isExtension() ? Links
::TARGET_INVOICE
: Links
::TARGET_RECEIPT
;
189 if ($hash = $links->generateNewLink($ltype, $contrib->id
)) {
190 $link_pdf = $this->preferences
->getURL() .
191 $this->routeparser
->urlFor('directlink', ['hash' => $hash]);
194 $texts->setContribLink($link_pdf);
196 $mail->setMessage($texts->getBody());
197 $sent = $mail->send();
202 array('/%name/', '/%email/'),
203 array($member->sname
, $member->getEmail()),
204 _T("Email sent to user %name (%email)")
209 array('/%name/', '/%email/'),
210 array($member->sname
, $member->getEmail()),
211 _T("A problem happened while sending contribution receipt to user %name (%email)")
213 $this->history
->add($txt);
214 $this->flash
->addMessage(
222 * Send new contribution email to admin
224 * @param Contribution $contrib Contribution
225 * @param boolean $new New contribution or editing existing one
229 private function sendAdminEmail(Contribution
$contrib, bool $new): void
232 $this->preferences
->pref_mail_method
== GaletteMail
::METHOD_DISABLED
233 ||
!$this->preferences
->pref_bool_mailadh
234 ||
(!$new && $contrib->member
!= $this->login
->id
)
239 // Get member information
240 $member = new Adherent($this->zdb
);
241 $member->load($contrib->member
);
249 ->setContribution($contrib);
251 // Sent email to admin if pref checked
252 // Get email text in database
254 if (!$contrib->isFee()) {
255 $text = 'newdonation';
257 $texts->getTexts($text, $this->preferences
->pref_lang
);
259 $mail = new GaletteMail($this->preferences
);
260 $mail->setSubject($texts->getSubject());
263 foreach ($this->preferences
->vpref_email_newadh
as $pref_email) {
264 $recipients[$pref_email] = $pref_email;
266 $mail->setRecipients($recipients);
268 $mail->setMessage($texts->getBody());
269 $sent = $mail->send();
274 array('/%name/', '/%email/'),
275 array($member->sname
, $member->getEmail()),
276 _T("Email sent to admin for user %name (%email)")
281 array('/%name/', '/%email/'),
282 array($member->sname
, $member->getEmail()),
283 _T("A problem happened while sending to admin notification for user %name (%email) contribution")
285 $this->history
->add($txt);
286 $this->flash
->addMessage(
294 * Call post contribution script from Preferences
296 * @param Contribution $contrib Added contribution
300 private function callPostContributionScript(Contribution
$contrib): void
302 //if an external script has been configured, we call it
303 if ($this->preferences
->pref_new_contrib_script
) {
304 $es = new \Galette\IO\
ExternalScript($this->preferences
);
305 $res = $contrib->executePostScript($es);
308 //send admin an email with all details
309 if ($this->preferences
->pref_mail_method
> GaletteMail
::METHOD_DISABLED
) {
310 $mail = new GaletteMail($this->preferences
);
312 _T("Post contribution script failed")
316 foreach ($this->preferences
->vpref_email_newadh
as $pref_email) {
317 $recipients[$pref_email] = $pref_email;
319 $mail->setRecipients($recipients);
321 $message = _T("The configured post contribution script has failed.");
322 $message .= "\n" . _T("You can find contribution information and script output below.");
326 $mail->setMessage($message);
327 $sent = $mail->send();
330 $txt = _T('Post contribution script has failed.');
331 $this->history
->add($txt, $message);
332 $warning_detected[] = $txt;
333 //Mails are disabled... We log (not safe, but)...
335 'Email to admin has not been sent. Here was the data: ' .
336 "\n" . print_r($res, true),
341 //Mails are disabled... We log (not safe, but)...
343 'Post contribution script has failed. Here was the data: ' .
344 "\n" . print_r($res, true),