Merge commit 'origin/master' into fusionax
[platal.git] / modules / axletter.php
index 7b7f754..b27dc7d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -24,13 +24,13 @@ class AXLetterModule extends PLModule
     function handlers()
     {
         return array(
-            'ax'             => $this->make_hook('index',        AUTH_COOKIE),
+            'ax'             => $this->make_hook('index',  AUTH_COOKIE),
             'ax/out'         => $this->make_hook('out',    AUTH_PUBLIC),
             'ax/show'        => $this->make_hook('show',   AUTH_COOKIE),
             'ax/edit'        => $this->make_hook('submit', AUTH_MDP),
             'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP),
             'ax/edit/valid'  => $this->make_hook('valid',  AUTH_MDP),
-            'admin/axletter' => $this->make_hook('admin', AUTH_MDP, 'admin'),
+            'admin/axletter' => $this->make_hook('admin',  AUTH_MDP, 'admin'),
         );
     }
 
@@ -43,17 +43,17 @@ class AXLetterModule extends PLModule
                 return $this->handler_index($page, 'out');
             }
         }
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
         $page->changeTpl('axletter/unsubscribe.tpl');
         $page->assign('success', AXLetter::unsubscribe($hash, true));
     }
 
     function handler_index(&$page, $action = null)
     {
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
 
         $page->changeTpl('axletter/index.tpl');
-        $page->setTitle('Polytechnique.org - Envois de l\'AX');
+        $page->setTitle('Envois de l\'AX');
 
         switch ($action) {
           case 'in':  AXLetter::subscribe(); break;
@@ -73,7 +73,7 @@ class AXLetterModule extends PLModule
 
     function handler_submit(&$page, $action = null)
     {
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
         if (!AXLetter::hasPerms()) {
             return PL_FORBIDDEN;
         }
@@ -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_to  = preg_split("/[ ,;\:\n\r]+/", Post::v('subset_to'), -1, PREG_SPLIT_NO_EMPTY);
+        $subset     = (count($subset_to) > 0);
         $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,8 @@ class AXLetterModule extends PLModule
             $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
             if ($res->numRows()) {
                 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
+                $subset_to = ($subset ? explode("\n", $subset) : null);
+                $subset = (count($subset_to) > 0);
                 $saved = true;
             } else  {
                 XDB::execute("INSERT INTO axletter SET id = NULL");
@@ -149,17 +153,17 @@ class AXLetterModule extends PLModule
 
             switch (@Post::v('valid')) {
               case 'Aperçu':
-                require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+                $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 ? implode("\n", $subset_to) : null);
                 if (!$saved) {
                     global $globals;
                     $mailer = new PlMailer();
@@ -201,12 +205,14 @@ 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);
         $page->assign('saved', $saved);
         $page->assign('new', $new);
-        $page->assign('is_xorg', S::has_perms());
+        $page->assign('is_xorg', S::admin());
 
         if (!$saved) {
             $select = '';
@@ -225,7 +231,7 @@ class AXLetterModule extends PLModule
 
     function handler_cancel(&$page, $force = null)
     {
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
         if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
             return PL_FORBIDDEN;
         }
@@ -240,12 +246,12 @@ class AXLetterModule extends PLModule
             return;
         }
 
-        $page->kill("L'envoi de l'annonce {$al->title()} est annulé");
+        $page->killSuccess("L'envoi de l'annonce {$al->title()} est annulé.");
     }
 
     function handler_valid(&$page, $force = null)
     {
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
         if (!AXLetter::hasPerms() || !S::has_xsrf_token()) {
             return PL_FORBIDDEN;
         }
@@ -260,30 +266,34 @@ class AXLetterModule extends PLModule
             return;
         }
 
-        $page->kill("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
+        $page->killSuccess("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
     }
 
     function handler_show(&$page, $nid = 'last')
     {
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
         $page->changeTpl('axletter/show.tpl');
 
-        $nl  = new AXLetter($nid);
-        if (Get::has('text')) {
-            $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
-        } else {
-            $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
-        }
-        if (Post::has('send')) {
-            $nl->sendTo(S::user()->login(), S::user()->bestEmail(),
-                        S::v('prenom'), S::v('nom'),
-                        S::v('femme'), S::v('mail_fmt') != 'texte');
+        try {
+            $nl = new AXLetter($nid);
+            if (Get::has('text')) {
+                $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
+            } else {
+                $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
+            }
+            if (Post::has('send')) {
+                $nl->sendTo(S::user()->login(), S::user()->bestEmail(),
+                            S::v('prenom'), S::v('nom'),
+                            S::v('femme'), S::v('mail_fmt') != 'texte');
+            }
+        } catch (MailNotFound $e) {
+            return PL_NOT_FOUND;
         }
     }
 
     function handler_admin(&$page, $action = null, $uid = null)
     {
-        require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
+        $this->load('axletter.inc.php');
         if (Post::has('action')) {
             $action = Post::v('action');
             $uid    = Post::v('uid');
@@ -309,10 +319,9 @@ class AXLetterModule extends PLModule
 
         $page->changeTpl('axletter/admin.tpl');
         $res = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom,
-                                     u.prenom, u.promo, a.alias AS forlife
+                                     u.prenom, u.promo, u.hruid
                                 FROM axletter_rights AS ar
-                          INNER JOIN auth_user_md5   AS u USING(user_id)
-                          INNER JOIN aliases         AS a ON (u.user_id = a.id AND a.type = 'a_vie')");
+                          INNER JOIN auth_user_md5   AS u USING(user_id)");
         $page->assign('admins', $res);
 
         $importer = new CSVImporter('axletter_ins');
@@ -321,7 +330,7 @@ class AXLetterModule extends PLModule
         $importer->apply($page, "admin/axletter", array('user_id', 'email', 'prenom', 'nom', 'promo', 'flag', 'hash'));
     }
 
-    function idFromMail($line, $key)
+    function idFromMail($line, $key, $relation = null)
     {
         static $field;
         global $globals;
@@ -366,7 +375,7 @@ class AXLetterModule extends PLModule
         return $id ? $id : '0';
     }
 
-    function createHash($line, $key)
+    function createHash($line, $key, $relation)
     {
         $hash = implode(time(), $line) . rand();
         $hash = md5($hash);