2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 function build_javascript_names($data, $isFemale)
25 foreach (explode(';-;', $data) as $key => $item) {
26 $names[$key] = explode(';', $item);
29 'lastname_main' => $names[0][0],
30 'lastname_ordinary' => $names[0][1],
31 'lastname_marital' => $names[0][2],
32 'pseudonym' => $names[0][3]
35 'firstname_main' => $names[1][0],
36 'firstname_ordinary' => $names[1][1]
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]);
44 return build_first_name($firstnames) . ' ' . build_full_last_name($lastnames, $isFemale) . ';' . build_private_name($private_names);
47 function build_email_alias(array $public_names)
49 return PlUser
::makeUserName(build_first_name($public_names), build_short_last_name($public_names));
52 function build_display_names(array $public_names, array $private_names, $isFemale)
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);
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;
66 return $display_names;
69 function build_short_last_name(array $lastnames)
71 return ($lastnames['lastname_ordinary'] == '') ?
$lastnames['lastname_main'] : $lastnames['lastname_ordinary'];
74 function build_full_last_name(array $lastnames, $isFemale)
76 if ($lastnames['lastname_ordinary'] != '') {
77 $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')';
79 $name = $lastnames['lastname_main'];
81 if ($lastnames['lastname_marital'] != '' ||
$lastnames['pseudonym'] != '') {
83 if ($lastnames['lastname_marital'] != '') {
84 $name .= ($isFemale ?
'Mme ' : 'M ') . $lastnames['lastname_marital'];
85 $name .= (($lastnames['pseudonym'] == '') ?
'' : ', ');
87 $name .= (($lastnames['pseudonym'] == '')?
'' : $lastnames['pseudonym']) . ')';
92 function build_first_name(array $firstnames)
94 return ($firstnames['firstname_ordinary'] ?
$firstnames['firstname_ordinary'] : $firstnames['firstname_main']);
97 function build_private_name(array $private_names)
99 if (is_null($private_names) ||
count($private_names) == 0) {
103 static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : ');
104 $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array());
106 foreach ($private_names as $private_name) {
107 $names_sorted[$private_name['type']][] = $private_name['name'];
110 $names_array = array();
111 foreach ($names_sorted as $type => $names) {
113 $names_array[] = $types[$type] . implode(', ', $names);
117 return ' (' . implode(', ', $names_array) . ')';
120 /** Splits a name into tokens, as used in search_name.
121 * Used for search_name rebuilding and for queries.
123 function split_name_for_search($name) {
124 return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)),
125 -1, PREG_SPLIT_NO_EMPTY
);
128 /** Transform a name to its canonical value so it can be compared
129 * to another form (different case, with accents or with - instead
131 * @see compare_basename to compare
133 function name_to_basename($value) {
134 $value = mb_strtoupper(replace_accent($value));
135 return preg_replace('/[^A-Z]/', ' ', $value);
138 /** Compares two strings and check if they are two forms of the
139 * same name (different case, with accents or with - instead of
141 * @see name_to_basename to retreive the compared string
143 function compare_basename($a, $b) {
144 return name_to_basename($a) == name_to_basename($b);
147 function update_account_from_profile($uid)
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
160 function update_display_names(Profile
$profile, array $public_names, array $private_names = null
)
162 if (is_null($private_names)) {
163 $private_names = XDB
::fetchAllAssoc('SELECT type, name
164 FROM profile_private_names
169 $display_names = build_display_names($public_names, $private_names, $profile->isFemale());
171 XDB
::execute('UPDATE profile_display
172 SET public_name = {?}, private_name = {?},
173 directory_name = {?}, short_name = {?}, sort_name = {?}
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());
179 Profile
::rebuildSearchTokens($profile->id(), false
);
181 if ($profile->owner()) {
182 update_account_from_profile($profile->owner()->id());
186 function update_public_names($pid, array $public_names)
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);
196 XDB
::execute('UPDATE profile_public_names
197 SET particles = {?}, lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
198 firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
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);
204 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: