]> 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 18b0ffc3b30d9b626a8437ac215acc7eb55f17d6..ebee1834eb064bbc4f0be95c21d57fe0960c5c1b 100644 (file)
@@ -7,7 +7,7 @@
  *
  * PHP version 5
  *
- * Copyright © 2020-2021 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-2021 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 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;
@@ -55,7 +55,7 @@ use Galette\Repository\PaymentTypes;
  * @name      ContributionsController
  * @package   Galette
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020-2021 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
@@ -63,6 +63,8 @@ use Galette\Repository\PaymentTypes;
 
 class ContributionsController extends CrudController
 {
+    use BatchList;
+
     // CRUD - Create
 
     /**
@@ -84,6 +86,18 @@ class ContributionsController extends CrudController
         string $type,
         Contribution $contrib
     ): Response {
+        $post = $request->getParsedBody();
+
+        // check for ajax mode
+        $ajax = false;
+        if (
+            ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest')
+            || isset($post['ajax'])
+            && $post['ajax'] == 'true'
+        ) {
+            $ajax = true;
+        }
+
         // contribution types
         $ct = new ContributionsTypes($this->zdb);
         $contributions_types = $ct->getList($type === Contribution::TYPE_FEE);
@@ -115,8 +129,9 @@ class ContributionsController extends CrudController
 
         // members
         $m = new Members();
-        $members = $m->getSelectizedMembers(
+        $members = $m->getDropdownMembers(
             $this->zdb,
+            $this->login,
             $contrib->member > 0 ? $contrib->member : null
         );
 
@@ -130,16 +145,17 @@ 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;
         $params['autocomplete'] = true;
+        $params['mode'] = ($ajax ? 'ajax' : '');
 
         // display page
         $this->view->render(
             $response,
-            'ajouter_contribution.tpl',
+            'pages/contribution_form.html.twig',
             $params
         );
         return $response;
@@ -180,7 +196,7 @@ class ContributionsController extends CrudController
             $contrib = new Contribution(
                 $this->zdb,
                 $this->login,
-                (count($cparams) > 0 ? $cparams : null)
+                $cparams
             );
 
             if (isset($cparams['adh'])) {
@@ -222,23 +238,23 @@ 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
         $this->view->render(
             $response,
-            'mass_choose_type.tpl',
+            '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;
@@ -256,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,
-            'mass_add_contribution.tpl',
+            '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,
@@ -317,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);
@@ -340,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(
@@ -379,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';
@@ -403,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');
@@ -421,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) {
@@ -442,9 +464,8 @@ class ContributionsController extends CrudController
         }
 
         if (!$this->login->isAdmin() && !$this->login->isStaff() && $value != $this->login->id) {
-            if ($value == 'all') {
-                $value = null;
-                $filters->filtre_cotis_children = $this->login->id;
+            if ($value === 'all' || empty($value)) {
+                $value = $this->login->id;
             } else {
                 $member = new Adherent(
                     $this->zdb,
@@ -458,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(
@@ -471,6 +492,22 @@ class ContributionsController extends CrudController
             $filters->filtre_cotis_children = $value;
         }
 
+        $class = '\\Galette\\Entity\\' . ucwords(trim($raw_type, 's'));
+        $contrib = new $class($this->zdb, $this->login);
+
+        if (!$contrib->canShow($this->login)) {
+            Analog::log(
+                'Trying to display contributions without appropriate ACLs',
+                Analog::WARNING
+            );
+            return $response
+                ->withStatus(301)
+                ->withHeader(
+                    'Location',
+                    $this->routeparser->urlFor('me')
+                );
+        }
+
         $class = '\\Galette\\Repository\\' . ucwords($raw_type);
         $contrib = new $class($this->zdb, $this->login, $filters);
         $contribs_list = $contrib->getList(true);
@@ -481,7 +518,7 @@ class ContributionsController extends CrudController
         }
 
         //assign pagination variables to the template and add pagination links
-        $filters->setSmartyPagination($this->router, $this->view->getSmarty());
+        $filters->setSmartyPagination($this->routeparser, $this->view);
 
         $tpl_vars = [
             'page_title'        => $raw_type === 'contributions' ?
@@ -495,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;
         }
@@ -513,51 +551,76 @@ class ContributionsController extends CrudController
             $tpl_vars['pmember'] = $member;
         }
 
+        // hide column action in ajax mode
+        if ($ajax === true) {
+            $tpl_vars['no_action'] = true;
+        }
+
         // display page
         $this->view->render(
             $response,
-            'gestion_' . $raw_type . '.tpl',
+            'pages/' . $raw_type . '_list.html.twig',
             $tpl_vars
         );
         return $response;
     }
 
+    /**
+     * List page for logged-in member
+     *
+     * @param Request  $request  PSR Request
+     * @param Response $response PSR Response
+     * @param string   $type     One of 'transactions' or 'contributions'
+     *
+     * @return Response
+     */
+    public function myList(Request $request, Response $response, string $type = null): Response
+    {
+        return $this->list(
+            $request->withQueryParams(
+                $request->getQueryParams() + [
+                    Adherent::PK => $this->login->id
+                ]
+            ),
+            $response,
+            null,
+            null,
+            $type
+        );
+    }
+
     /**
      * Filtering
      *
      * @param Request     $request  PSR Request
      * @param Response    $response PSR Response
-     * @param string|null $type     Contribution type
+     * @param string|null $type     One of 'transactions' or 'contributions'
      *
      * @return Response
      */
     public function filter(Request $request, Response $response, string $type = null): Response
     {
-        $raw_type = null;
-        switch ($type) {
-            case 'transactions':
-                $raw_type = 'transactions';
-                break;
-            case 'contributions':
-                $raw_type = 'contributions';
-                break;
+        $ajax = false;
+        $filter_name = 'filter_' . $type;
+        if ($request->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
+            $ajax = true;
+            $filter_name .= '_ajax';
         }
 
-        $type = 'filter_' . $raw_type;
         $post = $request->getParsedBody();
         $error_detected = [];
 
-        if ($this->session->$type !== null) {
-            $filters = $this->session->$type;
+        if ($this->session->$filter_name !== null) {
+            $filters = $this->session->$filter_name;
         } else {
-            $filter_class = '\\Galette\\Filters\\' . ucwords($raw_type) . 'List';
+            $filter_class = '\\Galette\\Filters\\' . ucwords($type) . 'List';
             $filters = new $filter_class();
         }
 
         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;
             }
 
@@ -567,16 +630,16 @@ class ContributionsController extends CrudController
                 $filters->show = $post['nbshow'];
             }
 
+            if (isset($post['date_field'])) {
+                $filters->date_field = $post['date_field'];
+            }
+
             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'];
                 }
             }
 
@@ -598,7 +661,7 @@ class ContributionsController extends CrudController
             }
         }
 
-        $this->session->$type = $filters;
+        $this->session->$filter_name = $filters;
 
         if (count($error_detected) > 0) {
             //report errors
@@ -612,7 +675,52 @@ class ContributionsController extends CrudController
 
         return $response
             ->withStatus(301)
-            ->withHeader('Location', $this->router->pathFor('contributions', ['type' => $raw_type]));
+            ->withHeader('Location', $this->routeparser->urlFor('contributions', ['type' => $type]));
+    }
+
+    /**
+     * Batch actions handler
+     *
+     * @param Request  $request  PSR Request
+     * @param Response $response PSR Response
+     * @param string   $type     One of 'transactions' or 'contributions'
+     *
+     * @return Response
+     */
+    public function handleBatch(Request $request, Response $response, string $type): Response
+    {
+        $filter_name = 'filter_' . $type;
+        $post = $request->getParsedBody();
+
+        if (isset($post['entries_sel'])) {
+            $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->routeparser->urlFor('csv-contributionslist', ['type' => $type]));
+            }
+
+            if (isset($post['delete'])) {
+                return $response
+                    ->withStatus(301)
+                    ->withHeader('Location', $this->routeparser->urlFor('removeContributions', ['type' => $type]));
+            }
+
+            throw new \RuntimeException('Does not know what to batch :(');
+        } else {
+            $this->flash->addMessage(
+                'error_detected',
+                _T("No contribution was selected, please check at least one.")
+            );
+
+            return $response
+                ->withStatus(301)
+                ->withHeader('Location', $this->routeparser->urlFor('contributions', ['type' => $type]));
+        }
     }
 
     // /CRUD - Read
@@ -647,7 +755,7 @@ class ContributionsController extends CrudController
                 );
                 return $response
                     ->withStatus(301)
-                    ->withHeader('Location', $this->router->pathFor(
+                    ->withHeader('Location', $this->routeparser->urlFor(
                         'contributions',
                         ['type' => 'contributions']
                     ));
@@ -686,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'];
@@ -725,13 +833,13 @@ class ContributionsController extends CrudController
             $error_detected = array_merge($error_detected, $valid);
         }
 
+        // send email to member
+        if (isset($post['mail_confirm']) && $post['mail_confirm'] == '1') {
+            $contrib->setSendmail(); //flag to send creation email
+        }
+
         //all goes well, we can proceed
         if (count($error_detected) == 0) {
-            // send email to member
-            if (isset($post['mail_confirm']) && $post['mail_confirm'] == '1') {
-                $contrib->setSendmail(); //flag to send creation email
-            }
-
             $store = $contrib->store();
             if ($store === true) {
                 $this->flash->addMessage(
@@ -755,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'
@@ -775,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) {
@@ -804,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']]);
     }
 
     /**
@@ -816,7 +924,7 @@ class ContributionsController extends CrudController
      */
     public function formUri(array $args)
     {
-        return $this->router->pathFor(
+        return $this->routeparser->urlFor(
             'doRemoveContribution',
             $args
         );
@@ -885,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'];
+    }
 }