Commit | Line | Data |
---|---|---|
6e32823c SJ |
1 | <?php |
2 | /*************************************************************************** | |
ba6ae046 | 3 | * Copyright (C) 2003-2013 Polytechnique.org * |
6e32823c SJ |
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 | ||
14d49826 SJ |
22 | class Particles |
23 | { | |
24 | // Some particles should not be capitalized (cf "ORTHOTYPO", by Jean-Pierre | |
25 | // Lacroux). | |
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'); | |
32 | } | |
75e295ff SJ |
33 | |
34 | ||
e8a7cf31 | 35 | function build_javascript_names($data, $isFemale) |
6e32823c | 36 | { |
0e1dfbad SJ |
37 | $names = array(); |
38 | foreach (explode(';-;', $data) as $key => $item) { | |
39 | $names[$key] = explode(';', $item); | |
40 | } | |
41 | $lastnames = array( | |
42 | 'lastname_main' => $names[0][0], | |
43 | 'lastname_ordinary' => $names[0][1], | |
44 | 'lastname_marital' => $names[0][2], | |
45 | 'pseudonym' => $names[0][3] | |
46 | ); | |
47 | $firstnames = array( | |
48 | 'firstname_main' => $names[1][0], | |
49 | 'firstname_ordinary' => $names[1][1] | |
50 | ); | |
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]); | |
55 | } | |
56 | ||
57 | return build_first_name($firstnames) . ' ' . build_full_last_name($lastnames, $isFemale) . ';' . build_private_name($private_names); | |
58 | } | |
dced83b4 | 59 | |
0e1dfbad SJ |
60 | function build_email_alias(array $public_names) |
61 | { | |
62 | return PlUser::makeUserName(build_first_name($public_names), build_short_last_name($public_names)); | |
dced83b4 SJ |
63 | } |
64 | ||
0e1dfbad | 65 | function build_display_names(array $public_names, array $private_names, $isFemale) |
dced83b4 | 66 | { |
0e1dfbad SJ |
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); | |
71 | ||
72 | $display_names = array(); | |
3ba2fdf6 | 73 | $display_names['public_name'] = build_full_name($firstname, $full_last_name); |
0e1dfbad | 74 | $display_names['private_name'] = $display_names['public_name'] . $private_last_name_end; |
3ba2fdf6 SJ |
75 | $display_names['directory_name'] = build_directory_name($firstname, $full_last_name); |
76 | $display_names['short_name'] = build_full_name($firstname, $short_last_name); | |
233a2a96 | 77 | $display_names['sort_name'] = build_sort_name($firstname, $short_last_name); |
0e1dfbad SJ |
78 | |
79 | return $display_names; | |
6e32823c SJ |
80 | } |
81 | ||
0e1dfbad | 82 | function build_short_last_name(array $lastnames) |
6e32823c | 83 | { |
0e1dfbad | 84 | return ($lastnames['lastname_ordinary'] == '') ? $lastnames['lastname_main'] : $lastnames['lastname_ordinary']; |
6e32823c SJ |
85 | } |
86 | ||
0e1dfbad | 87 | function build_full_last_name(array $lastnames, $isFemale) |
6e32823c | 88 | { |
0e1dfbad SJ |
89 | if ($lastnames['lastname_ordinary'] != '') { |
90 | $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')'; | |
6e32823c | 91 | } else { |
0e1dfbad | 92 | $name = $lastnames['lastname_main']; |
6e32823c | 93 | } |
0e1dfbad SJ |
94 | if ($lastnames['lastname_marital'] != '' || $lastnames['pseudonym'] != '') { |
95 | $name .= ' ('; | |
96 | if ($lastnames['lastname_marital'] != '') { | |
97 | $name .= ($isFemale ? 'Mme ' : 'M ') . $lastnames['lastname_marital']; | |
98 | $name .= (($lastnames['pseudonym'] == '') ? '' : ', '); | |
6e32823c | 99 | } |
0e1dfbad | 100 | $name .= (($lastnames['pseudonym'] == '')? '' : $lastnames['pseudonym']) . ')'; |
6e32823c SJ |
101 | } |
102 | return $name; | |
103 | } | |
104 | ||
0e1dfbad | 105 | function build_first_name(array $firstnames) |
6e32823c | 106 | { |
0e1dfbad | 107 | return ($firstnames['firstname_ordinary'] ? $firstnames['firstname_ordinary'] : $firstnames['firstname_main']); |
6e32823c SJ |
108 | } |
109 | ||
0e1dfbad | 110 | function build_private_name(array $private_names) |
6e32823c | 111 | { |
0e1dfbad SJ |
112 | if (is_null($private_names) || count($private_names) == 0) { |
113 | return ''; | |
6e32823c | 114 | } |
6e32823c | 115 | |
0e1dfbad SJ |
116 | static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : '); |
117 | $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array()); | |
6e32823c | 118 | |
0e1dfbad SJ |
119 | foreach ($private_names as $private_name) { |
120 | $names_sorted[$private_name['type']][] = $private_name['name']; | |
6e32823c | 121 | } |
cb0af6e5 | 122 | |
0e1dfbad SJ |
123 | $names_array = array(); |
124 | foreach ($names_sorted as $type => $names) { | |
125 | if (count($names)) { | |
126 | $names_array[] = $types[$type] . implode(', ', $names); | |
127 | } | |
cb0af6e5 | 128 | } |
dced83b4 | 129 | |
0e1dfbad | 130 | return ' (' . implode(', ', $names_array) . ')'; |
dced83b4 SJ |
131 | } |
132 | ||
3ba2fdf6 SJ |
133 | function build_directory_name($firstname, $lastname) |
134 | { | |
135 | if ($firstname == '') { | |
136 | return mb_strtoupper($lastname); | |
137 | } | |
138 | return mb_strtoupper($lastname) . ' ' . $firstname; | |
139 | } | |
140 | ||
141 | function build_full_name($firstname, $lastname) | |
142 | { | |
143 | if ($firstname == '') { | |
144 | return $lastname; | |
145 | } | |
146 | return $firstname . ' ' . $lastname; | |
147 | } | |
148 | ||
233a2a96 SJ |
149 | // Returns the name on which the sort is performed, according to French |
150 | // typographic rules. | |
151 | function build_sort_name($firstname, $lastname) | |
152 | { | |
153 | // Remove uncapitalized particles. | |
14d49826 | 154 | $particles = "/^(d'|(" . implode(Particles::$particles, '|') . ') )/'; |
233a2a96 SJ |
155 | $name = preg_replace($particles, '', $lastname); |
156 | // Mac must also be uniformized. | |
14d49826 | 157 | $lastname = preg_replace("/^(Mac|Mc)( |)/", 'Mac', $name); |
233a2a96 SJ |
158 | |
159 | if ($firstname == '') { | |
160 | return $lastname; | |
161 | } | |
162 | return $lastname . ' ' . $firstname; | |
163 | } | |
164 | ||
5ed1cce8 RB |
165 | /** Splits a name into tokens, as used in search_name. |
166 | * Used for search_name rebuilding and for queries. | |
167 | */ | |
168 | function split_name_for_search($name) { | |
169 | return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)), | |
170 | -1, PREG_SPLIT_NO_EMPTY); | |
171 | } | |
172 | ||
ac40839f PC |
173 | /** Transform a name to its canonical value so it can be compared |
174 | * to another form (different case, with accents or with - instead | |
175 | * of blanks). | |
176 | * @see compare_basename to compare | |
177 | */ | |
178 | function name_to_basename($value) { | |
179 | $value = mb_strtoupper(replace_accent($value)); | |
180 | return preg_replace('/[^A-Z]/', ' ', $value); | |
181 | } | |
182 | ||
183 | /** Compares two strings and check if they are two forms of the | |
184 | * same name (different case, with accents or with - instead of | |
185 | * blanks). | |
186 | * @see name_to_basename to retreive the compared string | |
187 | */ | |
188 | function compare_basename($a, $b) { | |
189 | return name_to_basename($a) == name_to_basename($b); | |
190 | } | |
191 | ||
0e1dfbad | 192 | function update_account_from_profile($uid) |
dced83b4 | 193 | { |
0e1dfbad SJ |
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), | |
b0788bf4 | 200 | a.full_name = pd.short_name, a.directory_name = pd.directory_name, a.sort_name = pd.sort_name |
0e1dfbad SJ |
201 | WHERE a.uid = {?}", |
202 | $uid); | |
203 | } | |
204 | ||
205 | function update_display_names(Profile $profile, array $public_names, array $private_names = null) | |
206 | { | |
207 | if (is_null($private_names)) { | |
208 | $private_names = XDB::fetchAllAssoc('SELECT type, name | |
209 | FROM profile_private_names | |
210 | WHERE pid = {?} | |
211 | ORDER BY type, id', | |
212 | $profile->id()); | |
dced83b4 | 213 | } |
0e1dfbad SJ |
214 | $display_names = build_display_names($public_names, $private_names, $profile->isFemale()); |
215 | ||
216 | XDB::execute('UPDATE profile_display | |
217 | SET public_name = {?}, private_name = {?}, | |
218 | directory_name = {?}, short_name = {?}, sort_name = {?} | |
219 | WHERE pid = {?}', | |
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()); | |
223 | ||
224 | Profile::rebuildSearchTokens($profile->id(), false); | |
b7753795 | 225 | |
0e1dfbad SJ |
226 | if ($profile->owner()) { |
227 | update_account_from_profile($profile->owner()->id()); | |
228 | } | |
229 | } | |
b7753795 | 230 | |
0e1dfbad SJ |
231 | function update_public_names($pid, array $public_names) |
232 | { | |
0e1dfbad | 233 | XDB::execute('UPDATE profile_public_names |
99a1a08c | 234 | SET lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?}, |
0e1dfbad SJ |
235 | firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?} |
236 | WHERE pid = {?}', | |
99a1a08c | 237 | $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'], |
0e1dfbad | 238 | $public_names['firstname_main'], $public_names['firstname_ordinary'], $public_names['pseudonym'], $pid); |
dced83b4 SJ |
239 | } |
240 | ||
75e295ff SJ |
241 | // Returns the @p name with all letters in lower case, but the first one. |
242 | function mb_ucfirst($name) | |
243 | { | |
244 | return mb_strtoupper(mb_substr($name, 0, 1)) . mb_substr($name, 1); | |
245 | } | |
246 | ||
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) | |
250 | { | |
251 | // Some suffixes should not be captitalized either, eg 's' in Bennett's. | |
252 | static $suffixes = array('h', 's', 't'); | |
253 | ||
254 | // Extracts the first token of the name. | |
255 | if (!preg_match('/^(\pL+)(([\' -])(.*))?$/ui', $name, $m)) { | |
cac02d6d | 256 | return ''; |
75e295ff SJ |
257 | } |
258 | ||
259 | $token = mb_strtolower($m[1]); | |
260 | $separator = (isset($m[3]) ? $m[3] : false); | |
261 | $tail = (isset($m[4]) ? $m[4] : false); | |
262 | ||
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); | |
269 | } | |
270 | ||
271 | // Capitalizes the first token. | |
14d49826 | 272 | if (!in_array($token, Particles::$particles)) { |
75e295ff SJ |
273 | $token = mb_ucfirst($token); |
274 | } | |
275 | ||
276 | // Capitalizes the tail of the name. | |
277 | if ($tail) { | |
278 | if (($tail = capitalize_name($tail))) { | |
279 | return $token . $separator . $tail; | |
280 | } | |
cac02d6d | 281 | return ''; |
75e295ff SJ |
282 | } |
283 | ||
284 | return $token . $separator; | |
285 | } | |
286 | ||
6e32823c SJ |
287 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
288 | ?> |