Merge remote branch 'origin/platal-1.0.0'
[platal.git] / modules / xnetevents / xnetevents.inc.php
index ff82344..91bb45e 100644 (file)
@@ -90,8 +90,8 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
         $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
     }
 
-    $montants = XDB::fetchColumn('SELECT  montant
-                                    FROM  ' . $globals->money->mpay_tprefix . 'transactions AS t
+    $montants = XDB::fetchColumn('SELECT  amount
+                                    FROM  payment_transactions AS t
                                    WHERE  ref = {?} AND uid = {?}',
                                    $evt['paiement_id'], S::v('uid'));
     $evt['telepaid'] = 0;
@@ -117,14 +117,14 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $count =
     $money  = $evt['money'] && (function_exists('may_update')) && may_update();
     $pay_id = $evt['paiement_id'];
 
-    $append = $item_id ? XDB::foramt(' AND ep.item_id = {?}', $item_id) : '';
+    $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
     $query = XDB::fetchAllAssoc('uid', 'SELECT  ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
                                                 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
                                           FROM  group_event_participants AS ep
                                          WHERE  ep.eid = {?} AND nb > 0 ' . $append . '
                                       GROUP BY  ep.uid', $eid);
     $uf = new UserFilter(new PFC_True(), $tri);
-    $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), $count, $offset));
+    $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($count, $offset)));
     $tab = array();
     foreach ($users as $user) {
         $uid = $user->id();
@@ -144,8 +144,8 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $count =
         $u['adminpaid'] = $u['paid'];
         $u['montant'] = 0;
         if ($money && $pay_id) {
-            $montants = XDB::fetchColumn('SELECT  montant
-                                            FROM  ' . $globals->money->mpay_tprefix . 'transactions AS t
+            $montants = XDB::fetchColumn('SELECT  amount
+                                            FROM  payment_transactions AS t
                                            WHERE  ref = {?} AND uid = {?}',
                                          $pay_id, $uid);
             foreach ($montants as $m) {
@@ -173,32 +173,32 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $count =
 // }}}
 
 //  {{{ function subscribe_lists_event()
-function subscribe_lists_event($participate, $uid, $evt, $paid, $payment = null)
+/** Subscribes user to various event related mailing lists.
+ *
+ * @param $uid: user's id.
+ * @param evt: events data, in particular ids of the lists at stake.
+ * @param participate: indicates if the user takes part at the event or not;
+ *      -1 means he did not answer, 0 means no, and 1 means yes.
+ * @param paid: has the user already payed anything?
+ *      0 means no, a positive amount means yes.
+ * @param payment: is this function called from a payment page?
+ *      If true, only payment related lists should be updated.
+ */
+function subscribe_lists_event($uid, $evt, $participate, $paid, $payment = false)
 {
-    global $globals;
-    $page =& Platal::page();
-
     $participant_list  = $evt['participant_list'];
     $absent_list       = $evt['absent_list'];
     $unpayed_list      = $evt['booked_unpayed_list'];
     $payed_list        = $evt['payed_list'];
 
     $user = User::getSilent($uid);
-    if ($user) {
-        $email = $user->forlifeEmail();
-    } else {
-        $res = XDB::query("SELECT  email
-                             FROM  group_members
-                            WHERE  uid = {?} AND asso_id = {?}",
-                          $uid, $globals->asso('id'));
-        $email = $res->fetchOneCell();
-    }
+    $email = $user->forlifeEmail();
 
     function subscribe($list, $email)
     {
         if ($list && $email) {
-            XDB::execute("REPLACE INTO  virtual_redirect
-                                VALUES  ({?},{?})",
+            XDB::execute('REPLACE INTO  virtual_redirect
+                                VALUES  ({?}, {?})',
                          $list, $email);
         }
     }
@@ -206,31 +206,45 @@ function subscribe_lists_event($participate, $uid, $evt, $paid, $payment = null)
     function unsubscribe($list, $email)
     {
         if ($list && $email) {
-            XDB::execute("DELETE FROM  virtual_redirect
-                                WHERE  vid = {?} AND redirect = {?}",
+            XDB::execute('DELETE FROM  virtual_redirect
+                                WHERE  vid = {?} AND redirect = {?}',
                          $list, $email);
         }
     }
 
-    if (is_null($payment)) {
-        if (is_null($participate)) {
+    /** If $payment is not null, we do not retrieve the value of $participate,
+     * thus we do not alter participant and absent lists.
+     */
+    if ($payment === true) {
+        if ($paid > 0) {
+            unsubscribe($unpayed_list, $email);
+            subscribe($payed_list, $email);
+        }
+    } else {
+        switch ($participate) {
+          case -1:
             unsubscribe($participant_list, $email);
+            unsubscribe($unpayed_list, $email);
+            unsubscribe($payed_list, $email);
             subscribe($absent_list, $email);
-        } elseif ($participate) {
-            subscribe($participant_list, $email);
-            unsubscribe($absent_list, $email);
-        } else {
+            break;
+          case 0:
             unsubscribe($participant_list, $email);
             unsubscribe($absent_list, $email);
-        }
-    }
-    if ($paid > 0) {
-        unsubscribe($unpayed_list, $email);
-        subscribe($payed_list, $email);
-    } else {
-        unsubscribe($payed_list, $email);
-        if (!is_null($participate)) {
-            subscribe($unpayed_list, $email);
+            unsubscribe($unpayed_list, $email);
+            unsubscribe($payed_list, $email);
+            break;
+          case 1:
+            subscribe($participant_list, $email);
+            unsubscribe($absent_list, $email);
+            if ($paid > 0) {
+                unsubscribe($unpayed_list, $email);
+                subscribe($payed_list, $email);
+            } else {
+                subscribe($unpayed_list, $email);
+                unsubscribe($payed_list, $email);
+            }
+            break;
         }
     }
 }