Moving to GitHub.
[platal.git] / include / name.func.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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 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 }
33
34
35 function build_javascript_names($data, $isFemale)
36 {
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 }
59
60 function build_email_alias(array $public_names)
61 {
62 return PlUser::makeUserName(build_first_name($public_names), build_short_last_name($public_names));
63 }
64
65 function build_display_names(array $public_names, array $private_names, $isFemale)
66 {
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();
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);
78
79 return $display_names;
80 }
81
82 function build_short_last_name(array $lastnames)
83 {
84 return ($lastnames['lastname_ordinary'] == '') ? $lastnames['lastname_main'] : $lastnames['lastname_ordinary'];
85 }
86
87 function build_full_last_name(array $lastnames, $isFemale)
88 {
89 if ($lastnames['lastname_ordinary'] != '') {
90 $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')';
91 } else {
92 $name = $lastnames['lastname_main'];
93 }
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'] == '') ? '' : ', ');
99 }
100 $name .= (($lastnames['pseudonym'] == '')? '' : $lastnames['pseudonym']) . ')';
101 }
102 return $name;
103 }
104
105 function build_first_name(array $firstnames)
106 {
107 return ($firstnames['firstname_ordinary'] ? $firstnames['firstname_ordinary'] : $firstnames['firstname_main']);
108 }
109
110 function build_private_name(array $private_names)
111 {
112 if (is_null($private_names) || count($private_names) == 0) {
113 return '';
114 }
115
116 static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : ');
117 $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array());
118
119 foreach ($private_names as $private_name) {
120 $names_sorted[$private_name['type']][] = $private_name['name'];
121 }
122
123 $names_array = array();
124 foreach ($names_sorted as $type => $names) {
125 if (count($names)) {
126 $names_array[] = $types[$type] . implode(', ', $names);
127 }
128 }
129
130 return ' (' . implode(', ', $names_array) . ')';
131 }
132
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
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.
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);
158
159 if ($firstname == '') {
160 return $lastname;
161 }
162 return $lastname . ' ' . $firstname;
163 }
164
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
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
192 function update_account_from_profile($uid)
193 {
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
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());
213 }
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);
225
226 if ($profile->owner()) {
227 update_account_from_profile($profile->owner()->id());
228 }
229 }
230
231 function update_public_names($pid, array $public_names)
232 {
233 XDB::execute('UPDATE profile_public_names
234 SET lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
235 firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
236 WHERE pid = {?}',
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);
239 }
240
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)) {
256 return '';
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.
272 if (!in_array($token, Particles::$particles)) {
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 }
281 return '';
282 }
283
284 return $token . $separator;
285 }
286
287 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
288 ?>