| 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 | function build_javascript_names($data, $isFemale) |
| 23 | { |
| 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 | } |
| 46 | |
| 47 | function build_email_alias(array $public_names) |
| 48 | { |
| 49 | return PlUser::makeUserName(build_first_name($public_names), build_short_last_name($public_names)); |
| 50 | } |
| 51 | |
| 52 | function build_display_names(array $public_names, array $private_names, $isFemale) |
| 53 | { |
| 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; |
| 67 | } |
| 68 | |
| 69 | function build_short_last_name(array $lastnames) |
| 70 | { |
| 71 | return ($lastnames['lastname_ordinary'] == '') ? $lastnames['lastname_main'] : $lastnames['lastname_ordinary']; |
| 72 | } |
| 73 | |
| 74 | function build_full_last_name(array $lastnames, $isFemale) |
| 75 | { |
| 76 | if ($lastnames['lastname_ordinary'] != '') { |
| 77 | $name = $lastnames['lastname_ordinary'] . ' (' . $lastnames['lastname_main'] . ')'; |
| 78 | } else { |
| 79 | $name = $lastnames['lastname_main']; |
| 80 | } |
| 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'] == '') ? '' : ', '); |
| 86 | } |
| 87 | $name .= (($lastnames['pseudonym'] == '')? '' : $lastnames['pseudonym']) . ')'; |
| 88 | } |
| 89 | return $name; |
| 90 | } |
| 91 | |
| 92 | function build_first_name(array $firstnames) |
| 93 | { |
| 94 | return ($firstnames['firstname_ordinary'] ? $firstnames['firstname_ordinary'] : $firstnames['firstname_main']); |
| 95 | } |
| 96 | |
| 97 | function build_private_name(array $private_names) |
| 98 | { |
| 99 | if (is_null($private_names) || count($private_names) == 0) { |
| 100 | return ''; |
| 101 | } |
| 102 | |
| 103 | static $types = array('nickname' => 'alias ', 'firstname' => 'autres prénoms : ', 'lastname' => 'autres noms : '); |
| 104 | $names_sorted = array('nickname' => array(), 'firstname' => array(), 'lastname' => array()); |
| 105 | |
| 106 | foreach ($private_names as $private_name) { |
| 107 | $names_sorted[$private_name['type']][] = $private_name['name']; |
| 108 | } |
| 109 | |
| 110 | $names_array = array(); |
| 111 | foreach ($names_sorted as $type => $names) { |
| 112 | if (count($names)) { |
| 113 | $names_array[] = $types[$type] . implode(', ', $names); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return ' (' . implode(', ', $names_array) . ')'; |
| 118 | } |
| 119 | |
| 120 | /** Splits a name into tokens, as used in search_name. |
| 121 | * Used for search_name rebuilding and for queries. |
| 122 | */ |
| 123 | function split_name_for_search($name) { |
| 124 | return preg_split('/[[:space:]\'\-]+/', strtolower(replace_accent($name)), |
| 125 | -1, PREG_SPLIT_NO_EMPTY); |
| 126 | } |
| 127 | |
| 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 | */ |
| 133 | function 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 | */ |
| 143 | function compare_basename($a, $b) { |
| 144 | return name_to_basename($a) == name_to_basename($b); |
| 145 | } |
| 146 | |
| 147 | function update_account_from_profile($uid) |
| 148 | { |
| 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 | |
| 160 | function 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()); |
| 168 | } |
| 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); |
| 180 | |
| 181 | if ($profile->owner()) { |
| 182 | update_account_from_profile($profile->owner()->id()); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | function 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 | } |
| 195 | |
| 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); |
| 202 | } |
| 203 | |
| 204 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 205 | ?> |