]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/ContributionsController.php
Scrutinizer Auto-Fixes (#59)
[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") : _T("Transactions management"),
347 'contribs' => $contrib,
348 'list' => $contribs_list,
349 'nb' => $contrib->getCount(),
350 'filters' => $filters,
351 'mode' => ($ajax === true ? 'ajax' : 'std')
352 ];
353
354 if ($filters->filtre_cotis_adh != null) {
355 $member = new Adherent($this->zdb);
356 $member->load($filters->filtre_cotis_adh);
357 $tpl_vars['member'] = $member;
358 }
359
360 // display page
361 $this->view->render(
362 $response,
363 'gestion_' . $raw_type . '.tpl',
364 $tpl_vars
365 );
366 return $response;
367 }
368
369 /**
370 * Filtering
371 *
372 * @param Request $request PSR Request
373 * @param Response $response PSR Response
374 * @param array $args Request arguments
375 *
376 * @return Response
377 */
378 public function filter(Request $request, Response $response, array $args = []) :Response
379 {
380 $raw_type = null;
381 switch ($args['type']) {
382 case 'transactions':
383 $raw_type = 'transactions';
384 break;
385 case 'contributions':
386 $raw_type = 'contributions';
387 break;
388 }
389
390 $type = 'filter_' . $raw_type;
391 $post = $request->getParsedBody();
392 $error_detected = [];
393
394 if ($this->session->$type !== null) {
395 $filters = $this->session->$type;
396 } else {
397 $filter_class = '\\Galette\\Filters\\' . ucwords($raw_type) . 'List';
398 $filters = new $filter_class();
399 }
400
401 if (isset($post['clear_filter'])) {
402 $filters->reinit();
403 } else {
404 if (isset($post['max_amount'])) {
405 $filters->max_amount = null;
406 }
407
408 if ((isset($post['nbshow']) && is_numeric($post['nbshow']))
409 ) {
410 $filters->show = $post['nbshow'];
411 }
412
413 if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
414 try {
415 if (isset($post['start_date_filter'])) {
416 $field = _T("start date filter");
417 $filters->start_date_filter = $post['start_date_filter'];
418 }
419 if (isset($post['end_date_filter'])) {
420 $field = _T("end date filter");
421 $filters->end_date_filter = $post['end_date_filter'];
422 }
423 } catch (\Exception $e) {
424 $error_detected[] = $e->getMessage();
425 }
426 }
427
428 if (isset($post['payment_type_filter'])) {
429 $ptf = (int)$post['payment_type_filter'];
430 $ptypes = new PaymentTypes(
431 $this->zdb,
432 $this->preferences,
433 $this->login
434 );
435 $ptlist = $ptypes->getList();
436 if (isset($ptlist[$ptf])) {
437 $filters->payment_type_filter = $ptf;
438 } elseif ($ptf == -1) {
439 $filters->payment_type_filter = null;
440 } else {
441 $error_detected[] = _T("- Unknown payment type!");
442 }
443 }
444 }
445
446 $this->session->$type = $filters;
447
448 if (count($error_detected) > 0) {
449 //report errors
450 foreach ($error_detected as $error) {
451 $this->flash->addMessage(
452 'error_detected',
453 $error
454 );
455 }
456 }
457
458 return $response
459 ->withStatus(301)
460 ->withHeader('Location', $this->router->pathFor('contributions', ['type' => $raw_type]));
461 }
462
463 // /CRUD - Read
464 // CRUD - Update
465
466 /**
467 * Edit page
468 *
469 * @param Request $request PSR Request
470 * @param Response $response PSR Response
471 * @param array $args Request arguments
472 *
473 * @return Response
474 */
475 public function edit(Request $request, Response $response, array $args = []) :Response
476 {
477 if ($this->session->contribution !== null) {
478 $contrib = $this->session->contribution;
479 $this->session->contribution = null;
480 } else {
481 $contrib = new Contribution($this->zdb, $this->login, (int)$args['id']);
482 if ($contrib->id == '') {
483 //not possible to load contribution, exit
484 $this->flash->addMessage(
485 'error_detected',
486 str_replace(
487 '%id',
488 $args['id'],
489 _T("Unable to load contribution #%id!")
490 )
491 );
492 return $response
493 ->withStatus(301)
494 ->withHeader('Location', $this->router->pathFor(
495 'contributions',
496 ['type' => 'contributions']
497 ));
498 }
499 }
500
501 return $this->addEditPage($request, $response, $args, $contrib);
502 }
503
504 /**
505 * Edit action
506 *
507 * @param Request $request PSR Request
508 * @param Response $response PSR Response
509 * @param array $args Request arguments
510 *
511 * @return Response
512 */
513 public function doEdit(Request $request, Response $response, array $args = []) :Response
514 {
515 $args['action'] = 'edit';
516 return $this->store($request, $response, $args);
517 }
518
519 /**
520 * Store contribution (new or existing)
521 *
522 * @param Request $request PSR Request
523 * @param Response $response PSR Response
524 * @param array $args Request arguments
525 *
526 * @return Response
527 */
528 public function store(Request $request, Response $response, array $args = []) :Response
529 {
530 $post = $request->getParsedBody();
531 $action = $args['action'];
532
533 if ($action == 'edit' && isset($post['btnreload'])) {
534 $redirect_url = $this->router->pathFor($action . 'Contribution', $args);
535 $redirect_url .= '?' . Adherent::PK . '=' . $post[Adherent::PK] . '&' .
536 ContributionsTypes::PK . '=' . $post[ContributionsTypes::PK] . '&' .
537 'montant_cotis=' . $post['montant_cotis'];
538 return $response
539 ->withStatus(301)
540 ->withHeader('Location', $redirect_url);
541 }
542
543 $success_detected = [];
544 $error_detected = [];
545 $warning_detected = [];
546 $redirect_url = null;
547
548 $id_cotis = null;
549 if (isset($args['id'])) {
550 $id_cotis = $args['id'];
551 }
552
553 $id_adh = $post['id_adh'];
554
555 if ($this->session->contribution !== null) {
556 $contrib = $this->session->contribution;
557 $this->session->contribution = null;
558 } else {
559 if ($id_cotis === null) {
560 $contrib = new Contribution($this->zdb, $this->login);
561 } else {
562 $contrib = new Contribution($this->zdb, $this->login, (int)$id_cotis);
563 }
564 }
565
566 // flagging required fields for first step only
567 $required = [
568 'id_type_cotis' => 1,
569 'id_adh' => 1,
570 'date_enreg' => 1,
571 'montant_cotis' => 1, //TODO: not always required, see #196
572 'date_debut_cotis' => 1,
573 'date_fin_cotis' => ($args['type'] === 'fee')
574 ];
575 $disabled = [];
576
577 // regular fields
578 $valid = $contrib->check($post, $required, $disabled);
579 if ($valid !== true) {
580 $error_detected = array_merge($error_detected, $valid);
581 }
582
583 if (count($error_detected) == 0) {
584 //all goes well, we can proceed
585 $new = false;
586 if ($contrib->id == '') {
587 $new = true;
588 }
589
590 if (count($error_detected) == 0) {
591 $store = $contrib->store();
592 if ($store === true) {
593 $success_detected[] = _T('Contribution has been successfully stored');
594 //contribution has been stored :)
595 if ($new) {
596 //if an external script has been configured, we call it
597 if ($this->preferences->pref_new_contrib_script) {
598 $es = new \Galette\IO\ExternalScript($this->preferences);
599 $res = $contrib->executePostScript($es);
600
601 if ($res !== true) {
602 //send admin an email with all details
603 if ($this->preferences->pref_mail_method > GaletteMail::METHOD_DISABLED) {
604 $mail = new GaletteMail($this->preferences);
605 $mail->setSubject(
606 _T("Post contribution script failed")
607 );
608
609 $recipients = [];
610 foreach ($this->preferences->vpref_email_newadh as $pref_email) {
611 $recipients[$pref_email] = $pref_email;
612 }
613 $mail->setRecipients($recipients);
614
615 $message = _T("The configured post contribution script has failed.");
616 $message .= "\n" . _T("You can find contribution information and script output below.");
617 $message .= "\n\n";
618 $message .= $res;
619
620 $mail->setMessage($message);
621 $sent = $mail->send();
622
623 if (!$sent) {
624 $txt = _T('Post contribution script has failed.');
625 $this->history->add($txt, $message);
626 $warning_detected[] = $txt;
627 //Mails are disabled... We log (not safe, but)...
628 Analog::log(
629 'Email to admin has not been sent. Here was the data: ' .
630 "\n" . print_r($res, true),
631 Analog::ERROR
632 );
633 }
634 } else {
635 //Mails are disabled... We log (not safe, but)...
636 Analog::log(
637 'Post contribution script has failed. Here was the data: ' .
638 "\n" . print_r($res, true),
639 Analog::ERROR
640 );
641 }
642 }
643 }
644 }
645 } else {
646 //something went wrong :'(
647 $error_detected[] = _T("An error occurred while storing the contribution.");
648 }
649 }
650 }
651
652 if (count($error_detected) == 0) {
653 // Get member information
654 $adh = new Adherent($this->zdb);
655 $adh->load($contrib->member);
656
657 if ($this->preferences->pref_mail_method > GaletteMail::METHOD_DISABLED) {
658 $texts = new Texts(
659 $this->preferences,
660 $this->router,
661 array(
662 'name_adh' => custom_html_entity_decode($adh->sname),
663 'firstname_adh' => custom_html_entity_decode($adh->surname),
664 'lastname_adh' => custom_html_entity_decode($adh->name),
665 'mail_adh' => custom_html_entity_decode($adh->getEmail()),
666 'login_adh' => custom_html_entity_decode($adh->login),
667 'deadline' => custom_html_entity_decode($contrib->end_date),
668 'contrib_info' => custom_html_entity_decode($contrib->info),
669 'contrib_amount' => custom_html_entity_decode($contrib->amount),
670 'contrib_type' => custom_html_entity_decode($contrib->type->libelle)
671 )
672 );
673 if ($new && isset($_POST['mail_confirm'])
674 && $_POST['mail_confirm'] == '1'
675 ) {
676 if (GaletteMail::isValidEmail($adh->getEmail())) {
677 $text = 'contrib';
678 if (!$contrib->isCotis()) {
679 $text = 'donation';
680 }
681 $mtxt = $texts->getTexts($text, $adh->language);
682
683 $mail = new GaletteMail($this->preferences);
684 $mail->setSubject($texts->getSubject());
685 $mail->setRecipients(
686 array(
687 $adh->getEmail() => $adh->sname
688 )
689 );
690
691 $link_card = '';
692 if (strpos($mtxt->tbody, '{LINK_MEMBERCARD}') !== false) {
693 //member card link is present in mail model, let's add it
694 $links = new Links($this->zdb);
695 if ($hash = $links->generateNewLink(Links::TARGET_MEMBERCARD, $contrib->member)) {
696 $link_card = $this->preferences->getURL() .
697 $this->router->pathFor('directlink', ['hash' => $hash]);
698 }
699 }
700
701 $link_pdf = '';
702 if (strpos($mtxt->tbody, '{LINK_MEMBERCARD}') !== false) {
703 //contribution receipt link is present in mail model, let's add it
704 $links = new Links($this->zdb);
705 $ltype = $contrib->type->isExtension() ? Links::TARGET_INVOICE : Links::TARGET_RECEIPT;
706 if ($hash = $links->generateNewLink($ltype, $contrib->id)) {
707 $link_pdf = $this->preferences->getURL() .
708 $this->router->pathFor('directlink', ['hash' => $hash]);
709 }
710 }
711
712 //set replacements, even if empty, to be sure.
713 $texts->setReplaces([
714 'link_membercard' => $link_card,
715 'link_contribpdf' => $link_pdf
716 ]);
717
718 $mail->setMessage($texts->getBody());
719 $sent = $mail->send();
720
721 if ($sent) {
722 $this->history->add(
723 preg_replace(
724 array('/%name/', '/%email/'),
725 array($adh->sname, $adh->getEmail()),
726 _T("Email sent to user %name (%email)")
727 )
728 );
729 } else {
730 $txt = preg_replace(
731 array('/%name/', '/%email/'),
732 array($adh->sname, $adh->getEmail()),
733 _T("A problem happened while sending contribution receipt to user %name (%email)")
734 );
735 $this->history->add($txt);
736 $error_detected[] = $txt;
737 }
738 } else {
739 $txt = preg_replace(
740 array('/%name/', '/%email/'),
741 array($adh->sname, $adh->getEmail()),
742 _T("Trying to send an email to a member (%name) with an invalid address: %email")
743 );
744 $this->history->add($txt);
745 $warning_detected[] = $txt;
746 }
747 }
748
749 // Sent email to admin if pref checked
750 if ($new && $this->preferences->pref_bool_mailadh) {
751 // Get email text in database
752 $text = 'newcont';
753 if (!$contrib->isCotis()) {
754 $text = 'newdonation';
755 }
756 $mtxt = $texts->getTexts($text, $this->preferences->pref_lang);
757
758 $mail = new GaletteMail($this->preferences);
759 $mail->setSubject($texts->getSubject());
760
761 $recipients = [];
762 foreach ($this->preferences->vpref_email_newadh as $pref_email) {
763 $recipients[$pref_email] = $pref_email;
764 }
765 $mail->setRecipients($recipients);
766
767 $mail->setMessage($texts->getBody());
768 $sent = $mail->send();
769
770 if ($sent) {
771 $this->history->add(
772 preg_replace(
773 array('/%name/', '/%email/'),
774 array($adh->sname, $adh->getEmail()),
775 _T("Email sent to admin for user %name (%email)")
776 )
777 );
778 } else {
779 $txt = preg_replace(
780 array('/%name/', '/%email/'),
781 array($adh->sname, $adh->getEmail()),
782 _T("A problem happened while sending to admin notification for user %name (%email) contribution")
783 );
784 $this->history->add($txt);
785 $warning_detected[] = $txt;
786 }
787 }
788 }
789
790 if (count($success_detected) > 0) {
791 foreach ($success_detected as $success) {
792 $this->flash->addMessage(
793 'success_detected',
794 $success
795 );
796 }
797 }
798
799
800 if (count($warning_detected) > 0) {
801 foreach ($warning_detected as $warning) {
802 $this->flash->addMessage(
803 'warning_detected',
804 $warning
805 );
806 }
807 }
808
809 if (count($error_detected) == 0) {
810 if ($contrib->isTransactionPart() && $contrib->transaction->getMissingAmount() > 0) {
811 //new contribution
812 $redirect_url = $this->router->pathFor(
813 'addContribution',
814 [
815 'type' => $post['contrib_type']
816 ]
817 ) . '?' . Transaction::PK . '=' . $contrib->transaction->id .
818 '&' . Adherent::PK . '=' . $contrib->member;
819 } else {
820 //contributions list for member
821 $redirect_url = $this->router->pathFor(
822 'contributions',
823 [
824 'type' => 'contributions'
825 ]
826 ) . '?' . Adherent::PK . '=' . $contrib->member;
827 }
828 }
829 }
830
831 if (count($error_detected) > 0) {
832 //something went wrong.
833 //store entity in session
834 $this->session->contribution = $contrib;
835 $redirect_url = $this->router->pathFor($args['action'] . 'Contribution', $args);
836
837 //report errors
838 foreach ($error_detected as $error) {
839 $this->flash->addMessage(
840 'error_detected',
841 $error
842 );
843 }
844 } else {
845 $this->session->contribution = null;
846 if ($redirect_url === null) {
847 $redirect_url = $this->router->pathFor('contributions', ['type' => $args['type']]);
848 }
849 }
850
851 //redirect to calling action
852 return $response
853 ->withStatus(301)
854 ->withHeader('Location', $redirect_url);
855 }
856
857 // /CRUD - Update
858 // CRUD - Delete
859
860 /**
861 * Get redirection URI
862 *
863 * @param array $args Route arguments
864 *
865 * @return string
866 */
867 public function redirectUri(array $args = [])
868 {
869 return $this->router->pathFor('contributions', ['type' => $args['type']]);
870 }
871
872 /**
873 * Get form URI
874 *
875 * @param array $args Route arguments
876 *
877 * @return string
878 */
879 public function formUri(array $args = [])
880 {
881 return $this->router->pathFor(
882 'doRemoveContribution',
883 $args
884 );
885 }
886
887 /**
888 * Get confirmation removal page title
889 *
890 * @param array $args Route arguments
891 *
892 * @return string
893 */
894 public function confirmRemoveTitle(array $args = [])
895 {
896 $raw_type = null;
897
898 switch ($args['type']) {
899 case 'transactions':
900 $raw_type = 'transactions';
901 break;
902 case 'contributions':
903 $raw_type = 'contributions';
904 break;
905 }
906
907 if (isset($args['ids'])) {
908 return sprintf(
909 _T('Remove %1$s %2$s'),
910 count($args['ids']),
911 ($raw_type === 'contributions') ? _T('contributions') : _T('transactions')
912 );
913 } else {
914 return sprintf(
915 _T('Remove %1$s #%2$s'),
916 ($raw_type === 'contributions') ? _T('contribution') : _T('transaction'),
917 $args['id']
918 );
919 }
920 }
921
922 /**
923 * Remove object
924 *
925 * @param array $args Route arguments
926 * @param array $post POST values
927 *
928 * @return boolean
929 */
930 protected function doDelete(array $args, array $post)
931 {
932 $raw_type = null;
933 switch ($args['type']) {
934 case 'transactions':
935 $raw_type = 'transactions';
936 break;
937 case 'contributions':
938 $raw_type = 'contributions';
939 break;
940 }
941
942 $class = '\\Galette\Repository\\' . ucwords($raw_type);
943 $contribs = new $class($this->zdb, $this->login);
944 $rm = $contribs->remove($args['ids'] ?? $args['id'], $this->history);
945 return $rm;
946 }
947
948 // /CRUD - Delete
949 // /CRUD
950 }