Adapts Xnet event aliases to new mail chain.
authorStéphane Jacob <sj@m4x.org>
Thu, 24 Feb 2011 17:54:31 +0000 (18:54 +0100)
committerStéphane Jacob <sj@m4x.org>
Sun, 27 Feb 2011 22:19:01 +0000 (23:19 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
configs/platal.ini
include/emails.inc.php
modules/payment.php
modules/xnetevents.php
modules/xnetevents/xnetevents.inc.php

index 4764c71..458ef35 100644 (file)
@@ -417,6 +417,27 @@ auth_baseurl = ""
 ; of an event are created.
 evts_domain  = ""
 
+; $globals->xnet->participant_list
+; Mailing list suffix used to contact event participants.
+participant_list = ""
+
+; $globals->xnet->absent_list
+; Mailing list suffix used to contact users who notified they won't attend to the event.
+absent_list = ""
+
+; $globals->xnet->unpayed_list
+; Mailing list suffix used to contact event participants who did not pay.
+unpayed_list = ""
+
+; $globals->xnet->payed_list
+; Mailing list suffix used to contact event participants who paid.
+payed_list = ""
+
+; $globals->xnet->
+; Event mailing lists suffixes. It must contain the 4 suffixes above, separated by a coma.
+event_lists = ""
+
+
 ; The merge section contains parameters about the merge's state.
 [Merge]
 
index 3af0096..e1750a7 100644 (file)
@@ -24,7 +24,7 @@ define('ERROR_INACTIVE_REDIRECTION', 2);
 define('ERROR_INVALID_EMAIL', 3);
 define('ERROR_LOOP_EMAIL', 4);
 
-function add_to_list_alias(User $user, $local_part, $domain)
+function add_to_list_alias(User $user, $local_part, $domain, $type = 'user')
 {
     Platal::assert($user !== null);
 
@@ -35,7 +35,7 @@ function add_to_list_alias(User $user, $local_part, $domain)
                  $local_part, $user->forlifeEmail(), $type, $domain);
 }
 
-function delete_from_list_alias(User $user, $local_part, $domain)
+function delete_from_list_alias(User $user, $local_part, $domain, $type = 'user')
 {
     Platal::assert($user !== null);
 
@@ -47,6 +47,17 @@ function delete_from_list_alias(User $user, $local_part, $domain)
                  $local_part, $domain, $user->forlifeEmail(), $type);
 }
 
+function update_list_alias(User $user, $former_email, $local_part, $domain, $type = 'user')
+{
+    Platal::assert($user !== null);
+
+    XDB::execute('UPDATE  email_virtual         AS v
+              INNER JOIN  email_virtual_domains AS d ON (v.domain = d.id)
+                     SET  v.redirect = {?}
+                   WHERE  v.redirect = {?} AND d.name = {?} AND v.email = {?} AND v.type = {?}',
+                 $user->forlifeEmail(), $former_email, $domain, $local_part, $type);
+}
+
 function list_alias_members($local_part, $domain)
 {
     $emails = XDB::fetchColumn('SELECT  DISTINCT(redirect)
index 52dd453..f068aff 100644 (file)
@@ -248,7 +248,7 @@ class PaymentModule extends PLModule
         if ($eid = $res->fetchOneCell()) {
             require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php';
             $evt = get_event_detail($eid);
-            subscribe_lists_event($uid, $evt, 1, $montant, true);
+            subscribe_lists_event($uid, $evt['short_name'], 1, $montant, true);
         }
 
         /* on genere le mail de confirmation */
@@ -345,7 +345,7 @@ class PaymentModule extends PLModule
             list($eid, $asso_id) = $res->fetchOneRow();
             require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php';
             $evt = get_event_detail($eid, false, $asso_id);
-            subscribe_lists_event($user->id(), $evt, 1, $amount, true);
+            subscribe_lists_event($user->id(), $evt['short_name'], 1, $amount, true);
         }
 
         /* on genere le mail de confirmation */
@@ -441,7 +441,7 @@ class PaymentModule extends PLModule
         if ($eid = $res->fetchOneCell()) {
             require_once dirname(__FILE__) . '/xnetevents/xnetevents.inc.php';
             $evt = get_event_detail($eid);
-            subscribe_lists_event($user->id(), $evt, 1, $montant, true);
+            subscribe_lists_event($user->id(), $evt['short_name'], 1, $montant, true);
         }
 
         /* on genere le mail de confirmation */
index 8af4c89..ba12305 100644 (file)
@@ -73,10 +73,9 @@ class XnetEventsModule extends PLModule
         if ($action == 'del') {
             // deletes the event mailing aliases
             if ($tmp[1]) {
-                foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
-                    XDB::execute("DELETE FROM  virtual
-                                        WHERE  type = 'evt' AND alias LIKE {?}",
-                                 $tmp[1] . $v . '%');
+                require_once 'emails.inc.php';
+                foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
+                    delete_list_alias($tmp[1] . $suffix, $globals->xnet->evts_domain, 'event');
                 }
             }
 
@@ -266,7 +265,7 @@ class XnetEventsModule extends PLModule
         }
         if ($updated !== false) {
             $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès.');
-            subscribe_lists_event(S::i('uid'), $evt, ($total > 0 ? 1 : 0), 0);
+            subscribe_lists_event(S::i('uid'), $evt['short_name'], ($total > 0 ? 1 : 0), 0);
 
             if ($evt['subscription_notification'] != 'nobody') {
                 $mailer = new PlMailer('xnetevents/subscription-notif.mail.tpl');
@@ -566,7 +565,7 @@ class XnetEventsModule extends PLModule
                                  SET paid = paid + {?}
                                WHERE uid = {?} AND eid = {?} AND item_id = 1",
                              $amount, $member->uid, $evt['eid']);
-                subscribe_lists_event($member->uid, $evt, 1, $amount);
+                subscribe_lists_event($member->uid, $evt['short_name'], 1, $amount);
             }
 
             // change the number of personns coming with a participant
@@ -598,10 +597,10 @@ class XnetEventsModule extends PLModule
                                         WHERE uid = {?} AND eid = {?}",
                                     $member->uid, $evt['eid']);
                     $u = 0;
-                    subscribe_lists_event($member->uid, $evt, -1, $paid);
+                    subscribe_lists_event($member->uid, $evt['short_name'], -1, $paid);
                 } else {
                     $u = $u['cnt'] ? $u['nb'] : null;
-                    subscribe_lists_event($member->uid, $evt, ($u > 0 ? 1 : 0), $paid);
+                    subscribe_lists_event($member->uid, $evt['short_name'], ($u > 0 ? 1 : 0), $paid);
                 }
             }
 
index fefc116..f329ad1 100644 (file)
@@ -27,30 +27,19 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
     if (is_null($asso_id)) {
         $asso_id = $globals->asso('id');
     }
-    $res = XDB::query('SELECT  SUM(nb) AS nb_tot, COUNT(DISTINCT ep.uid) AS nb, e.*,
-                               IF(e.deadline_inscription,
-                                     e.deadline_inscription >= LEFT(NOW(), 10),
-                                     1) AS inscr_open,
-                               LEFT(10, e.debut) AS start_day, LEFT(10, e.fin) AS last_day,
-                               LEFT(NOW(), 10) AS now,
-                               ei.titre, al.vid AS absent_list, pl.vid AS participant_list,
-                               pyl.vid AS payed_list, bl.vid AS booked_unpayed_list,
-                               e.subscription_notification
-                         FROM  group_events              AS e
-                   INNER JOIN  group_event_items        AS ei ON (e.eid = ei.eid)
-                    LEFT JOIN  group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
-                    LEFT JOIN  virtual AS al ON(al.type = \'evt\' AND al.alias = CONCAT(short_name, {?}))
-                    LEFT JOIN  virtual AS pl ON(pl.type = \'evt\' AND pl.alias = CONCAT(short_name, {?}))
-                    LEFT JOIN  virtual AS pyl ON(pyl.type = \'evt\' AND pyl.alias = CONCAT(short_name, {?}))
-                    LEFT JOIN  virtual AS bl ON(bl.type = \'evt\' AND bl.alias = CONCAT(short_name, {?}))
-                        WHERE  (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
-                     GROUP BY  ei.item_id',
-                   '-absents@'.$globals->xnet->evts_domain,
-                   '-participants@'.$globals->xnet->evts_domain,
-                   '-paye@' . $globals->xnet->evts_domain,
-                   '-participants-non-paye@' . $globals->xnet->evts_domain,
-                   $eid, $eid, $item_id ? $item_id : 1, $asso_id);
-    $evt = $res->fetchOneAssoc();
+    $evt = XDB::fetchOneAssoc('SELECT  SUM(nb) AS nb_tot, COUNT(DISTINCT ep.uid) AS nb, e.*,
+                                       IF(e.deadline_inscription,
+                                          e.deadline_inscription >= LEFT(NOW(), 10),
+                                          1) AS inscr_open,
+                                       LEFT(10, e.debut) AS start_day, LEFT(10, e.fin) AS last_day,
+                                       LEFT(NOW(), 10) AS now,
+                                       ei.titre, e.subscription_notification
+                                 FROM  group_events             AS e
+                           INNER JOIN  group_event_items        AS ei ON (e.eid = ei.eid)
+                            LEFT JOIN  group_event_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id)
+                                WHERE  (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
+                             GROUP BY  ei.item_id',
+                           $eid, $eid, $item_id ? $item_id : 1, $asso_id);
 
     if (!$evt) {
         return null;
@@ -178,7 +167,7 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $limit =
 /** 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 short_name: event's short_name, which corresponds to the beginning of the emails.
  * @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?
@@ -186,65 +175,48 @@ function get_event_participants(&$evt, $item_id, array $tri = array(), $limit =
  * @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)
+function subscribe_lists_event($uid, $short_name, $participate, $paid, $payment = false)
 {
-    $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);
-    $email = $user->forlifeEmail();
-
-    function subscribe($list, $email)
-    {
-        if ($list && $email) {
-            XDB::execute('INSERT IGNORE INTO  virtual_redirect
-                                      VALUES  ({?}, {?})',
-                         $list, $email);
-        }
-    }
+    global $globals;
+    require_once 'emails.inc.php';
 
-    function unsubscribe($list, $email)
-    {
-        if ($list && $email) {
-            XDB::execute('DELETE FROM  virtual_redirect
-                                WHERE  vid = {?} AND redirect = {?}',
-                         $list, $email);
-        }
+    if (is_null($short_name)) {
+        return;
     }
 
+    $user = User::getSilentWithUID($uid);
+
     /** 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);
+            delete_from_list_alias($user, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
+            add_to_list_alias($user, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
         }
     } else {
         switch ($participate) {
           case -1:
-            unsubscribe($participant_list, $email);
-            unsubscribe($unpayed_list, $email);
-            unsubscribe($payed_list, $email);
-            subscribe($absent_list, $email);
+            delete_from_list_alias($user, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
+            delete_from_list_alias($user, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
+            delete_from_list_alias($user, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
+            add_to_list_alias($user, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
             break;
           case 0:
-            unsubscribe($participant_list, $email);
-            unsubscribe($absent_list, $email);
-            unsubscribe($unpayed_list, $email);
-            unsubscribe($payed_list, $email);
+            delete_from_list_alias($user, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
+            delete_from_list_alias($user, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
+            delete_from_list_alias($user, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
+            delete_from_list_alias($user, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
             break;
           case 1:
-            subscribe($participant_list, $email);
-            unsubscribe($absent_list, $email);
+            add_to_list_alias($user, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
+            delete_from_list_alias($user, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
             if ($paid > 0) {
-                unsubscribe($unpayed_list, $email);
-                subscribe($payed_list, $email);
+                delete_from_list_alias($user, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
+                add_to_list_alias($user, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
             } else {
-                subscribe($unpayed_list, $email);
-                unsubscribe($payed_list, $email);
+                add_to_list_alias($user, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
+                delete_from_list_alias($user, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
             }
             break;
         }
@@ -256,6 +228,7 @@ function subscribe_lists_event($uid, $evt, $participate, $paid, $payment = false
 function event_change_shortname($page, $eid, $old, $new)
 {
     global $globals;
+    require_once 'emails.inc.php';
 
     if (is_null($old)) {
         $old = '';
@@ -292,13 +265,13 @@ function event_change_shortname($page, $eid, $old, $new)
 
     if ($old && $new) {
         // if had a previous shortname change the old lists
-        foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
-            $v .= $globals->xnet->evts_domain;
-            XDB::execute("UPDATE  virtual
-                             SET  alias = {?}
-                           WHERE  type = 'evt' AND alias = {?}",
-                         $new . $v, $old . $v);
+        foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
+            XDB::execute('UPDATE  email_virtual
+                             SET  email = {?}
+                           WHERE  type = \'event\' AND email = {?}',
+                         $new . $suffix, $old . $suffix);
         }
+
         return $new;
     }
 
@@ -306,52 +279,40 @@ function event_change_shortname($page, $eid, $old, $new)
         // if we have a first new short_name create the lists
         $lastid = array();
         $where = array(
-            '-participants@'          => 'ep.nb > 0',
-            '-paye@'                  => 'ep.paid > 0',
-            '-participants-non-paye@' => 'ep.nb > 0 AND ep.paid = 0'
+            $globals->xnet->participant_list => 'nb > 0',
+            $globals->xnet->payed_list       => 'paid > 0',
+            $globals->xnet->unpayed_list     => 'nb > 0 AND paid = 0'
         );
 
-        foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
-            XDB::execute("INSERT INTO  virtual
-                                  SET  type = 'evt', alias = {?}",
-                         $new . $v . $globals->xnet->evts_domain);
-
-            $lastid[$v] = XDB::insertId();
+        foreach (array($globals->xnet->participant_list, $globals->xnet->payed_list, $globals->xnet->unpayed_list) as $suffix) {
+            $uids = XDB::fetchColumn('SELECT  uid
+                                        FROM  group_event_participants
+                                       WHERE  eid = {?} AND ' . $where,
+                                     $eid);
+            $users = User::getBulkUsersWithUIDs($uids, null, null, false);
+            foreach ($users as $user) {
+                add_to_list_alias($user, $new . $suffix, $globals->xnet->evts_domain, 'event');
+            }
         }
-
-        foreach (array('-participants@', '-paye@', '-participants-non-paye@') as $v) {
-            XDB::execute("INSERT IGNORE INTO  virtual_redirect (
-                                      SELECT  {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
-                                        FROM  group_event_participants AS ep
-                                   LEFT JOIN  accounts AS a  ON (ep.uid = a.uid)
-                                   LEFT JOIN  aliases  AS al ON (al.uid = a.uid AND al.type = 'a_vie')
-                                       WHERE  ep.eid = {?} AND " . $where[$v] . "
-                                    GROUP BY  ep.uid)",
-                         $lastid[$v], '@' . $globals->mail->domain, $eid);
+        $uids = XDB::fetchColumn('SELECT  m.uid
+                                    FROM  group_members            AS m
+                               LEFT JOIN  group_event_participants AS e ON (e.uid = m.uid AND e.eid = {?})
+                                   WHERE  m.asso_id = {?} AND e.uid IS NULL',
+                                 $eid, $globals->asso('id'));
+        $users = User::getBulkUsersWithUIDs($uids, null, null, false);
+        foreach ($users as $user) {
+            add_to_list_alias($user, $new . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
         }
-        XDB::execute("INSERT IGNORE INTO  virtual_redirect (
-                                  SELECT  {?} AS vid, IF(al.alias IS NULL, a.email, CONCAT(al.alias, {?})) AS redirect
-                                    FROM  group_members AS m
-                               LEFT JOIN  accounts  AS a  ON (a.uid = m.uid)
-                               LEFT JOIN  aliases   AS al ON (al.uid = a.uid AND al.type = 'a_vie')
-                               LEFT JOIN  group_event_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
-                                   WHERE  m.asso_id = {?} AND ep.uid IS NULL
-                                GROUP BY  m.uid)",
-                     $lastid['-absents@'], '@' . $globals->mail->domain, $eid, $globals->asso('id'));
 
         return $new;
     }
 
     if ($old && !$new) {
         // if we delete the old short name, delete the lists
-        foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
-            $v .= $globals->xnet->evts_domain;
-            XDB::execute("DELETE  virtual, virtual_redirect
-                            FROM  virtual
-                       LEFT JOIN  virtual_redirect USING(vid)
-                           WHERE  virtual.alias = {?}",
-                         $infos['short_name'] . $v);
+        foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
+            delete_list_alias($old . $suffix, $globals->xnet->evts_domain);
         }
+
         return $new;
     }