Adds first and last names to accounts.
[platal.git] / include / name.func.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 function build_javascript_names($data, $isFemale)
23 {
24 $data_array = explode(';;', $data);
25 $n = count($data_array);
26 $n--;
27 for ($i = 0; $i < $n; $i++) {
28 $searchname = explode(';', $data_array[$i]);
29 if (isset($search_names[$searchname[0]])) {
30 $search_names[$searchname[0]][] = $searchname[1];
31 } else {
32 $search_names[$searchname[0]] = array('fullname' => $searchname[1]);
33 }
34 }
35
36 $sn_types_public = build_types('public');
37 $sn_types_private = build_types('private');
38 $full_name = build_full_name($search_names, $sn_types_public, $isFemale);
39 return build_public_name($search_names, $sn_types_public, $full_name) . ';' .
40 build_private_name($search_names, $sn_types_private);
41 }
42
43 function build_display_names(&$display_names, $search_names, $isFemale, $private_name_end = null, &$alias = null)
44 {
45 $sn_types_public = build_types('public');
46 $full_name = build_full_name($search_names, $sn_types_public, $isFemale);
47 $display_names['public_name'] = build_public_name($search_names, $sn_types_public, $full_name);
48 $display_names['private_name'] = $display_names['public_name'] . $private_name_end;
49 $display_names['directory_name'] = build_directory_name($search_names, $sn_types_public, $full_name);
50 $display_names['short_name'] = build_short_name($search_names, $sn_types_public, $alias);
51 $display_names['sort_name'] = build_sort_name($search_names, $sn_types_public);
52 }
53
54 function build_types($pub = null)
55 {
56 if ($pub == 'public') {
57 $sql_pub = "AND FIND_IN_SET('public', flags)";
58 } elseif ($pub == 'private') {
59 $sql_pub = "AND NOT FIND_IN_SET('public', flags)";
60 } else {
61 $sql_pub = "";
62 }
63 $sql = "SELECT id, type, name
64 FROM profile_name_enum
65 WHERE NOT FIND_IN_SET('not_displayed', flags)" . $sql_pub;
66 $sn_types = XDB::iterator($sql);
67 $types = array();
68 while ($sn_type = $sn_types->next()) {
69 if ($pub) {
70 $types[$sn_type['type']] = $sn_type['id'];
71 } else {
72 $types[$sn_type['id']] = $sn_type['name'];
73 }
74 }
75 return $types;
76 }
77
78 function build_full_name(&$search_names, &$sn_types, $isFemale)
79 {
80 $name = "";
81 if (isset($search_names[$sn_types['lastname_ordinary']])) {
82 $name .= $search_names[$sn_types['lastname_ordinary']]['fullname'] . " ("
83 . $search_names[$sn_types['lastname']]['fullname'] . ")";
84 } else {
85 $name .= $search_names[$sn_types['lastname']]['fullname'];
86 }
87 if (isset($search_names[$sn_types['lastname_marital']])
88 || isset($search_names[$sn_types['pseudonym']])) {
89 $name .= " (";
90 if (isset($search_names[$sn_types['lastname_marital']])) {
91 if ($isFemale) {
92 $name .= "Mme ";
93 } else {
94 $name .= "M ";
95 }
96 $name .= $search_names[$sn_types['lastname_marital']]['fullname'];
97 if (isset($search_names[$sn_types['pseudonym']])) {
98 $name .= ", ";
99 }
100 }
101 if (isset($search_names[$sn_types['pseudonym']])) {
102 $name .= $search_names[$sn_types['pseudonym']]['fullname'];
103 }
104 $name .= ")";
105 }
106 return $name;
107 }
108
109 function build_public_name(&$search_names, &$sn_types, $full_name)
110 {
111 return $search_names[$sn_types['firstname']]['fullname'] . " " . $full_name;
112 }
113
114 function build_private_name(&$search_names, &$sn_types)
115 {
116 $name = "";
117 if (isset($search_names[$sn_types['nickname']])
118 || (isset($search_names[$sn_types['name_other']])
119 || isset($search_names[$sn_types['name_other']]))) {
120 $name .= " (";
121 if (isset($search_names[$sn_types['nickname']])) {
122 $name .= "alias " . $search_names[$sn_types['nickname']]['fullname'];
123 $i = 0;
124 while (isset($search_names[$sn_types['nickname']][$i])) {
125 $name .= ", " . $search_names[$sn_types['nickname']][$i];
126 $i++;
127 }
128 if (isset($search_names[$sn_types['name_other']])
129 || isset($search_names[$sn_types['firstname_other']])) {
130 $name .= ", ";
131 }
132 }
133 if (isset($search_names[$sn_types['firstname_other']])) {
134 $name .= "autres prénoms : " . $search_names[$sn_types['firstname_other']]['fullname'];
135 $i = 0;
136 while (isset($search_names[$sn_types['firstname_other']][$i])) {
137 $name .= ", " . $search_names[$sn_types['firstname_other']][$i];
138 $i++;
139 }
140 if (isset($search_names[$sn_types['name_other']])) {
141 $name .= ", ";
142 }
143 }
144 if (isset($search_names[$sn_types['name_other']])) {
145 $name .= "autres noms : " . $search_names[$sn_types['name_other']]['fullname'];
146 $i = 0;
147 while (isset($search_names[$sn_types['name_other']][$i])) {
148 $name .= ", " . $search_names[$sn_types['name_other']][$i];
149 $i++;
150 }
151 }
152 $name .= ")";
153 }
154 return $name;
155 }
156
157 function build_directory_name(&$search_names, &$sn_types, $full_name)
158 {
159 return $full_name . " " . $search_names[$sn_types['firstname']]['fullname'];
160 }
161
162 function build_short_name(&$search_names, &$sn_types, &$alias = null)
163 {
164 if (isset($search_names[$sn_types['lastname_ordinary']])) {
165 $lastname = $search_names[$sn_types['lastname_ordinary']]['fullname'];
166 } else {
167 $lastname = $search_names[$sn_types['lastname']]['fullname'];
168 }
169 if (isset($search_names[$sn_types['firstname_ordinary']])) {
170 $firstname = $search_names[$sn_types['firstname_ordinary']]['fullname'];
171 } else {
172 $firstname = $search_names[$sn_types['firstname']]['fullname'];
173 }
174 if ($alias) {
175 $alias = PlUser::makeUserName($firstname, $lastname);
176 }
177 return $firstname . " " . $lastname;
178 }
179
180 function build_sort_name(&$search_names, &$sn_types)
181 {
182 $name = "";
183 if (isset($search_names[$sn_types['lastname_ordinary']])) {
184 $name .= $search_names[$sn_types['lastname_ordinary']]['name'];
185 } else {
186 $name .= $search_names[$sn_types['lastname']]['name'];
187 }
188 $name .= " " . $search_names[$sn_types['firstname']]['fullname'];
189 return $name;
190 }
191
192 function set_profile_display(&$display_names, Profile $profile)
193 {
194 XDB::execute("UPDATE profile_display
195 SET public_name = {?}, private_name = {?},
196 directory_name = {?}, short_name = {?}, sort_name = {?}
197 WHERE pid = {?}",
198 $display_names['public_name'], $display_names['private_name'],
199 $display_names['directory_name'], $display_names['short_name'],
200 $display_names['sort_name'], $profile->id());
201
202 $owner = $profile->owner();
203 if ($owner) {
204 XDB::execute('UPDATE accounts
205 SET full_name = {?}, directory_name = {?}
206 WHERE uid = {?}',
207 $display_names['public_name'], $display_names['directory_name'], $owner->id());
208 }
209 }
210
211 function build_sn_pub($pid)
212 {
213 $res = XDB::iterator("SELECT CONCAT(sn.particle, sn.name) AS fullname, sn.typeid,
214 sn.particle, sn.name, sn.id
215 FROM profile_name AS sn
216 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
217 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
218 AND FIND_IN_SET('public', e.flags)
219 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
220 $pid);
221 $sn_old = array();
222 while ($old = $res->next()) {
223 $sn_old[$old['typeid']] = array('fullname' => $old['fullname'],
224 'name' => $old['name'],
225 'particle' => $old['particle'],
226 'id' => $old['id']);
227 }
228 return $sn_old;
229 }
230
231 /** Splits a name into tokens, as used in search_name.
232 * Used for search_name rebuilding and for queries.
233 */
234 function split_name_for_search($name) {
235 return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)),
236 -1, PREG_SPLIT_NO_EMPTY);
237 }
238
239 /** Transform a name to its canonical value so it can be compared
240 * to another form (different case, with accents or with - instead
241 * of blanks).
242 * @see compare_basename to compare
243 */
244 function name_to_basename($value) {
245 $value = mb_strtoupper(replace_accent($value));
246 return preg_replace('/[^A-Z]/', ' ', $value);
247 }
248
249 /** Compares two strings and check if they are two forms of the
250 * same name (different case, with accents or with - instead of
251 * blanks).
252 * @see name_to_basename to retreive the compared string
253 */
254 function compare_basename($a, $b) {
255 return name_to_basename($a) == name_to_basename($b);
256 }
257
258 function set_alias_names(&$sn_new, $sn_old, $pid, PlUser $user, $update_new = false, $new_alias = null)
259 {
260 $has_new = false;
261 foreach ($sn_new as $typeid => $sn) {
262 if (isset($sn['pub']) && !is_null($sn['fullname'])) {
263 if (isset($sn_old[$typeid]) && $update_new) {
264 XDB::execute("UPDATE profile_name
265 SET particle = {?}, name = {?}, typeid = {?}
266 WHERE id = {?} AND pid = {?}",
267 $sn['particle'], $sn['name'], $typeid, $sn_old[$typeid]['id'], $pid);
268 unset($sn_old[$typeid]);
269 } elseif ($sn['fullname'] == $sn_old[$typeid]['fullname'] && $sn['name'] == $sn_old[$typeid]['name']) {
270 XDB::execute('INSERT INTO profile_name (particle, name, typeid, pid)
271 VALUES ({?}, {?}, {?}, {?})',
272 $sn_old[$typeid]['particle'], $sn_old[$typeid]['name'], $typeid, $pid);
273 unset($sn_old[$typeid]);
274 } else {
275 $has_new = true;
276 }
277 } else {
278 if ($sn['fullname'] != '') {
279 XDB::execute("INSERT INTO profile_name (particle, name, typeid, pid)
280 VALUES ('', {?}, {?}, {?})",
281 $sn['fullname'], $typeid, $pid);
282 }
283 $i = 0;
284 while (isset($sn[$i])) {
285 XDB::execute("INSERT INTO profile_name (particle, name, typeid, pid)
286 VALUES ('', {?}, {?}, {?})",
287 $sn[$i], $typeid, $pid);
288 $i++;
289 }
290 }
291 }
292 if (count($sn_old) > 0) {
293 if (!$update_new) {
294 $has_new = true;
295 foreach ($sn_old as $typeid => $sn) {
296 XDB::execute("INSERT INTO profile_name (particle, name, typeid, pid)
297 VALUES ({?}, {?}, {?}, {?})",
298 $sn['particle'], $sn['name'], $typeid, $pid);
299 }
300 } else {
301 foreach ($sn_old as $typeid => $sn) {
302 XDB::execute("DELETE FROM profile_name
303 WHERE pid = {?} AND id = {?}",
304 $pid, $sn['id']);
305 }
306 }
307 }
308 if ($update_new) {
309 XDB::execute('DELETE FROM email_source_account
310 WHERE FIND_IN_SET(\'usage\', flags) AND uid = {?} AND type = \'alias\'',
311 $user->id());
312 }
313 if ($new_alias) {
314 XDB::execute('INSERT INTO email_source_account (email, uid, type, flags, domain)
315 SELECT {?}, {?}, \'alias\', \'usage\', id
316 FROM email_virtual_domains
317 WHERE name = {?}',
318 $new_alias, $user->id(), $user->mainEmailDomain());
319 }
320
321 // XXX: Improve this when we optimize names handling.
322 // Updates accounts firt and last names.
323 XDB::execute('UPDATE TABLE accounts AS a
324 INNER JOIN profile_name_enum AS le ON (le.type = \'lastname\')
325 INNER JOIN profile_name_enum AS ce ON (ce.type = \'lastname_ordinary\')
326 INNER JOIN profile_name AS l ON (a.uid = l.uid AND le.id = l.typeid)
327 LEFT JOIN profile_name AS c ON (a.uid = c.uid AND ce.id = c.typeid)
328 SET a.lastname = IF(c.uid IS NULL, IF(l.particle != \'\', l.name, CONCAT(l.particle, \' \', l.name))
329 IF(c.particle != \'\', c.name, CONCAT(c.particle, \' \', c.name)))
330 WHERE a.uid = {?}',
331 $user->id());
332
333 XDB::execute('UPDATE TABLE accounts AS a
334 INNER JOIN profile_name_enum AS fe ON (fe.type = \'firstname\')
335 INNER JOIN profile_name_enum AS ce ON (ce.type = \'firstname_ordinary\')
336 INNER JOIN profile_name AS f ON (a.uid = f.uid AND fe.id = f.typeid)
337 LEFT JOIN profile_name AS c ON (a.uid = c.uid AND ce.id = c.typeid)
338 SET a.firstname = IF(c.uid IS NULL, f.name, c.name)
339 WHERE a.uid = {?}',
340 $user->id());
341
342 Profile::rebuildSearchTokens($pid, false);
343 return $has_new;
344 }
345
346 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
347 ?>