bug 291 : change bestalias for homonyms when loses alias
authorPascal Corpet <pascal.corpet@m4x.org>
Sat, 19 Feb 2005 17:03:23 +0000 (17:03 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 26 Jun 2008 21:28:12 +0000 (23:28 +0200)
git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-485

htdocs/admin/homonymes.php
include/homonymes.inc.php [new file with mode: 0644]

index bda5fd0..5fc943c 100644 (file)
 
 require_once("xorg.inc.php");
 new_admin_page('admin/homonymes.tpl');
-require_once("diogenes/diogenes.hermes.inc.php");
+require_once("homonymes.inc.php");
 
 $op     = Env::get('op', 'list');
 $target = Env::getInt('target');
 
 if ($target) {
-    $res = $globals->xdb->query("SELECT  prenom,nom,a.alias AS forlife,h.alias AS loginbis
-                                   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 h ON (h.id=u.user_id AND h.expire!='')
-                                  WHERE  user_id = {?}", $target);
-    if (! list($prenom,$nom,$forlife,$loginbis) = $res->fetchOneRow()) {
+    if (! list($prenom,$nom,$forlife,$loginbis) = select_if_homonyme($target)) {
         $target=0;
     } else {
         $page->assign('nom',$nom);
@@ -48,32 +43,19 @@ $page->assign('baseurl',$globals->baseurl);
 
 // on a un $target valide, on prepare les mails
 if ($target) {
-    // from
-    $cc = "support+homonyme@polytechnique.org";
-    $FROM = "\"Support Polytechnique.org\" <$cc>";
     
     // on examine l'op a effectuer
     switch ($op) {
         case 'mail':
-            $mymail = new HermesMailer();
-            $mymail->setFrom($FROM);
-            $mymail->setSubject("Dans 2 semaines, suppression de $loginbis@polytechnique.org");
-            $mymail->addTo("$prenom $nom <$forlife@polytechnique.org>");
-            $mymail->addCc($cc);
-            $mymail->setTxtBody(Env::get('mailbody'));
-            $mymail->send();
+           send_warning_homonyme($prenom, $nom, $forlife, $loginbis);
+           switch_bestalias($target, $loginbis);
             $op = 'list';
             break;
         case 'correct':
+           switch_bestalias($target, $loginbis);
             $globals->xdb->execute("UPDATE aliases SET type='homonyme',expire=NOW() WHERE alias={?}", $loginbis);
             $globals->xdb->execute("REPLACE INTO homonymes (homonyme_id,user_id) VALUES({?},{?})", $target, $target);
-            $mymail = new HermesMailer();
-            $mymail->setFrom($FROM);
-            $mymail->setSubject("Mise en place du robot $loginbis@polytechnique.org");
-            $mymail->addTo("$prenom $nom <$forlife@polytechnique.org>");
-            $mymail->addCc($cc);
-            $mymail->setTxtBody(Env::get('mailbody'));
-            $mymail->send();
+           send_robot_homonyme($prenom, $nom, $forlife, $loginbis);
             $op = 'list';
             break;
     }
diff --git a/include/homonymes.inc.php b/include/homonymes.inc.php
new file mode 100644 (file)
index 0000000..1323af3
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2004 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+function select_if_homonyme($uid) {
+    global $globals;
+    $res = $globals->xdb->query("SELECT  prenom,nom,a.alias AS forlife,h.alias AS loginbis
+                                   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 h ON (h.id=u.user_id AND h.expire!='')
+                                  WHERE  user_id = {?}", $uid);
+    return $res->fetchOneRow();
+}
+
+function send_warning_homonyme($prenom, $nom, $forlife, $loginbis) {
+    require_once("diogenes/diogenes.hermes.inc.php");
+    $cc = "support+homonyme@polytechnique.org";
+    $FROM = "\"Support Polytechnique.org\" <$cc>";
+    $mymail = new HermesMailer();
+    $mymail->setFrom($FROM);
+    $mymail->setSubject("Dans 2 semaines, suppression de $loginbis@polytechnique.org");
+    $mymail->addTo("$prenom $nom <$forlife@polytechnique.org>");
+    $mymail->addCc($cc);
+    $mymail->setTxtBody(Env::get('mailbody'));
+    $mymail->send();
+}
+
+function send_robot_homonyme($prenom, $nom, $forlife, $loginbis) {
+    require_once("diogenes/diogenes.hermes.inc.php");
+    $cc = "support+homonyme@polytechnique.org";
+    $FROM = "\"Support Polytechnique.org\" <$cc>";
+    $mymail = new HermesMailer();
+    $mymail->setFrom($FROM);
+    $mymail->setSubject("Mise en place du robot $loginbis@polytechnique.org");
+    $mymail->addTo("$prenom $nom <$forlife@polytechnique.org>");
+    $mymail->addCc($cc);
+    $mymail->setTxtBody(Env::get('mailbody'));
+    $mymail->send();
+}
+
+function switch_bestalias($uid, $loginbis) {
+    global $globals;
+    // check if loginbis was the bestalias
+    $res = $globals->xdb->query("SELECT alias FROM aliases WHERE id = {?} AND FIND_IN_SET('bestalias', flags)", $uid);
+    $bestalias = $res->fetchOneCell();
+    if ($bestalias && $bestalias != $loginbis) return false;
+    
+    // select the shortest alias still alive
+    $res = $globals->xdb->query("SELECT alias FROM aliases WHERE id = {?} AND alias != {?} AND expire IS NULL ORDER BY LENGTH(alias) LIMIT 1", $uid, $loginbis);
+    $newbest = $res->fetchOneCell();
+    // change the bestalias flag
+    $globals->xdb->execute("UPDATE aliases SET flags = CONCAT(IF(FIND_IN_SET('epouse', flags), 'epouse', ''), ',' , IF(alias = {?}, 'bestalias', '')) WHERE id = {?}", $newbest, $uid);
+
+    return $newbest;
+}
+
+?>