8590eb7f47896fcfcfe38d4dc4f7836e11ccd8ef
[platal.git] / include / validations / names.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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
22 // {{{ class NamesReq
23
24 class NamesReq extends ProfileValidate
25 {
26 // {{{ properties
27
28 public $unique = true;
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)');
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).";
35
36 // }}}
37 // {{{ constructor
38
39 public function __construct(User $_user, Profile $_profile, array $_public_names, array $_old_public_names)
40 {
41 parent::__construct($_user, $_profile, true, 'usage');
42
43 $this->public_names = $_public_names;
44 $this->old_public_names = $_old_public_names;
45
46 if (!is_null($this->profileOwner)) {
47 require_once 'name.func.inc.php';
48
49 $this->new_alias = build_email_alias($this->public_names);
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());
54
55 if ($this->old_alias == $this->new_alias) {
56 $this->old_alias = $this->new_alias = null;
57 } else {
58 $used = XDB::fetchOneCell('SELECT COUNT(uid)
59 FROM email_source_account
60 WHERE email = {?} AND type != \'alias_aux\'',
61 $this->new_alias);
62 if ($used) {
63 $this->new_alias = null;
64 }
65 }
66 }
67 }
68
69 // }}}
70 // {{{ function formu()
71
72 public function formu()
73 {
74 return 'include/form.valid.names.tpl';
75 }
76
77 // }}}
78 // {{{ function _mail_subj()
79
80 protected function _mail_subj()
81 {
82 return "[Polytechnique.org/NOMS] Changement de noms";
83 }
84
85 // }}}
86 // {{{ function _mail_body
87
88 protected function _mail_body($isok)
89 {
90 if ($isok) {
91 $res = " Le changement de nom que tu as demandé vient d'être effectué.";
92 if (!is_null($this->profileOwner)) {
93 if ($this->old_alias != $this->new_alias) {
94 if ($this->old_alias) {
95 $res .= "\n\n L'alias {$this->old_alias}@{$this->mail_domain} a été supprimé.";
96 }
97 if ($this->new_alias) {
98 $res .= "\n\n L'alias {$this->new_alias}@{$this->mail_domain} est maintenant à ta disposition !";
99 }
100 }
101 if ($globals->mailstorage->googleapps_domain) {
102 require_once 'googleapps.inc.php';
103 $account = new GoogleAppsAccount($this->profileOwner);
104 if ($account->active()) {
105 $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.";
106 }
107 }
108 }
109 return $res;
110 } else {
111 return " La demande de changement de nom que tu avais faite a été refusée.";
112 }
113 }
114
115 // }}}
116 // {{{ function commit()
117
118 public function commit()
119 {
120 require_once 'name.func.inc.php';
121
122 update_public_names($this->profile->id(), $this->public_names);
123 update_display_names($this->profile, $this->public_names);
124
125 if (!is_null($this->profileOwner)) {
126 if (!is_null($this->old_alias)) {
127 XDB::execute('DELETE FROM email_source_account
128 WHERE FIND_IN_SET(\'usage\', flags) AND uid = {?} AND type = \'alias\'',
129 $this->profileOwner->id());
130 }
131 if (!is_null($this->new_alias)) {
132 XDB::execute('INSERT INTO email_source_account (email, uid, type, flags, domain)
133 SELECT {?}, {?}, \'alias\', \'usage\', id
134 FROM email_virtual_domains
135 WHERE name = {?}',
136 $this->new_alias, $this->profileOwner->id(), $this->profileOwner->mainEmailDomain());
137 }
138 require_once 'emails.inc.php';
139 fix_bestalias($this->profileOwner);
140
141 // Update the local User object, to pick up the new bestalias.
142 $this->profileOwner = User::getSilentWithUID($this->profileOwner->id());
143 }
144
145 return true;
146 }
147
148 // }}}
149 // {{{ function getPublicNames()
150
151 static public function getPublicNames($pid)
152 {
153 if ($request = parent::get_typed_request($pid, 'usage')) {
154 return $request->public_names;
155 }
156 return false;
157 }
158
159 // }}}
160
161 }
162 // }}}
163
164 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
165 ?>