Feature #306 - Upgrading the function api_get_person_name(), now it can turn to upercase selected parts or all name parts.

skala
Ivan Tcholakov 16 years ago
parent 22a61cf7c1
commit a8962d7cd1
  1. 20
      main/inc/lib/internationalization.lib.php
  2. 21
      main/inc/lib/internationalization_internal.lib.php

@ -425,8 +425,9 @@ function api_get_months_long($language = null) {
* @param string $first_name The first name of the preson.
* @param string $last_name The last name of the person.
* @param string $title The title of the person.
* @param int/string $format (optional) The person name format. It may be a pattern-string (for example '%t. %l, %f') or some of the constants PERSON_NAME_COMMON_CONVENTION (default), PERSON_NAME_WESTERN_ORDER, PERSON_NAME_EASTERN_ORDER, PERSON_NAME_LIBRARY_ORDER.
* @param int/string $format (optional) The person name format. It may be a pattern-string (for example '%t %l, %f' or '%T %F %L', ...) or some of the constants PERSON_NAME_COMMON_CONVENTION (default), PERSON_NAME_WESTERN_ORDER, PERSON_NAME_EASTERN_ORDER, PERSON_NAME_LIBRARY_ORDER.
* @param string $language (optional) The language indentificator. If it is omited, the current interface language is assumed. This parameter has meaning with the format PERSON_NAME_COMMON_CONVENTION only.
* @param string $encoding (optional) The used internally by this function character encoding. If it is omitted, the platform character set will be used by default.
* @return bool The result is sort of full name of the person.
* Sample results:
* Peter Ustinoff or Dr. Peter Ustinoff - the Western order
@ -436,7 +437,7 @@ function api_get_months_long($language = null) {
* @author Carlos Vargas <carlos.vargas@dokeos.com> - initial implementation.
* @author Ivan Tcholakov
*/
function api_get_person_name($first_name, $last_name, $title = null, $format = null, $language = null) {
function api_get_person_name($first_name, $last_name, $title = null, $format = null, $language = null, $encoding = null) {
static $valid = array();
if (empty($format)) {
$format = PERSON_NAME_COMMON_CONVENTION;
@ -444,6 +445,9 @@ function api_get_person_name($first_name, $last_name, $title = null, $format = n
if (empty($language)) {
$language = api_get_interface_language();
}
if (empty($encoding)) {
$encoding = _api_mb_internal_encoding();
}
if (!isset($valid[$format][$language])) {
if (is_int($format)) {
switch ($format) {
@ -461,12 +465,18 @@ function api_get_person_name($first_name, $last_name, $title = null, $format = n
break;
default:
$valid[$format][$language] = '%t %f %l';
break;
}
} else {
$valid[$format][$language] = _api_validate_person_name_format($format);
}
}
return _api_clean_person_name(str_replace(array('%f', '%l', '%t'), array($first_name, $last_name, $title), $valid[$format][$language]));
$format = $valid[$format][$language];
$person_name = str_replace(array('%f', '%l', '%t'), array($first_name, $last_name, $title), $format);
if (strpos($format, '%F') !== false || strpos($format, '%L') !== false || strpos($format, '%T') !== false) {
$person_name = str_replace(array('%F', '%L', '%T'), array(api_strtoupper($first_name, $encoding), api_strtoupper($last_name, $encoding), api_strtoupper($title, $encoding)), $person_name);
}
return _api_clean_person_name($person_name);
}
/**
@ -487,7 +497,7 @@ function api_is_western_name_order($format = null, $language = null) {
}
if (!isset($order[$format][$language])) {
$test_name = api_get_person_name('%f', '%l', '%t', $format, $language);
$order[$format][$language] = strpos($test_name, '%f') <= strpos($test_name, '%l');
$order[$format][$language] = stripos($test_name, '%f') <= stripos($test_name, '%l');
}
return $order[$format][$language];
}
@ -940,7 +950,7 @@ function api_transliterate($string, $unknown = '?', $from_encoding = null) {
$bank = $ord >> 8;
// Check if we need to load a new bank
if (!isset($map[$bank])) {
$file = dirname(__FILE__) . '/internationalization_database/transliteration/' . sprintf('x%02x', $bank) . '.php';
$file = dirname(__FILE__).'/internationalization_database/transliteration/' . sprintf('x%02x', $bank) . '.php';
if (file_exists($file)) {
$map[$bank] = include ($file);
} else {

@ -137,16 +137,19 @@ function _api_get_person_name_convention($language, $type) {
static $conventions;
$language = api_purify_language_id($language);
if (!isset($conventions)) {
$file = dirname(__FILE__) . '/internationalization_database/name_order_conventions.php';
$file = dirname(__FILE__).'/internationalization_database/name_order_conventions.php';
if (file_exists($file)) {
$conventions = include ($file);
} else {
$conventions = array('english' => array('format' => 'title first_name last_name', 'sort_by' => 'first_name'));
}
$search = array('first_name', 'last_name', 'title');
$replacement = array('%f', '%l', '%t');
$search1 = array('FIRST_NAME', 'LAST_NAME', 'TITLE');
$replacement1 = array('%F', '%L', '%T');
$search2 = array('first_name', 'last_name', 'title');
$replacement2 = array('%f', '%l', '%t');
foreach (array_keys($conventions) as $key) {
$conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search, $replacement, $conventions[$key]['format']))));
$conventions[$key]['format'] = str_replace($search1, $replacement1, $conventions[$key]['format']);
$conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search2, $replacement2, $conventions[$key]['format']))));
$conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
}
}
@ -165,7 +168,7 @@ function _api_get_person_name_convention($language, $type) {
* @return bool Returns the same format if is is valid, otherwise returns a valid English format.
*/
function _api_validate_person_name_format($format) {
if (empty($format) || strpos($format, '%f') === false || strpos($format, '%l') === false) {
if (empty($format) || stripos($format, '%f') === false || stripos($format, '%l') === false) {
return '%t %f %l';
}
return $format;
@ -276,7 +279,7 @@ function _api_convert_encoding($string, $to_encoding, $from_encoding) {
function _api_get_character_map_name($encoding) {
static $character_map_selector;
if (!isset($character_map_selector)) {
$file = dirname(__FILE__) . '/internationalization_database/conversion/character_map_selector.php';
$file = dirname(__FILE__).'/internationalization_database/conversion/character_map_selector.php';
if (file_exists($file)) {
$character_map_selector = include ($file);
} else {
@ -294,7 +297,7 @@ function _api_get_character_map_name($encoding) {
*/
function &_api_parse_character_map($name) {
$result = array();
$file = dirname(__FILE__) . '/internationalization_database/conversion/' . $name . '.TXT';
$file = dirname(__FILE__).'/internationalization_database/conversion/' . $name . '.TXT';
if (file_exists($file)) {
$text = @file_get_contents($file);
if ($text !== false) {
@ -601,7 +604,7 @@ function &_api_utf8_get_letter_case_properties($codepoint, $type = 'lower') {
return null;
}
if (!isset($config[$range[$codepoint]])) {
$file = dirname(__FILE__) . '/internationalization_database/casefolding/' . $range[$codepoint] . '.php';
$file = dirname(__FILE__).'/internationalization_database/casefolding/' . $range[$codepoint] . '.php';
if (file_exists($file)) {
include $file;
}
@ -856,7 +859,7 @@ function api_get_default_locale() {
function & _api_non_utf8_encodings() {
static $encodings;
if (!isset($encodings)) {
$file = dirname(__FILE__) . '/internationalization_database/non_utf8_encodings.php';
$file = dirname(__FILE__).'/internationalization_database/non_utf8_encodings.php';
if (file_exists($file)) {
$encodings = include ($file);
} else {

Loading…
Cancel
Save