Displays entreprise in upper case in addresses list.
[platal.git] / include / name.func.inc.php
CommitLineData
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
e8a7cf31 22function build_javascript_names($data, $isFemale)
6e32823c 23{
0e1dfbad
SJ
24 $names = array();
25 foreach (explode(';-;', $data) as $key => $item) {
26 $names[$key] = explode(';', $item);
27 }
28 $lastnames = array(
29 'lastname_main' => $names[0][0],
30 'lastname_ordinary' => $names[0][1],
31 'lastname_marital' => $names[0][2],
32 'pseudonym' => $names[0][3]
33 );
34 $firstnames = array(
35 'firstname_main' => $names[1][0],
36 'firstname_ordinary' => $names[1][1]
37 );
38 $private_names_count = intval(count($names[2]) / 2);
39 $private_names = array();
40 for ($i = 0; $i < $private_names_count; ++$i) {
41 $private_names[] = array('type' => $names[2][2 * $i], 'name' => $names[2][2 * $i + 1]);
42 }
43
44 return build_first_name($firstnames) . ' ' . build_full_last_name($lastnames, $isFemale) . ';' . build_private_name($private_names);
45}
dced83b4 46
0e1dfbad
SJ
47function build_email_alias(array $public_names)
48{
49 return PlUser::makeUserName(build_first_name($public_names), build_short_last_name($public_names));
dced83b4
SJ
50}
51
0e1dfbad 52function build_display_names(array $public_names, array $private_names, $isFemale)
dced83b4 53{
0e1dfbad
SJ
54 $short_last_name = build_short_last_name($public_names);
55 $full_last_name = build_full_last_name($public_names, $isFemale);
56 $private_last_name_end = build_private_name($private_names);
57 $firstname = build_first_name($public_names);
58
59 $display_names = array();
60 $display_names['public_name'] = $firstname . ' ' . $full_last_name;
61 $display_names['private_name'] = $display_names['public_name'] . $private_last_name_end;
62 $display_names['directory_name'] = $full_last_name . ' ' . $firstname;
63 $display_names['short_name'] = $firstname . ' ' . $short_last_name;
64 $display_names['sort_name'] = $short_last_name . ' ' . $firstname;
65
66 return $display_names;
6e32823c
SJ
67}
68
0e1dfbad 69function build_short_last_name(array $lastnames)
6e32823c 70{
0e1dfbad 71 return ($lastnames['lastname_ordinary'] == '') ? $lastnames['lastname_main'] : $lastnames['lastname_ordinary'];
6e32823c
SJ
72}
73
0e1dfbad 74function build_full_last_name(array $lastnames, $isFemale)
6e32823c 75{
0e1dfbad
SJ
76 if ($lastnames['lastname_ordinary'] != '') {
77 $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')';
6e32823c 78 } else {
0e1dfbad 79 $name = $lastnames['lastname_main'];
6e32823c 80 }
0e1dfbad
SJ
81 if ($lastnames['lastname_marital'] != '' || $lastnames['pseudonym'] != '') {
82 $name .= ' (';
83 if ($lastnames['lastname_marital'] != '') {
84 $name .= ($isFemale ? 'Mme ' : 'M ') . $lastnames['lastname_marital'];
85 $name .= (($lastnames['pseudonym'] == '') ? '' : ', ');
6e32823c 86 }
0e1dfbad 87 $name .= (($lastnames['pseudonym'] == '')? '' : $lastnames['pseudonym']) . ')';
6e32823c
SJ
88 }
89 return $name;
90}
91
0e1dfbad 92function build_first_name(array $firstnames)
6e32823c 93{
0e1dfbad 94 return ($firstnames['firstname_ordinary'] ? $firstnames['firstname_ordinary'] : $firstnames['firstname_main']);
6e32823c
SJ
95}
96
0e1dfbad 97function build_private_name(array $private_names)
6e32823c 98{
0e1dfbad
SJ
99 if (is_null($private_names) || count($private_names) == 0) {
100 return '';
6e32823c 101 }
6e32823c 102
0e1dfbad
SJ
103 static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : ');
104 $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array());
6e32823c 105
0e1dfbad
SJ
106 foreach ($private_names as $private_name) {
107 $names_sorted[$private_name['type']][] = $private_name['name'];
6e32823c 108 }
cb0af6e5 109
0e1dfbad
SJ
110 $names_array = array();
111 foreach ($names_sorted as $type => $names) {
112 if (count($names)) {
113 $names_array[] = $types[$type] . implode(', ', $names);
114 }
cb0af6e5 115 }
dced83b4 116
0e1dfbad 117 return ' (' . implode(', ', $names_array) . ')';
dced83b4
SJ
118}
119
5ed1cce8
RB
120/** Splits a name into tokens, as used in search_name.
121 * Used for search_name rebuilding and for queries.
122 */
123function split_name_for_search($name) {
124 return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)),
125 -1, PREG_SPLIT_NO_EMPTY);
126}
127
ac40839f
PC
128/** Transform a name to its canonical value so it can be compared
129 * to another form (different case, with accents or with - instead
130 * of blanks).
131 * @see compare_basename to compare
132 */
133function name_to_basename($value) {
134 $value = mb_strtoupper(replace_accent($value));
135 return preg_replace('/[^A-Z]/', ' ', $value);
136}
137
138/** Compares two strings and check if they are two forms of the
139 * same name (different case, with accents or with - instead of
140 * blanks).
141 * @see name_to_basename to retreive the compared string
142 */
143function compare_basename($a, $b) {
144 return name_to_basename($a) == name_to_basename($b);
145}
146
0e1dfbad 147function update_account_from_profile($uid)
dced83b4 148{
0e1dfbad
SJ
149 XDB::execute("UPDATE accounts AS a
150 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
151 INNER JOIN profile_public_names AS ppn ON (ppn.pid = ap.pid)
152 INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
153 SET a.lastname = IF(ppn.lastname_ordinary = '', ppn.lastname_main, ppn.lastname_ordinary),
154 a.firstname = IF(ppn.firstname_ordinary = '', ppn.firstname_main, ppn.firstname_ordinary),
155 a.full_name = pd.short_name, a.directory_name = pd.directory_name
156 WHERE a.uid = {?}",
157 $uid);
158}
159
160function update_display_names(Profile $profile, array $public_names, array $private_names = null)
161{
162 if (is_null($private_names)) {
163 $private_names = XDB::fetchAllAssoc('SELECT type, name
164 FROM profile_private_names
165 WHERE pid = {?}
166 ORDER BY type, id',
167 $profile->id());
dced83b4 168 }
0e1dfbad
SJ
169 $display_names = build_display_names($public_names, $private_names, $profile->isFemale());
170
171 XDB::execute('UPDATE profile_display
172 SET public_name = {?}, private_name = {?},
173 directory_name = {?}, short_name = {?}, sort_name = {?}
174 WHERE pid = {?}',
175 $display_names['public_name'], $display_names['private_name'],
176 $display_names['directory_name'], $display_names['short_name'],
177 $display_names['sort_name'], $profile->id());
178
179 Profile::rebuildSearchTokens($profile->id(), false);
b7753795 180
0e1dfbad
SJ
181 if ($profile->owner()) {
182 update_account_from_profile($profile->owner()->id());
183 }
184}
b7753795 185
0e1dfbad
SJ
186function update_public_names($pid, array $public_names)
187{
188 $public_names['particles'] = new PlFlagSet();
189 static $suffixes = array('main', 'marital', 'ordinary');
190 foreach ($suffixes as $suffix) {
191 if (isset($public_names['particle_' . $suffix]) && ($public_names['particle_' . $suffix] == 1 || $public_names['particle_' . $suffix] == 'on')) {
192 $public_names['particles']->addFlag($suffix, 1);
193 }
194 }
b7753795 195
0e1dfbad
SJ
196 XDB::execute('UPDATE profile_public_names
197 SET particles = {?}, lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
198 firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
199 WHERE pid = {?}',
200 $public_names['particles'], $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'],
201 $public_names['firstname_main'], $public_names['firstname_ordinary'], $public_names['pseudonym'], $pid);
dced83b4
SJ
202}
203
6e32823c
SJ
204// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
205?>