Close #711: Fix advanced search when autocompletion is not explicitly
[platal.git] / modules / xnetgrp.php
index ede566c..85dcdf0 100644 (file)
@@ -46,7 +46,7 @@ function get_infos($email)
                                 WHERE user_id = {?}", $user['uid']);
             $user = array_merge($user, $res->fetchOneAssoc());
         }
-        return $user;                        
+        return $user;
     } elseif ($dom == 'polytechnique.org' || $dom == 'm4x.org') {
         $res = XDB::query(
                 "SELECT  user_id AS uid, u.promo,
@@ -97,11 +97,11 @@ class XnetGrpModule extends PLModule
             '%grp/member/new'
                  => $this->make_hook('admin_member_new', AUTH_MDP, 'groupadmin'),
             '%grp/member/new/ajax'
-                 => $this->make_hook('admin_member_new_ajax', AUTH_MDP, 'user', NO_AUTH),         
+                 => $this->make_hook('admin_member_new_ajax', AUTH_MDP, 'user', NO_AUTH),
             '%grp/member/del'
                  => $this->make_hook('admin_member_del', AUTH_MDP, 'groupadmin'),
 
-            '%grp/rss'             => $this->make_hook('rss', AUTH_PUBLIC),         
+            '%grp/rss'             => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
             '%grp/announce/new'    => $this->make_hook('edit_announce', AUTH_MDP,  'groupadmin'),
             '%grp/announce/edit'   => $this->make_hook('edit_announce', AUTH_MDP,  'groupadmin'),
             '%grp/admin/announces' => $this->make_hook('admin_announce', AUTH_MDP, 'groupadmin'),
@@ -122,7 +122,7 @@ class XnetGrpModule extends PLModule
                 XDB::query('DELETE r.*
                               FROM groupex.announces_read AS r
                         INNER JOIN groupex.announces AS a ON a.id = r.announce_id
-                             WHERE peremption < CURRENT_DATE()');       
+                             WHERE peremption < CURRENT_DATE()');
                 XDB::query('INSERT INTO groupex.announces_read
                                  VALUES ({?}, {?})',
                             Env::i('read'), S::i('uid'));
@@ -170,7 +170,7 @@ class XnetGrpModule extends PLModule
             $page->setRssLink("Polytechnique.net :: {$globals->asso("nom")} :: News",
                               'rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
         }
-        
+
         $page->register_modifier('wiki_to_html', array('MiniWiki','WikiToHTML'));
         $page->assign('articles', $arts);
 
@@ -355,7 +355,7 @@ class XnetGrpModule extends PLModule
                                      1, 1)) as letter, COUNT(*)
                            FROM  groupex.membres AS m
                       LEFT JOIN  auth_user_md5   AS u ON ( u.user_id = m.uid)
-                          WHERE  asso_id = {?} and u.perms != \'pending\'
+                          WHERE  asso_id = {?} and (u.perms != \'pending\' OR u.perms IS NULL)
                        GROUP BY  letter
                        ORDER BY  letter', $globals->asso('id'));
         } else {
@@ -430,7 +430,7 @@ class XnetGrpModule extends PLModule
                 LEFT JOIN  emails          AS e ON ( e.flags = 'active' AND e.uid = m.uid)
                     WHERE  m.asso_id = {?} $ini
                            AND (m.origine != 'X' OR u.perms != 'pending' OR m.email IS NOT NULL)
-                 GROUP BY  m.uid          
+                 GROUP BY  m.uid
                  ORDER BY  $tri
                     LIMIT  {?},{?}", $globals->asso('id'), $ofs*NB_PER_PAGE, NB_PER_PAGE);
         $page->assign('ann', $ann);
@@ -446,7 +446,7 @@ class XnetGrpModule extends PLModule
     {
         pl_redirect('annuaire/geoloc');
     }
-    
+
     function handler_vcard(&$page, $photos = null)
     {
         global $globals;
@@ -582,7 +582,7 @@ class XnetGrpModule extends PLModule
     {
         if (Env::has('right') && (may_update() || S::has('suid'))) {
             switch (Env::v('right')) {
-              case 'admin':              
+              case 'admin':
                 XnetSession::killSuid();
                 break;
               case 'anim':
@@ -591,7 +591,7 @@ class XnetGrpModule extends PLModule
                 is_member(true);
                 break;
               case 'member':
-                XnetSession::doSelfSuid();  
+                XnetSession::doSelfSuid();
                 may_update(false, true);
                 is_member(true);
                 break;
@@ -679,7 +679,7 @@ class XnetGrpModule extends PLModule
                             FROM  auth_user_md5 AS u
                       INNER JOIN  aliases       AS a ON (u.user_id = a.id)
                            WHERE  a.alias={?}', $globals->asso('id'), $forlife);
-                pl_redirect("member/$email");
+                pl_redirect("member/$forlife");
             } else {
                 $page->trig($email." n'est pas un alias polytechnique.org valide");
             }
@@ -698,7 +698,7 @@ class XnetGrpModule extends PLModule
                                 $market = new Marketing($uid, $email, 'group', $globals->asso('nom'),
                                                         Env::v('market_from'), S::v('uid'));
                                 $market->add();
-                            }            
+                            }
                         }
                         XDB::execute('INSERT INTO groupex.membres (uid, asso_id, origine, email)
                                            VALUES ({?}, {?}, "X", {?})',
@@ -724,10 +724,12 @@ class XnetGrpModule extends PLModule
     {
         header('Content-Type: text/html; charset="UTF-8"');
         $page->changeTpl('xnetgrp/membres-new-search.tpl', NO_SKIN);
+        $res = null;
         if (Env::has('login')) {
             require_once 'user.func.inc.php';
             $res = get_not_registered_user(Env::v('login'), true);
-        } else {
+        }
+        if (is_null($res)) {
             list($nom, $prenom) = str_replace(array('-', ' ', "'"), '%', array(Env::v('nom'), Env::v('prenom')));
             $where = "perms = 'pending'";
             if (!empty($nom)) {
@@ -836,6 +838,72 @@ class XnetGrpModule extends PLModule
         }
     }
 
+    private function changeLogin(PlatalPage &$page, array &$user, MMList &$mmlist, $login)
+    {
+        require_once 'user.func.inc.php';
+        // Search the uid of the user...
+        $res = XDB::query("SELECT  f.id, f.alias
+                             FROM  aliases AS a
+                       INNER JOIN  aliases AS f ON (f.id = a.id AND f.type = 'a_vie')
+                            WHERE  a.alias = {?}",
+                          $login);
+        if ($res->numRows() == 0) {
+            $x = get_not_registered_user($login);
+            if (!$x) {
+                $page->trig("Le login $login ne correspond à aucun X");
+                return false;
+            } else if (count($x) > 1) {
+                $page->trig("Le login $login correspond a plusieurs camarades");
+                return false;
+            }
+            $uid = $x[0]['user_id'];
+            $sub = false;
+        } else {
+            list($uid, $login) = $res->fetchOneRow();
+            $sub = true;
+        }
+
+        // Check if the user is already in the group
+        global $globals;
+        $res = XDB::query("SELECT  uid, email
+                             FROM  groupex.membres
+                            WHERE  uid = {?} AND asso_id = {?}",
+                          $uid, $globals->asso('id'));
+        if ($res->numRows()) {
+            list($uid, $email) = $res->fetchOneRow();
+            XDB::execute("DELETE FROM groupex.membres
+                                WHERE uid = {?}",
+                         $user['uid']);
+        } else {
+            $email = $user['email'];
+            XDB::execute("UPDATE  groupex.membres
+                             SET  uid = {?}, origine = 'X'
+                           WHERE  uid = {?} AND asso_id = {?}",
+                         $uid, $user['uid'], $globals->asso('id'));
+        }
+        if ($sub) {
+            $email = $login . '@' . $globals->mail->domain;
+        }
+
+        // Update subscription to aliases
+        if ($email != $user['email']) {
+            XDB::execute("UPDATE IGNORE  virtual_redirect AS vr
+                             INNER JOIN  virtual AS v ON(vr.vid = v.vid AND SUBSTRING_INDEX(alias, '@', 2) = {?})
+                                    SET  vr.redirect = {?}
+                                  WHERE  vr.redirect = {?}",
+                         $globals->asso('mail_domain'), $email, $user['email']);
+            XDB::execute("DELETE  vr.*
+                            FROM  virtual_redirect AS vr
+                      INNER JOIN  virtual AS v ON(vr.vid = v.vid AND SUBSTRING_INDEX(alias, '@', 2) = {?})
+                           WHERE  vr.redirect = {?}",
+                         $globals->asso('mail_domain'), $user['email']);
+            foreach (Env::v('ml1', array()) as $ml => $state) {
+                $mmlist->replace_email($ml, $user['email'], $email);
+            }
+        }
+        return $login;
+    }
+
     function handler_admin_member(&$page, $user)
     {
         global $globals;
@@ -851,6 +919,15 @@ class XnetGrpModule extends PLModule
                              $globals->asso('mail_domain'));
 
         if (Post::has('change')) {
+            // Convert user status to X
+            if ($user['origine'] == 'ext' && trim(Post::v('login_X'))) {
+                $forlife = $this->changeLogin($page, $user, $mmlist, trim(Post::v('login_X')));
+                if ($forlife) {
+                    pl_redirect('member/' . $forlife);
+                }
+            }
+
+            // Update user info
             $email_changed = ($user['origine'] != 'X' && strtolower($user['email']) != strtolower(Post::v('email')));
             $from_email = $user['email'];
             if ($user['origine'] != 'X') {
@@ -878,6 +955,7 @@ class XnetGrpModule extends PLModule
                 $page->trig('permissions modifiées');
             }
 
+            // Update ML subscriptions
             foreach (Env::v('ml1', array()) as $ml => $state) {
                 $ask = empty($_REQUEST['ml2'][$ml]) ? 0 : 2;
                 if ($ask == $state) {
@@ -904,6 +982,7 @@ class XnetGrpModule extends PLModule
                 }
             }
 
+            // Change subscriptioin to aliases
             foreach (Env::v('ml3', array()) as $ml => $state) {
                 $ask = !empty($_REQUEST['ml4'][$ml]);
                 if($state == $ask) continue;
@@ -1015,9 +1094,9 @@ class XnetGrpModule extends PLModule
             $promo_min = ($art['public'] ? 0 : $art['promo_min']);
             $promo_max = ($art['public'] ? 0 : $art['promo_max']);
             if (is_null($aid)) {
-                $fulltext = $art['texte']; 
-                if (!empty($art['contact_html'])) { 
-                    $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html']; 
+                $fulltext = $art['texte'];
+                if (!empty($art['contact_html'])) {
+                    $fulltext .= "\n\n'''Contacts :'''\\\\\n" . $art['contact_html'];
                 }
                 $post = null;
                 if ($globals->asso('forum')) {
@@ -1060,7 +1139,7 @@ class XnetGrpModule extends PLModule
         }
         if (Post::v('valid') == 'Enregistrer' || Post::v('valid') == 'Annuler') {
             pl_redirect("");
-        } 
+        }
 
         if (empty($art) && !is_null($aid)) {
             $res = XDB::query("SELECT a.*, u.nom, u.prenom, u.promo, l.alias AS forlife,
@@ -1086,7 +1165,7 @@ class XnetGrpModule extends PLModule
             if ($events->total()) {
                 $page->assign('events', $events);
             }
-        } 
+        }
 
         $art['contact_html'] = @MiniWiki::WikiToHTML($art['contact_html']);
         $page->assign('art', $art);