]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/Controllers/Crud/ContributionsController.php
Disable events from mass changes; closes #1733
[galette.git] / galette / lib / Galette / Controllers / Crud / ContributionsController.php
index 6f319395575c43a2be09e887ce45f7657f2efdc7..ebee1834eb064bbc4f0be95c21d57fe0960c5c1b 100644 (file)
@@ -7,7 +7,7 @@
  *
  * PHP version 5
  *
- * Copyright © 2020-2022 The Galette Team
+ * Copyright © 2020-2023 The Galette Team
  *
  * This file is part of Galette (http://galette.tuxfamily.org).
  *
@@ -28,7 +28,7 @@
  * @package   Galette
  *
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020-2022 The Galette Team
+ * @copyright 2020-2023 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @link      http://galette.tuxfamily.org
  * @since     Available since 0.9.4dev - 2020-05-08
 
 namespace Galette\Controllers\Crud;
 
-use Galette\Filters\ContributionsList;
-use Throwable;
+use Galette\Features\BatchList;
 use Analog\Analog;
 use Galette\Controllers\CrudController;
-use Slim\Http\Request;
-use Slim\Http\Response;
+use Slim\Psr7\Request;
+use Slim\Psr7\Response;
 use Galette\Entity\Adherent;
 use Galette\Entity\Contribution;
 use Galette\Entity\Transaction;
@@ -56,7 +55,7 @@ use Galette\Repository\PaymentTypes;
  * @name      ContributionsController
  * @package   Galette
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020-2022 The Galette Team
+ * @copyright 2020-2023 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @link      http://galette.tuxfamily.org
  * @since     Available since 0.9.4dev - 2020-05-02
@@ -64,6 +63,8 @@ use Galette\Repository\PaymentTypes;
 
 class ContributionsController extends CrudController
 {
+    use BatchList;
+
     // CRUD - Create
 
     /**
@@ -90,7 +91,7 @@ class ContributionsController extends CrudController
         // check for ajax mode
         $ajax = false;
         if (
-            $request->isXhr()
+            ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
             || isset($post['ajax'])
             && $post['ajax'] == 'true'
         ) {
@@ -144,7 +145,7 @@ class ContributionsController extends CrudController
         }
 
         $ext_membership = '';
-        if ($contrib->isFee() || !isset($contrib) && $type === Contribution::TYPE_FEE) {
+        if ($contrib->isFee() || $type === Contribution::TYPE_FEE) {
             $ext_membership = $this->preferences->pref_membership_ext;
         }
         $params['pref_membership_ext'] = $ext_membership;
@@ -195,7 +196,7 @@ class ContributionsController extends CrudController
             $contrib = new Contribution(
                 $this->zdb,
                 $this->login,
-                (count($cparams) > 0 ? $cparams : null)
+                $cparams
             );
 
             if (isset($cparams['adh'])) {
@@ -237,7 +238,7 @@ class ContributionsController extends CrudController
         $filters = $this->session->filter_members;
         $data = [
             'id'            => $filters->selected,
-            'redirect_uri'  => $this->router->pathFor('members')
+            'redirect_uri'  => $this->routeparser->urlFor('members')
         ];
 
         // display page
@@ -245,15 +246,15 @@ class ContributionsController extends CrudController
             $response,
             'modals/mass_choose_contributions_type.html.twig',
             array(
-                'mode'          => $request->isXhr() ? 'ajax' : '',
+                'mode'          => ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : '',
                 'page_title'    => str_replace(
                     '%count',
                     count($data['id']),
                     _T('Mass add contribution on %count members')
                 ),
                 'data'          => $data,
-                'form_url'      => $this->router->pathFor('massAddContributions'),
-                'cancel_uri'    => $this->router->pathFor('members')
+                'form_url'      => $this->routeparser->urlFor('massAddContributions'),
+                'cancel_uri'    => $this->routeparser->urlFor('members')
             )
         );
         return $response;
@@ -271,32 +272,36 @@ class ContributionsController extends CrudController
     {
         $post = $request->getParsedBody();
         $filters = $this->session->filter_members;
-        $contribution = new Contribution($this->zdb, $this->login);
-
         $type = $post['type'];
+
+        $ct = new ContributionsTypes($this->zdb);
+        $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
+
+        $contribution = new Contribution(
+            $this->zdb,
+            $this->login,
+            ['type' => array_keys($contributions_types)[0]]
+        );
+
         $data = [
             'id'            => $filters->selected,
-            'redirect_uri'  => $this->router->pathFor('members'),
+            'redirect_uri'  => $this->routeparser->urlFor('members'),
             'type'          => $type
         ];
 
-        // contribution types
-        $ct = new ContributionsTypes($this->zdb);
-        $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
-
         // display page
         $this->view->render(
             $response,
             'modals/mass_add_contributions.html.twig',
             array(
-                'mode'          => $request->isXhr() ? 'ajax' : '',
+                'mode'          => ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') ? 'ajax' : '',
                 'page_title'    => str_replace(
                     '%count',
                     count($data['id']),
                     _T('Mass add contribution on %count members')
                 ),
-                'form_url'      => $this->router->pathFor('doMassAddContributions'),
-                'cancel_uri'    => $this->router->pathFor('members'),
+                'form_url'      => $this->routeparser->urlFor('doMassAddContributions'),
+                'cancel_uri'    => $this->routeparser->urlFor('members'),
                 'data'          => $data,
                 'contribution'  => $contribution,
                 'type'          => $type,
@@ -332,6 +337,7 @@ class ContributionsController extends CrudController
         foreach ($members_ids as $member_id) {
             $post[Adherent::PK] = (int)$member_id;
             $contrib = new Contribution($this->zdb, $this->login);
+            $contrib->disableEvents();
 
             // regular fields
             $valid = $contrib->check($post, $contrib->getRequired(), $disabled);
@@ -355,11 +361,11 @@ class ContributionsController extends CrudController
         }
 
         if (count($error_detected) == 0) {
-            $redirect_url = $this->router->pathFor('members');
+            $redirect_url = $this->routeparser->urlFor('members');
         } else {
             //something went wrong.
             //store entity in session
-            $redirect_url = $this->router->pathFor('massAddContributions');
+            $redirect_url = $this->routeparser->urlFor('massAddContributions');
             //report errors
             foreach ($error_detected as $error) {
                 $this->flash->addMessage(
@@ -394,14 +400,6 @@ class ContributionsController extends CrudController
         $ajax = false;
         $get = $request->getQueryParams();
 
-        if (
-            $request->isXhr()
-            || isset($get['ajax'])
-            && $get['ajax'] == 'true'
-        ) {
-            $ajax = true;
-        }
-
         switch ($type) {
             case 'transactions':
                 $raw_type = 'transactions';
@@ -418,13 +416,21 @@ class ContributionsController extends CrudController
                     ->withStatus(301)
                     ->withHeader(
                         'Location',
-                        $this->router->pathFor('me')
+                        $this->routeparser->urlFor('me')
                     );
         }
 
         $filter_name = 'filter_' . $raw_type;
+        if (
+            ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
+            || isset($get['ajax'])
+            && $get['ajax'] == 'true'
+        ) {
+            $ajax = true;
+            $filter_name .= '_ajax';
+        }
 
-        if (isset($this->session->$filter_name) && $ajax === false) {
+        if (isset($this->session->$filter_name)) {
             $filters = $this->session->$filter_name;
         } else {
             $filter_class = '\\Galette\\Filters\\' . ucwords($raw_type . 'List');
@@ -436,10 +442,11 @@ class ContributionsController extends CrudController
             $filters->filtre_cotis_adh = (int)$get[Adherent::PK];
         }
 
-        $filters->filtre_transactions = false;
-        if (isset($request->getQueryParams()['max_amount'])) {
-            $filters->filtre_transactions = true;
-            $filters->max_amount = (int)$request->getQueryParams()['max_amount'];
+        if ($type === 'contributions') {
+            if (isset($request->getQueryParams()['max_amount'])) {
+                $filters->filtre_transactions = true;
+                $filters->max_amount = (int)$request->getQueryParams()['max_amount'];
+            }
         }
 
         if ($option !== null) {
@@ -472,7 +479,7 @@ class ContributionsController extends CrudController
                 );
                 if (
                     !$member->hasParent() ||
-                    $member->hasParent() && $member->parent->id != $this->login->id
+                    $member->parent->id != $this->login->id
                 ) {
                     $value = $this->login->id;
                     Analog::log(
@@ -497,7 +504,7 @@ class ContributionsController extends CrudController
                 ->withStatus(301)
                 ->withHeader(
                     'Location',
-                    $this->router->pathFor('me')
+                    $this->routeparser->urlFor('me')
                 );
         }
 
@@ -511,7 +518,7 @@ class ContributionsController extends CrudController
         }
 
         //assign pagination variables to the template and add pagination links
-        $filters->setSmartyPagination($this->router, $this->view);
+        $filters->setSmartyPagination($this->routeparser, $this->view);
 
         $tpl_vars = [
             'page_title'        => $raw_type === 'contributions' ?
@@ -525,6 +532,7 @@ class ContributionsController extends CrudController
 
         if ($filters->filtre_cotis_adh != null) {
             $member = new Adherent($this->zdb);
+            $member->enableDep('children');
             $member->load($filters->filtre_cotis_adh);
             $tpl_vars['member'] = $member;
         }
@@ -581,7 +589,7 @@ class ContributionsController extends CrudController
         );
     }
 
-        /**
+    /**
      * Filtering
      *
      * @param Request     $request  PSR Request
@@ -592,7 +600,13 @@ class ContributionsController extends CrudController
      */
     public function filter(Request $request, Response $response, string $type = null): Response
     {
+        $ajax = false;
         $filter_name = 'filter_' . $type;
+        if ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
+            $ajax = true;
+            $filter_name .= '_ajax';
+        }
+
         $post = $request->getParsedBody();
         $error_detected = [];
 
@@ -604,9 +618,9 @@ class ContributionsController extends CrudController
         }
 
         if (isset($post['clear_filter'])) {
-            $filters->reinit();
+            $filters->reinit($ajax);
         } else {
-            if (isset($post['max_amount'])) {
+            if (!isset($post['max_amount'])) {
                 $filters->max_amount = null;
             }
 
@@ -621,15 +635,11 @@ class ContributionsController extends CrudController
             }
 
             if (isset($post['end_date_filter']) || isset($post['start_date_filter'])) {
-                try {
-                    if (isset($post['start_date_filter'])) {
-                        $filters->start_date_filter = $post['start_date_filter'];
-                    }
-                    if (isset($post['end_date_filter'])) {
-                        $filters->end_date_filter = $post['end_date_filter'];
-                    }
-                } catch (Throwable $e) {
-                    $error_detected[] = $e->getMessage();
+                if (isset($post['start_date_filter'])) {
+                    $filters->start_date_filter = $post['start_date_filter'];
+                }
+                if (isset($post['end_date_filter'])) {
+                    $filters->end_date_filter = $post['end_date_filter'];
                 }
             }
 
@@ -665,7 +675,7 @@ class ContributionsController extends CrudController
 
         return $response
             ->withStatus(301)
-            ->withHeader('Location', $this->router->pathFor('contributions', ['type' => $type]));
+            ->withHeader('Location', $this->routeparser->urlFor('contributions', ['type' => $type]));
     }
 
     /**
@@ -683,20 +693,21 @@ class ContributionsController extends CrudController
         $post = $request->getParsedBody();
 
         if (isset($post['entries_sel'])) {
-            $filters = $this->session->$filter_name ?? new ContributionsList();
+            $filter_class = '\\Galette\\Filters\\' . ucwords($type . 'List');
+            $filters = $this->session->$filter_name ?? new $filter_class();
             $filters->selected = $post['entries_sel'];
             $this->session->$filter_name = $filters;
 
             if (isset($post['csv'])) {
                 return $response
                     ->withStatus(301)
-                    ->withHeader('Location', $this->router->pathFor('csv-contributionslist', ['type' => $type]));
+                    ->withHeader('Location', $this->routeparser->urlFor('csv-contributionslist', ['type' => $type]));
             }
 
             if (isset($post['delete'])) {
                 return $response
                     ->withStatus(301)
-                    ->withHeader('Location', $this->router->pathFor('removeContributions'));
+                    ->withHeader('Location', $this->routeparser->urlFor('removeContributions', ['type' => $type]));
             }
 
             throw new \RuntimeException('Does not know what to batch :(');
@@ -708,7 +719,7 @@ class ContributionsController extends CrudController
 
             return $response
                 ->withStatus(301)
-                ->withHeader('Location', $this->router->pathFor('contributions', ['type' => $type]));
+                ->withHeader('Location', $this->routeparser->urlFor('contributions', ['type' => $type]));
         }
     }
 
@@ -744,7 +755,7 @@ class ContributionsController extends CrudController
                 );
                 return $response
                     ->withStatus(301)
-                    ->withHeader('Location', $this->router->pathFor(
+                    ->withHeader('Location', $this->routeparser->urlFor(
                         'contributions',
                         ['type' => 'contributions']
                     ));
@@ -783,16 +794,16 @@ class ContributionsController extends CrudController
     public function store(Request $request, Response $response, $action, string $type, $id = null): Response
     {
         $post = $request->getParsedBody();
-        $args = [
+        $url_args = [
             'action'    => $action,
             'type'      => $type
         ];
         if ($id !== null) {
-            $args['id'] = $id;
+            $url_args['id'] = $id;
         }
 
         if ($action == 'edit' && isset($post['btnreload'])) {
-            $redirect_url = $this->router->pathFor($action . 'Contribution', $args);
+            $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $url_args);
             $redirect_url .= '?' . Adherent::PK . '=' . $post[Adherent::PK] . '&' .
                 ContributionsTypes::PK . '=' . $post[ContributionsTypes::PK] . '&' .
                 'montant_cotis=' . $post['montant_cotis'];
@@ -852,16 +863,16 @@ class ContributionsController extends CrudController
             $this->session->contribution = null;
             if ($contrib->isTransactionPart() && $contrib->transaction->getMissingAmount() > 0) {
                 //new contribution
-                $redirect_url = $this->router->pathFor(
+                $redirect_url = $this->routeparser->urlFor(
                     'addContribution',
                     [
-                        'type'      => $post['contrib_type']
+                        'type'      => $post['contrib_type'] ?? $type
                     ]
                 ) . '?' . Transaction::PK . '=' . $contrib->transaction->id .
                 '&' . Adherent::PK . '=' . $contrib->member;
             } else {
                 //contributions list for member
-                $redirect_url = $this->router->pathFor(
+                $redirect_url = $this->routeparser->urlFor(
                     'contributions',
                     [
                         'type'      => 'contributions'
@@ -872,7 +883,7 @@ class ContributionsController extends CrudController
             //something went wrong.
             //store entity in session
             $this->session->contribution = $contrib;
-            $redirect_url = $this->router->pathFor($action . 'Contribution', $args);
+            $redirect_url = $this->routeparser->urlFor($action . 'Contribution', $url_args);
 
             //report errors
             foreach ($error_detected as $error) {
@@ -901,7 +912,7 @@ class ContributionsController extends CrudController
      */
     public function redirectUri(array $args)
     {
-        return $this->router->pathFor('contributions', ['type' => $args['type']]);
+        return $this->routeparser->urlFor('contributions', ['type' => $args['type']]);
     }
 
     /**
@@ -913,7 +924,7 @@ class ContributionsController extends CrudController
      */
     public function formUri(array $args)
     {
-        return $this->router->pathFor(
+        return $this->routeparser->urlFor(
             'doRemoveContribution',
             $args
         );
@@ -982,4 +993,16 @@ class ContributionsController extends CrudController
 
     // /CRUD - Delete
     // /CRUD
+
+    /**
+     * Get filter name in session
+     *
+     * @param array|null $args Route arguments
+     *
+     * @return string
+     */
+    public function getFilterName(array $args = null): string
+    {
+        return 'filter_' . $args['type'];
+    }
 }