1df275b3cad40689a62d1eccfa77e9611731fd1b
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 ***************************************************************************/
24 // Some particles should not be capitalized (cf "ORTHOTYPO", by Jean-Pierre
26 // Note that some of them are allowed to use capital letters in some cases,
27 // for instance "De" in American English.
28 public static $particles = array('d', 'de', 'an', 'auf', 'von', 'von dem',
29 'von der', 'zu', 'of', 'del', 'de las',
30 'de les', 'de los', 'las', 'los', 'y', 'a',
31 'da', 'das', 'do', 'dos', 'af', 'av');
35 function build_javascript_names($data, $isFemale)
38 foreach (explode(';-;', $data) as $key => $item) {
39 $names[$key] = explode(';', $item);
42 'lastname_main' => $names[0][0],
43 'lastname_ordinary' => $names[0][1],
44 'lastname_marital' => $names[0][2],
45 'pseudonym' => $names[0][3]
48 'firstname_main' => $names[1][0],
49 'firstname_ordinary' => $names[1][1]
51 $private_names_count = intval(count($names[2]) / 2);
52 $private_names = array();
53 for ($i = 0; $i < $private_names_count; ++
$i) {
54 $private_names[] = array('type' => $names[2][2 * $i], 'name' => $names[2][2 * $i +
1]);
57 return build_first_name($firstnames) . ' ' . build_full_last_name($lastnames, $isFemale) . ';' . build_private_name($private_names);
60 function build_email_alias(array $public_names)
62 return PlUser
::makeUserName(build_first_name($public_names), build_short_last_name($public_names));
65 function build_display_names(array $public_names, array $private_names, $isFemale)
67 $short_last_name = build_short_last_name($public_names);
68 $full_last_name = build_full_last_name($public_names, $isFemale);
69 $private_last_name_end = build_private_name($private_names);
70 $firstname = build_first_name($public_names);
72 $display_names = array();
73 $display_names['public_name'] = build_full_name($firstname, $full_last_name);
74 $display_names['private_name'] = $display_names['public_name'] . $private_last_name_end;
75 $display_names['directory_name'] = build_directory_name($firstname, $full_last_name);
76 $display_names['short_name'] = build_full_name($firstname, $short_last_name);
77 $display_names['sort_name'] = build_sort_name($firstname, $short_last_name);
79 return $display_names;
82 function build_short_last_name(array $lastnames)
84 return ($lastnames['lastname_ordinary'] == '') ?
$lastnames['lastname_main'] : $lastnames['lastname_ordinary'];
87 function build_full_last_name(array $lastnames, $isFemale)
89 if ($lastnames['lastname_ordinary'] != '') {
90 $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')';
92 $name = $lastnames['lastname_main'];
94 if ($lastnames['lastname_marital'] != '' ||
$lastnames['pseudonym'] != '') {
96 if ($lastnames['lastname_marital'] != '') {
97 $name .= ($isFemale ?
'Mme ' : 'M ') . $lastnames['lastname_marital'];
98 $name .= (($lastnames['pseudonym'] == '') ?
'' : ', ');
100 $name .= (($lastnames['pseudonym'] == '')?
'' : $lastnames['pseudonym']) . ')';
105 function build_first_name(array $firstnames)
107 return ($firstnames['firstname_ordinary'] ?
$firstnames['firstname_ordinary'] : $firstnames['firstname_main']);
110 function build_private_name(array $private_names)
112 if (is_null($private_names) ||
count($private_names) == 0) {
116 static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : ');
117 $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array());
119 foreach ($private_names as $private_name) {
120 $names_sorted[$private_name['type']][] = $private_name['name'];
123 $names_array = array();
124 foreach ($names_sorted as $type => $names) {
126 $names_array[] = $types[$type] . implode(', ', $names);
130 return ' (' . implode(', ', $names_array) . ')';
133 function build_directory_name($firstname, $lastname)
135 if ($firstname == '') {
136 return mb_strtoupper($lastname);
138 return mb_strtoupper($lastname) . ' ' . $firstname;
141 function build_full_name($firstname, $lastname)
143 if ($firstname == '') {
146 return $firstname . ' ' . $lastname;
149 // Returns the name on which the sort is performed, according to French
150 // typographic rules.
151 function build_sort_name($firstname, $lastname)
153 // Remove uncapitalized particles.
154 $particles = "/^(d'|(" . implode(Particles
::$particles, '|') . ') )/';
155 $name = preg_replace($particles, '', $lastname);
156 // Mac must also be uniformized.
157 $lastname = preg_replace("/^(Mac|Mc)( |)/", 'Mac', $name);
159 if ($firstname == '') {
162 return $lastname . ' ' . $firstname;
165 /** Splits a name into tokens, as used in search_name.
166 * Used for search_name rebuilding and for queries.
168 function split_name_for_search($name) {
169 return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)),
170 -1, PREG_SPLIT_NO_EMPTY
);
173 /** Transform a name to its canonical value so it can be compared
174 * to another form (different case, with accents or with - instead
176 * @see compare_basename to compare
178 function name_to_basename($value) {
179 $value = mb_strtoupper(replace_accent($value));
180 return preg_replace('/[^A-Z]/', ' ', $value);
183 /** Compares two strings and check if they are two forms of the
184 * same name (different case, with accents or with - instead of
186 * @see name_to_basename to retreive the compared string
188 function compare_basename($a, $b) {
189 return name_to_basename($a) == name_to_basename($b);
192 function update_account_from_profile($uid)
194 XDB
::execute("UPDATE accounts AS a
195 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
196 INNER JOIN profile_public_names AS ppn ON (ppn.pid = ap.pid)
197 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
198 SET a.lastname = IF(ppn.lastname_ordinary = '', ppn.lastname_main, ppn.lastname_ordinary),
199 a.firstname = IF(ppn.firstname_ordinary = '', ppn.firstname_main, ppn.firstname_ordinary),
200 a.full_name = pd.short_name, a.directory_name = pd.directory_name, a.sort_name = pd.sort_name
205 function update_display_names(Profile
$profile, array $public_names, array $private_names = null
)
207 if (is_null($private_names)) {
208 $private_names = XDB
::fetchAllAssoc('SELECT type, name
209 FROM profile_private_names
214 $display_names = build_display_names($public_names, $private_names, $profile->isFemale());
216 XDB
::execute('UPDATE profile_display
217 SET public_name = {?}, private_name = {?},
218 directory_name = {?}, short_name = {?}, sort_name = {?}
220 $display_names['public_name'], $display_names['private_name'],
221 $display_names['directory_name'], $display_names['short_name'],
222 $display_names['sort_name'], $profile->id());
224 Profile
::rebuildSearchTokens($profile->id(), false
);
226 if ($profile->owner()) {
227 update_account_from_profile($profile->owner()->id());
231 function update_public_names($pid, array $public_names)
233 XDB
::execute('UPDATE profile_public_names
234 SET lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
235 firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
237 $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'],
238 $public_names['firstname_main'], $public_names['firstname_ordinary'], $public_names['pseudonym'], $pid);
241 // Returns the @p name with all letters in lower case, but the first one.
242 function mb_ucfirst($name)
244 return mb_strtoupper(mb_substr($name, 0, 1)) . mb_substr($name, 1);
247 // Capitalizes the @p name using French typographic rules. Returns
248 // false when capitalization rule is not known for the name format.
249 function capitalize_name($name)
251 // Some suffixes should not be captitalized either, eg 's' in Bennett's.
252 static $suffixes = array('h', 's', 't');
254 // Extracts the first token of the name.
255 if (!preg_match('/^(\pL+)(([\' -])(.*))?$/ui', $name, $m)) {
259 $token = mb_strtolower($m[1]);
260 $separator = (isset($m[3]) ?
$m[3] : false
);
261 $tail = (isset($m[4]) ?
$m[4] : false
);
263 // Special case for "Malloc'h".
264 if ($separator == "'" && in_array(strtolower($tail[0]), $suffixes) &&
265 (strlen($tail) == 1 ||
$tail[1] == ' ')) {
266 $token .= "'" . strtolower($tail[0]);
267 $separator = (strlen($tail) == 1 ? false
: $tail[1]);
268 $tail = (strlen($tail) > 2 ?
substr($tail, 2) : false
);
271 // Capitalizes the first token.
272 if (!in_array($token, Particles
::$particles)) {
273 $token = mb_ucfirst($token);
276 // Capitalizes the tail of the name.
278 if (($tail = capitalize_name($tail))) {
279 return $token . $separator . $tail;
284 return $token . $separator;
287 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: