Merge remote branch 'origin/xorg/f/geocoding' into xorg/master
[platal.git] / include / validations / names.inc.php
index 2c3ff1a..8590eb7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 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                *
  ***************************************************************************/
 
-// {{{ class NamesReq4
+// {{{ class NamesReq
 
-class NamesReq extends Validate
+class NamesReq extends ProfileValidate
 {
     // {{{ properties
 
     public $unique = true;
-
-    public $sn_old;
-    public $sn_new;
-    public $display_names;
-    public $old_alias;
-    public $new_alias;
-    public $sn_types;
-
+    public $public_names;
+    public $old_public_names;
+    public $old_alias = null;
+    public $new_alias = null;
+    public $descriptions = array('lastname_main' => 'Nom patronymique', 'lastname_marital' => 'Nom marital', 'lastname_ordinary' => 'Nom usuel', 'firstname_main' => 'Prénom', 'firstname_ordinary' => 'Prénom usuel', 'pseudonym' => 'Pseudonyme (nom de plume)');
     public $rules = "Refuser tout ce qui n'est visiblement pas un nom de famille (ce qui est extremement rare car à peu près n'importe quoi peut être un nom de famille).";
 
     // }}}
     // {{{ constructor
 
-    public function __construct(User &$_user, $_search_names, $_private_name_end)
+    public function __construct(User $_user, Profile $_profile, array $_public_names, array $_old_public_names)
     {
-        parent::__construct($_user, true, 'usage');
-        require_once 'name.func.inc.php';
-
-        $this->sn_types  = build_types();
-        $this->sn_old    = build_sn_pub();
-        $this->sn_new    = $_search_names;
-        $this->new_alias = true;
-        $this->display_names = array();
-
-        build_display_names($this->display_names, $_search_names, $_private_name_end, $this->new_alias);
-        foreach ($this->sn_new AS $key => &$sn) {
-            if (!isset($sn['pub'])) {
-                unset($this->sn_new[$key]);
-            }
-        }
-        $res = XDB::query("SELECT  alias
-                             FROM  aliases
-                            WHERE  id = {?} AND type = 'alias' AND FIND_IN_SET('usage', flags)",
-                          $this->user->id());
-        $this->old_alias  = $res->fetchOneCell();
-        if ($this->old_alias != $this->new_alias) {
-            $res = XDB::query("SELECT  id
-                                 FROM  aliases
-                                WHERE  alias = {?}",
-                              $this->new_alias);
-            if ($res->fetchOneCell()) {
-                $this->new_alias = null;
+        parent::__construct($_user, $_profile, true, 'usage');
+
+        $this->public_names = $_public_names;
+        $this->old_public_names = $_old_public_names;
+
+        if (!is_null($this->profileOwner)) {
+            require_once 'name.func.inc.php';
+
+            $this->new_alias = build_email_alias($this->public_names);
+            $this->old_alias = XDB::fetchOneCell('SELECT  email
+                                                    FROM  email_source_account
+                                                   WHERE  uid = {?} AND type = \'alias\' AND FIND_IN_SET(\'usage\', flags)',
+                                                 $this->profileOwner->id());
+
+            if ($this->old_alias == $this->new_alias) {
+                $this->old_alias = $this->new_alias = null;
+            } else {
+                $used = XDB::fetchOneCell('SELECT  COUNT(uid)
+                                             FROM  email_source_account
+                                            WHERE  email = {?} AND type != \'alias_aux\'',
+                                          $this->new_alias);
+                if ($used) {
+                    $this->new_alias = null;
+                }
             }
         }
     }
@@ -93,22 +87,23 @@ class NamesReq extends Validate
 
     protected function _mail_body($isok)
     {
-        global $globals;
         if ($isok) {
             $res = "  Le changement de nom que tu as demandé vient d'être effectué.";
-            if ($this->old_alias != $this->new_alias) {
-                if ($this->old_alias) {
-                    $res .= "\n\n  Les alias {$this->old_alias}@{$globals->mail->domain} et @{$globals->mail->domain2} ont été supprimés.";
-                }
-                if ($this->new_alias) {
-                    $res .= "\n\n  Les alias {$this->new_alias}@{$globals->mail->domain} et @{$globals->mail->domain2} sont maintenant à ta disposition !";
+            if (!is_null($this->profileOwner)) {
+                if ($this->old_alias != $this->new_alias) {
+                    if ($this->old_alias) {
+                        $res .= "\n\n  L'alias {$this->old_alias}@{$this->mail_domain} a été supprimé.";
+                    }
+                    if ($this->new_alias) {
+                        $res .= "\n\n  L'alias {$this->new_alias}@{$this->mail_domain} est maintenant à ta disposition !";
+                    }
                 }
-            }
-            if ($globals->mailstorage->googleapps_domain) {
-                require_once 'googleapps.inc.php';
-                $account = new GoogleAppsAccount($this->user);
-                if ($account->active()) {
-                    $res .= "\n\n  Si tu utilises Google Apps, tu peux changer ton nom d'usage sur https://mail.google.com/a/polytechnique.org/#settings/accounts.";
+                if ($globals->mailstorage->googleapps_domain) {
+                    require_once 'googleapps.inc.php';
+                    $account = new GoogleAppsAccount($this->profileOwner);
+                    if ($account->active()) {
+                        $res .= "\n\n  Si tu utilises Google Apps, tu peux changer ton nom d'usage sur https://mail.google.com/a/polytechnique.org/#settings/accounts.";
+                    }
                 }
             }
             return $res;
@@ -122,16 +117,47 @@ class NamesReq extends Validate
 
     public function commit()
     {
-        require_once 'notifs.inc.php';
         require_once 'name.func.inc.php';
 
-        register_watch_op($this->user->id(), WATCH_FICHE, '', 'search_names');
-        set_profile_display($this->display_names);
-        set_alias_names($this->sn_new, $this->sn_old, true, $this->new_alias);
+        update_public_names($this->profile->id(), $this->public_names);
+        update_display_names($this->profile, $this->public_names);
+
+        if (!is_null($this->profileOwner)) {
+            if (!is_null($this->old_alias)) {
+                XDB::execute('DELETE FROM  email_source_account
+                                    WHERE  FIND_IN_SET(\'usage\', flags) AND uid = {?} AND type = \'alias\'',
+                             $this->profileOwner->id());
+            }
+            if (!is_null($this->new_alias)) {
+                XDB::execute('INSERT INTO  email_source_account (email, uid, type, flags, domain)
+                                   SELECT  {?}, {?}, \'alias\', \'usage\', id
+                                     FROM  email_virtual_domains
+                                    WHERE  name = {?}',
+                             $this->new_alias, $this->profileOwner->id(), $this->profileOwner->mainEmailDomain());
+            }
+            require_once 'emails.inc.php';
+            fix_bestalias($this->profileOwner);
+
+            // Update the local User object, to pick up the new bestalias.
+            $this->profileOwner = User::getSilentWithUID($this->profileOwner->id());
+        }
+
         return true;
     }
 
     // }}}
+    // {{{ function getPublicNames()
+
+    static public function getPublicNames($pid)
+    {
+        if ($request = parent::get_typed_request($pid, 'usage')) {
+            return $request->public_names;
+        }
+        return false;
+    }
+
+    // }}}
+
 }
 // }}}