Rename CoreLogger to PlLogger
[platal.git] / modules / marketing.php
index b51a825..83dcd89 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -79,6 +79,7 @@ class MarketingModule extends PLModule
     function handler_private(&$page, $uid = null,
                              $action = null, $value = null)
     {
+        global $globals;
         $page->changeTpl('marketing/private.tpl');
 
         if (is_null($uid)) {
@@ -87,9 +88,9 @@ class MarketingModule extends PLModule
 
         $page->assign('path', 'marketing/private/'.$uid);
 
-        $res = XDB::query("SELECT nom, prenom, promo, matricule
-                                       FROM auth_user_md5
-                                      WHERE user_id={?} AND perms='pending'", $uid);
+        $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('user.func.inc.php');
@@ -104,43 +105,46 @@ class MarketingModule extends PLModule
         }
 
         if ($action == 'del') {
-            XDB::execute('DELETE FROM register_marketing WHERE uid={?} AND email={?}',
-                                   $uid, $value);
+            Marketing::clear($uid, $value);
         }
 
         if ($action == 'rel') {
-            require_once('marketing.inc.php');
-            list($to, $title, $text) = mark_text_mail($uid, $value);
-            $from = mark_from_mail($uid, $value);
-            $page->assign('rel_from_user', $from);
-            $page->assign('rel_from_staff',
-                          "\"Equipe Polytechnique.org\" <register@polytechnique.org>");
-            $page->assign('rel_to', $to);
-            $page->assign('rel_title', $title);
-            $page->assign('rel_text', $text);
-            $page->assign('rel_email', $value);
+            $market = Marketing::get($uid, $value);
+            if ($market == null) {
+                $page->trigWarning("Aucun marketing n'a été effectué vers $value");
+            } else {
+                $to    = $market->user['to'];
+                $title = $market->getTitle();
+                $text  = $market->getText();
+                $from  = $market->sender_mail;
+                $page->assign('rel_from_user', $from);
+                $page->assign('rel_from_staff',
+                              '"Equipe Polytechnique.org" <register@' . $globals->mail->domain . '>');
+                $page->assign('rel_to', $to);
+                $page->assign('rel_title', $title);
+                $page->assign('rel_text', $text);
+                $page->assign('rel_email', $value);
+            }
         }
 
         if ($action == 'relforce') {
-            require_once('marketing.inc.php');
-            mark_send_mail($uid, $value, Post::v('from'), Post::v('to'),
-                           Post::v('title'), Post::v('message'));
-            $page->trig("Mail envoyé");
+            $market = Marketing::get($uid, Post::v('to'));
+            if (is_null($market)) {
+                $market = new Marketing($uid, Post::v('to'), 'default', null, 'staff');
+            }
+            $market->send(Post::v('title'), Post::v('message'));
+            $page->trigSuccess("Mail envoyé");
         }
 
         if ($action == 'insrel') {
-            require_once 'marketing.inc.php';
-            if (relance($uid)) {
-                $page->trig('relance faite');
+            if (Marketing::relance($uid)) {
+                $page->trigSuccess('relance faite');
             }
         }
 
         if ($action == 'add' && Post::has('email') && Post::has('type')) {
-            XDB::execute(
-                "INSERT INTO register_marketing
-                         SET uid = {?}, sender = {?}, email = {?},
-                             date = NOW(), type = {?}",
-                $uid, S::v('uid'), Post::v('email'), Post::v('type')); 
+            $market = new Marketing($uid, Post::v('email'), 'default', null, Post::v('type'), S::v('uid'));
+            $market->add(false);
         }
 
         $res = XDB::iterator(
@@ -152,7 +156,7 @@ class MarketingModule extends PLModule
         $page->assign('addr', $res);
 
         $res = XDB::query("SELECT date, relance FROM register_pending
-                                      WHERE uid = {?}", $uid);
+                            WHERE uid = {?}", $uid);
         if (list($pending, $relance) = $res->fetchOneRow()) {
             $page->assign('pending', $pending);
             $page->assign('relance', $relance);
@@ -175,7 +179,8 @@ class MarketingModule extends PLModule
         }
 
         $res = Xdb::query("SELECT  u.nom, u.prenom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe,
-                                   a.alias AS forlife, b.alias AS bestalias, e.email, e.last
+                                   u.deces = '0000-00-00' AS alive, a.alias AS forlife, b.alias AS bestalias,
+                                   IF(e.email IS NOT NULL, e.email, IF(FIND_IN_SET('googleapps', u.mail_storage), 'googleapps', NULL)) AS email, e.last
                              FROM  auth_user_md5 AS u
                        INNER JOIN  aliases       AS a ON (a.id = u.user_id AND a.type = 'a_vie')
                        INNER JOIN  aliases       AS b ON (b.id = u.user_id AND FIND_IN_SET('bestalias', b.flags))
@@ -196,8 +201,17 @@ class MarketingModule extends PLModule
         if (Post::has('valide') && isvalid_email_redirection($email)) {
             // security stuff
             check_email($email, "Proposition d'une adresse surveillee pour " . $user['forlife'] . " par " . S::v('forlife'));
-            if ($user['email'] && !trim(Post::v('comment'))) {
-                $page->trig("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le "
+            $res = XDB::query("SELECT  e.flags
+                                 FROM  emails   AS e
+                           INNER JOIN  aliases  AS a ON (a.id = e.uid)
+                                WHERE  e.email = {?} AND a.alias = {?}", $email, $user['forlife']);
+            $state = $res->numRows() ? $res->fetchOneCell() : null;
+            if ($state == 'panne') {
+                $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user['prenom']} et est en panne.");
+            } elseif ($state == 'active') {
+                $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user['prenom']}");
+            } elseif ($user['email'] && !trim(Post::v('comment'))) {
+                $page->trigError("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le "
                            ."besoin de changer la redirection de " . $user['prenom']);
             } else {
                 require_once 'validations.inc.php';
@@ -206,7 +220,7 @@ class MarketingModule extends PLModule
                 $page->assign('sent', true);
             }
         } elseif ($email) {
-            $page->trig("L'adresse proposée n'est pas une adresse acceptable pour une redirection");
+            $page->trigError("L'adresse proposée n'est pas une adresse acceptable pour une redirection");
         }
     }
 
@@ -220,7 +234,7 @@ class MarketingModule extends PLModule
         $page->assign('promo', $promo);
 
         $sql = "SELECT  u.user_id, u.nom, u.prenom, u.last_known_email, u.matricule_ax,
-                        IF(MAX(m.last)>p.relance, MAX(m.last), p.relance) AS dern_rel, p.email
+                        IF(MAX(m.last) > p.relance, MAX(m.last), p.relance) AS dern_rel, p.email
                   FROM  auth_user_md5      AS u
              LEFT JOIN  register_pending   AS p ON p.uid = u.user_id
              LEFT JOIN  register_marketing AS m ON m.uid = u.user_id
@@ -248,26 +262,22 @@ class MarketingModule extends PLModule
 
             if (Post::has('valide')) {
                 require_once('xorg.misc.inc.php');
-
                 $email = trim(Post::v('mail'));
-                $res   = XDB::query('SELECT COUNT(*) FROM register_marketing
-                                                WHERE uid={?} AND email={?}', $uid, $email);
 
                 if (!isvalid_email_redirection($email)) {
-                    $page->trig("Email invalide !");
-                } elseif ($res->fetchOneCell()) {
-                    $page->assign('already', true);
+                    $page->trigError("Email invalide !");
                 } else {
-                    $page->assign('ok', true);
-                    check_email($email, "Une adresse surveillée est proposée au marketing par " . S::v('forlife'));
-                    XDB::execute(
-                            "INSERT INTO  register_marketing (uid,sender,email,date,last,nb,type,hash)
-                                  VALUES  ({?}, {?}, {?}, NOW(), 0, 0, {?}, '')",
-                            $uid, S::v('uid'), $email, Post::v('origine'));
-                    require_once('validations.inc.php');
-                    $req = new MarkReq(S::v('uid'), $uid, $email,
-                                       Post::v('origine')=='user');
-                    $req->submit();
+                    // On cherche les marketings précédents sur cette adresse
+                    // email, en se restreignant au dernier mois
+
+                    if (Marketing::get($uid, $email, true)) {
+                        $page->assign('already', true);
+                    } else {
+                        $page->assign('ok', true);
+                        check_email($email, "Une adresse surveillée est proposée au marketing par " . S::v('forlife'));
+                        $market = new Marketing($uid, $email, 'default', null, Post::v('origine'), S::v('uid'));
+                        $market->add();
+                    }
                 }
             }
         }
@@ -316,14 +326,12 @@ class MarketingModule extends PLModule
         $page->changeTpl('marketing/relance.tpl');
 
         if (Post::has('relancer')) {
-            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)) {
+                if ($tmp = Marketing::relance($uid, $nbdix)) {
                     $sent[] = $tmp.' a été relancé';
                 }
             }