Implements best_domain (Closes #1404).
authorStéphane Jacob <sj@m4x.org>
Mon, 7 Mar 2011 15:33:08 +0000 (16:33 +0100)
committerStéphane Jacob <sj@m4x.org>
Mon, 7 Mar 2011 15:33:08 +0000 (16:33 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
ChangeLog
classes/user.php
include/emails.inc.php
include/vcard.inc.php
modules/carnet/outlook.inc.php
modules/email.php
templates/emails/index.tpl
upgrade/1.1.0/13_best_domain.sql [new file with mode: 0644]
upgrade/1.1.0/best_domain.php [new file with mode: 0755]

index 5ba7dd4..26e5f8e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,7 @@ Bug/Wish:
     * Emails:
         - Allows antispam level choice for each redirection                -JAC
         - #1035: Bases emails on users types (eg educationnal types)       -JAC
+        - #1404: Implements best_domain                                    -JAC
 
     * Payments:
         - #1398: Creates csv for payments                                  -JAC
index a169ebb..b11bacf 100644 (file)
@@ -163,7 +163,6 @@ class User extends PlUser
                                       IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', mf.name)) AS forlife,
                                       IF(ef.email IS NULL, NULL, CONCAT(ef.email, \'@\', df.name)) AS forlife_alternate,
                                       IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', mb.name)) AS bestalias,
-                                      IF(eb.email IS NULL, NULL, CONCAT(eb.email, \'@\', db.name)) AS bestalias_alternate,
                                       (er.redirect IS NULL AND a.state = \'active\') AS lost,
                                       a.email, a.full_name, a.directory_name, a.display_name, a.sex = \'female\' AS gender,
                                       IF(a.state = \'active\', CONCAT(at.perms, \',\', IF(a.user_perms IS NULL, \'\', a.user_perms)), \'\') AS perms,
@@ -180,9 +179,7 @@ class User extends PlUser
                            LEFT JOIN  email_virtual_domains  AS df ON (df.aliasing = mf.id AND
                                                                        df.name LIKE CONCAT(\'%\', {?}) AND df.name NOT LIKE \'alumni.%\')
                            LEFT JOIN  email_source_account   AS eb ON (eb.uid = a.uid AND eb.flags = \'bestalias\')
-                           LEFT JOIN  email_virtual_domains  AS mb ON (eb.domain = mb.id)
-                           LEFT JOIN  email_virtual_domains  AS db ON (db.aliasing = mb.id AND
-                                                                       db.name LIKE CONCAT(\'%\', {?}) AND db.name NOT LIKE \'alumni.%\')
+                           LEFT JOIN  email_virtual_domains  AS mb ON (a.best_domain = mb.id)
                            LEFT JOIN  email_redirect_account AS er ON (er.uid = a.uid AND er.flags = \'active\' AND er.broken_level < 3
                                                                        AND er.type != \'imap\' AND er.type != \'homonym\')
                            LEFT JOIN  homonyms_list          AS h  ON (h.uid = a.uid)
index aac9a25..0ff0d33 100644 (file)
@@ -176,6 +176,22 @@ function mark_broken_email($email, $admin = false)
 // eventually selects a new bestalias when required.
 function fix_bestalias(User $user)
 {
+    // First check if best_domain is properly set.
+    $count = XDB::fetchOneCell('SELECT  COUNT(*)
+                                  FROM  accounts              AS a
+                            INNER JOIN  email_virtual_domains AS d ON (d.id = a.best_domain)
+                            INNER JOIN  email_virtual_domains AS m ON (d.aliasing = m.id)
+                                 WHERE  a.uid = {?} AND m.name = {?}',
+                               $user->id(), $user->mainEmailDomain());
+    if ($count == 0) {
+        XDB::execute('UPDATE  accounts              AS a
+                  INNER JOIN  email_virtual_domains AS d ON (d.name = {?})
+                         SET  a.best_domain = d.id
+                       WHERE  a.uid = {?}',
+                     $user->mainEmailDomain(), $user->id());
+    }
+
+    // Then check the alias.
     $count = XDB::fetchOneCell('SELECT  COUNT(*)
                                   FROM  email_source_account
                                  WHERE  uid = {?} AND FIND_IN_SET(\'bestalias\', flags) AND expire IS NULL',
index ddaa420..99086cc 100644 (file)
@@ -75,9 +75,10 @@ class VCard extends PlVCard
         // Emails
         if (!is_null($user)) {
             $entry->addMail(null, $user->bestalias, true);
-            $entry->addMail(null, $user->bestalias_alternate);
             if ($user->forlife != $user->bestalias) {
                 $entry->addMail(null, $user->forlife);
+            }
+            if ($user->forlife_alternate != $user->bestalias) {
                 $entry->addMail(null, $user->forlife_alternate);
             }
         }
index e7eb413..1f33f0a 100644 (file)
@@ -91,8 +91,6 @@ class Outlook {
         if ($user) {
             $contact['Adresse de messagerie'] = $user->bestalias;
             $contact['Nom complet de l\'adresse de messagerie'] = $p->fullName().' <'.$user->bestalias.'>';
-            $contact['Adresse de messagerie 2'] = $user->bestalias_alternate;
-            $contact['Nom complet de l\'adresse de messagerie 2'] = $p->fullName().' <'.$user->bestalias_alternate.'>';
             if ($user->bestalias != $user->forlife) {
                 $contact['Adresse de messagerie 3'] = $user->forlife;
                 $contact['Nom complet de l\'adresse de messagerie 3'] = $p->fullName().' <'.$user->forlife.'>';
index d412b2a..ef77898 100644 (file)
@@ -71,6 +71,11 @@ class EmailModule extends PLModule
                       INNER JOIN  email_virtual_domains AS d ON (s.domain = d.id)
                              SET  s.flags = CONCAT_WS(',', IF(s.flags = '', NULL, s.flags), 'bestalias')
                            WHERE  s.uid = {?} AND s.email = {?} AND d.name = {?}", $user->id(), $email, $domain);
+            XDB::execute('UPDATE  accounts              AS a
+                      INNER JOIN  email_virtual_domains AS d ON (d.name = {?})
+                             SET  a.best_domain = d.id
+                           WHERE  a.uid = {?}',
+                         $domain, $user->id());
 
             // As having a non-null bestalias value is critical in
             // plat/al's code, we do an a posteriori check on the
@@ -80,11 +85,13 @@ class EmailModule extends PLModule
 
         // Fetch and display aliases.
         $aliases = XDB::iterator("SELECT  CONCAT(s.email, '@', d.name) AS email, (s.type = 'forlife') AS forlife,
-                                          (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year,
-                                          FIND_IN_SET('bestalias', s.flags) AS bestalias, s.expire,
-                                          (s.type = 'alias_aux') AS alias
+                                          (s.email REGEXP '\\\\.[0-9]{2}$') AS hundred_year, s.expire,
+                                          (FIND_IN_SET('bestalias', s.flags) AND a.best_domain = d.id) AS bestalias,
+                                          ((s.type = 'alias_aux') AND d.aliasing = d.id) AS alias
                                     FROM  email_source_account  AS s
-                              INNER JOIN  email_virtual_domains AS d ON (s.domain = d.id)
+                              INNER JOIN  accounts              AS a ON (s.uid = a.uid)
+                              INNER JOIN  email_virtual_domains AS m ON (s.domain = m.id)
+                              INNER JOIN  email_virtual_domains AS d ON (d.aliasing = m.id)
                                    WHERE  s.uid = {?}
                                 ORDER BY  !alias, s.email",
                                  $user->id());
index 9c545bf..c8bafdd 100644 (file)
@@ -46,8 +46,7 @@
       {iterate from=$aliases item=a}
       <label>
         <input type='radio' {if $a.bestalias}checked="checked"{/if} name='best' value='{$a.email}' />
-        <strong>{$a.email}</strong>{if $a.alias} et <strong>@{#globals.mail.alias_dom2#}</strong>{else}
-        {foreach from=$mail_domains item=domain} et <strong>@{$domain}</strong>{/foreach}{/if}
+        <strong>{$a.email}</strong>
       </label>&nbsp;{if $a.forlife}(**){/if}{if $a.hundred_year}(*){/if}
       {if $a.expire}<span class='erreur'>(expire le {$a.expire|date_format})</span>{/if}
       {if $a.alias}<a href="emails/alias">(changer ou supprimer mon alias melix)</a>{/if}
diff --git a/upgrade/1.1.0/13_best_domain.sql b/upgrade/1.1.0/13_best_domain.sql
new file mode 100644 (file)
index 0000000..6988e99
--- /dev/null
@@ -0,0 +1,4 @@
+ALTER TABLE accounts ADD COLUMN best_domain SMALLINT(3) UNSIGNED DEFAULT NULL;
+ALTER TABLE accounts ADD FOREIGN KEY (best_domain) REFERENCES email_virtual_domains (id) ON DELETE CASCADE ON UPDATE CASCADE;
+
+-- vim:set syntax=mysql:
diff --git a/upgrade/1.1.0/best_domain.php b/upgrade/1.1.0/best_domain.php
new file mode 100755 (executable)
index 0000000..aaf8291
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/php5
+<?php
+
+require_once 'connect.db.inc.php';
+
+$globals->debug = 0; // Do not store backtraces.
+
+$domains = array(
+    'x'      => 'polytechnique.org',
+    'master' => 'master.polytechnique.org',
+    'phd'    => 'doc.polytechnique.org'
+);
+$domains_ids = XDB::fetchAllAssoc('name', 'SELECT  name, id
+                                             FROM  email_virtual_domains
+                                            WHERE  name IN {?}',
+                                  $domains);
+foreach ($domains as $type => $domain) {
+    XDB::execute('UPDATE  accounts
+                     SET  best_domain = {?}
+                   WHERE  type = {?}',
+                 $domains_ids[$domain], $type);
+}
+
+/* vim:set et sw=4 sts=4 ts=4: */
+?>