* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************/
+class Particles
+{
+ // Some particles should not be capitalized (cf "ORTHOTYPO", by Jean-Pierre
+ // Lacroux).
+ // Note that some of them are allowed to use capital letters in some cases,
+ // for instance "De" in American English.
+ public static $particles = array('d', 'de', 'an', 'auf', 'von', 'von dem',
+ 'von der', 'zu', 'of', 'del', 'de las',
+ 'de les', 'de los', 'las', 'los', 'y', 'a',
+ 'da', 'das', 'do', 'dos', 'af', 'av');
+}
+
+
function build_javascript_names($data, $isFemale)
{
$names = array();
$firstname = build_first_name($public_names);
$display_names = array();
- $display_names['public_name'] = $firstname . ' ' . $full_last_name;
+ $display_names['public_name'] = build_full_name($firstname, $full_last_name);
$display_names['private_name'] = $display_names['public_name'] . $private_last_name_end;
- $display_names['directory_name'] = $full_last_name . ' ' . $firstname;
- $display_names['short_name'] = $firstname . ' ' . $short_last_name;
- $display_names['sort_name'] = $short_last_name . ' ' . $firstname;
+ $display_names['directory_name'] = build_directory_name($firstname, $full_last_name);
+ $display_names['short_name'] = build_full_name($firstname, $short_last_name);
+ $display_names['sort_name'] = build_sort_name($firstname, $short_last_name);
return $display_names;
}
return ' (' . implode(', ', $names_array) . ')';
}
+function build_directory_name($firstname, $lastname)
+{
+ if ($firstname == '') {
+ return mb_strtoupper($lastname);
+ }
+ return mb_strtoupper($lastname) . ' ' . $firstname;
+}
+
+function build_full_name($firstname, $lastname)
+{
+ if ($firstname == '') {
+ return $lastname;
+ }
+ return $firstname . ' ' . $lastname;
+}
+
+// Returns the name on which the sort is performed, according to French
+// typographic rules.
+function build_sort_name($firstname, $lastname)
+{
+ // Remove uncapitalized particles.
+ $particles = "/^(d'|(" . implode(Particles::$particles, '|') . ') )/';
+ $name = preg_replace($particles, '', $lastname);
+ // Mac must also be uniformized.
+ $lastname = preg_replace("/^(Mac|Mc)( |)/", 'Mac', $name);
+
+ if ($firstname == '') {
+ return $lastname;
+ }
+ return $lastname . ' ' . $firstname;
+}
+
/** Splits a name into tokens, as used in search_name.
* Used for search_name rebuilding and for queries.
*/
INNER JOIN profile_display AS pd ON (pd.pid = ap.pid)
SET a.lastname = IF(ppn.lastname_ordinary = '', ppn.lastname_main, ppn.lastname_ordinary),
a.firstname = IF(ppn.firstname_ordinary = '', ppn.firstname_main, ppn.firstname_ordinary),
- a.full_name = pd.short_name, a.directory_name = pd.directory_name
+ a.full_name = pd.short_name, a.directory_name = pd.directory_name, a.sort_name = pd.sort_name
WHERE a.uid = {?}",
$uid);
}
function update_public_names($pid, array $public_names)
{
- $public_names['particles'] = new PlFlagSet();
- static $suffixes = array('main', 'marital', 'ordinary');
- foreach ($suffixes as $suffix) {
- if (isset($public_names['particle_' . $suffix]) && ($public_names['particle_' . $suffix] == 1 || $public_names['particle_' . $suffix] == 'on')) {
- $public_names['particles']->addFlag($suffix, 1);
- }
- }
-
XDB::execute('UPDATE profile_public_names
- SET particles = {?}, lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
+ SET lastname_main = {?}, lastname_marital = {?}, lastname_ordinary = {?},
firstname_main = {?}, firstname_ordinary = {?}, pseudonym = {?}
WHERE pid = {?}',
- $public_names['particles'], $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'],
+ $public_names['lastname_main'], $public_names['lastname_marital'], $public_names['lastname_ordinary'],
$public_names['firstname_main'], $public_names['firstname_ordinary'], $public_names['pseudonym'], $pid);
}
+// Returns the @p name with all letters in lower case, but the first one.
+function mb_ucfirst($name)
+{
+ return mb_strtoupper(mb_substr($name, 0, 1)) . mb_substr($name, 1);
+}
+
+// Capitalizes the @p name using French typographic rules. Returns
+// false when capitalization rule is not known for the name format.
+function capitalize_name($name)
+{
+ // Some suffixes should not be captitalized either, eg 's' in Bennett's.
+ static $suffixes = array('h', 's', 't');
+
+ // Extracts the first token of the name.
+ if (!preg_match('/^(\pL+)(([\' -])(.*))?$/ui', $name, $m)) {
+ return false;
+ }
+
+ $token = mb_strtolower($m[1]);
+ $separator = (isset($m[3]) ? $m[3] : false);
+ $tail = (isset($m[4]) ? $m[4] : false);
+
+ // Special case for "Malloc'h".
+ if ($separator == "'" && in_array(strtolower($tail[0]), $suffixes) &&
+ (strlen($tail) == 1 || $tail[1] == ' ')) {
+ $token .= "'" . strtolower($tail[0]);
+ $separator = (strlen($tail) == 1 ? false : $tail[1]);
+ $tail = (strlen($tail) > 2 ? substr($tail, 2) : false);
+ }
+
+ // Capitalizes the first token.
+ if (!in_array($token, Particles::$particles)) {
+ $token = mb_ucfirst($token);
+ }
+
+ // Capitalizes the tail of the name.
+ if ($tail) {
+ if (($tail = capitalize_name($tail))) {
+ return $token . $separator . $tail;
+ }
+ return false;
+ }
+
+ return $token . $separator;
+}
+
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
?>