Fixes display of emails in profile edition.
[platal.git] / include / emails.combobox.inc.php
index 31df31c..288aa66 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-function fill_email_combobox(PlPage& $page)
+function fill_email_combobox(PlPage& $page, $user = null, $profile = null)
 {
     global $globals;
 
-    $user = S::user();
-    $email_type = "directory";
-
-    $res = XDB::query(
-            "SELECT  email_directory
-               FROM  profile_directory
-              WHERE  uid = {?}", $user->id());
-    $email_directory = $res->fetchOneCell();
-    if ($email_directory) {
-        $page->assign('email_directory', $email_directory);
-        list($alias, $domain) = explode('@', $email_directory);
-    } else {
-        $page->assign('email_directory', '');
-        $email_type = NULL;
-        $alias = $domain = '';
+    if (is_null($user)) {
+        $user = S::user();
     }
+    if (is_null($profile)) {
+        /* Always refetch the profile. */
+        $profile = $user->profile(true);
+    }
+    $email_type = "directory";
 
-    $res = XDB::query(
-            "SELECT  alias
-               FROM  virtual
-         INNER JOIN  virtual_redirect USING(vid)
-              WHERE  (redirect = {?} OR redirect = {?})
-                     AND alias LIKE '%@{$globals->mail->alias_dom}'",
-            $user->forlifeEmail(),
-            // TODO: remove this über-ugly hack. The issue is that you need
-            // to remove all @m4x.org addresses in virtual_redirect first.
-            $user->login() . '@' . $globals->mail->domain2);
-    $melix = $res->fetchOneCell();
-    if ($melix) {
-        list($melix) = explode('@', $melix);
-        $page->assign('melix', $melix);
-        if (($domain == $globals->mail->alias_dom) || ($domain == $globals->mail->alias_dom2)) {
-            $email_type = "melix";
+    if ($profile) {
+        $email_directory = $profile->email_directory;
+        if ($email_directory) {
+            $page->assign('email_directory', $email_directory);
+            list($alias, $domain) = explode('@', $email_directory);
+        } else {
+            $page->assign('email_directory', '');
+            $email_type = NULL;
+            $alias = $domain = '';
         }
-    }
 
-    $res = XDB::query(
-            "SELECT  alias
-               FROM  aliases
-              WHERE  id={?} AND (type='a_vie' OR type='alias')", $user->id());
-    $res = $res->fetchAllAssoc();
-    $page->assign('list_email_X', $res);
-    if (($domain == $globals->mail->domain) || ($domain == $globals->mail->domain2)) {
+        $res = XDB::query(
+                "SELECT  email
+                   FROM  profile_job
+                  WHERE  pid = {?}", $profile->id());
+        $res = $res->fetchAllAssoc();
+        $pro = array();
         foreach ($res as $res_it) {
-            if ($alias == $res_it['alias']) {
-                $email_type = "X";
+            if ($res_it['email'] != '') {
+                $pro[] = $res_it['email'];
+                if ($email_directory == $res_it['email']) {
+                    $email_type = "pro";
+                }
             }
         }
+        $page->assign('list_email_pro', $pro);
     }
 
-    require_once 'emails.inc.php';
-    $redirect = new Redirect($user);
-    $redir    = array();
-    foreach ($redirect->emails as $redirect_it) {
-        if ($redirect_it instanceof EmailRedirection) {
-            $redir[] = $redirect_it->email;
-            if ($email_directory == $redirect_it->email) {
-                $email_type = "redir";
+    if ($user) {
+        $melix = $user->emailAlias();
+        if ($melix) {
+            list($melix) = explode('@', $melix);
+            $page->assign('melix', $melix);
+            if (($domain == $globals->mail->alias_dom) || ($domain == $globals->mail->alias_dom2)) {
+                $email_type = "melix";
             }
         }
-    }
-    $page->assign('list_email_redir', $redir);
 
-    $res = XDB::query(
-            "SELECT  email
-               FROM  profile_job
-              WHERE  uid = {?}", $user->id());
-    $res = $res->fetchAllAssoc();
-    $pro = array();
-    foreach ($res as $res_it) {
-        if ($res_it['email'] != '') {
-            $pro[] = $res_it['email'];
-            if ($email_directory == $res_it['email']) {
-                $email_type = "pro";
+        $res = XDB::query(
+                "SELECT  alias
+                   FROM  aliases
+                  WHERE  uid={?} AND (type='a_vie' OR type='alias')", $user->id());
+        $res = $res->fetchAllAssoc();
+        $page->assign('list_email_X', $res);
+        if (($domain == $globals->mail->domain) || ($domain == $globals->mail->domain2)) {
+            foreach ($res as $res_it) {
+                if ($alias == $res_it['alias']) {
+                    $email_type = "X";
+                }
             }
         }
-    }
-    $page->assign('list_email_pro', $pro);
 
-    $page->assign('email_type', $email_type);
+        require_once 'emails.inc.php';
+        $redirect = new Redirect($user);
+        $redir    = array();
+        foreach ($redirect->emails as $redirect_it) {
+            if ($redirect_it instanceof EmailRedirection) {
+                $redir[] = $redirect_it->email;
+                if ($email_directory == $redirect_it->email) {
+                    $email_type = "redir";
+                }
+            }
+        }
+        $page->assign('list_email_redir', $redir);
+        $page->assign('email_type', $email_type);
+    } else {
+        $page->assign('list_email_X', array());
+        $page->assign('list_email_redir', array());
+        $page->assign('list_email_pro', array());
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: