Enables axletter mailings to a subset of subscribers
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Sat, 13 Jun 2009 22:45:39 +0000 (00:45 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Sat, 27 Jun 2009 22:57:23 +0000 (00:57 +0200)
* Creation of an auxiliary table, axletter_subset, which contains the list of user_ids
    to which letter shall be sent
* When sending email, sent only to those in that list and in the promotion range and who
    have subscribed to the letter
* When creating the letter, it is possible to give those mail adresses, which are internally
    converted to X.org user_ids
* No warnings when some adresses aren't recognized, ...

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
modules/axletter.php
modules/axletter/axletter.inc.php
templates/axletter/edit.tpl
upgrade/0.10.1/02_axletter.sql [new file with mode: 0644]

index ea4d2f4..4dd6b0c 100644 (file)
@@ -90,6 +90,8 @@ class AXLetterModule extends PLModule
         $signature  = trim(Post::v('signature'));
         $promo_min  = Post::i('promo_min');
         $promo_max  = Post::i('promo_max');
+        $subset     = Post::b('subset_to');
+        $subset_to  = preg_split("/ *[ ,;\:\n\r]+ */", Post::v('subset_to'));
         $echeance   = Post::has('echeance_date') ?
               preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('echeance_date')) . ' ' . Post::v('echeance_time')
             : Post::v('echeance');
@@ -100,6 +102,10 @@ class AXLetterModule extends PLModule
             $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
             if ($res->numRows()) {
                 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
+                if ($subset) {
+                    $res = XDB::query('SELECT email FROM axletter_subset WHERE letter_id = {?}', $id);
+                    $subset_to = $res->fetchColumn();
+                }
                 $saved = true;
             } else  {
                 XDB::execute("INSERT INTO axletter SET id = NULL");
@@ -129,6 +135,10 @@ class AXLetterModule extends PLModule
                 $page->trigError("L'intervalle de promotions n'est pas valide");
                 Post::kill('valid');
             }
+            if ($subset && !count($subset_to)) {
+                $page->trigError("La liste d'adresses mails sélectionnée est vide");
+                Post::kill('valid');
+            }
             if (empty($short_name)) {
                 $page->trigError("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
                 Post::kill('valid');
@@ -151,15 +161,25 @@ class AXLetterModule extends PLModule
               case 'Aperçu':
                 $this->load('axletter.inc.php');
                 $al = new AXLetter(array($id, $short_name, $subject, $title, $body, $signature,
-                                         $promo_min, $promo_max, $echeance, 0, 'new'));
+                                         $promo_min, $promo_max, $subset, $echeance, 0, 'new'));
                 $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
                 break;
 
               case 'Confirmer':
                 XDB::execute("REPLACE INTO  axletter
                                        SET  id = {?}, short_name = {?}, subject = {?}, title = {?}, body = {?},
-                                            signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}",
-                             $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance);
+                                            signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}, subset = {?}",
+                             $id, $short_name, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance, $subset);
+                if ($subset) {
+                    XDB::execute('DELETE FROM axletter_subset WHERE letter_id = {?}', $id);
+                    foreach ($subset_to as $email) {
+                //        $email = trim($email);
+                        $uid = $this->idFromMail(array('email' => $email));
+                        if ($uid) {
+                            XDB::execute('INSERT INTO axletter_subset SET letter_id = {?}, user_id = {?}, email = {?}', $id, $uid, $email);
+                        }
+                    }
+                }
                 if (!$saved) {
                     global $globals;
                     $mailer = new PlMailer();
@@ -201,6 +221,8 @@ class AXLetterModule extends PLModule
         $page->assign('signature', $signature);
         $page->assign('promo_min', $promo_min);
         $page->assign('promo_max', $promo_max);
+        $page->assign('subset_to', implode("\n", $subset_to));
+        $page->assign('subset', $subset);
         $page->assign('echeance', $echeance);
         $page->assign('echeance_date', $echeance_date);
         $page->assign('echeance_time', $echeance_time);
index 924447a..3305381 100644 (file)
@@ -27,6 +27,7 @@ class AXLetter extends MassMailer
     public $_signature;
     public $_promo_min;
     public $_promo_max;
+    public $_subset;
     public $_echeance;
     public $_date;
     public $_bits;
@@ -54,7 +55,7 @@ class AXLetter extends MassMailer
         }
         list($this->_id, $this->_shortname, $this->_title_mail, $this->_title,
              $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max,
-             $this->_echeance, $this->_date, $this->_bits) = $id;
+             $this->_subset, $this->_echeance, $this->_date, $this->_bits) = $id;
         if ($this->_date == '0000-00-00') {
             $this->_date = 0;
         }
@@ -107,6 +108,7 @@ class AXLetter extends MassMailer
                         IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
                         IF(ni.user_id = 0, ni.hash, 0) AS hash
                   FROM  axletter_ins  AS ni
+             {$this->subsetJoin()}
              LEFT JOIN  auth_user_md5   AS u  USING(user_id)
              LEFT JOIN  auth_user_quick AS q  ON(q.user_id = u.user_id)
              LEFT JOIN  aliases         AS a  ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
@@ -185,6 +187,14 @@ class AXLetter extends MassMailer
         return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
     }
 
+    protected function subsetJoin()
+    {
+        if ($this->_subset) {
+            return "INNER JOIN axletter_subset AS c ON (c.letter_id = {$this->_id} AND ni.user_id = c.user_id)";
+        }
+        // TODO : force use of the adresses given by AX, not "canonical" ones
+    }
+
     protected function subscriptionWhere()
     {
         if (!$this->_promo_min && !$this->_promo_max) {
index a7a019d..76fd6fb 100644 (file)
       </td>
     </tr>
     {include file="include/field.promo.tpl" prefix=""}
+    <tr>
+      <td class="titre">Envoyer à une liste d'adresses</td>
+      <td><textarea name="subset_to" rows="7" cols="78">{$subset_to}</textarea><br />
+      <span class="smaller">Indiquez une liste d'adresses mails : la lettre sera envoyée uniquement aux personnes des promotions sélectionnées, dont l'adresse figure dans la liste, et qui souhaitent recevoir les mailings de l'AX.</span>
+      </td>
+    </tr>
     {if !$saved}
     <tr>
       <td class="titre">Echéance d'envoi</td>
diff --git a/upgrade/0.10.1/02_axletter.sql b/upgrade/0.10.1/02_axletter.sql
new file mode 100644 (file)
index 0000000..d79cf6b
--- /dev/null
@@ -0,0 +1,12 @@
+DROP TABLE IF EXISTS axletter_subset;
+
+CREATE TABLE IF NOT EXISTS axletter_subset (
+    letter_id INT(11) UNSIGNED NOT NULL,
+    user_id INT(11) NOT NULL,
+    email VARCHAR(255) NOT NULL
+)
+
+ALTER TABLE axletter ADD subset smallint(1) NOT NULL DEFAULT 0 AFTER promo_max
+
+-- vim:set syntax=mysql:
+