Make group-NL related code safer by using methods of the Newsletter class instead...
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Fri, 1 Jul 2011 20:36:51 +0000 (22:36 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Fri, 1 Jul 2011 20:36:51 +0000 (22:36 +0200)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
include/newsletter.inc.php
modules/xnetgrp.php
modules/xnetnl.php

index 3359249..2f6ccca 100644 (file)
@@ -361,11 +361,26 @@ class NewsLetter
         }
         if (self::maySubscribe($user)) {
             XDB::execute('INSERT IGNORE INTO  newsletter_ins (nlid, uid, last, hash)
-                                      VALUES  ({?}, {?}, 0, hash)',
+                                      VALUES  ({?}, {?}, NULL, hash)',
                          $this->id, $user->id());
         }
     }
 
+    /** Subscribe a batch of users to a newsletter.
+     * This skips 'maySubscribe' test.
+     *
+     * @p $user_ids Array of user IDs to subscribe to the newsletter.
+     */
+    public function bulkSubscribe($user_ids)
+    {
+        // TODO: use a 'bulkMaySubscribe'.
+        XDB::execute('INSERT IGNORE INTO  newsletter_ins (nlid, uid, last, hash)
+                                  SELECT  {?}, a.uid, NULL, NULL
+                                    FROM  accounts AS a
+                                   WHERE  a.uid IN {?}',
+                     $this->id, $user_ids);
+    }
+
     /** Retrieve subscription state of a user
      * @p $user Target user; if null, use current user.
      * @return Boolean: true if the user has subscribed to the NL.
index 89477ee..9b04afc 100644 (file)
@@ -1278,30 +1278,24 @@ class XnetGrpModule extends PLModule
             }
 
             if ($globals->asso('has_nl')) {
+                $nl = NewsLetter::forGroup($globals->asso('shortname');
                 // Updates group's newsletter subscription.
                 if (Post::i('newsletter') == 1) {
-                    XDB::execute('INSERT IGNORE INTO  newsletter_ins (uid, nlid)
-                                              SELECT  {?}, id
-                                                FROM  newsletters
-                                               WHERE  group_id = {?}',
-                                 $user->id(), $globals->asso('id'));
+                    $nl->subscribe($user);
                 } else {
-                    XDB::execute('DELETE  ni
-                                    FROM  newsletter_ins AS ni
-                              INNER JOIN  newsletters    AS n  ON (n.id = ni.nlid)
-                                   WHERE  ni.uid = {?} AND n.group_id = {?}',
-                                 $user->id(), $globals->asso('id'));
+                    $nl->unsubscribe(null, $user->id);
                 }
             }
         }
 
         $res = XDB::rawFetchAllAssoc('SHOW COLUMNS FROM group_members LIKE \'position\'');
         $positions = str_replace(array('enum(', ')', '\''), '', $res[0]['Type']);
-        $nl_registered = XDB::fetchOneCell('SELECT  COUNT(ni.uid)
-                                              FROM  newsletter_ins AS ni
-                                        INNER JOIN  newsletters    AS n  ON (n.id = ni.nlid)
-                                             WHERE  ni.uid = {?} AND n.group_id = {?}',
-                                           $user->id(), $globals->asso('id'));
+        if ($globals->asso('has_nl')) {
+            $nl = NewsLetter::forGroup($globals->asso('shortname'));
+            $nl_registered = $nl->subscriptionState($user);
+        } else {
+            $nl_registered = false;
+        }
 
         $page->assign('user', $user);
         $page->assign('suggest', $this->suggest($user));
index a6fb168..b849193 100644 (file)
@@ -57,16 +57,12 @@ class XnetNlModule extends NewsletterModule
         if (Env::has('add_users')) {
             S::assert_xsrf_token();
 
-            XDB::execute('INSERT IGNORE INTO  newsletter_ins (uid, nlid)
-                                      SELECT  g.uid, n.id
-                                        FROM  group_members AS g
-                                  INNER JOIN  newsletters   AS n  ON (n.group_id = g.asso_id)
-                                       WHERE  g.uid IN {?} AND g.asso_id = {?}',
-                         array_keys(Env::v('add_users')), $globals->asso('id'));
+            $nl->bulkSubscribe(array_keys(Env::v('add_users')));
 
             $page->trigSuccess('Ajouts réalisés avec succès.');
         }
 
+        // TODO(x2006barrois): remove raw SQL query.
         $uids = XDB::fetchColumn('SELECT  DISTINCT(g.uid)
                                     FROM  group_members AS g
                                    WHERE  g.asso_id = {?} AND NOT EXISTS (SELECT  ni.*