]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/ContributionsController.php
Fix PDF conribution link in mails
[galette.git] / galette / lib / Galette / Controllers / Crud / ContributionsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette contributions controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
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 0.9.4dev - 2020-05-08
35 */
36
37 namespace Galette\Controllers\Crud;
38
39 use Galette\Controllers\CrudController;
40 use Slim\Http\Request;
41 use Slim\Http\Response;
42 use Galette\Entity\Adherent;
43 use Galette\Entity\Contribution;
44 use Galette\Entity\Transaction;
45 use Galette\Repository\Contributions;
46 use Galette\Repository\Transactions;
47 use Galette\Repository\Members;
48 use Galette\Entity\ContributionsTypes;
49 use Galette\Core\GaletteMail;
50 use Galette\Entity\Texts;
51 use Galette\IO\PdfMembersCards;
52 use Galette\Repository\PaymentTypes;
53 use Galette\Core\Links;
54 use Analog\Analog;
55
56 /**
57 * Galette contributions controller
58 *
59 * @category Controllers
60 * @name ContributionsController
61 * @package Galette
62 * @author Johan Cwiklinski <johan@x-tnd.be>
63 * @copyright 2020 The Galette Team
64 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
65 * @link http://galette.tuxfamily.org
66 * @since Available since 0.9.4dev - 2020-05-02
67 */
68
69 class ContributionsController extends CrudController
70 {
71 // CRUD - Create
72
73 /**
74 * Add/Edit page
75 *
76 * Only a few things changes in add and edit pages,
77 * boths methods will use this common one.
78 *
79 * @param Request $request PSR Request
80 * @param Response $response PSR Response
81 * @param array $args Request arguments
82 * @param Contribution $contrib Contribution instance
83 *
84 * @return Response
85 */
86 public function addEditPage(
87 Request $request,
88 Response $response,
89 array $args,
90 Contribution $contrib
91 ): Response {
92 // contribution types
93 $ct = new ContributionsTypes($this->zdb);
94 $contributions_types = $ct->getList($args['type'] === 'fee');
95
96 $disabled = array();
97
98 if (!is_int($contrib->id)) {
99 // initialiser la structure contribution à vide (nouvelle contribution)
100 $contribution['duree_mois_cotis'] = $this->preferences->pref_membership_ext;
101 }
102
103 // template variable declaration
104 $title = null;
105 if ($args['type'] === 'fee') {
106 $title = _T("Membership fee");
107 } else {
108 $title = _T("Donation");
109 }
110
111 if ($contrib->id != '') {
112 $title .= ' (' . _T("modification") . ')';
113 } else {
114 $title .= ' (' . _T("creation") . ')';
115 }
116
117 // required fields
118 $required = [
119 'id_type_cotis' => 1,
120 'id_adh' => 1,
121 'date_enreg' => 1,
122 'date_debut_cotis' => 1,
123 'date_fin_cotis' => $contrib->isCotis(),
124 'montant_cotis' => $contrib->isCotis() ? 1 : 0
125 ];
126
127 $params = [
128 'page_title' => $title,
129 'required' => $required,
130 'disabled' => $disabled,
131 'contribution' => $contrib,
132 'adh_selected' => $contrib->member,
133 'type' => $args['type']
134 ];
135
136 // contribution types
137 $params['type_cotis_options'] = $contributions_types;
138
139 // members
140 $m = new Members();
141 $members = $m->getSelectizedMembers(
142 $this->zdb,
143 isset($contrib) && $contrib->member > 0 ? $contrib->member : null
144 );
145
146 $params['members'] = [
147 'filters' => $m->getFilters(),
148 'count' => $m->getCount()
149 ];
150
151 if (count($members)) {
152 $params['members']['list'] = $members;
153 }
154
155 $ext_membership = '';
156 if (isset($contrib) && $contrib->isCotis() || !isset($contrib) && $args['type'] === 'fee') {
157 $ext_membership = $this->preferences->pref_membership_ext;
158 }
159 $params['pref_membership_ext'] = $ext_membership;
160 $params['autocomplete'] = true;
161
162 // display page
163 $this->view->render(
164 $response,
165 'ajouter_contribution.tpl',
166 $params
167 );
168 return $response;
169 }
170
171 /**
172 * Add page
173 *
174 * @param Request $request PSR Request
175 * @param Response $response PSR Response
176 * @param array $args Request arguments
177 *
178 * @return Response
179 */
180 public function add(Request $request, Response $response, array $args = []): Response
181 {
182 if ($this->session->contribution !== null) {
183 $contrib = $this->session->contribution;
184 $this->session->contribution = null;
185 } else {
186 $get = $request->getQueryParams();
187
188 $ct = new ContributionsTypes($this->zdb);
189 $contributions_types = $ct->getList($args['type'] === 'fee');
190
191 $cparams = ['type' => array_keys($contributions_types)[0]];
192
193 //member id
194 if (isset($get[Adherent::PK]) && $get[Adherent::PK] > 0) {
195 $cparams['adh'] = (int)$get[Adherent::PK];
196 }
197
198 //transaction id
199 if (isset($get[Transaction::PK]) && $get[Transaction::PK] > 0) {
200 $cparams['trans'] = $get[Transaction::PK];
201 }
202
203 $contrib = new Contribution(
204 $this->zdb,
205 $this->login,
206 (count($cparams) > 0 ? $cparams : null)
207 );
208
209 if (isset($cparams['adh'])) {
210 $contrib->member = $cparams['adh'];
211 }
212
213 if (isset($get['montant_cotis']) && $get['montant_cotis'] > 0) {
214 $contrib->amount = $get['montant_cotis'];
215 }
216 }
217
218 return $this->addEditPage($request, $response, $args, $contrib);
219 }
220
221 /**
222 * Add action
223 *
224 * @param Request $request PSR Request
225 * @param Response $response PSR Response
226 * @param array $args Request arguments
227 *
228 * @return Response
229 */
230 public function doAdd(Request $request, Response $response, array $args = []): Response
231 {
232 $args['action'] = 'add';
233 return $this->store($request, $response, $args);
234 }
235
236 // /CRUD - Create
237 // CRUD - Read
238
239 /**
240 * List page
241 *
242 * @param Request $request PSR Request
243 * @param Response $response PSR Response
244 * @param array $args Request arguments
245 *
246 * @return Response
247 */
248 public function list(Request $request, Response $response, array $args = []): Response
249 {
250 $ajax = false;
251 if (
252 $request->isXhr()
253 || isset($request->getQueryParams()['ajax'])
254 && $request->getQueryParams()['ajax'] == 'true'
255 ) {
256 $ajax = true;
257 }
258 $get = $request->getQueryParams();
259
260 $option = $args['option'] ?? null;
261 $value = $args['value'] ?? null;
262 $raw_type = null;
263
264 switch ($args['type']) {
265 case 'transactions':
266 $raw_type = 'transactions';
267 break;
268 case 'contributions':
269 $raw_type = 'contributions';
270 break;
271 }
272
273 $filter_name = 'filter_' . $raw_type;
274
275 if (isset($this->session->$filter_name) && $ajax === false) {
276 $filters = $this->session->$filter_name;
277 } else {
278 $filter_class = '\\Galette\\Filters\\' . ucwords($raw_type . 'List');
279 $filters = new $filter_class();
280 }
281
282 //member id
283 if (isset($get[Adherent::PK]) && $get[Adherent::PK] > 0) {
284 $filters->filtre_cotis_adh = (int)$get[Adherent::PK];
285 }
286
287 $filters->filtre_transactions = false;
288 if (isset($request->getQueryParams()['max_amount'])) {
289 $filters->filtre_transactions = true;
290 $filters->max_amount = (int)$request->getQueryParams()['max_amount'];
291 }
292
293 if ($option !== null) {
294 switch ($option) {
295 case 'page':
296 $filters->current_page = (int)$value;
297 break;
298 case 'order':
299 $filters->orderby = $value;
300 break;
301 case 'member':
302 if (
303 ($this->login->isAdmin()
304 || $this->login->isStaff())
305 ) {
306 if ($value == 'all') {
307 $filters->filtre_cotis_adh = null;
308 } else {
309 $filters->filtre_cotis_adh = $value;
310 }
311 }
312 break;
313 }
314 }
315
316 if (!$this->login->isAdmin() && !$this->login->isStaff()) {
317 $filters->filtre_cotis_adh = $this->login->id;
318 }
319
320 $class = '\\Galette\\Repository\\' . ucwords($raw_type);
321 $contrib = new $class($this->zdb, $this->login, $filters);
322 $contribs_list = $contrib->getList(true);
323
324 //store filters into session
325 if ($ajax === false) {
326 $this->session->$filter_name = $filters;
327 }
328
329 //assign pagination variables to the template and add pagination links
330 $filters->setSmartyPagination($this->router, $this->view->getSmarty());
331
332 $tpl_vars = [
333 'page_title' => $raw_type === 'contributions' ?
334 _T("Contributions management") : _T("Transactions management"),
335 'contribs' => $contrib,
336 'list' => $contribs_list,
337 'nb' => $contrib->getCount(),
338 'filters' => $filters,
339 'mode' => ($ajax === true ? 'ajax' : 'std')
340 ];
341
342 if ($filters->filtre_cotis_adh != null) {
343 $member = new Adherent($this->zdb);
344 $member->load($filters->filtre_cotis_adh);
345 $tpl_vars['member'] = $member;
346 }
347
348 // display page
349 $this->view->render(
350 $response,
351 'gestion_' . $raw_type . '.tpl',
352 $tpl_vars
353 );
354 return $response;
355 }
356
357 /**
358 * Filtering
359 *
360 * @param Request $request PSR Request
361 * @param Response $response PSR Response
362 * @param array $args Request arguments
363 *
364 * @return Response
365 */
366 public function filter(Request $request, Response $response, array $args = []): Response
367 {
368 $raw_type = null;
369 switch ($args['type']) {
370 case 'transactions':
371 $raw_type = 'transactions';
372 break;
373 case 'contributions':
374 $raw_type = 'contributions';
375 break;
376 }
377
378 $type = 'filter_' . $raw_type;
379 $post = $request->getParsedBody();
380 $error_detected = [];
381
382 if ($this->session->$type !== null) {
383 $filters = $this->session->$type;
384 } else {
385 $filter_class = '\\Galette\\Filters\\' . ucwords($raw_type) . 'List';
386 $filters = new $filter_class();
387 }
388
389 if (isset($post['clear_filter'])) {
390 $filters->reinit();
391 } else {
392 if (isset($post['max_amount'])) {
393 $filters->max_amount = null;
394 }
395
396 if (
397 (isset($post['nbshow']) && is_numeric($post['nbshow']))
398 ) {
399 $filters->show = $post['nbshow'];
400 }
401
402 if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
403 try {
404 if (isset($post['start_date_filter'])) {
405 $filters->start_date_filter = $post['start_date_filter'];
406 }
407 if (isset($post['end_date_filter'])) {
408 $filters->end_date_filter = $post['end_date_filter'];
409 }
410 } catch (\Exception $e) {
411 $error_detected[] = $e->getMessage();
412 }
413 }
414
415 if (isset($post['payment_type_filter'])) {
416 $ptf = (int)$post['payment_type_filter'];
417 $ptypes = new PaymentTypes(
418 $this->zdb,
419 $this->preferences,
420 $this->login
421 );
422 $ptlist = $ptypes->getList();
423 if (isset($ptlist[$ptf])) {
424 $filters->payment_type_filter = $ptf;
425 } elseif ($ptf == -1) {
426 $filters->payment_type_filter = null;
427 } else {
428 $error_detected[] = _T("- Unknown payment type!");
429 }
430 }
431 }
432
433 $this->session->$type = $filters;
434
435 if (count($error_detected) > 0) {
436 //report errors
437 foreach ($error_detected as $error) {
438 $this->flash->addMessage(
439 'error_detected',
440 $error
441 );
442 }
443 }
444
445 return $response
446 ->withStatus(301)
447 ->withHeader('Location', $this->router->pathFor('contributions', ['type' => $raw_type]));
448 }
449
450 // /CRUD - Read
451 // CRUD - Update
452
453 /**
454 * Edit page
455 *
456 * @param Request $request PSR Request
457 * @param Response $response PSR Response
458 * @param array $args Request arguments
459 *
460 * @return Response
461 */
462 public function edit(Request $request, Response $response, array $args = []): Response
463 {
464 if ($this->session->contribution !== null) {
465 $contrib = $this->session->contribution;
466 $this->session->contribution = null;
467 } else {
468 $contrib = new Contribution($this->zdb, $this->login, (int)$args['id']);
469 if ($contrib->id == '') {
470 //not possible to load contribution, exit
471 $this->flash->addMessage(
472 'error_detected',
473 str_replace(
474 '%id',
475 $args['id'],
476 _T("Unable to load contribution #%id!")
477 )
478 );
479 return $response
480 ->withStatus(301)
481 ->withHeader('Location', $this->router->pathFor(
482 'contributions',
483 ['type' => 'contributions']
484 ));
485 }
486 }
487
488 return $this->addEditPage($request, $response, $args, $contrib);
489 }
490
491 /**
492 * Edit action
493 *
494 * @param Request $request PSR Request
495 * @param Response $response PSR Response
496 * @param array $args Request arguments
497 *
498 * @return Response
499 */
500 public function doEdit(Request $request, Response $response, array $args = []): Response
501 {
502 $args['action'] = 'edit';
503 return $this->store($request, $response, $args);
504 }
505
506 /**
507 * Store contribution (new or existing)
508 *
509 * @param Request $request PSR Request
510 * @param Response $response PSR Response
511 * @param array $args Request arguments
512 *
513 * @return Response
514 */
515 public function store(Request $request, Response $response, array $args = []): Response
516 {
517 $post = $request->getParsedBody();
518 $action = $args['action'];
519
520 if ($action == 'edit' && isset($post['btnreload'])) {
521 $redirect_url = $this->router->pathFor($action . 'Contribution', $args);
522 $redirect_url .= '?' . Adherent::PK . '=' . $post[Adherent::PK] . '&' .
523 ContributionsTypes::PK . '=' . $post[ContributionsTypes::PK] . '&' .
524 'montant_cotis=' . $post['montant_cotis'];
525 return $response
526 ->withStatus(301)
527 ->withHeader('Location', $redirect_url);
528 }
529
530 $success_detected = [];
531 $error_detected = [];
532 $warning_detected = [];
533 $redirect_url = null;
534
535 $id_cotis = null;
536 if (isset($args['id'])) {
537 $id_cotis = $args['id'];
538 }
539
540 if ($this->session->contribution !== null) {
541 $contrib = $this->session->contribution;
542 $this->session->contribution = null;
543 } else {
544 if ($id_cotis === null) {
545 $contrib = new Contribution($this->zdb, $this->login);
546 } else {
547 $contrib = new Contribution($this->zdb, $this->login, (int)$id_cotis);
548 }
549 }
550
551 // flagging required fields for first step only
552 $required = [
553 'id_type_cotis' => 1,
554 'id_adh' => 1,
555 'date_enreg' => 1,
556 'montant_cotis' => 1, //TODO: not always required, see #196
557 'date_debut_cotis' => 1,
558 'date_fin_cotis' => ($args['type'] === 'fee')
559 ];
560 $disabled = [];
561
562 // regular fields
563 $valid = $contrib->check($post, $required, $disabled);
564 if ($valid !== true) {
565 $error_detected = array_merge($error_detected, $valid);
566 }
567
568 if (count($error_detected) == 0) {
569 //all goes well, we can proceed
570 $new = false;
571 if ($contrib->id == '') {
572 $new = true;
573 }
574
575 if (count($error_detected) == 0) {
576 $store = $contrib->store();
577 if ($store === true) {
578 $success_detected[] = _T('Contribution has been successfully stored');
579 //contribution has been stored :)
580 if ($new) {
581 //if an external script has been configured, we call it
582 if ($this->preferences->pref_new_contrib_script) {
583 $es = new \Galette\IO\ExternalScript($this->preferences);
584 $res = $contrib->executePostScript($es);
585
586 if ($res !== true) {
587 //send admin an email with all details
588 if ($this->preferences->pref_mail_method > GaletteMail::METHOD_DISABLED) {
589 $mail = new GaletteMail($this->preferences);
590 $mail->setSubject(
591 _T("Post contribution script failed")
592 );
593
594 $recipients = [];
595 foreach ($this->preferences->vpref_email_newadh as $pref_email) {
596 $recipients[$pref_email] = $pref_email;
597 }
598 $mail->setRecipients($recipients);
599
600 $message = _T("The configured post contribution script has failed.");
601 $message .= "\n" . _T("You can find contribution information and script output below.");
602 $message .= "\n\n";
603 $message .= $res;
604
605 $mail->setMessage($message);
606 $sent = $mail->send();
607
608 if (!$sent) {
609 $txt = _T('Post contribution script has failed.');
610 $this->history->add($txt, $message);
611 $warning_detected[] = $txt;
612 //Mails are disabled... We log (not safe, but)...
613 Analog::log(
614 'Email to admin has not been sent. Here was the data: ' .
615 "\n" . print_r($res, true),
616 Analog::ERROR
617 );
618 }
619 } else {
620 //Mails are disabled... We log (not safe, but)...
621 Analog::log(
622 'Post contribution script has failed. Here was the data: ' .
623 "\n" . print_r($res, true),
624 Analog::ERROR
625 );
626 }
627 }
628 }
629 }
630 } else {
631 //something went wrong :'(
632 $error_detected[] = _T("An error occurred while storing the contribution.");
633 }
634 }
635 }
636
637 if (count($error_detected) == 0) {
638 // Get member information
639 $adh = new Adherent($this->zdb);
640 $adh->load($contrib->member);
641
642 if ($this->preferences->pref_mail_method > GaletteMail::METHOD_DISABLED) {
643 $texts = new Texts(
644 $this->preferences,
645 $this->router,
646 array(
647 'name_adh' => custom_html_entity_decode($adh->sname),
648 'firstname_adh' => custom_html_entity_decode($adh->surname),
649 'lastname_adh' => custom_html_entity_decode($adh->name),
650 'mail_adh' => custom_html_entity_decode($adh->getEmail()),
651 'login_adh' => custom_html_entity_decode($adh->login),
652 'deadline' => custom_html_entity_decode($contrib->end_date),
653 'contrib_info' => custom_html_entity_decode($contrib->info),
654 'contrib_amount' => custom_html_entity_decode($contrib->amount),
655 'contrib_type' => custom_html_entity_decode($contrib->type->libelle)
656 )
657 );
658 if (
659 $new && isset($_POST['mail_confirm'])
660 && $_POST['mail_confirm'] == '1'
661 ) {
662 if (GaletteMail::isValidEmail($adh->getEmail())) {
663 $text = 'contrib';
664 if (!$contrib->isCotis()) {
665 $text = 'donation';
666 }
667 $mtxt = $texts->getTexts($text, $adh->language);
668
669 $mail = new GaletteMail($this->preferences);
670 $mail->setSubject($texts->getSubject());
671 $mail->setRecipients(
672 array(
673 $adh->getEmail() => $adh->sname
674 )
675 );
676
677 $link_card = '';
678 if (strpos($mtxt->tbody, '{LINK_MEMBERCARD}') !== false) {
679 //member card link is present in mail model, let's add it
680 $links = new Links($this->zdb);
681 if ($hash = $links->generateNewLink(Links::TARGET_MEMBERCARD, $contrib->member)) {
682 $link_card = $this->preferences->getURL() .
683 $this->router->pathFor('directlink', ['hash' => $hash]);
684 }
685 }
686
687 $link_pdf = '';
688 if (strpos($mtxt->tbody, '{LINK_CONTRIBPDF}') !== false) {
689 //contribution receipt link is present in mail model, let's add it
690 $links = new Links($this->zdb);
691 $ltype = $contrib->type->isExtension() ? Links::TARGET_INVOICE : Links::TARGET_RECEIPT;
692 if ($hash = $links->generateNewLink($ltype, $contrib->id)) {
693 $link_pdf = $this->preferences->getURL() .
694 $this->router->pathFor('directlink', ['hash' => $hash]);
695 }
696 }
697
698 //set replacements, even if empty, to be sure.
699 $texts->setReplaces([
700 'link_membercard' => $link_card,
701 'link_contribpdf' => $link_pdf
702 ]);
703
704 $mail->setMessage($texts->getBody());
705 $sent = $mail->send();
706
707 if ($sent) {
708 $this->history->add(
709 preg_replace(
710 array('/%name/', '/%email/'),
711 array($adh->sname, $adh->getEmail()),
712 _T("Email sent to user %name (%email)")
713 )
714 );
715 } else {
716 $txt = preg_replace(
717 array('/%name/', '/%email/'),
718 array($adh->sname, $adh->getEmail()),
719 _T("A problem happened while sending contribution receipt to user %name (%email)")
720 );
721 $this->history->add($txt);
722 $error_detected[] = $txt;
723 }
724 } else {
725 $txt = preg_replace(
726 array('/%name/', '/%email/'),
727 array($adh->sname, $adh->getEmail()),
728 _T("Trying to send an email to a member (%name) with an invalid address: %email")
729 );
730 $this->history->add($txt);
731 $warning_detected[] = $txt;
732 }
733 }
734
735 // Sent email to admin if pref checked
736 if ($new && $this->preferences->pref_bool_mailadh) {
737 // Get email text in database
738 $text = 'newcont';
739 if (!$contrib->isCotis()) {
740 $text = 'newdonation';
741 }
742 $texts->getTexts($text, $this->preferences->pref_lang);
743
744 $mail = new GaletteMail($this->preferences);
745 $mail->setSubject($texts->getSubject());
746
747 $recipients = [];
748 foreach ($this->preferences->vpref_email_newadh as $pref_email) {
749 $recipients[$pref_email] = $pref_email;
750 }
751 $mail->setRecipients($recipients);
752
753 $mail->setMessage($texts->getBody());
754 $sent = $mail->send();
755
756 if ($sent) {
757 $this->history->add(
758 preg_replace(
759 array('/%name/', '/%email/'),
760 array($adh->sname, $adh->getEmail()),
761 _T("Email sent to admin for user %name (%email)")
762 )
763 );
764 } else {
765 $txt = preg_replace(
766 array('/%name/', '/%email/'),
767 array($adh->sname, $adh->getEmail()),
768 _T("A problem happened while sending to admin notification for user %name (%email) contribution")
769 );
770 $this->history->add($txt);
771 $warning_detected[] = $txt;
772 }
773 }
774 }
775
776 if (count($success_detected) > 0) {
777 foreach ($success_detected as $success) {
778 $this->flash->addMessage(
779 'success_detected',
780 $success
781 );
782 }
783 }
784
785
786 if (count($warning_detected) > 0) {
787 foreach ($warning_detected as $warning) {
788 $this->flash->addMessage(
789 'warning_detected',
790 $warning
791 );
792 }
793 }
794
795 if (count($error_detected) == 0) {
796 if ($contrib->isTransactionPart() && $contrib->transaction->getMissingAmount() > 0) {
797 //new contribution
798 $redirect_url = $this->router->pathFor(
799 'addContribution',
800 [
801 'type' => $post['contrib_type']
802 ]
803 ) . '?' . Transaction::PK . '=' . $contrib->transaction->id .
804 '&' . Adherent::PK . '=' . $contrib->member;
805 } else {
806 //contributions list for member
807 $redirect_url = $this->router->pathFor(
808 'contributions',
809 [
810 'type' => 'contributions'
811 ]
812 ) . '?' . Adherent::PK . '=' . $contrib->member;
813 }
814 }
815 }
816
817 if (count($error_detected) > 0) {
818 //something went wrong.
819 //store entity in session
820 $this->session->contribution = $contrib;
821 $redirect_url = $this->router->pathFor($args['action'] . 'Contribution', $args);
822
823 //report errors
824 foreach ($error_detected as $error) {
825 $this->flash->addMessage(
826 'error_detected',
827 $error
828 );
829 }
830 } else {
831 $this->session->contribution = null;
832 if ($redirect_url === null) {
833 $redirect_url = $this->router->pathFor('contributions', ['type' => $args['type']]);
834 }
835 }
836
837 //redirect to calling action
838 return $response
839 ->withStatus(301)
840 ->withHeader('Location', $redirect_url);
841 }
842
843 // /CRUD - Update
844 // CRUD - Delete
845
846 /**
847 * Get redirection URI
848 *
849 * @param array $args Route arguments
850 *
851 * @return string
852 */
853 public function redirectUri(array $args = [])
854 {
855 return $this->router->pathFor('contributions', ['type' => $args['type']]);
856 }
857
858 /**
859 * Get form URI
860 *
861 * @param array $args Route arguments
862 *
863 * @return string
864 */
865 public function formUri(array $args = [])
866 {
867 return $this->router->pathFor(
868 'doRemoveContribution',
869 $args
870 );
871 }
872
873 /**
874 * Get confirmation removal page title
875 *
876 * @param array $args Route arguments
877 *
878 * @return string
879 */
880 public function confirmRemoveTitle(array $args = [])
881 {
882 $raw_type = null;
883
884 switch ($args['type']) {
885 case 'transactions':
886 $raw_type = 'transactions';
887 break;
888 case 'contributions':
889 $raw_type = 'contributions';
890 break;
891 }
892
893 if (isset($args['ids'])) {
894 return sprintf(
895 _T('Remove %1$s %2$s'),
896 count($args['ids']),
897 ($raw_type === 'contributions') ? _T('contributions') : _T('transactions')
898 );
899 } else {
900 return sprintf(
901 _T('Remove %1$s #%2$s'),
902 ($raw_type === 'contributions') ? _T('contribution') : _T('transaction'),
903 $args['id']
904 );
905 }
906 }
907
908 /**
909 * Remove object
910 *
911 * @param array $args Route arguments
912 * @param array $post POST values
913 *
914 * @return boolean
915 */
916 protected function doDelete(array $args, array $post)
917 {
918 $raw_type = null;
919 switch ($args['type']) {
920 case 'transactions':
921 $raw_type = 'transactions';
922 break;
923 case 'contributions':
924 $raw_type = 'contributions';
925 break;
926 }
927
928 $class = '\\Galette\Repository\\' . ucwords($raw_type);
929 $contribs = new $class($this->zdb, $this->login);
930 $rm = $contribs->remove($args['ids'] ?? $args['id'], $this->history);
931 return $rm;
932 }
933
934 // /CRUD - Delete
935 // /CRUD
936 }