]> git.agnieray.net Git - galette.git/commitdiff
Ensure events are emitted once the whole process is complete; closes #1496
authorJohan Cwiklinski <johan@x-tnd.be>
Sun, 25 Oct 2020 21:57:38 +0000 (22:57 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Thu, 29 Oct 2020 21:28:15 +0000 (22:28 +0100)
galette/lib/Galette/Entity/Adherent.php
galette/lib/Galette/Entity/Contribution.php
galette/lib/Galette/Entity/Transaction.php
galette/lib/Galette/Repository/Members.php

index c5a2b589d5980d79c025a7850929dac3d1ff336b..f69453a9574894c83fbc671ecec76bfa9f585bf0 100644 (file)
@@ -1353,6 +1353,7 @@ class Adherent
     public function store()
     {
         global $hist, $emitter;
+        $event = null;
 
         try {
             $values = array();
@@ -1457,7 +1458,7 @@ class Adherent
                     }
                     $success = true;
 
-                    $emitter->emit('member.add', $this);
+                    $event = 'member.add';
                 } else {
                     $hist->add(_T("Fail to add new member."));
                     throw new \Exception(
@@ -1497,7 +1498,7 @@ class Adherent
                 }
                 $success = true;
 
-                $emitter->emit('member.edit', $this);
+                $event = 'member.edit';
             }
 
             //dynamic fields
@@ -1505,6 +1506,10 @@ class Adherent
                 $success = $this->dynamicsStore();
             }
 
+            //send event at the end of process, once all has been stored
+            if ($event !== null) {
+                $emitter->emit($event, $this);
+            }
             return $success;
         } catch (\Exception $e) {
             Analog::log(
index 84334049df3be421162a3a55808d81895b6f3a3d..cf310f31cc246ca1a1e3b6b1084f0303d2ba3835 100644 (file)
@@ -561,6 +561,8 @@ class Contribution
     {
         global $hist, $emitter;
 
+        $event = null;
+
         if (count($this->errors) > 0) {
             throw new \RuntimeException(
                 'Existing errors prevents storing contribution: ' .
@@ -617,8 +619,7 @@ class Contribution
                         Adherent::getSName($this->zdb, $this->_member)
                     );
                     $success = true;
-
-                    $emitter->emit('contribution.add', $this);
+                    $event = 'contribution.add';
                 } else {
                     $hist->add(_T("Fail to add new contribution."));
                     throw new \Exception(
@@ -648,8 +649,7 @@ class Contribution
                     );
                 }
                 $success = true;
-
-                $emitter->emit('contribution.edit', $this);
+                $event = 'contribution.edit';
             }
             //update deadline
             if ($this->isCotis()) {
@@ -667,6 +667,12 @@ class Contribution
 
             $this->zdb->connection->commit();
             $this->_orig_amount = $this->_amount;
+
+            //send event at the end of process, once all has been stored
+            if ($event !== null) {
+                $emitter->emit($event, $this);
+            }
+
             return true;
         } catch (\Exception $e) {
             $this->zdb->connection->rollBack();
index 24b9cd56cc5a49a7d1da234848edd5249e98227c..1bc7eec6823f2efc4afb108768f9667555f377e4 100644 (file)
@@ -178,6 +178,8 @@ class Transaction
     {
         global $emitter;
 
+        $event = null;
+
         try {
             if ($transaction) {
                 $this->zdb->connection->beginTransaction();
@@ -206,6 +208,7 @@ class Transaction
             if ($transaction) {
                 $this->zdb->connection->commit();
             }
+
             $emitter->emit('transaction.remove', $this);
             return true;
         } catch (\Exception $e) {
@@ -406,8 +409,7 @@ class Transaction
                         Adherent::getSName($this->zdb, $this->_member)
                     );
                     $success = true;
-
-                    $emitter->emit('transaction.add', $this);
+                    $event = 'transaction.add';
                 } else {
                     $hist->add(_T("Fail to add new transaction."));
                     throw new \Exception(
@@ -430,8 +432,7 @@ class Transaction
                     );
                 }
                 $success = true;
-
-                $emitter->emit('transaction.edit', $this);
+                $event = 'transaction.edit';
             }
 
             //dynamic fields
@@ -440,6 +441,12 @@ class Transaction
             }
 
             $this->zdb->connection->commit();
+
+            //send event at the end of process, once all has been stored
+            if ($event !== null) {
+                $emitter->emit($event, $this);
+            }
+
             return true;
         } catch (\Exception $e) {
             $this->zdb->connection->rollBack();
index 062fd153fa74a8b555a090c5598dab0925912e02..faa80bccabf0133d95704c2a8c8526bfc39ffe28 100644 (file)
@@ -284,6 +284,7 @@ class Members
     {
         global $zdb, $hist, $emitter;
 
+        $processed = array();
         $list = array();
         if (is_numeric($ids)) {
             //we've got only one identifier
@@ -329,12 +330,12 @@ class Members
                         }
                     }
 
-                    $emitter->emit('member.remove', [
+                    $processed[] = [
                         'id_adh' => $member->id_adh,
                         'nom_adh' => $member->nom_adh,
                         'prenom_adh' => $member->prenom_adh,
                         'email_adh' => $member->email_adh
-                    ]);
+                    ];
                 }
 
                 //delete contributions
@@ -402,6 +403,10 @@ class Members
                 //commit all changes
                 $zdb->connection->commit();
 
+                foreach ($processed as $p) {
+                    $emitter->emit('member.remove', $p);
+                }
+
                 //add an history entry
                 $hist->add(
                     _T("Delete members cards, transactions and dues"),