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