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