Fix alias overriding bug when changing names
[platal.git] / include / validations / names.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
ba6ae046 3 * Copyright (C) 2003-2013 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
453cbb00 22// {{{ class NamesReq
0337d704 23
c3d1e6b6 24class NamesReq extends ProfileValidate
0337d704 25{
26 // {{{ properties
27
612a2d8a 28 public $unique = true;
0e1dfbad
SJ
29 public $public_names;
30 public $old_public_names;
31 public $old_alias = null;
32 public $new_alias = null;
33 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)');
dced83b4 34 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).";
0337d704 35
36 // }}}
37 // {{{ constructor
38
0e1dfbad 39 public function __construct(User $_user, Profile $_profile, array $_public_names, array $_old_public_names)
0337d704 40 {
c3d1e6b6 41 parent::__construct($_user, $_profile, true, 'usage');
dced83b4 42
0e1dfbad
SJ
43 $this->public_names = $_public_names;
44 $this->old_public_names = $_old_public_names;
c3d1e6b6
SJ
45
46 if (!is_null($this->profileOwner)) {
0e1dfbad
SJ
47 require_once 'name.func.inc.php';
48
49 $this->new_alias = build_email_alias($this->public_names);
f036c896
SJ
50 $this->old_alias = XDB::fetchOneCell('SELECT email
51 FROM email_source_account
52 WHERE uid = {?} AND type = \'alias\' AND FIND_IN_SET(\'usage\', flags)',
53 $this->profileOwner->id());
0e1dfbad
SJ
54
55 if ($this->old_alias == $this->new_alias) {
56 $this->old_alias = $this->new_alias = null;
57 } else {
f036c896
SJ
58 $used = XDB::fetchOneCell('SELECT COUNT(uid)
59 FROM email_source_account
60 WHERE email = {?} AND type != \'alias_aux\'',
61 $this->new_alias);
e88ea173
NI
62 if (!$used) {
63 // Check against homonyms
64 $used = XDB::fetchOneCell('SELECT COUNT(email)
65 FROM email_source_other
66 WHERE email = {?}',
67 $this->new_alias);
68 }
c0436d0b 69 if ($used) {
c3d1e6b6
SJ
70 $this->new_alias = null;
71 }
dced83b4 72 }
5daf68f6 73 }
0337d704 74 }
75
76 // }}}
0337d704 77 // {{{ function formu()
78
612a2d8a 79 public function formu()
80 {
dced83b4 81 return 'include/form.valid.names.tpl';
612a2d8a 82 }
0337d704 83
84 // }}}
85 // {{{ function _mail_subj()
86
612a2d8a 87 protected function _mail_subj()
0337d704 88 {
dced83b4 89 return "[Polytechnique.org/NOMS] Changement de noms";
0337d704 90 }
91
92 // }}}
93 // {{{ function _mail_body
94
612a2d8a 95 protected function _mail_body($isok)
0337d704 96 {
0337d704 97 if ($isok) {
dced83b4 98 $res = " Le changement de nom que tu as demandé vient d'être effectué.";
c3d1e6b6
SJ
99 if (!is_null($this->profileOwner)) {
100 if ($this->old_alias != $this->new_alias) {
101 if ($this->old_alias) {
f036c896 102 $res .= "\n\n L'alias {$this->old_alias}@{$this->mail_domain} a été supprimé.";
c3d1e6b6
SJ
103 }
104 if ($this->new_alias) {
f036c896 105 $res .= "\n\n L'alias {$this->new_alias}@{$this->mail_domain} est maintenant à ta disposition !";
c3d1e6b6 106 }
dced83b4 107 }
c3d1e6b6
SJ
108 if ($globals->mailstorage->googleapps_domain) {
109 require_once 'googleapps.inc.php';
110 $account = new GoogleAppsAccount($this->profileOwner);
111 if ($account->active()) {
112 $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.";
113 }
fc0342c3
VZ
114 }
115 }
0337d704 116 return $res;
117 } else {
dced83b4 118 return " La demande de changement de nom que tu avais faite a été refusée.";
0337d704 119 }
120 }
121
122 // }}}
123 // {{{ function commit()
124
612a2d8a 125 public function commit()
0337d704 126 {
6cb58d39 127 require_once 'name.func.inc.php';
dced83b4 128
0e1dfbad
SJ
129 update_public_names($this->profile->id(), $this->public_names);
130 update_display_names($this->profile, $this->public_names);
01ebbdbf 131
c3d1e6b6 132 if (!is_null($this->profileOwner)) {
0e1dfbad
SJ
133 if (!is_null($this->old_alias)) {
134 XDB::execute('DELETE FROM email_source_account
135 WHERE FIND_IN_SET(\'usage\', flags) AND uid = {?} AND type = \'alias\'',
136 $this->profileOwner->id());
137 }
138 if (!is_null($this->new_alias)) {
139 XDB::execute('INSERT INTO email_source_account (email, uid, type, flags, domain)
140 SELECT {?}, {?}, \'alias\', \'usage\', id
141 FROM email_virtual_domains
142 WHERE name = {?}',
143 $this->new_alias, $this->profileOwner->id(), $this->profileOwner->mainEmailDomain());
144 }
145 require_once 'emails.inc.php';
146 fix_bestalias($this->profileOwner);
c3d1e6b6
SJ
147
148 // Update the local User object, to pick up the new bestalias.
14da7ef4 149 $this->profileOwner = User::getSilentWithUID($this->profileOwner->id());
c3d1e6b6 150 }
01ebbdbf 151
0337d704 152 return true;
153 }
154
155 // }}}
0e1dfbad
SJ
156 // {{{ function getPublicNames()
157
158 static public function getPublicNames($pid)
159 {
160 if ($request = parent::get_typed_request($pid, 'usage')) {
161 return $request->public_names;
162 }
163 return false;
164 }
165
166 // }}}
167
0337d704 168}
169// }}}
170
a7de4ef7 171// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 172?>