3aecf92f6daf1c42fbbf15e2ca37cbd57587073c
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // Some particles should not be capitalized (cf "ORTHOTYPO", by Jean-Pierre
24 // Note that some of them are allowed to use capital letters in some cases,
25 // for instance "De" in American English.
26 static $particles = array('d', 'de', 'an', 'auf', 'von', 'von dem',
27 'von der', 'zu', 'of', 'del', 'de las',
28 'de les', 'de los', 'las', 'los', 'y', 'a',
29 'da', 'das', 'do', 'dos', 'af', 'av');
32 function build_javascript_names($data, $isFemale)
35 foreach (explode(';-;', $data) as $key => $item) {
36 $names[$key] = explode(';', $item);
39 'lastname_main' => $names[0][0],
40 'lastname_ordinary' => $names[0][1],
41 'lastname_marital' => $names[0][2],
42 'pseudonym' => $names[0][3]
45 'firstname_main' => $names[1][0],
46 'firstname_ordinary' => $names[1][1]
48 $private_names_count = intval(count($names[2]) / 2);
49 $private_names = array();
50 for ($i = 0; $i < $private_names_count; ++
$i) {
51 $private_names[] = array('type' => $names[2][2 * $i], 'name' => $names[2][2 * $i +
1]);
54 return build_first_name($firstnames) . ' ' . build_full_last_name($lastnames, $isFemale) . ';' . build_private_name($private_names);
57 function build_email_alias(array $public_names)
59 return PlUser
::makeUserName(build_first_name($public_names), build_short_last_name($public_names));
62 function build_display_names(array $public_names, array $private_names, $isFemale)
64 $short_last_name = build_short_last_name($public_names);
65 $full_last_name = build_full_last_name($public_names, $isFemale);
66 $private_last_name_end = build_private_name($private_names);
67 $firstname = build_first_name($public_names);
69 $display_names = array();
70 $display_names['public_name'] = $firstname . ' ' . $full_last_name;
71 $display_names['private_name'] = $display_names['public_name'] . $private_last_name_end;
72 $display_names['directory_name'] = $full_last_name . ' ' . $firstname;
73 $display_names['short_name'] = $firstname . ' ' . $short_last_name;
74 $display_names['sort_name'] = $short_last_name . ' ' . $firstname;
76 return $display_names;
79 function build_short_last_name(array $lastnames)
81 return ($lastnames['lastname_ordinary'] == '') ?
$lastnames['lastname_main'] : $lastnames['lastname_ordinary'];
84 function build_full_last_name(array $lastnames, $isFemale)
86 if ($lastnames['lastname_ordinary'] != '') {
87 $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')';
89 $name = $lastnames['lastname_main'];
91 if ($lastnames['lastname_marital'] != '' ||
$lastnames['pseudonym'] != '') {
93 if ($lastnames['lastname_marital'] != '') {
94 $name .= ($isFemale ?
'Mme ' : 'M ') . $lastnames['lastname_marital'];
95 $name .= (($lastnames['pseudonym'] == '') ?
'' : ', ');
97 $name .= (($lastnames['pseudonym'] == '')?
'' : $lastnames['pseudonym']) . ')';
102 function build_first_name(array $firstnames)
104 return ($firstnames['firstname_ordinary'] ?
$firstnames['firstname_ordinary'] : $firstnames['firstname_main']);
107 function build_private_name(array $private_names)
109 if (is_null($private_names) ||
count($private_names) == 0) {
113 static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : ');
114 $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array());
116 foreach ($private_names as $private_name) {
117 $names_sorted[$private_name['type']][] = $private_name['name'];
120 $names_array = array();
121 foreach ($names_sorted as $type => $names) {
123 $names_array[] = $types[$type] . implode(', ', $names);
127 return ' (' . implode(', ', $names_array) . ')';
130 /** Splits a name into tokens, as used in search_name.
131 * Used for search_name rebuilding and for queries.
133 function split_name_for_search($name) {
134 return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)),
135 -1, PREG_SPLIT_NO_EMPTY
);
138 /** Transform a name to its canonical value so it can be compared
139 * to another form (different case, with accents or with - instead
141 * @see compare_basename to compare
143 function name_to_basename($value) {
144 $value = mb_strtoupper(replace_accent($value));
145 return preg_replace('/[^A-Z]/', ' ', $value);
148 /** Compares two strings and check if they are two forms of the
149 * same name (different case, with accents or with - instead of
151 * @see name_to_basename to retreive the compared string
153 function compare_basename($a, $b) {
154 return name_to_basename($a) == name_to_basename($b);
157 function update_account_from_profile($uid)
159 XDB
::execute("UPDATE accounts AS a
160 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
161 INNER JOIN profile_public_names AS ppn ON (ppn.pid = ap.pid)
162 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
163 SET a.lastname = IF(ppn.lastname_ordinary = '', ppn.lastname_main, ppn.lastname_ordinary),
164 a.firstname = IF(ppn.firstname_ordinary = '', ppn.firstname_main, ppn.firstname_ordinary),
165 a.full_name = pd.short_name, a.directory_name = pd.directory_name
170 function update_display_names(Profile
$profile, array $public_names, array $private_names = null
)
172 if (is_null($private_names)) {
173 $private_names = XDB
::fetchAllAssoc('SELECT type, name
174 FROM profile_private_names
179 $display_names = build_display_names($public_names, $private_names, $profile->isFemale());
181 XDB
::execute('UPDATE profile_display
182 SET public_name = {?}, private_name = {?},
183 directory_name = {?}, short_name = {?}, sort_name = {?}
185 $display_names['public_name'], $display_names['private_name'],
186 $display_names['directory_name'], $display_names['short_name'],
187 $display_names['sort_name'], $profile->id());
189 Profile
::rebuildSearchTokens($profile->id(), false
);
191 if ($profile->owner()) {
192 update_account_from_profile($profile->owner()->id());
196 function update_public_names($pid, array $public_names)
198 XDB
::execute('UPDATE profile_public_names
199 SET lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
200 firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
202 $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'],
203 $public_names['firstname_main'], $public_names['firstname_ordinary'], $public_names['pseudonym'], $pid);
206 // Returns the @p name with all letters in lower case, but the first one.
207 function mb_ucfirst($name)
209 return mb_strtoupper(mb_substr($name, 0, 1)) . mb_substr($name, 1);
212 // Capitalizes the @p name using French typographic rules. Returns
213 // false when capitalization rule is not known for the name format.
214 function capitalize_name($name)
216 // Some suffixes should not be captitalized either, eg 's' in Bennett's.
217 static $suffixes = array('h', 's', 't');
219 // Extracts the first token of the name.
220 if (!preg_match('/^(\pL+)(([\' -])(.*))?$/ui', $name, $m)) {
224 $token = mb_strtolower($m[1]);
225 $separator = (isset($m[3]) ?
$m[3] : false
);
226 $tail = (isset($m[4]) ?
$m[4] : false
);
228 // Special case for "Malloc'h".
229 if ($separator == "'" && in_array(strtolower($tail[0]), $suffixes) &&
230 (strlen($tail) == 1 ||
$tail[1] == ' ')) {
231 $token .= "'" . strtolower($tail[0]);
232 $separator = (strlen($tail) == 1 ? false
: $tail[1]);
233 $tail = (strlen($tail) > 2 ?
substr($tail, 2) : false
);
236 // Capitalizes the first token.
237 if (!in_array($token, $particles)) {
238 $token = mb_ucfirst($token);
241 // Capitalizes the tail of the name.
243 if (($tail = capitalize_name($tail))) {
244 return $token . $separator . $tail;
249 return $token . $separator;
252 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: