44296c2fcf9ca3012ac347fc78432d54b7b2bdee
[platal.git] / include / name.func.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 // 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
32 function build_javascript_names($data, $isFemale)
33 {
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 }
56
57 function build_email_alias(array $public_names)
58 {
59 return PlUser::makeUserName(build_first_name($public_names), build_short_last_name($public_names));
60 }
61
62 function build_display_names(array $public_names, array $private_names, $isFemale)
63 {
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();
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;
75
76 return $display_names;
77 }
78
79 function build_short_last_name(array $lastnames)
80 {
81 return ($lastnames['lastname_ordinary'] == '') ? $lastnames['lastname_main'] : $lastnames['lastname_ordinary'];
82 }
83
84 function build_full_last_name(array $lastnames, $isFemale)
85 {
86 if ($lastnames['lastname_ordinary'] != '') {
87 $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')';
88 } else {
89 $name = $lastnames['lastname_main'];
90 }
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'] == '') ? '' : ', ');
96 }
97 $name .= (($lastnames['pseudonym'] == '')? '' : $lastnames['pseudonym']) . ')';
98 }
99 return $name;
100 }
101
102 function build_first_name(array $firstnames)
103 {
104 return ($firstnames['firstname_ordinary'] ? $firstnames['firstname_ordinary'] : $firstnames['firstname_main']);
105 }
106
107 function build_private_name(array $private_names)
108 {
109 if (is_null($private_names) || count($private_names) == 0) {
110 return '';
111 }
112
113 static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : ');
114 $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array());
115
116 foreach ($private_names as $private_name) {
117 $names_sorted[$private_name['type']][] = $private_name['name'];
118 }
119
120 $names_array = array();
121 foreach ($names_sorted as $type => $names) {
122 if (count($names)) {
123 $names_array[] = $types[$type] . implode(', ', $names);
124 }
125 }
126
127 return ' (' . implode(', ', $names_array) . ')';
128 }
129
130 /** Splits a name into tokens, as used in search_name.
131 * Used for search_name rebuilding and for queries.
132 */
133 function split_name_for_search($name) {
134 return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)),
135 -1, PREG_SPLIT_NO_EMPTY);
136 }
137
138 /** Transform a name to its canonical value so it can be compared
139 * to another form (different case, with accents or with - instead
140 * of blanks).
141 * @see compare_basename to compare
142 */
143 function name_to_basename($value) {
144 $value = mb_strtoupper(replace_accent($value));
145 return preg_replace('/[^A-Z]/', ' ', $value);
146 }
147
148 /** Compares two strings and check if they are two forms of the
149 * same name (different case, with accents or with - instead of
150 * blanks).
151 * @see name_to_basename to retreive the compared string
152 */
153 function compare_basename($a, $b) {
154 return name_to_basename($a) == name_to_basename($b);
155 }
156
157 function update_account_from_profile($uid)
158 {
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
166 WHERE a.uid = {?}",
167 $uid);
168 }
169
170 function update_display_names(Profile $profile, array $public_names, array $private_names = null)
171 {
172 if (is_null($private_names)) {
173 $private_names = XDB::fetchAllAssoc('SELECT type, name
174 FROM profile_private_names
175 WHERE pid = {?}
176 ORDER BY type, id',
177 $profile->id());
178 }
179 $display_names = build_display_names($public_names, $private_names, $profile->isFemale());
180
181 XDB::execute('UPDATE profile_display
182 SET public_name = {?}, private_name = {?},
183 directory_name = {?}, short_name = {?}, sort_name = {?}
184 WHERE pid = {?}',
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());
188
189 Profile::rebuildSearchTokens($profile->id(), false);
190
191 if ($profile->owner()) {
192 update_account_from_profile($profile->owner()->id());
193 }
194 }
195
196 function update_public_names($pid, array $public_names)
197 {
198 $public_names['particles'] = new PlFlagSet();
199 static $suffixes = array('main', 'marital', 'ordinary');
200 foreach ($suffixes as $suffix) {
201 if (isset($public_names['particle_' . $suffix]) && ($public_names['particle_' . $suffix] == 1 || $public_names['particle_' . $suffix] == 'on')) {
202 $public_names['particles']->addFlag($suffix, 1);
203 }
204 }
205
206 XDB::execute('UPDATE profile_public_names
207 SET particles = {?}, lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
208 firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
209 WHERE pid = {?}',
210 $public_names['particles'], $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'],
211 $public_names['firstname_main'], $public_names['firstname_ordinary'], $public_names['pseudonym'], $pid);
212 }
213
214 // Returns the @p name with all letters in lower case, but the first one.
215 function mb_ucfirst($name)
216 {
217 return mb_strtoupper(mb_substr($name, 0, 1)) . mb_substr($name, 1);
218 }
219
220 // Capitalizes the @p name using French typographic rules. Returns
221 // false when capitalization rule is not known for the name format.
222 function capitalize_name($name)
223 {
224 // Some suffixes should not be captitalized either, eg 's' in Bennett's.
225 static $suffixes = array('h', 's', 't');
226
227 // Extracts the first token of the name.
228 if (!preg_match('/^(\pL+)(([\' -])(.*))?$/ui', $name, $m)) {
229 return false;
230 }
231
232 $token = mb_strtolower($m[1]);
233 $separator = (isset($m[3]) ? $m[3] : false);
234 $tail = (isset($m[4]) ? $m[4] : false);
235
236 // Special case for "Malloc'h".
237 if ($separator == "'" && in_array(strtolower($tail[0]), $suffixes) &&
238 (strlen($tail) == 1 || $tail[1] == ' ')) {
239 $token .= "'" . strtolower($tail[0]);
240 $separator = (strlen($tail) == 1 ? false : $tail[1]);
241 $tail = (strlen($tail) > 2 ? substr($tail, 2) : false);
242 }
243
244 // Capitalizes the first token.
245 if (!in_array($token, $particles)) {
246 $token = mb_ucfirst($token);
247 }
248
249 // Capitalizes the tail of the name.
250 if ($tail) {
251 if (($tail = capitalize_name($tail))) {
252 return $token . $separator . $tail;
253 }
254 return false;
255 }
256
257 return $token . $separator;
258 }
259
260 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
261 ?>