3f00f83022cf765d042cd6901d5decab85d71074
[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
30 public $sn_old;
31 public $sn_new;
32 public $display_names;
33 public $old_alias;
34 public $new_alias;
35 public $sn_types;
36
37 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).";
38
39 // }}}
40 // {{{ constructor
41
42 public function __construct(User $_user, Profile $_profile, $_search_names, $_private_name_end)
43 {
44 parent::__construct($_user, $_profile, true, 'usage');
45 require_once 'name.func.inc.php';
46
47 $this->sn_types = build_types();
48 $this->sn_old = build_sn_pub($this->profile->id());
49 $this->sn_new = $_search_names;
50 $this->new_alias = true;
51 $this->display_names = array();
52
53 build_display_names($this->display_names, $_search_names,
54 $this->profile->isFemale(), $_private_name_end, $this->new_alias);
55 foreach ($this->sn_new AS $key => &$sn) {
56 if (!isset($sn['pub'])) {
57 unset($this->sn_new[$key]);
58 }
59 }
60
61 if (!is_null($this->profileOwner)) {
62 $this->old_alias = XDB::fetchOneCell('SELECT s.email
63 FROM email_source_account AS s
64 INNER JOIN email_virtual_domains AS d ON (s.domain = d.id)
65 WHERE s.uid = {?} AND s.type = \'alias\' AND FIND_IN_SET(\'usage\', s.flags) AND d.name = {?}',
66 $this->profileOwner->id(), Platal::globals()->mail->domain);
67 if ($this->old_alias != $this->new_alias) {
68 $used = XDB::fetchOneCell('SELECT COUNT(s.uid)
69 FROM email_source_account AS s
70 INNER JOIN email_virtual_domains AS d ON (s.domain = d.id)
71 WHERE s.email = {?} AND d.name = {?}',
72 $this->new_alias, Platal::globals()->mail->domain);
73 if ($used) {
74 $this->new_alias = null;
75 }
76 }
77 }
78 }
79
80 // }}}
81 // {{{ function formu()
82
83 public function formu()
84 {
85 return 'include/form.valid.names.tpl';
86 }
87
88 // }}}
89 // {{{ function _mail_subj()
90
91 protected function _mail_subj()
92 {
93 return "[Polytechnique.org/NOMS] Changement de noms";
94 }
95
96 // }}}
97 // {{{ function _mail_body
98
99 protected function _mail_body($isok)
100 {
101 global $globals;
102 if ($isok) {
103 $res = " Le changement de nom que tu as demandé vient d'être effectué.";
104 if (!is_null($this->profileOwner)) {
105 if ($this->old_alias != $this->new_alias) {
106 if ($this->old_alias) {
107 $res .= "\n\n Les alias {$this->old_alias}@{$globals->mail->domain} et @{$globals->mail->domain2} ont été supprimés.";
108 }
109 if ($this->new_alias) {
110 $res .= "\n\n Les alias {$this->new_alias}@{$globals->mail->domain} et @{$globals->mail->domain2} sont maintenant à ta disposition !";
111 }
112 }
113 if ($globals->mailstorage->googleapps_domain) {
114 require_once 'googleapps.inc.php';
115 $account = new GoogleAppsAccount($this->profileOwner);
116 if ($account->active()) {
117 $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.";
118 }
119 }
120 }
121 return $res;
122 } else {
123 return " La demande de changement de nom que tu avais faite a été refusée.";
124 }
125 }
126
127 // }}}
128 // {{{ function commit()
129
130 public function commit()
131 {
132 require_once 'name.func.inc.php';
133
134 set_profile_display($this->display_names, $this->profile);
135
136 if (!is_null($this->profileOwner)) {
137 set_alias_names($this->sn_new, $this->sn_old, $this->profile->id(),
138 $this->profileOwner->id(), true, $this->new_alias);
139
140 // Update the local User object, to pick up the new bestalias.
141 $this->profileOwner = User::getSilentWithUID($this->profileOwner->id());
142 }
143
144 return true;
145 }
146
147 // }}}
148 }
149 // }}}
150
151 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
152 ?>