2006 => 2007 Happy New Year\!
[platal.git] / modules / marketing.php
index 4ce614e..0235efa 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -32,20 +32,19 @@ class MarketingModule extends PLModule
 
             'marketing/private'    => $this->make_hook('private',    AUTH_MDP, 'admin'),
             'marketing/public'     => $this->make_hook('public',     AUTH_COOKIE),
+            'marketing/broken'     => $this->make_hook('broken',     AUTH_COOKIE),
         );
     }
 
     function handler_marketing(&$page)
     {
-        global $globals;
-
         $page->changeTpl('marketing/index.tpl');
 
         $page->assign('xorg_title','Polytechnique.org - Marketing');
 
         // Quelques statistiques
 
-        $res   = $globals->xdb->query(
+        $res   = XDB::query(
                   "SELECT COUNT(*) AS vivants,
                           COUNT(NULLIF(perms='admin' OR perms='user', 0)) AS inscrits,
                           100*COUNT(NULLIF(perms='admin' OR perms='user', 0))/COUNT(*) AS ins_rate,
@@ -62,28 +61,24 @@ class MarketingModule extends PLModule
         $stats = $res->fetchOneAssoc();
         $page->assign('stats', $stats);
 
-        $res   = $globals->xdb->query("SELECT count(*) FROM auth_user_md5 WHERE date_ins > ".
+        $res   = XDB::query("SELECT count(*) FROM auth_user_md5 WHERE date_ins > ".
                                       date('Ymd000000', strtotime('1 week ago')));
         $page->assign('nbInsSem', $res->fetchOneCell());
 
-        $res = $globals->xdb->query("SELECT count(*) FROM register_pending WHERE hash != 'INSCRIT'");
+        $res = XDB::query("SELECT count(*) FROM register_pending WHERE hash != 'INSCRIT'");
         $page->assign('nbInsEnCours', $res->fetchOneCell());
 
-        $res = $globals->xdb->query("SELECT count(*) FROM register_marketing");
+        $res = XDB::query("SELECT count(*) FROM register_marketing");
         $page->assign('nbInsMarket', $res->fetchOneCell());
 
-        $res = $globals->xdb->query("SELECT count(*) FROM register_mstats
+        $res = XDB::query("SELECT count(*) FROM register_mstats
                                       WHERE TO_DAYS(NOW()) - TO_DAYS(success) <= 7");
         $page->assign('nbInsMarkOK', $res->fetchOneCell());
-
-        return PL_OK;
     }
 
     function handler_private(&$page, $uid = null,
                              $action = null, $value = null)
     {
-        global $globals;
-
         $page->changeTpl('marketing/private.tpl');
 
         if (is_null($uid)) {
@@ -92,12 +87,12 @@ class MarketingModule extends PLModule
 
         $page->assign('path', 'marketing/private/'.$uid);
 
-        $res = $globals->xdb->query("SELECT nom, prenom, promo, matricule
+        $res = XDB::query("SELECT nom, prenom, promo, matricule
                                        FROM auth_user_md5
                                       WHERE user_id={?} AND perms='pending'", $uid);
 
         if (list($nom, $prenom, $promo, $matricule) = $res->fetchOneRow()) {
-            require_once('register.inc.php');
+            require_once('user.func.inc.php');
             $matricule_X = get_X_mat($matricule);
             $page->assign('nom', $nom);
             $page->assign('prenom', $prenom);
@@ -109,7 +104,7 @@ class MarketingModule extends PLModule
         }
 
         if ($action == 'del') {
-            $globals->xdb->execute('DELETE FROM register_marketing WHERE uid={?} AND email={?}',
+            XDB::execute('DELETE FROM register_marketing WHERE uid={?} AND email={?}',
                                    $uid, $value);
         }
 
@@ -128,27 +123,27 @@ class MarketingModule extends PLModule
 
         if ($action == 'relforce') {
             require_once('marketing.inc.php');
-            mark_send_mail($uid, $value, Post::get('from'), Post::get('to'),
-                           Post::get('title'), Post::get('message'));
-            $page->trig("Mail envoyé");
+            mark_send_mail($uid, $value, Post::v('from'), Post::v('to'),
+                           Post::v('title'), Post::v('message'));
+            $page->trig("Mail envoyé");
         }
 
         if ($action == 'insrel') {
-            require_once('marketing.inc.php');
+            require_once 'marketing.inc.php';
             if (relance($uid)) {
                 $page->trig('relance faite');
             }
         }
 
         if ($action == 'add' && Post::has('email') && Post::has('type')) {
-            $globals->xdb->execute(
+            XDB::execute(
                 "INSERT INTO register_marketing
                          SET uid = {?}, sender = {?}, email = {?},
                              date = NOW(), type = {?}",
-                $uid, Session::get('uid'), Post::get('email'), Post::get('type')); 
+                $uid, S::v('uid'), Post::v('email'), Post::v('type')); 
         }
 
-        $res = $globals->xdb->iterator(
+        $res = XDB::iterator(
                 "SELECT  r.*, a.alias
                    FROM  register_marketing AS r
              INNER JOIN  aliases            AS a ON (r.sender=a.id AND a.type = 'a_vie')
@@ -156,24 +151,62 @@ class MarketingModule extends PLModule
                ORDER BY  date", $uid);
         $page->assign('addr', $res);
 
-        $res = $globals->xdb->query("SELECT date, relance FROM register_pending
+        $res = XDB::query("SELECT date, relance FROM register_pending
                                       WHERE uid = {?}", $uid);
-        if (list($pending, $relance) = $res->fetchOneCell()) {
+        if (list($pending, $relance) = $res->fetchOneRow()) {
             $page->assign('pending', $pending);
             $page->assign('relance', $relance);
         }
+    }
+
+    function handler_broken(&$page, $uid = null)
+    {
+        require_once('user.func.inc.php');
+        $page->changeTpl('marketing/broken.tpl');
+
+        if (is_null($uid)) {
+            return PL_NOT_FOUND;
+        }
+        $forlife = get_user_forlife($uid);
+        if (!$forlife) {
+            return PL_NOT_FOUND;
+        } elseif ($forlife == S::v('forlife')) {
+            pl_redirect('emails/redirect');
+        }
 
-        return PL_OK;
+        $res = Xdb::query("SELECT  u.nom, u.prenom, u.promo, a.alias AS forlife
+                             FROM  auth_user_md5 AS u
+                       INNER JOIN  aliases       AS a ON a.id = u.user_id
+                            WHERE  a.alias = {?}", $forlife);
+        if (!$res->numRows()) {
+            return PL_NOT_FOUND;
+        }
+        $user = $res->fetchOneAssoc();
+        $page->assign('user', $user);
+
+        $email = trim(Post::v('mail'));
+        if (Post::has('valide') && strlen($email) > 0) {
+            $mailer = new PlMailer();
+            $mailer->setFrom(S::v('bestalias') . '@polytechnique.org');
+            $mailer->addTo('resetpass@polytechnique.org');
+            $mailer->setSubject("Proposition d'adresse mail pour " . $user['forlife']);
+
+            $message = S::v('nom') . ' ' . S::v('prenom') . ' (X' . S::v('promo') . ') '
+                     . 'propose l\'adresse suivante pour un camarade qui n\'a plus de '
+                     . 'redirections actives :' . "\n\n"
+                     . '* ' . $user['forlife'] . ' => ' . $email . "\n";
+            $mailer->setTxtBody(wordwrap($message, 78));
+            $mailer->send();
+            $page->assign('sent', true);
+        }
     }
 
     function handler_promo(&$page, $promo = null)
     {
-        global $globals;
-
         $page->changeTpl('marketing/promo.tpl');
 
         if (is_null($promo)) {
-            $promo = Session::getInt('promo');
+            $promo = S::v('promo');
         }
         $page->assign('promo', $promo);
 
@@ -185,22 +218,18 @@ class MarketingModule extends PLModule
                  WHERE  u.promo = {?} AND u.deces = 0 AND u.perms='pending'
               GROUP BY  u.user_id
               ORDER BY  nom, prenom";
-        $page->assign('nonins', $globals->xdb->iterator($sql, $promo));
-
-        return PL_OK;
+        $page->assign('nonins', XDB::iterator($sql, $promo));
     }
 
     function handler_public(&$page, $uid = null)
     {
-        global $globals;
-
         $page->changeTpl('marketing/public.tpl');
 
         if (is_null($uid)) {
             return PL_NOT_FOUND;
         }
 
-        $res = $globals->xdb->query("SELECT nom, prenom, promo FROM auth_user_md5
+        $res = XDB::query("SELECT nom, prenom, promo FROM auth_user_md5
                                       WHERE user_id={?} AND perms='pending'", $uid);
 
         if (list($nom, $prenom, $promo) = $res->fetchOneRow()) {
@@ -211,8 +240,8 @@ class MarketingModule extends PLModule
             if (Post::has('valide')) {
                 require_once('xorg.misc.inc.php');
 
-                $email = trim(Post::get('mail'));
-                $res   = $globals->xdb->query('SELECT COUNT(*) FROM register_marketing
+                $email = trim(Post::v('mail'));
+                $res   = XDB::query('SELECT COUNT(*) FROM register_marketing
                                                 WHERE uid={?} AND email={?}', $uid, $email);
 
                 if (!isvalid_email_redirection($email)) {
@@ -221,25 +250,21 @@ class MarketingModule extends PLModule
                     $page->assign('already', true);
                 } else {
                     $page->assign('ok', true);
-                    $globals->xdb->execute(
+                    XDB::execute(
                             "INSERT INTO  register_marketing (uid,sender,email,date,last,nb,type,hash)
                                   VALUES  ({?}, {?}, {?}, NOW(), 0, 0, {?}, '')",
-                            $uid, Session::getInt('uid'), $email, Post::get('origine'));
+                            $uid, S::v('uid'), $email, Post::v('origine'));
                     require_once('validations.inc.php');
-                    $req = new MarkReq(Session::getInt('uid'), $uid, $email,
-                                       Post::get('origine')=='user');
+                    $req = new MarkReq(S::v('uid'), $uid, $email,
+                                       Post::v('origine')=='user');
                     $req->submit();
                 }
             }
         }
-
-        return PL_OK;
     }
 
     function handler_week(&$page, $sorting = 'per_promo')
     {
-        global $globals;
-
         $page->changeTpl('marketing/this_week.tpl');
 
         $sort = $sorting == 'per_promo' ? 'promo' : 'date_ins';
@@ -249,18 +274,14 @@ class MarketingModule extends PLModule
             INNER JOIN  aliases        AS a ON (u.user_id = a.id AND a.type='a_vie')
                  WHERE  u.date_ins > ".date("Ymd000000", strtotime ('1 week ago'))."
               ORDER BY  u.$sort DESC";
-        $page->assign('ins', $globals->xdb->iterator($sql));
-
-        return PL_OK;
+        $page->assign('ins', XDB::iterator($sql));
     }
 
     function handler_volontaire(&$page, $promo = null)
     {
-        global $globals;
-
         $page->changeTpl('marketing/volontaire.tpl');
 
-        $res = $globals->xdb->query(
+        $res = XDB::query(
                 "SELECT
                DISTINCT  a.promo
                    FROM  register_marketing AS m
@@ -276,26 +297,24 @@ class MarketingModule extends PLModule
                 INNER JOIN  auth_user_md5      AS a  ON a.user_id = m.uid AND a.promo = {?}
                 INNER JOIN  aliases            AS sa ON (m.sender = sa.id AND sa.type='a_vie')
                   ORDER BY  a.nom";
-            $page->assign('addr', $globals->xdb->iterator($sql, $promo));
+            $page->assign('addr', XDB::iterator($sql, $promo));
         }
-
-        return PL_OK;
     }
 
     function handler_relance(&$page)
     {
-        global $globals;
-
         $page->changeTpl('marketing/relance.tpl');
 
         if (Post::has('relancer')) {
-            $res   = $globals->xdb->query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
+            require_once 'marketing.inc.php';
+
+            $res   = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
             $nbdix = $res->fetchOneCell();
 
             $sent  = Array();
             foreach (array_keys($_POST['relance']) as $uid) {
                 if ($tmp = relance($uid, $nbdix)) {
-                    $sent[] = $tmp.' a été relancé';
+                    $sent[] = $tmp.' a été relancé';
                 }
             }
             $page->assign('sent', $sent);
@@ -306,9 +325,7 @@ class MarketingModule extends PLModule
             INNER JOIN  auth_user_md5    AS u ON r. uid = u.user_id
                  WHERE  hash!='INSCRIT'
               ORDER BY  date DESC";
-        $page->assign('relance', $globals->xdb->iterator($sql));
-
-        return PL_OK;
+        $page->assign('relance', XDB::iterator($sql));
     }
 }